diff --git a/src/GRPC/Dispatcher.php b/src/GRPC/Dispatcher.php index 5c47b02..8f997f4 100644 --- a/src/GRPC/Dispatcher.php +++ b/src/GRPC/Dispatcher.php @@ -7,24 +7,23 @@ use Psr\Container\ContainerInterface; use Spiral\Boot\DispatcherInterface; use Spiral\Boot\FinalizerInterface; -use Spiral\RoadRunner\Environment\Mode; -use Spiral\RoadRunner\EnvironmentInterface; use Spiral\RoadRunner\GRPC\Server; use Spiral\RoadRunner\WorkerInterface; use Spiral\Exceptions\ExceptionReporterInterface; +use Spiral\RoadRunnerBridge\RoadRunnerMode; final class Dispatcher implements DispatcherInterface { public function __construct( - private readonly EnvironmentInterface $env, private readonly ContainerInterface $container, - private readonly FinalizerInterface $finalizer + private readonly FinalizerInterface $finalizer, + private readonly RoadRunnerMode $mode ) { } public function canServe(): bool { - return \PHP_SAPI === 'cli' && $this->env->getMode() === Mode::MODE_GRPC; + return \PHP_SAPI === 'cli' && $this->mode === RoadRunnerMode::Grpc; } public function serve(): void diff --git a/src/Http/Dispatcher.php b/src/Http/Dispatcher.php index 1439635..155a966 100644 --- a/src/Http/Dispatcher.php +++ b/src/Http/Dispatcher.php @@ -12,23 +12,22 @@ use Spiral\Exceptions\ExceptionHandlerInterface; use Spiral\Exceptions\Verbosity; use Spiral\Http\Http; -use Spiral\RoadRunner\Environment\Mode; -use Spiral\RoadRunner\EnvironmentInterface; use Spiral\RoadRunner\Http\PSR7WorkerInterface; +use Spiral\RoadRunnerBridge\RoadRunnerMode; final class Dispatcher implements DispatcherInterface { public function __construct( - private readonly EnvironmentInterface $env, private readonly ContainerInterface $container, private readonly ErrorHandlerInterface $errorHandler, - private readonly FinalizerInterface $finalizer + private readonly FinalizerInterface $finalizer, + private readonly RoadRunnerMode $mode ) { } public function canServe(): bool { - return \PHP_SAPI === 'cli' && $this->env->getMode() === Mode::MODE_HTTP; + return \PHP_SAPI === 'cli' && $this->mode === RoadRunnerMode::Http; } public function serve(): void diff --git a/src/Queue/Dispatcher.php b/src/Queue/Dispatcher.php index b5b1211..947ff9a 100644 --- a/src/Queue/Dispatcher.php +++ b/src/Queue/Dispatcher.php @@ -9,25 +9,24 @@ use Spiral\Boot\DispatcherInterface; use Spiral\Boot\FinalizerInterface; use Spiral\Queue\HandlerRegistryInterface; -use Spiral\RoadRunner\Environment\Mode; -use Spiral\RoadRunner\EnvironmentInterface; use Spiral\RoadRunner\Jobs\ConsumerInterface; use Spiral\RoadRunner\Jobs\Exception\JobsException; use Spiral\RoadRunner\Jobs\Task\ReceivedTaskInterface; use Spiral\Queue\Failed\FailedJobHandlerInterface; +use Spiral\RoadRunnerBridge\RoadRunnerMode; final class Dispatcher implements DispatcherInterface { public function __construct( private readonly ContainerInterface $container, private readonly FinalizerInterface $finalizer, - private readonly EnvironmentInterface $env + private readonly RoadRunnerMode $mode ) { } public function canServe(): bool { - return \PHP_SAPI === 'cli' && $this->env->getMode() === Mode::MODE_JOBS; + return \PHP_SAPI === 'cli' && $this->mode === RoadRunnerMode::Jobs; } /** diff --git a/src/RoadRunnerMode.php b/src/RoadRunnerMode.php new file mode 100644 index 0000000..ac743cb --- /dev/null +++ b/src/RoadRunnerMode.php @@ -0,0 +1,27 @@ +getMode(); + + return self::tryFrom($value) ?? self::Unknown; + } +} diff --git a/src/Tcp/Dispatcher.php b/src/Tcp/Dispatcher.php index b7bd775..e9da6ed 100644 --- a/src/Tcp/Dispatcher.php +++ b/src/Tcp/Dispatcher.php @@ -8,22 +8,21 @@ use Spiral\Boot\DispatcherInterface; use Spiral\Boot\FinalizerInterface; use Spiral\Exceptions\ExceptionReporterInterface; -use Spiral\RoadRunner\Environment\Mode; -use Spiral\RoadRunner\EnvironmentInterface; use Spiral\RoadRunner\WorkerInterface; +use Spiral\RoadRunnerBridge\RoadRunnerMode; final class Dispatcher implements DispatcherInterface { public function __construct( - private readonly EnvironmentInterface $env, private readonly ContainerInterface $container, - private readonly FinalizerInterface $finalizer + private readonly FinalizerInterface $finalizer, + private readonly RoadRunnerMode $mode ) { } public function canServe(): bool { - return \PHP_SAPI === 'cli' && $this->env->getMode() === Mode::MODE_TCP; + return \PHP_SAPI === 'cli' && $this->mode === RoadRunnerMode::Tcp; } public function serve(): void diff --git a/tests/src/GRPC/DispatcherTest.php b/tests/src/GRPC/DispatcherTest.php index 5555dea..be563cc 100644 --- a/tests/src/GRPC/DispatcherTest.php +++ b/tests/src/GRPC/DispatcherTest.php @@ -6,12 +6,11 @@ use Spiral\App\GRPC\EchoService\Message; use Spiral\Boot\FinalizerInterface; -use Spiral\RoadRunner\Environment; -use Spiral\RoadRunner\EnvironmentInterface; use Spiral\RoadRunner\Payload; use Spiral\RoadRunner\Worker; use Spiral\RoadRunner\WorkerInterface; use Spiral\RoadRunnerBridge\GRPC\Dispatcher; +use Spiral\RoadRunnerBridge\RoadRunnerMode; use Spiral\Tests\ConsoleTestCase; final class DispatcherTest extends ConsoleTestCase @@ -29,24 +28,14 @@ public function testCanServeShouldReturnFalseWithWrongEnvironment(): void public function testCanServe(): void { - $this->getContainer()->bind(EnvironmentInterface::class, function () { - return new Environment([ - 'RR_MODE' => 'grpc', - ]); - }); - + $this->getContainer()->bind(RoadRunnerMode::class, RoadRunnerMode::Grpc); $this->assertDispatcherCanBeServed(Dispatcher::class); } public function testServe() { $worker = $this->mockContainer(WorkerInterface::class, Worker::class); - - $this->getContainer()->bind(EnvironmentInterface::class, function () { - return new Environment([ - 'RR_MODE' => 'grpc', - ]); - }); + $this->getContainer()->bind(RoadRunnerMode::class, RoadRunnerMode::Grpc); $finalizer = $this->mockContainer(FinalizerInterface::class); $finalizer->shouldReceive('finalize')->once()->with(false); diff --git a/tests/src/Http/DispatcherTest.php b/tests/src/Http/DispatcherTest.php index f996a87..d698680 100644 --- a/tests/src/Http/DispatcherTest.php +++ b/tests/src/Http/DispatcherTest.php @@ -12,11 +12,10 @@ use Psr\Http\Server\RequestHandlerInterface; use Spiral\Boot\FinalizerInterface; use Spiral\Http\Http; -use Spiral\RoadRunner\Environment; -use Spiral\RoadRunner\EnvironmentInterface; use Spiral\RoadRunner\Http\PSR7WorkerInterface; use Spiral\RoadRunnerBridge\Http\Dispatcher; use Spiral\RoadRunnerBridge\Http\ErrorHandlerInterface; +use Spiral\RoadRunnerBridge\RoadRunnerMode; use Spiral\Tests\TestCase; final class DispatcherTest extends TestCase @@ -28,22 +27,14 @@ public function testCanServeShouldReturnFalseWithWrongEnvironment(): void public function testCanServe(): void { - $this->getContainer()->bind(EnvironmentInterface::class, function () { - return new Environment([ - 'RR_MODE' => 'http', - ]); - }); + $this->getContainer()->bind(RoadRunnerMode::class, RoadRunnerMode::Http); $this->assertDispatcherCanBeServed(Dispatcher::class); } public function testServe(): void { - $this->getContainer()->bind(EnvironmentInterface::class, function () { - return new Environment([ - 'RR_MODE' => 'http', - ]); - }); + $this->getContainer()->bind(RoadRunnerMode::class, RoadRunnerMode::Http); $finalizer = $this->mockContainer(FinalizerInterface::class); $finalizer->shouldReceive('finalize')->once()->with(false); @@ -68,11 +59,7 @@ public function testServe(): void public function testServeWithError(): void { - $this->getContainer()->bind(EnvironmentInterface::class, function () { - return new Environment([ - 'RR_MODE' => 'http', - ]); - }); + $this->getContainer()->bind(RoadRunnerMode::class, RoadRunnerMode::Http); $finalizer = $this->mockContainer(FinalizerInterface::class); $finalizer->shouldReceive('finalize')->once()->with(false); diff --git a/tests/src/Queue/DispatcherTest.php b/tests/src/Queue/DispatcherTest.php index b686f28..0784be2 100644 --- a/tests/src/Queue/DispatcherTest.php +++ b/tests/src/Queue/DispatcherTest.php @@ -9,11 +9,10 @@ use Spiral\Queue\Failed\FailedJobHandlerInterface; use Spiral\Queue\HandlerInterface; use Spiral\Queue\HandlerRegistryInterface; -use Spiral\RoadRunner\Environment; -use Spiral\RoadRunner\EnvironmentInterface; use Spiral\RoadRunner\Jobs\ConsumerInterface; use Spiral\RoadRunner\Jobs\Task\ReceivedTaskInterface; use Spiral\RoadRunnerBridge\Queue\Dispatcher; +use Spiral\RoadRunnerBridge\RoadRunnerMode; use Spiral\Tests\TestCase; final class DispatcherTest extends TestCase @@ -25,22 +24,13 @@ public function testCanServeShouldReturnFalseWithWrongEnvironment(): void public function testCanServe(): void { - $this->getContainer()->bind(EnvironmentInterface::class, function () { - return new Environment([ - 'RR_MODE' => 'jobs', - ]); - }); - + $this->getContainer()->bind(RoadRunnerMode::class, RoadRunnerMode::Jobs); $this->assertDispatcherCanBeServed(Dispatcher::class); } public function testServeReceivedTask(): void { - $this->getContainer()->bind(EnvironmentInterface::class, function () { - return new Environment([ - 'RR_MODE' => 'jobs', - ]); - }); + $this->getContainer()->bind(RoadRunnerMode::class, RoadRunnerMode::Jobs); $finalizer = $this->mockContainer(FinalizerInterface::class); $finalizer->shouldReceive('finalize')->once()->with(false); @@ -66,11 +56,7 @@ public function testServeReceivedTask(): void public function testServeReceivedTaskWithThrownException(): void { - $this->getContainer()->bind(EnvironmentInterface::class, function () { - return new Environment([ - 'RR_MODE' => 'jobs', - ]); - }); + $this->getContainer()->bind(RoadRunnerMode::class, RoadRunnerMode::Jobs); $e = new \Exception('Something went wrong'); diff --git a/tests/src/Queue/QueueTest.php b/tests/src/Queue/QueueTest.php index 3600647..5e27f61 100644 --- a/tests/src/Queue/QueueTest.php +++ b/tests/src/Queue/QueueTest.php @@ -6,7 +6,6 @@ use Mockery as m; use Spiral\Core\Container; -use Spiral\Queue\Job\CallableJob; use Spiral\Queue\Job\ObjectJob; use Spiral\Queue\Options; use Spiral\RoadRunner\Jobs\Queue\CreateInfoInterface; @@ -112,26 +111,4 @@ public function testPushObject() $this->assertSame('task-id', $this->queue->pushObject($object)); } - - public function testPushCallable() - { - $this->registry->shouldReceive('getPipeline') - ->once() - ->with('default') - ->andReturn($queue = m::mock(QueueInterface::class)); - - $callback = function () { - return 'bar'; - }; - - $preparedTask = m::mock(PreparedTaskInterface::class); - $queuedTask = m::mock(QueuedTaskInterface::class); - $queue->shouldReceive('create')->once() - ->withSomeOfArgs(CallableJob::class, ['callback' => $callback]) - ->andReturn($preparedTask); - $queue->shouldReceive('dispatch')->once()->with($preparedTask)->andReturn($queuedTask); - $queuedTask->shouldReceive('getId')->once()->andReturn('task-id'); - - $this->assertSame('task-id', $this->queue->pushCallable($callback)); - } } diff --git a/tests/src/RoadRunnerModeTest.php b/tests/src/RoadRunnerModeTest.php new file mode 100644 index 0000000..92a53ba --- /dev/null +++ b/tests/src/RoadRunnerModeTest.php @@ -0,0 +1,42 @@ +getContainer()->removeBinding(RoadRunnerMode::class); + } + + /** + * @dataProvider roadRunnerModes + */ + public function testDetectMode(string $mode, RoadRunnerMode $expected): void + { + $this->getContainer()->bind(EnvironmentInterface::class, static fn () => new Environment([ + 'RR_MODE' => $mode, + ])); + + $this->assertSame($expected, $this->getContainer()->get(RoadRunnerMode::class)); + } + + public function roadRunnerModes() + { + return [ + 'http' => ['http', RoadRunnerMode::Http], + 'tcp' => ['tcp', RoadRunnerMode::Tcp], + 'grpc' => ['grpc', RoadRunnerMode::Grpc], + 'temporal' => ['temporal', RoadRunnerMode::Temporal], + 'jobs' => ['jobs', RoadRunnerMode::Jobs], + 'test' => ['test', RoadRunnerMode::Unknown], + ]; + } +} diff --git a/tests/src/Tcp/DispatcherTest.php b/tests/src/Tcp/DispatcherTest.php index 84c6e53..e40f21f 100644 --- a/tests/src/Tcp/DispatcherTest.php +++ b/tests/src/Tcp/DispatcherTest.php @@ -8,11 +8,10 @@ use Spiral\App\Tcp\TestInterceptor; use Spiral\App\Tcp\TestService; use Spiral\Boot\FinalizerInterface; -use Spiral\RoadRunner\Environment; -use Spiral\RoadRunner\EnvironmentInterface; use Spiral\RoadRunner\Payload; use Spiral\RoadRunner\Tcp\TcpWorkerInterface; use Spiral\RoadRunner\WorkerInterface; +use Spiral\RoadRunnerBridge\RoadRunnerMode; use Spiral\RoadRunnerBridge\Tcp\Dispatcher; use Spiral\Tests\TestCase; @@ -25,11 +24,7 @@ public function testCanServeShouldReturnFalseWithWrongEnvironment(): void public function testCanServe(): void { - $this->getContainer()->bind(EnvironmentInterface::class, function () { - return new Environment([ - 'RR_MODE' => Environment\Mode::MODE_TCP, - ]); - }); + $this->getContainer()->bind(RoadRunnerMode::class, RoadRunnerMode::Tcp); $this->assertDispatcherCanBeServed(Dispatcher::class); } @@ -38,11 +33,7 @@ public function testServe(): void { $worker = $this->mockContainer(WorkerInterface::class); - $this->getContainer()->bind(EnvironmentInterface::class, function () { - return new Environment([ - 'RR_MODE' => Environment\Mode::MODE_TCP, - ]); - }); + $this->getContainer()->bind(RoadRunnerMode::class, RoadRunnerMode::Tcp); $this->updateConfig('tcp.services', ['tcp-server' => TestService::class]); $finalizer = $this->mockContainer(FinalizerInterface::class); @@ -75,11 +66,7 @@ public function testServeWithInterceptor(): void { $worker = $this->mockContainer(WorkerInterface::class); - $this->getContainer()->bind(EnvironmentInterface::class, function () { - return new Environment([ - 'RR_MODE' => Environment\Mode::MODE_TCP, - ]); - }); + $this->getContainer()->bind(RoadRunnerMode::class, RoadRunnerMode::Tcp); $this->updateConfig('tcp.services', ['tcp-server' => TestService::class]); $this->updateConfig('tcp.interceptors', ['tcp-server' => TestInterceptor::class]); @@ -119,11 +106,7 @@ public function testServeWithHandleExceptionAndClose(): void { $worker = $this->mockContainer(WorkerInterface::class); - $this->getContainer()->bind(EnvironmentInterface::class, function () { - return new Environment([ - 'RR_MODE' => Environment\Mode::MODE_TCP, - ]); - }); + $this->getContainer()->bind(RoadRunnerMode::class, RoadRunnerMode::Tcp); $this->updateConfig('tcp.services', ['tcp-server' => ServiceWithException::class]); $finalizer = $this->mockContainer(FinalizerInterface::class);