| <?php | |
| // Licensed to the Apache Software Foundation (ASF) under one | |
| // or more contributor license agreements. See the NOTICE file | |
| // distributed with this work for additional information | |
| // regarding copyright ownership. The ASF licenses this file | |
| // to you under the Apache License, Version 2.0 (the | |
| // "License"); you may not use this file except in compliance | |
| // with the License. You may obtain a copy of the License at | |
| // | |
| // http://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, | |
| // software distributed under the License is distributed on an | |
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| // KIND, either express or implied. See the License for the | |
| // specific language governing permissions and limitations | |
| // under the License. | |
| declare(strict_types=1); | |
| use Iggy\Client as IggyClient; | |
| use Iggy\Exception\AuthenticationException; | |
| use Iggy\Exception\ConnectionException; | |
| use Iggy\Exception\IggyException; | |
| use Iggy\Exception\NotFoundException; | |
| use Iggy\Exception\ReadException; | |
| use Iggy\Exception\TransientException; | |
| use Iggy\PollingStrategy; | |
| use Iggy\ReceiveMessage; | |
| use Iggy\SendMessage; | |
| use PHPUnit\Framework\Attributes\TestDox; | |
| use PHPUnit\Framework\TestCase; | |
| final class ErrorTest extends TestCase | |
| { | |
| #[TestDox('Root policy refusal keeps its typed exception and shared code')] | |
| public function testRootPolicyRefusalKeepsTypedCode(): void | |
| { | |
| $fixture = json_decode((string) file_get_contents(__DIR__ . '/fixtures/resource_options.json'), true, 512, JSON_THROW_ON_ERROR); | |
| $vector = array_values(array_filter( | |
| $fixture['update_resource'], | |
| static fn (array $entry): bool => $entry['case'] === 'user_username_and_status', | |
| ))[0]; | |
| $body = hex2bin($vector['body_hex']); | |
| assert_true($body !== false); | |
| $body[3] = "\0"; | |
| $client = new_client(); | |
| $error = assert_throws(static fn () => $client->sendBinaryRequest(16, $body)); | |
| assert_instance_of(AuthenticationException::class, $error); | |
| assert_same($fixture['constants']['error_codes']['cannot_modify_root_user'], $error->getCode()); | |
| } | |
| #[TestDox('Connection configuration errors use the typed connection exception')] | |
| public function testConnectionConfigurationErrorsUseTypedException(): void | |
| { | |
| $throwable = assert_throws(static fn () => new IggyClient('127.0.0.1')); | |
| assert_instance_of(ConnectionException::class, $throwable); | |
| assert_instance_of(IggyException::class, $throwable); | |
| } | |
| #[TestDox('Authentication failures use the typed authentication exception')] | |
| public function testAuthenticationErrorsUseTypedException(): void | |
| { | |
| $client = new IggyClient(server_host() . ':' . server_port()); | |
| $client->connect(); | |
| $throwable = assert_throws(static fn () => $client->loginUser('iggy', 'wrong-password')); | |
| assert_instance_of(AuthenticationException::class, $throwable); | |
| assert_instance_of(IggyException::class, $throwable); | |
| } | |
| #[TestDox('Missing resources use the typed not-found exception')] | |
| public function testNotFoundErrorsUseTypedException(): void | |
| { | |
| $client = new_client(); | |
| $throwable = assert_throws(static fn () => $client->deleteStream(unique_name('missing-stream'))); | |
| assert_instance_of(NotFoundException::class, $throwable); | |
| assert_instance_of(IggyException::class, $throwable); | |
| } | |
| #[TestDox('Transient exceptions share the Iggy exception base class')] | |
| public function testTransientExceptionUsesIggyBase(): void | |
| { | |
| assert_true(is_subclass_of(TransientException::class, IggyException::class)); | |
| } | |
| #[TestDox('Read exceptions share the Iggy exception base class')] | |
| public function testReadExceptionUsesIggyBase(): void | |
| { | |
| assert_true(is_subclass_of(ReadException::class, IggyException::class)); | |
| } | |
| #[TestDox('Callback exceptions are rethrown without changing their PHP type')] | |
| public function testCallbackExceptionIsRethrownAsOriginalException(): void | |
| { | |
| $client = new_client(); | |
| $consumerName = unique_name('callback-exception-consumer'); | |
| $streamName = unique_name('callback-exception-stream'); | |
| $topicName = unique_name('callback-exception-topic'); | |
| try { | |
| create_stream_and_topic($client, $streamName, $topicName); | |
| $client->sendMessages( | |
| $streamName, | |
| $topicName, | |
| 0, | |
| [new SendMessage('callback exception payload')], | |
| ); | |
| $consumer = $client->consumerGroup($consumerName, $streamName, $topicName, PollingStrategy::next()); | |
| $throwable = assert_throws( | |
| static fn () => $consumer->consumeMessages( | |
| static function (ReceiveMessage $message): void { | |
| throw new RuntimeException('callback abort'); | |
| }, | |
| 1, | |
| ), | |
| ); | |
| assert_instance_of(RuntimeException::class, $throwable); | |
| assert_same('callback abort', $throwable->getMessage()); | |
| } finally { | |
| cleanup_stream_with_topics($client, $streamName, [$topicName]); | |
| } | |
| } | |
| } |