Skip to content

Commit

Permalink
Fixes FallbackDispatcher name and small code refactoring (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
butschster committed Feb 20, 2023
1 parent 9f411f0 commit 78d3418
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 155 deletions.
4 changes: 2 additions & 2 deletions src/Bootloader/RoadRunnerBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
});
}
Expand Down
45 changes: 0 additions & 45 deletions src/FailbackDispatcher.php

This file was deleted.

53 changes: 53 additions & 0 deletions src/FallbackDispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunnerBridge;

use Spiral\Boot\DispatcherInterface;
use Spiral\RoadRunnerBridge\Exception\DispatcherNotFoundException;
use Spiral\RoadRunnerBridge\Centrifugo\Dispatcher as Centrifugo;
use Spiral\RoadRunnerBridge\GRPC\Dispatcher as GRPC;
use Spiral\RoadRunnerBridge\Http\Dispatcher as Http;
use Spiral\RoadRunnerBridge\Queue\Dispatcher as Queue;
use Spiral\RoadRunnerBridge\Tcp\Dispatcher as Tcp;

final class FallbackDispatcher implements DispatcherInterface
{
private const ERROR = 'To use RoadRunner in `%s` mode, please register dispatcher `%s`.';

public function __construct(
private readonly RoadRunnerMode $mode,
) {
}

public function canServe(): bool
{
return \PHP_SAPI === 'cli' && $this->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<DispatcherInterface> $class
*
* @throws DispatcherNotFoundException
*/
private function throwException(string $class): void
{
throw new DispatcherNotFoundException(\sprintf(self::ERROR, $this->mode->name, $class));
}
}
5 changes: 4 additions & 1 deletion src/GRPC/Interceptor/Invoker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions tests/src/Bootloader/QueueBootloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/src/Bootloader/RoadRunnerBootloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)]);
}
}
52 changes: 0 additions & 52 deletions tests/src/FailbackDispatcherTest.php

This file was deleted.

67 changes: 67 additions & 0 deletions tests/src/FallbackDispatcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Spiral\Tests;

use Spiral\RoadRunnerBridge\Exception\DispatcherNotFoundException;
use Spiral\RoadRunnerBridge\FallbackDispatcher;
use Spiral\RoadRunnerBridge\RoadRunnerMode;

final class FallbackDispatcherTest extends TestCase
{
/**
* @dataProvider canServeDataProvider
*/
public function testCanServe(RoadRunnerMode $mode, bool $expected): void
{
$dispatcher = new FallbackDispatcher($mode);

$this->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.',
];
}
}
66 changes: 35 additions & 31 deletions tests/src/Queue/QueueManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,63 @@
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');
$connection = $this->accessProtected($core, 'connection');

$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']));
}
}
Loading

0 comments on commit 78d3418

Please sign in to comment.