-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a writer that dispatch in a bus each item as message (#64)
* Replace MessageBusInterface mocks with test dummies * Add an item writer that will dispatch in a bus each written item as a message * Add DispatchEachItemAsMessageWriter in main doc * Remove phpspec/prophecy-phpunit dev dependency from packages that are no longer using it
- Loading branch information
1 parent
9460b82
commit 3f3acb0
Showing
9 changed files
with
153 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
todo \Yokai\Batch\Bridge\Symfony\Messenger\Writer\DispatchEachItemAsMessageWriter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yokai\Batch\Bridge\Symfony\Messenger\Writer; | ||
|
||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Yokai\Batch\Exception\UnexpectedValueException; | ||
use Yokai\Batch\Job\Item\ItemWriterInterface; | ||
use Yokai\Batch\Job\JobExecutionAwareInterface; | ||
use Yokai\Batch\Job\JobExecutionAwareTrait; | ||
|
||
/** | ||
* This {@see ItemWriterInterface} will consider each written item to be a message. | ||
* Every item will be sent individually to a {@see MessageBusInterface}. | ||
*/ | ||
final class DispatchEachItemAsMessageWriter implements ItemWriterInterface, JobExecutionAwareInterface | ||
{ | ||
use JobExecutionAwareTrait; | ||
|
||
public function __construct( | ||
private MessageBusInterface $messageBus, | ||
) { | ||
} | ||
|
||
public function write(iterable $items): void | ||
{ | ||
foreach ($items as $item) { | ||
if (!\is_object($item)) { | ||
throw UnexpectedValueException::type('object', $item); | ||
} | ||
$this->messageBus->dispatch($item); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yokai\Batch\Tests\Bridge\Symfony\Messenger\Dummy; | ||
|
||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
|
||
final class BufferingMessageBus implements MessageBusInterface | ||
{ | ||
/** | ||
* @var Envelope[] | ||
*/ | ||
private array $envelopes = []; | ||
|
||
public function dispatch(object $message, array $stamps = []): Envelope | ||
{ | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yokai\Batch\Tests\Bridge\Symfony\Messenger\Dummy; | ||
|
||
final class DummyMessage | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yokai\Batch\Tests\Bridge\Symfony\Messenger\Dummy; | ||
|
||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Exception\ExceptionInterface; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
|
||
final class FailingMessageBus implements MessageBusInterface | ||
{ | ||
public function __construct( | ||
private ExceptionInterface $exception, | ||
) { | ||
} | ||
|
||
public function dispatch(object $message, array $stamps = []): Envelope | ||
{ | ||
throw $this->exception; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yokai\Batch\Tests\Bridge\Symfony\Messenger\Writer; | ||
|
||
use Yokai\Batch\Bridge\Symfony\Messenger\Writer\DispatchEachItemAsMessageWriter; | ||
use PHPUnit\Framework\TestCase; | ||
use Yokai\Batch\Exception\UnexpectedValueException; | ||
use Yokai\Batch\Tests\Bridge\Symfony\Messenger\Dummy\BufferingMessageBus; | ||
use Yokai\Batch\Tests\Bridge\Symfony\Messenger\Dummy\DummyMessage; | ||
|
||
class DispatchEachItemAsMessageWriterTest extends TestCase | ||
{ | ||
public function test(): void | ||
{ | ||
$writer = new DispatchEachItemAsMessageWriter($messageBus = new BufferingMessageBus()); | ||
$writer->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]); | ||
} | ||
} |