Skip to content

Commit

Permalink
Add reset message queues before scenario with zentruck/messenger-test
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiidonii committed Oct 20, 2024
1 parent 1640b33 commit ab34cbb
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 30 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ You can use regular expressions to validate messages that contain dynamic or var
### Verify Message Count in a Transport
Ensure that a specific number of messages exist in a given transport.

### Auto clean queue messages before scenario
Check details in [documentation](docs/MessengerContext/clear_transport_with_zentruck.md)

* Documentation: [Count Messages in Transport](docs/MessengerContext/count_message_transport.md)

[master Build Status]: https://github.com/macpaw/behat-messenger-context/actions?query=workflow%3ACI+branch%3Amaster
Expand Down
43 changes: 43 additions & 0 deletions docs/MessengerContext/clear_transport_with_zentruck.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Clear queues messages before scenario with zentruck

## Installation

1. Install the library:

```bash
composer require --dev zenstruck/messenger-test
```
2. If not added automatically by Symfony Flex, add the bundle in `config/bundles.php`:

```php
Zenstruck\Messenger\Test\ZenstruckMessengerTestBundle::class => ['test' => true],
```

3. Update `config/packages/messenger.yaml` and override your transport(s)
in your `test` environment with `test://`:

```yaml
# config/packages/messenger.yaml
# ...
when@test:
framework:
messenger:
transports:
async: test://
```

## Transport

You can interact with the test transports in your tests by using the
`InteractsWithMessenger` trait in your `KernelTestCase`/`WebTestCase` tests.
You can assert the different steps of message processing by asserting on the queue
and the different states of message processing like "acknowledged", "rejected" and so on.

> **Note**: If you only need to know if a message has been dispatched you can
> make assertions [on the bus itself](#bus).

More details you can see in [origin package repository](https://github.com/zenstruck/messenger-test)

Zentruck will be used automatically after installation.
39 changes: 34 additions & 5 deletions src/Context/MessengerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@

use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Hook\AfterFeature;
use Behat\Hook\AfterScenario;
use Behat\Hook\BeforeFeature;
use Behat\Hook\BeforeScenario;
use Exception;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport;
use Symfony\Component\Messenger\Transport\Sync\SyncTransport;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Zenstruck\Messenger\Test\Bus\TestBus;
use Zenstruck\Messenger\Test\InteractsWithMessenger;
use Zenstruck\Messenger\Test\Transport\TestTransport;

class MessengerContext implements Context
{
Expand All @@ -29,14 +35,37 @@ public function __construct(
$this->transportRetriever = $transportRetriever;
}

#[BeforeFeature]

Check warning on line 38 in src/Context/MessengerContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/MessengerContext.php#L38

Added line #L38 was not covered by tests
public static function startTrackMessages(): void
{
if (class_exists(TestTransport::class)) {
TestTransport::resetAll();
TestTransport::enableMessagesCollection();
TestTransport::disableResetOnKernelShutdown();
TestBus::enableMessagesCollection();

Check warning on line 45 in src/Context/MessengerContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/MessengerContext.php#L41-L45

Added lines #L41 - L45 were not covered by tests
}
}

#[AfterFeature]

Check warning on line 49 in src/Context/MessengerContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/MessengerContext.php#L49

Added line #L49 was not covered by tests
public static function stopTrackMessages(): void
{
if (class_exists(TestTransport::class)) {
TestTransport::resetAll();

Check warning on line 53 in src/Context/MessengerContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/MessengerContext.php#L52-L53

Added lines #L52 - L53 were not covered by tests
}
}

#[BeforeScenario]

Check warning on line 57 in src/Context/MessengerContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/MessengerContext.php#L57

Added line #L57 was not covered by tests
public function clearMessenger(): void
{
$transports = $this->transportRetriever->getAllTransports();

foreach ($transports as $transport) {
if ($transport instanceof InMemoryTransport) {
$transport->reset();
if (class_exists(TestTransport::class)) {
TestTransport::resetAll();

Check warning on line 61 in src/Context/MessengerContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/MessengerContext.php#L60-L61

Added lines #L60 - L61 were not covered by tests
} else {
$transports = $this->transportRetriever->getAllTransports();

Check warning on line 63 in src/Context/MessengerContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/MessengerContext.php#L63

Added line #L63 was not covered by tests

foreach ($transports as $transport) {
if ($transport instanceof InMemoryTransport) {
$transport->reset();

Check warning on line 67 in src/Context/MessengerContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/MessengerContext.php#L65-L67

Added lines #L65 - L67 were not covered by tests
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/messenger_context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<services>
<service public="true" autowire="true" id="BehatMessengerContext\Context\MessengerContext" class="BehatMessengerContext\Context\MessengerContext">
<argument key="$container" type="service" id="test.service_container"/>
<argument key="$transportRetriever" type="service" id="BehatMessengerContext\Context\TransportRetriever"/>
</service>
<service public="true" autowire="true" id="BehatMessengerContext\Context\TransportRetriever" class="BehatMessengerContext\Context\TransportRetriever">
<argument key="$receiverLocator" type="service" id="messenger.receiver_locator"/>
Expand Down
25 changes: 0 additions & 25 deletions tests/MessengerContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,6 @@ protected function setUp(): void
);
}

public function testClearMessenger(): void
{
$serviceProvider = $this->createMock(ServiceProviderInterface::class);
$serviceProvider
->expects($this->once())
->method('getProvidedServices')
->willReturn(['messenger.transport.test']);

$serviceProvider
->expects(self::once())
->method('get')
->with('messenger.transport.test')
->willReturn($this->inMemoryTransport);

$this->inMemoryTransport
->expects($this->once())
->method('reset');

(new MessengerContext(
$this->container,
$this->normalizer,
new TransportRetriever($serviceProvider),
))->clearMessenger();
}

public function testTransportShouldContainMessageWithJson(): void
{
$message = new \stdClass();
Expand Down

0 comments on commit ab34cbb

Please sign in to comment.