From f1857b84172d52cee9fe10900522518f646129a1 Mon Sep 17 00:00:00 2001 From: butschster Date: Wed, 22 Jun 2022 13:17:14 +0400 Subject: [PATCH 1/4] Adds RoadRunnerMode enum --- src/GRPC/Dispatcher.php | 9 ++++----- src/Http/Dispatcher.php | 9 ++++----- src/Queue/Dispatcher.php | 7 +++---- src/RoadRunnerMode.php | 27 +++++++++++++++++++++++++++ src/Tcp/Dispatcher.php | 9 ++++----- 5 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 src/RoadRunnerMode.php 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..338862f --- /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 From 06c462baa6610632cfb3e4b2360f2b314cdb23c4 Mon Sep 17 00:00:00 2001 From: butschster Date: Wed, 22 Jun 2022 13:34:51 +0400 Subject: [PATCH 2/4] Fixes tests --- tests/src/GRPC/DispatcherTest.php | 17 +++-------------- tests/src/Http/DispatcherTest.php | 21 ++++----------------- tests/src/Queue/DispatcherTest.php | 20 ++++---------------- tests/src/Queue/QueueTest.php | 23 ----------------------- tests/src/Tcp/DispatcherTest.php | 25 +++++-------------------- 5 files changed, 16 insertions(+), 90 deletions(-) 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..e0d6233 100644 --- a/tests/src/Queue/DispatcherTest.php +++ b/tests/src/Queue/DispatcherTest.php @@ -14,6 +14,7 @@ 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 +26,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 +58,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/Tcp/DispatcherTest.php b/tests/src/Tcp/DispatcherTest.php index 84c6e53..b9bd272 100644 --- a/tests/src/Tcp/DispatcherTest.php +++ b/tests/src/Tcp/DispatcherTest.php @@ -13,6 +13,7 @@ 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 +26,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 +35,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 +68,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 +108,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); From b6db9c3055a3ca8170d85104b2aa27b8758995cf Mon Sep 17 00:00:00 2001 From: butschster Date: Wed, 22 Jun 2022 15:18:25 +0400 Subject: [PATCH 3/4] Adds tests for RoadRunnerMode enum --- tests/src/RoadRunnerModeTest.php | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 tests/src/RoadRunnerModeTest.php diff --git a/tests/src/RoadRunnerModeTest.php b/tests/src/RoadRunnerModeTest.php new file mode 100644 index 0000000..a206700 --- /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], + ]; + } +} From 1eb90a8e60ecd4500db46191dda605950e6aa016 Mon Sep 17 00:00:00 2001 From: butschster Date: Wed, 22 Jun 2022 15:43:04 +0400 Subject: [PATCH 4/4] Fixes cs --- src/RoadRunnerMode.php | 2 +- tests/src/Queue/DispatcherTest.php | 2 -- tests/src/RoadRunnerModeTest.php | 4 ++-- tests/src/Tcp/DispatcherTest.php | 2 -- 4 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/RoadRunnerMode.php b/src/RoadRunnerMode.php index 338862f..ac743cb 100644 --- a/src/RoadRunnerMode.php +++ b/src/RoadRunnerMode.php @@ -22,6 +22,6 @@ public static function detect(EnvironmentInterface $environment): self { $value = $environment->getMode(); - return (self::tryFrom($value) ?? self::Unknown); + return self::tryFrom($value) ?? self::Unknown; } } diff --git a/tests/src/Queue/DispatcherTest.php b/tests/src/Queue/DispatcherTest.php index e0d6233..0784be2 100644 --- a/tests/src/Queue/DispatcherTest.php +++ b/tests/src/Queue/DispatcherTest.php @@ -9,8 +9,6 @@ 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; diff --git a/tests/src/RoadRunnerModeTest.php b/tests/src/RoadRunnerModeTest.php index a206700..92a53ba 100644 --- a/tests/src/RoadRunnerModeTest.php +++ b/tests/src/RoadRunnerModeTest.php @@ -21,8 +21,8 @@ protected function setUp(): void */ public function testDetectMode(string $mode, RoadRunnerMode $expected): void { - $this->getContainer()->bind(EnvironmentInterface::class, static fn() => new Environment([ - 'RR_MODE' => $mode + $this->getContainer()->bind(EnvironmentInterface::class, static fn () => new Environment([ + 'RR_MODE' => $mode, ])); $this->assertSame($expected, $this->getContainer()->get(RoadRunnerMode::class)); diff --git a/tests/src/Tcp/DispatcherTest.php b/tests/src/Tcp/DispatcherTest.php index b9bd272..e40f21f 100644 --- a/tests/src/Tcp/DispatcherTest.php +++ b/tests/src/Tcp/DispatcherTest.php @@ -8,8 +8,6 @@ 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;