diff --git a/src/Exception/AdapterConfiguration/ChannelIncorrectlyConfigured.php b/src/Exception/AdapterConfiguration/ChannelIncorrectlyConfigured.php index 4d4ea331..286f8aee 100644 --- a/src/Exception/AdapterConfiguration/ChannelIncorrectlyConfigured.php +++ b/src/Exception/AdapterConfiguration/ChannelIncorrectlyConfigured.php @@ -19,11 +19,13 @@ class ChannelIncorrectlyConfigured extends InvalidArgumentException implements F * * @param string $channel * @param mixed|object $definition + * @param int $code + * @param Throwable|null $previous */ public function __construct(string $channel, $definition, int $code = 0, ?Throwable $previous = null) { $adapterClass = AdapterInterface::class; - $realType = is_object($definition) ? get_class($definition) : gettype($definition); + $realType = get_debug_type($definition); $message = "Channel \"$channel\" is not properly configured: definition must return $adapterClass, $realType returned"; $this->channel = $channel; diff --git a/tests/Integration/QueueFactoryTest.php b/tests/Integration/QueueFactoryTest.php index 06488e8f..a08e2fef 100644 --- a/tests/Integration/QueueFactoryTest.php +++ b/tests/Integration/QueueFactoryTest.php @@ -15,6 +15,7 @@ use Yiisoft\Yii\Queue\Middleware\Push\PushMiddlewareDispatcher; use Yiisoft\Yii\Queue\Queue; use Yiisoft\Yii\Queue\QueueFactory; +use Yiisoft\Yii\Queue\QueueFactoryInterface; use Yiisoft\Yii\Queue\Tests\App\FakeAdapter; use Yiisoft\Yii\Queue\Worker\WorkerInterface; @@ -23,12 +24,7 @@ final class QueueFactoryTest extends TestCase public function testQuickChange(): void { $worker = $this->createMock(WorkerInterface::class); - $queue = new Queue( - $worker, - $this->createMock(LoopInterface::class), - $this->createMock(LoggerInterface::class), - new PushMiddlewareDispatcher($this->createMock(MiddlewareFactoryPushInterface::class)), - ); + $queue = $this->getDefaultQueue($worker); $container = $this->createMock(ContainerInterface::class); $factory = new QueueFactory( [], @@ -48,12 +44,7 @@ public function testQuickChange(): void public function testConfiguredChange(): void { $worker = $this->createMock(WorkerInterface::class); - $queue = new Queue( - $worker, - $this->createMock(LoopInterface::class), - $this->createMock(LoggerInterface::class), - new PushMiddlewareDispatcher($this->createMock(MiddlewareFactoryPushInterface::class)), - ); + $queue = $this->getDefaultQueue($worker); $container = $this->createMock(ContainerInterface::class); $factory = new QueueFactory( [ @@ -61,6 +52,10 @@ public function testConfiguredChange(): void 'class' => FakeAdapter::class, 'withChannel()' => ['test-channel'], ], + QueueFactoryInterface::DEFAULT_CHANNEL_NAME => [ + 'class' => FakeAdapter::class, + 'withChannel()' => [QueueFactoryInterface::DEFAULT_CHANNEL_NAME], + ], ], $queue, $container, @@ -72,5 +67,16 @@ public function testConfiguredChange(): void $queue = $factory->get('test-channel'); self::assertEquals('test-channel', $queue->getChannelName()); + self::assertEquals(QueueFactoryInterface::DEFAULT_CHANNEL_NAME, $factory->get()->getChannelName()); + } + + private function getDefaultQueue(WorkerInterface $worker): Queue + { + return new Queue( + $worker, + $this->createMock(LoopInterface::class), + $this->createMock(LoggerInterface::class), + new PushMiddlewareDispatcher($this->createMock(MiddlewareFactoryPushInterface::class)), + ); } } diff --git a/tests/Unit/JobStatusTest.php b/tests/Unit/JobStatusTest.php index 65fce8b6..330d5993 100644 --- a/tests/Unit/JobStatusTest.php +++ b/tests/Unit/JobStatusTest.php @@ -5,7 +5,9 @@ namespace Yiisoft\Yii\Queue\Tests\Unit; use Yiisoft\Yii\Queue\Enum\JobStatus; +use Yiisoft\Yii\Queue\Exception\InvalidStatusException; use Yiisoft\Yii\Queue\Tests\TestCase; +use Yiisoft\Yii\Queue\Tests\Unit\Support\TestJobStatus; final class JobStatusTest extends TestCase { @@ -55,4 +57,16 @@ public function testInstanceValue(string $statusName, string $positiveMethod, ar self::assertFalse($status->$negative(), "$negative must be false for status $statusName"); } } + + public function testException(): void + { + try { + TestJobStatus::withStatus(4)->isDone(); + } catch (InvalidStatusException $exception) { + self::assertSame($exception::class, InvalidStatusException::class); + self::assertSame($exception->getName(), 'Invalid job status provided'); + self::assertSame($exception->getStatus(), 4); + $this->assertMatchesRegularExpression('/JobStatus::DONE/', $exception->getSolution()); + } + } } diff --git a/tests/Unit/Middleware/Consume/MiddlewareDispatcherTest.php b/tests/Unit/Middleware/Consume/MiddlewareDispatcherTest.php index ef11cb93..7ce71d23 100644 --- a/tests/Unit/Middleware/Consume/MiddlewareDispatcherTest.php +++ b/tests/Unit/Middleware/Consume/MiddlewareDispatcherTest.php @@ -24,11 +24,12 @@ final class MiddlewareDispatcherTest extends TestCase public function testCallableMiddlewareCalled(): void { $request = $this->getConsumeRequest(); + $queue = $this->createMock(QueueInterface::class); $dispatcher = $this->createDispatcher()->withMiddlewares( [ - static function (ConsumeRequest $request): ConsumeRequest { - return $request->withMessage(new Message('test', 'New closure test data')); + static function (ConsumeRequest $request) use ($queue): ConsumeRequest { + return $request->withMessage(new Message('test', 'New closure test data'))->withQueue($queue); }, ] ); diff --git a/tests/Unit/QueueFactoryTest.php b/tests/Unit/QueueFactoryTest.php index adbcec1c..58b77dea 100644 --- a/tests/Unit/QueueFactoryTest.php +++ b/tests/Unit/QueueFactoryTest.php @@ -69,36 +69,44 @@ public function testThrowExceptionOnEmptyAdapter(): void public function testThrowExceptionOnEmptyDefinition(): void { - $this->expectException(ChannelNotConfiguredException::class); - - $queue = $this->createMock(QueueInterface::class); - $factory = new QueueFactory( - [], - $queue, - $this->getContainer(), - new CallableFactory($this->getContainer()), - new Injector($this->getContainer()), - false - ); - - $factory->get('test'); + try { + $queue = $this->createMock(QueueInterface::class); + $factory = new QueueFactory( + [], + $queue, + $this->getContainer(), + new CallableFactory($this->getContainer()), + new Injector($this->getContainer()), + false + ); + + $factory->get('test'); + } catch (ChannelNotConfiguredException $exception) { + self::assertSame($exception::class, ChannelNotConfiguredException::class); + self::assertSame($exception->getName(), 'Queue channel is not properly configured'); + $this->assertMatchesRegularExpression('/"test"/', $exception->getSolution()); + } } public function testThrowExceptionOnIncorrectDefinition(): void { - $this->expectException(ChannelIncorrectlyConfigured::class); - - $queue = $this->createMock(QueueInterface::class); - $factory = new QueueFactory( - ['test' => new stdClass()], - $queue, - $this->getContainer(), - new CallableFactory($this->getContainer()), - new Injector($this->getContainer()), - false - ); - - $factory->get('test'); + try { + $queue = $this->createMock(QueueInterface::class); + $factory = new QueueFactory( + ['test' => new stdClass()], + $queue, + $this->getContainer(), + new CallableFactory($this->getContainer()), + new Injector($this->getContainer()), + false + ); + + $factory->get('test'); + } catch (ChannelIncorrectlyConfigured $exception) { + self::assertSame($exception::class, ChannelIncorrectlyConfigured::class); + self::assertSame($exception->getName(), 'Incorrect queue channel configuration'); + $this->assertMatchesRegularExpression('/"test"/', $exception->getSolution()); + } } public function testSuccessfulDefinitionWithDefaultAdapter(): void diff --git a/tests/Unit/QueueTest.php b/tests/Unit/QueueTest.php index 9c8e89ea..8297bf85 100644 --- a/tests/Unit/QueueTest.php +++ b/tests/Unit/QueueTest.php @@ -4,6 +4,8 @@ namespace Yiisoft\Yii\Queue\Tests\Unit; +use Yiisoft\Yii\Queue\Cli\SignalLoop; +use Yiisoft\Yii\Queue\Exception\AdapterConfiguration\AdapterNotConfiguredException; use Yiisoft\Yii\Queue\Message\Message; use Yiisoft\Yii\Queue\Tests\App\FakeAdapter; use Yiisoft\Yii\Queue\Tests\TestCase; @@ -94,4 +96,44 @@ public function testStatus(): void $status = $queue->status($id); self::assertTrue($status->isDone()); } + + public function testAdapterNotConfiguredException(): void + { + try { + $queue = $this->getQueue(); + $message = new Message('simple', null); + $queue->push($message); + $queue->status($message->getId()); + } catch (AdapterNotConfiguredException $exception) { + self::assertSame($exception::class, AdapterNotConfiguredException::class); + self::assertSame($exception->getName(), 'Adapter is not configured'); + $this->assertMatchesRegularExpression('/withAdapter/', $exception->getSolution()); + } + } + + public function testAdapterNotConfiguredExceptionForRun(): void + { + try { + $this->getQueue()->run(); + } catch (AdapterNotConfiguredException $exception) { + self::assertSame($exception::class, AdapterNotConfiguredException::class); + self::assertSame($exception->getName(), 'Adapter is not configured'); + $this->assertMatchesRegularExpression('/withAdapter/', $exception->getSolution()); + } + } + + public function testRunWithSignalLoop(): void + { + $this->loop = new SignalLoop(); + $queue = $this + ->getQueue() + ->withAdapter($this->getAdapter()); + $message = new Message('simple', null); + $message2 = clone $message; + $queue->push($message); + $queue->push($message2); + $queue->run(); + + self::assertEquals(2, $this->executionTimes); + } } diff --git a/tests/Unit/Support/TestJobStatus.php b/tests/Unit/Support/TestJobStatus.php new file mode 100644 index 00000000..7e8bb046 --- /dev/null +++ b/tests/Unit/Support/TestJobStatus.php @@ -0,0 +1,15 @@ +expectException(JobFailureException::class); - $this->expectExceptionMessage( - "Processing of message #null is stopped because of an exception:\nTest exception." - ); - $message = new Message('simple', ['test-data']); $logger = new SimpleLogger(); $handler = new FakeHandler(); @@ -176,6 +171,10 @@ public function testJobFailWithDefinitionHandlerException(): void try { $worker->process($message, $queue); + } catch (JobFailureException $exception) { + self::assertSame($exception::class, JobFailureException::class); + self::assertSame($exception->getMessage(), "Processing of message #null is stopped because of an exception:\nTest exception."); + self::assertEquals(['test-data'], $exception->getQueueMessage()->getData()); } finally { $messages = $logger->getMessages(); $this->assertNotEmpty($messages);