diff --git a/src/Bootloader/RoadRunnerBootloader.php b/src/Bootloader/RoadRunnerBootloader.php index 4229e63..99583e6 100644 --- a/src/Bootloader/RoadRunnerBootloader.php +++ b/src/Bootloader/RoadRunnerBootloader.php @@ -21,7 +21,7 @@ use Spiral\RoadRunner\Http\PSR7WorkerInterface; use Spiral\RoadRunner\Worker; use Spiral\RoadRunner\WorkerInterface; -use Spiral\RoadRunnerBridge\FailbackDispatcher; +use Spiral\RoadRunnerBridge\FallbackDispatcher; final class RoadRunnerBootloader extends Bootloader { @@ -71,7 +71,7 @@ public function init(Container $container, AbstractKernel $kernel): void // // Register FailbackDispatcher after all dispatchers // - $kernel->bootstrapped(static function (FailbackDispatcher $dispatcher, KernelInterface $kernel): void { + $kernel->bootstrapped(static function (FallbackDispatcher $dispatcher, KernelInterface $kernel): void { $kernel->addDispatcher($dispatcher); }); } diff --git a/src/FailbackDispatcher.php b/src/FailbackDispatcher.php deleted file mode 100644 index f16ce4d..0000000 --- a/src/FailbackDispatcher.php +++ /dev/null @@ -1,45 +0,0 @@ -mode !== RoadRunnerMode::Unknown; - } - - public function serve(): void - { - match ($this->mode) { - RoadRunnerMode::Http => throw new Exception(\sprintf(self::ERROR, $this->mode->name, Http::class)), - RoadRunnerMode::Jobs => throw new Exception(\sprintf(self::ERROR, $this->mode->name, Queue::class)), - RoadRunnerMode::Grpc => throw new Exception(\sprintf(self::ERROR, $this->mode->name, GRPC::class)), - RoadRunnerMode::Tcp => throw new Exception(\sprintf(self::ERROR, $this->mode->name, Tcp::class)), - RoadRunnerMode::Centrifuge => throw new Exception( - \sprintf(self::ERROR, $this->mode->name, Centrifugo::class) - ), - RoadRunnerMode::Temporal => throw new Exception( - 'To use Temporal with RoadRunner, please install the package `spiral/temporal-bridge`.' - ), - RoadRunnerMode::Unknown => null - }; - } -} diff --git a/src/FallbackDispatcher.php b/src/FallbackDispatcher.php new file mode 100644 index 0000000..f8daae5 --- /dev/null +++ b/src/FallbackDispatcher.php @@ -0,0 +1,53 @@ +mode !== RoadRunnerMode::Unknown; + } + + public function serve(): void + { + match ($this->mode) { + RoadRunnerMode::Http => $this->throwException(Http::class), + RoadRunnerMode::Jobs => $this->throwException(Queue::class), + RoadRunnerMode::Grpc => $this->throwException(GRPC::class), + RoadRunnerMode::Tcp => $this->throwException(Tcp::class), + RoadRunnerMode::Centrifuge => $this->throwException(Centrifugo::class), + RoadRunnerMode::Temporal => throw new DispatcherNotFoundException( + 'To use Temporal with RoadRunner, please install `spiral/temporal-bridge` package.' + ), + RoadRunnerMode::Unknown => null, + }; + } + + /** + * @param class-string $class + * + * @throws DispatcherNotFoundException + */ + private function throwException(string $class): void + { + throw new DispatcherNotFoundException(\sprintf(self::ERROR, $this->mode->name, $class)); + } +} diff --git a/src/GRPC/Interceptor/Invoker.php b/src/GRPC/Interceptor/Invoker.php index 6036b9f..cb9ae21 100644 --- a/src/GRPC/Interceptor/Invoker.php +++ b/src/GRPC/Interceptor/Invoker.php @@ -10,7 +10,10 @@ use Spiral\RoadRunner\GRPC\Method; use Spiral\RoadRunner\GRPC\ServiceInterface; -class Invoker implements InvokerInterface +/** + * @internal + */ +final class Invoker implements InvokerInterface { public function __construct( private readonly CoreInterface $core diff --git a/tests/src/Bootloader/QueueBootloaderTest.php b/tests/src/Bootloader/QueueBootloaderTest.php index d6f7b10..d92bfd4 100644 --- a/tests/src/Bootloader/QueueBootloaderTest.php +++ b/tests/src/Bootloader/QueueBootloaderTest.php @@ -8,8 +8,6 @@ use Spiral\Core\ConfigsInterface; use Spiral\Exceptions\ExceptionReporterInterface; use Spiral\Queue\HandlerRegistryInterface; -use Spiral\Queue\Queue; -use Spiral\Queue\QueueInterface; use Spiral\Serializer\SerializerInterface; use Spiral\RoadRunnerBridge\Queue\Consumer; use Spiral\RoadRunner\Jobs\ConsumerInterface; @@ -115,14 +113,6 @@ public function testGetsConsumerInterface(): void ); } - public function testGetsQueueInterface(): void - { - $this->assertContainerBoundAsSingleton( - QueueInterface::class, - Queue::class - ); - } - public function testConfigShouldBeDefined(): void { $configurator = $this->getContainer()->get(ConfigsInterface::class); diff --git a/tests/src/Bootloader/RoadRunnerBootloaderTest.php b/tests/src/Bootloader/RoadRunnerBootloaderTest.php index 0be436e..c3040cd 100644 --- a/tests/src/Bootloader/RoadRunnerBootloaderTest.php +++ b/tests/src/Bootloader/RoadRunnerBootloaderTest.php @@ -14,7 +14,7 @@ use Spiral\RoadRunner\Http\PSR7WorkerInterface; use Spiral\RoadRunner\Worker; use Spiral\RoadRunner\WorkerInterface; -use Spiral\RoadRunnerBridge\FailbackDispatcher; +use Spiral\RoadRunnerBridge\FallbackDispatcher; use Spiral\Tests\TestCase; final class RoadRunnerBootloaderTest extends TestCase @@ -83,6 +83,6 @@ public function testFailbackDispatcherShouldBeLast(): void $dispatchers = (new \ReflectionProperty($kernel, 'dispatchers'))->getValue($kernel); $dispatchers = \array_filter($dispatchers, static fn (mixed $disp): bool => !$disp instanceof ConsoleDispatcher); - $this->assertInstanceOf(FailbackDispatcher::class, $dispatchers[\array_key_last($dispatchers)]); + $this->assertInstanceOf(FallbackDispatcher::class, $dispatchers[\array_key_last($dispatchers)]); } } diff --git a/tests/src/FailbackDispatcherTest.php b/tests/src/FailbackDispatcherTest.php deleted file mode 100644 index 2a22b58..0000000 --- a/tests/src/FailbackDispatcherTest.php +++ /dev/null @@ -1,52 +0,0 @@ -assertSame($expected, $dispatcher->canServe()); - } - - /** - * @dataProvider exceptionDataProvider - */ - public function testException(RoadRunnerMode $mode): void - { - $dispatcher = new FailbackDispatcher($mode); - - $this->expectException(DispatcherNotFoundException::class); - $dispatcher->serve(); - } - - public function canServeDataProvider(): \Traversable - { - yield [RoadRunnerMode::Http, true]; - yield [RoadRunnerMode::Temporal, true]; - yield [RoadRunnerMode::Jobs, true]; - yield [RoadRunnerMode::Grpc, true]; - yield [RoadRunnerMode::Tcp, true]; - yield [RoadRunnerMode::Unknown, false]; - } - - public function exceptionDataProvider(): \Traversable - { - yield [RoadRunnerMode::Http]; - yield [RoadRunnerMode::Temporal]; - yield [RoadRunnerMode::Jobs]; - yield [RoadRunnerMode::Grpc]; - yield [RoadRunnerMode::Tcp]; - } -} diff --git a/tests/src/FallbackDispatcherTest.php b/tests/src/FallbackDispatcherTest.php new file mode 100644 index 0000000..a693ee8 --- /dev/null +++ b/tests/src/FallbackDispatcherTest.php @@ -0,0 +1,67 @@ +assertSame($expected, $dispatcher->canServe()); + } + + /** + * @dataProvider exceptionDataProvider + */ + public function testException(RoadRunnerMode $mode, string $message): void + { + $this->expectException(DispatcherNotFoundException::class); + $this->expectExceptionMessage($message); + + (new FallbackDispatcher($mode))->serve(); + } + + public function canServeDataProvider(): \Traversable + { + yield [RoadRunnerMode::Http, true]; + yield [RoadRunnerMode::Temporal, true]; + yield [RoadRunnerMode::Jobs, true]; + yield [RoadRunnerMode::Grpc, true]; + yield [RoadRunnerMode::Tcp, true]; + yield [RoadRunnerMode::Unknown, false]; + } + + public function exceptionDataProvider(): \Traversable + { + yield 'http' => [ + RoadRunnerMode::Http, + 'To use RoadRunner in `Http` mode, please register dispatcher `Spiral\RoadRunnerBridge\Http\Dispatcher`.', + ]; + yield 'jobs' => [ + RoadRunnerMode::Jobs, + 'To use RoadRunner in `Jobs` mode, please register dispatcher `Spiral\RoadRunnerBridge\Queue\Dispatcher`.', + ]; + yield 'grpc' => [ + RoadRunnerMode::Grpc, + 'To use RoadRunner in `Grpc` mode, please register dispatcher `Spiral\RoadRunnerBridge\GRPC\Dispatcher`.', + ]; + yield 'tcp' => [ + RoadRunnerMode::Tcp, + 'To use RoadRunner in `Tcp` mode, please register dispatcher `Spiral\RoadRunnerBridge\Tcp\Dispatcher`.', + ]; + yield 'temporal' => [ + RoadRunnerMode::Temporal, + 'To use Temporal with RoadRunner, please install `spiral/temporal-bridge` package.', + ]; + } +} diff --git a/tests/src/Queue/QueueManagerTest.php b/tests/src/Queue/QueueManagerTest.php index 7006112..b028ab7 100644 --- a/tests/src/Queue/QueueManagerTest.php +++ b/tests/src/Queue/QueueManagerTest.php @@ -5,31 +5,22 @@ namespace Spiral\Tests\Queue; use Mockery as m; +use Spiral\Core\Container; +use Spiral\Core\FactoryInterface; +use Spiral\Queue\Config\QueueConfig; use Spiral\Queue\QueueConnectionProviderInterface; -use Spiral\RoadRunner\Jobs\QueueInterface; -use Spiral\RoadRunner\Jobs\Task\PreparedTaskInterface; -use Spiral\RoadRunner\Jobs\Task\QueuedTaskInterface; -use Spiral\RoadRunnerBridge\Queue\PipelineRegistryInterface; +use Spiral\Queue\QueueInterface; +use Spiral\Queue\QueueManager; use Spiral\RoadRunnerBridge\Queue\Queue; use Spiral\Tests\TestCase; class QueueManagerTest extends TestCase { - private QueueConnectionProviderInterface $manager; - - protected function setUp(): void - { - parent::setUp(); - - $this->registry = m::mock(PipelineRegistryInterface::class); - $this->getContainer()->bind(PipelineRegistryInterface::class, $this->registry); - - $this->manager = $this->getContainer()->get(QueueConnectionProviderInterface::class); - } - public function testGetsRoadRunnerQueue(): void { - $queue = $this->manager->getConnection('roadrunner'); + $manager = $this->getContainer()->get(QueueConnectionProviderInterface::class); + + $queue = $manager->getConnection('roadrunner'); $core = $this->accessProtected($queue, 'core'); $core = $this->accessProtected($core, 'core'); @@ -37,27 +28,40 @@ public function testGetsRoadRunnerQueue(): void $this->assertInstanceOf( Queue::class, - $connection + $connection, ); } public function testPushIntoDefaultRoadRunnerPipeline() { - $this->registry->shouldReceive('getPipeline') - ->once() - ->with('memory', 'foo') - ->andReturn($queue = m::mock(QueueInterface::class)); - - $queuedTask = m::mock(QueuedTaskInterface::class); - $preparedTask = m::mock(PreparedTaskInterface::class); - $queuedTask->shouldReceive('getId')->once()->andReturn('task-id'); + $factory = m::mock(FactoryInterface::class); - $queue->shouldReceive('dispatch')->once()->with($preparedTask)->andReturn($queuedTask); - $queue->shouldReceive('create')->once()->andReturn($preparedTask); + $factory->shouldReceive('make')->once() + ->with('roadrunner', [ + 'driver' => 'roadrunner', + 'pipelines' => [], + ]) + ->andReturn($driver = m::mock(QueueInterface::class)); - $this->assertSame( - 'task-id', - $this->manager->getConnection('roadrunner')->push('foo', ['boo' => 'bar']) + $manager = new QueueManager( + new QueueConfig([ + 'connections' => [ + 'roadrunner' => [ + 'driver' => 'roadrunner', + 'pipelines' => [], + ], + ], + ]), + new Container(), + $factory ); + + $queue = $manager->getConnection('roadrunner'); + + $driver->shouldReceive('push') + ->once() + ->withSomeOfArgs('foo', ['boo' => 'bar'])->andReturn('task-id'); + + $this->assertSame('task-id', $queue->push('foo', ['boo' => 'bar'])); } } diff --git a/tests/src/Queue/QueueTest.php b/tests/src/Queue/QueueTest.php index 6353ac9..833eccd 100644 --- a/tests/src/Queue/QueueTest.php +++ b/tests/src/Queue/QueueTest.php @@ -5,7 +5,7 @@ namespace Spiral\Tests\Queue; use Mockery as m; -use Spiral\Core\Container; +use Spiral\Core\FactoryInterface; use Spiral\Queue\Job\ObjectJob; use Spiral\Queue\Options as QueueOptions; use Spiral\RoadRunner\Jobs\Queue\CreateInfoInterface; @@ -19,17 +19,13 @@ final class QueueTest extends TestCase { private Queue $queue; - /** @var m\LegacyMockInterface|m\MockInterface|PipelineRegistryInterface */ - private $registry; + private m\LegacyMockInterface|FactoryInterface|m\MockInterface $factory; protected function setUp(): void { parent::setUp(); - $container = new Container(); - - $this->registry = m::mock(PipelineRegistryInterface::class); - $container->bind(PipelineRegistryInterface::class, $this->registry); + $this->factory = m::mock(FactoryInterface::class); $pipelines = [ 'memory' => [ @@ -42,12 +38,17 @@ protected function setUp(): void 'user-data' => 'memory', ]; - $this->queue = new Queue($container, $pipelines, $aliases, 'default'); + $this->queue = new Queue($this->factory, $pipelines, $aliases, 'default'); } public function testTaskShouldBePushedToDefaultQueue(): void { - $this->registry->shouldReceive('getPipeline') + $this->factory->shouldReceive('make') + ->once() + ->withSomeOfArgs(PipelineRegistryInterface::class) + ->andReturn($registry = m::mock(PipelineRegistryInterface::class)); + + $registry->shouldReceive('getPipeline') ->once() ->with('default', 'foo') ->andReturn($queue = m::mock(QueueInterface::class)); @@ -66,12 +67,17 @@ public function testTaskShouldBePushedToDefaultQueue(): void public function testTaskShouldBePushedToCustomQueue(): void { - $this->registry->shouldReceive('getPipeline') + $this->factory->shouldReceive('make') + ->twice() + ->withSomeOfArgs(PipelineRegistryInterface::class) + ->andReturn($registry = m::mock(PipelineRegistryInterface::class)); + + $registry->shouldReceive('getPipeline') ->once() ->with('foo', 'foo') ->andReturn($fooQueue = m::mock(QueueInterface::class)); - $this->registry->shouldReceive('getPipeline') + $registry->shouldReceive('getPipeline') ->once() ->with('bar', 'bar') ->andReturn($barQueue = m::mock(QueueInterface::class)); @@ -93,7 +99,12 @@ public function testTaskShouldBePushedToCustomQueue(): void public function testPushObject(): void { - $this->registry->shouldReceive('getPipeline') + $this->factory->shouldReceive('make') + ->once() + ->withSomeOfArgs(PipelineRegistryInterface::class) + ->andReturn($registry = m::mock(PipelineRegistryInterface::class)); + + $registry->shouldReceive('getPipeline') ->once() ->with('default', ObjectJob::class) ->andReturn($queue = m::mock(QueueInterface::class));