Skip to content

Commit ab34cbb

Browse files
committed
Add reset message queues before scenario with zentruck/messenger-test
1 parent 1640b33 commit ab34cbb

File tree

5 files changed

+81
-30
lines changed

5 files changed

+81
-30
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ You can use regular expressions to validate messages that contain dynamic or var
2929
### Verify Message Count in a Transport
3030
Ensure that a specific number of messages exist in a given transport.
3131

32+
### Auto clean queue messages before scenario
33+
Check details in [documentation](docs/MessengerContext/clear_transport_with_zentruck.md)
34+
3235
* Documentation: [Count Messages in Transport](docs/MessengerContext/count_message_transport.md)
3336

3437
[master Build Status]: https://github.com/macpaw/behat-messenger-context/actions?query=workflow%3ACI+branch%3Amaster
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Clear queues messages before scenario with zentruck
2+
3+
## Installation
4+
5+
1. Install the library:
6+
7+
```bash
8+
composer require --dev zenstruck/messenger-test
9+
```
10+
2. If not added automatically by Symfony Flex, add the bundle in `config/bundles.php`:
11+
12+
```php
13+
Zenstruck\Messenger\Test\ZenstruckMessengerTestBundle::class => ['test' => true],
14+
```
15+
16+
3. Update `config/packages/messenger.yaml` and override your transport(s)
17+
in your `test` environment with `test://`:
18+
19+
```yaml
20+
# config/packages/messenger.yaml
21+
22+
# ...
23+
24+
when@test:
25+
framework:
26+
messenger:
27+
transports:
28+
async: test://
29+
```
30+
31+
## Transport
32+
33+
You can interact with the test transports in your tests by using the
34+
`InteractsWithMessenger` trait in your `KernelTestCase`/`WebTestCase` tests.
35+
You can assert the different steps of message processing by asserting on the queue
36+
and the different states of message processing like "acknowledged", "rejected" and so on.
37+
38+
> **Note**: If you only need to know if a message has been dispatched you can
39+
> make assertions [on the bus itself](#bus).
40+
41+
More details you can see in [origin package repository](https://github.com/zenstruck/messenger-test)
42+
43+
Zentruck will be used automatically after installation.

src/Context/MessengerContext.php

+34-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66

77
use Behat\Behat\Context\Context;
88
use Behat\Gherkin\Node\PyStringNode;
9+
use Behat\Hook\AfterFeature;
10+
use Behat\Hook\AfterScenario;
11+
use Behat\Hook\BeforeFeature;
912
use Behat\Hook\BeforeScenario;
1013
use Exception;
1114
use Symfony\Component\DependencyInjection\ContainerInterface;
1215
use Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport;
1316
use Symfony\Component\Messenger\Transport\Sync\SyncTransport;
1417
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
18+
use Zenstruck\Messenger\Test\Bus\TestBus;
19+
use Zenstruck\Messenger\Test\InteractsWithMessenger;
20+
use Zenstruck\Messenger\Test\Transport\TestTransport;
1521

1622
class MessengerContext implements Context
1723
{
@@ -29,14 +35,37 @@ public function __construct(
2935
$this->transportRetriever = $transportRetriever;
3036
}
3137

38+
#[BeforeFeature]
39+
public static function startTrackMessages(): void
40+
{
41+
if (class_exists(TestTransport::class)) {
42+
TestTransport::resetAll();
43+
TestTransport::enableMessagesCollection();
44+
TestTransport::disableResetOnKernelShutdown();
45+
TestBus::enableMessagesCollection();
46+
}
47+
}
48+
49+
#[AfterFeature]
50+
public static function stopTrackMessages(): void
51+
{
52+
if (class_exists(TestTransport::class)) {
53+
TestTransport::resetAll();
54+
}
55+
}
56+
3257
#[BeforeScenario]
3358
public function clearMessenger(): void
3459
{
35-
$transports = $this->transportRetriever->getAllTransports();
36-
37-
foreach ($transports as $transport) {
38-
if ($transport instanceof InMemoryTransport) {
39-
$transport->reset();
60+
if (class_exists(TestTransport::class)) {
61+
TestTransport::resetAll();
62+
} else {
63+
$transports = $this->transportRetriever->getAllTransports();
64+
65+
foreach ($transports as $transport) {
66+
if ($transport instanceof InMemoryTransport) {
67+
$transport->reset();
68+
}
4069
}
4170
}
4271
}

src/Resources/config/messenger_context.xml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<services>
77
<service public="true" autowire="true" id="BehatMessengerContext\Context\MessengerContext" class="BehatMessengerContext\Context\MessengerContext">
88
<argument key="$container" type="service" id="test.service_container"/>
9+
<argument key="$transportRetriever" type="service" id="BehatMessengerContext\Context\TransportRetriever"/>
910
</service>
1011
<service public="true" autowire="true" id="BehatMessengerContext\Context\TransportRetriever" class="BehatMessengerContext\Context\TransportRetriever">
1112
<argument key="$receiverLocator" type="service" id="messenger.receiver_locator"/>

tests/MessengerContextTest.php

-25
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,6 @@ protected function setUp(): void
4747
);
4848
}
4949

50-
public function testClearMessenger(): void
51-
{
52-
$serviceProvider = $this->createMock(ServiceProviderInterface::class);
53-
$serviceProvider
54-
->expects($this->once())
55-
->method('getProvidedServices')
56-
->willReturn(['messenger.transport.test']);
57-
58-
$serviceProvider
59-
->expects(self::once())
60-
->method('get')
61-
->with('messenger.transport.test')
62-
->willReturn($this->inMemoryTransport);
63-
64-
$this->inMemoryTransport
65-
->expects($this->once())
66-
->method('reset');
67-
68-
(new MessengerContext(
69-
$this->container,
70-
$this->normalizer,
71-
new TransportRetriever($serviceProvider),
72-
))->clearMessenger();
73-
}
74-
7550
public function testTransportShouldContainMessageWithJson(): void
7651
{
7752
$message = new \stdClass();

0 commit comments

Comments
 (0)