diff --git a/README.md b/README.md index e5ed466..c9cb988 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ composer require yokai/batch-symfony-messenger This package provides: - a [job launcher](docs/job-launcher.md) that uses messages to launch jobs +- a [writer](docs/dispatch-each-item-writer.md) that will write each item as a message ## Contribution diff --git a/composer.json b/composer.json index 38a6903..804a679 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,6 @@ } }, "require-dev": { - "phpspec/prophecy-phpunit": "^2.0", "phpunit/phpunit": "^9.5" }, "autoload-dev": { diff --git a/docs/dispatch-each-item-writer.md b/docs/dispatch-each-item-writer.md new file mode 100644 index 0000000..5d21cfd --- /dev/null +++ b/docs/dispatch-each-item-writer.md @@ -0,0 +1 @@ +todo \Yokai\Batch\Bridge\Symfony\Messenger\Writer\DispatchEachItemAsMessageWriter diff --git a/src/Writer/DispatchEachItemAsMessageWriter.php b/src/Writer/DispatchEachItemAsMessageWriter.php new file mode 100644 index 0000000..86d3849 --- /dev/null +++ b/src/Writer/DispatchEachItemAsMessageWriter.php @@ -0,0 +1,35 @@ +messageBus->dispatch($item); + } + } +} diff --git a/tests/DispatchMessageJobLauncherTest.php b/tests/DispatchMessageJobLauncherTest.php index 84bd45d..3c9569c 100644 --- a/tests/DispatchMessageJobLauncherTest.php +++ b/tests/DispatchMessageJobLauncherTest.php @@ -5,11 +5,7 @@ namespace Yokai\Batch\Tests\Bridge\Symfony\Messenger; use PHPUnit\Framework\TestCase; -use Prophecy\Argument; -use Prophecy\PhpUnit\ProphecyTrait; -use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Exception\TransportException; -use Symfony\Component\Messenger\MessageBusInterface; use Yokai\Batch\BatchStatus; use Yokai\Batch\Bridge\Symfony\Messenger\DispatchMessageJobLauncher; use Yokai\Batch\Bridge\Symfony\Messenger\LaunchJobMessage; @@ -17,29 +13,17 @@ use Yokai\Batch\Factory\UniqidJobExecutionIdGenerator; use Yokai\Batch\Test\Factory\SequenceJobExecutionIdGenerator; use Yokai\Batch\Test\Storage\InMemoryJobExecutionStorage; +use Yokai\Batch\Tests\Bridge\Symfony\Messenger\Dummy\BufferingMessageBus; +use Yokai\Batch\Tests\Bridge\Symfony\Messenger\Dummy\FailingMessageBus; final class DispatchMessageJobLauncherTest extends TestCase { - use ProphecyTrait; - public function testLaunch(): void { - $messageBus = $this->prophesize(MessageBusInterface::class); - $messageAssertions = Argument::that( - static function ($message): bool { - return $message instanceof LaunchJobMessage - && $message->getJobName() === 'testing' - && $message->getConfiguration() === ['_id' => '123456789', 'foo' => ['bar']]; - } - ); - $messageBus->dispatch($messageAssertions) - ->shouldBeCalled() - ->willReturn(new Envelope(new LaunchJobMessage('unused'))); - $jobLauncher = new DispatchMessageJobLauncher( new JobExecutionFactory(new UniqidJobExecutionIdGenerator()), $storage = new InMemoryJobExecutionStorage(), - $messageBus->reveal() + $messageBus = new BufferingMessageBus() ); $jobExecutionFromLauncher = $jobLauncher->launch('testing', ['_id' => '123456789', 'foo' => ['bar']]); @@ -51,26 +35,15 @@ static function ($message): bool { self::assertSame('123456789', $jobExecutionFromStorage->getId()); self::assertSame(BatchStatus::PENDING, $jobExecutionFromStorage->getStatus()->getValue()); self::assertSame(['bar'], $jobExecutionFromStorage->getParameters()->get('foo')); + self::assertJobWasTriggered($messageBus, 'testing', ['_id' => '123456789', 'foo' => ['bar']]); } public function testLaunchWithNoId(): void { - $messageBus = $this->prophesize(MessageBusInterface::class); - $messageAssertions = Argument::that( - static function ($message): bool { - return $message instanceof LaunchJobMessage - && $message->getJobName() === 'testing' - && $message->getConfiguration() === ['_id' => '123456789']; - } - ); - $messageBus->dispatch($messageAssertions) - ->shouldBeCalled() - ->willReturn(new Envelope(new LaunchJobMessage('unused'))); - $jobLauncher = new DispatchMessageJobLauncher( new JobExecutionFactory(new SequenceJobExecutionIdGenerator(['123456789'])), $storage = new InMemoryJobExecutionStorage(), - $messageBus->reveal() + $messageBus = new BufferingMessageBus() ); $jobExecutionFromLauncher = $jobLauncher->launch('testing'); @@ -81,19 +54,15 @@ static function ($message): bool { self::assertSame('testing', $jobExecutionFromStorage->getJobName()); self::assertSame('123456789', $jobExecutionFromStorage->getId()); self::assertSame(BatchStatus::PENDING, $jobExecutionFromStorage->getStatus()->getValue()); + self::assertJobWasTriggered($messageBus, 'testing', ['_id' => '123456789']); } public function testLaunchAndMessengerFail(): void { - $messageBus = $this->prophesize(MessageBusInterface::class); - $messageBus->dispatch(Argument::any()) - ->shouldBeCalled() - ->willThrow(new TransportException('This is a test')); - $jobLauncher = new DispatchMessageJobLauncher( new JobExecutionFactory(new UniqidJobExecutionIdGenerator()), $storage = new InMemoryJobExecutionStorage(), - $messageBus->reveal() + new FailingMessageBus(new TransportException('This is a test')) ); $jobExecutionFromLauncher = $jobLauncher->launch('testing'); @@ -108,4 +77,14 @@ public function testLaunchAndMessengerFail(): void self::assertSame(TransportException::class, $failure->getClass()); self::assertSame('This is a test', $failure->getMessage()); } + + private static function assertJobWasTriggered(BufferingMessageBus $bus, string $jobName, array $config): void + { + $messages = $bus->getMessages(); + self::assertCount(1, $messages); + $message = $messages[0]; + self::assertInstanceOf(LaunchJobMessage::class, $message); + self::assertSame($jobName, $message->getJobName()); + self::assertSame($config, $message->getConfiguration()); + } } diff --git a/tests/Dummy/BufferingMessageBus.php b/tests/Dummy/BufferingMessageBus.php new file mode 100644 index 0000000..37a99c9 --- /dev/null +++ b/tests/Dummy/BufferingMessageBus.php @@ -0,0 +1,39 @@ +envelopes[] = $envelope = new Envelope($message, $stamps); + + return $envelope; + } + + /** + * @return object[] + */ + public function getMessages(): array + { + return \array_map(fn (Envelope $envelope) => $envelope->getMessage(), $this->envelopes); + } + + /** + * @return Envelope[] + */ + public function getEnvelopes(): array + { + return $this->envelopes; + } +} diff --git a/tests/Dummy/DummyMessage.php b/tests/Dummy/DummyMessage.php new file mode 100644 index 0000000..ba95630 --- /dev/null +++ b/tests/Dummy/DummyMessage.php @@ -0,0 +1,9 @@ +exception; + } +} diff --git a/tests/Writer/DispatchEachItemAsMessageWriterTest.php b/tests/Writer/DispatchEachItemAsMessageWriterTest.php new file mode 100644 index 0000000..7e6fa5c --- /dev/null +++ b/tests/Writer/DispatchEachItemAsMessageWriterTest.php @@ -0,0 +1,29 @@ +write([$message1 = new DummyMessage(), $message2 = new DummyMessage()]); + self::assertSame([$message1, $message2], $messageBus->getMessages()); + } + + public function testInvalidItemType(): void + { + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage('Expecting argument to be object, but got int.'); + $writer = new DispatchEachItemAsMessageWriter(new BufferingMessageBus()); + $writer->write([1]); + } +}