Skip to content

Commit 797dc94

Browse files
authored
Merge pull request #40 from MacPaw/feat/update-docs
feat: update documentation for context
2 parents 6b8c2f7 + 89ea1d0 commit 797dc94

9 files changed

+258
-80
lines changed

.github/workflows/ci.yaml

+4-3
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ jobs:
7474
if: matrix.coverage == 'xdebug'
7575

7676
- name: Run codecov
77-
uses: codecov/codecov-action@v1
77+
uses: codecov/codecov-action@v4.0.1
7878
if: matrix.coverage == 'xdebug'
7979
with:
80-
file: './coverage.xml'
81-
fail_ci_if_error: true
80+
token: ${{ secrets.CODECOV_TOKEN }}
81+
file: './coverage.xml'
82+
fail_ci_if_error: true

README.md

+18-70
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,35 @@
1-
Behat Messenger Context Bundle
2-
=================================
1+
# Behat Messenger Context Bundle
32

43
| Version | Build Status | Code Coverage |
54
|:---------:|:-------------:|:-----:|
65
| `master`| [![CI][master Build Status Image]][master Build Status] | [![Coverage Status][master Code Coverage Image]][master Code Coverage] |
76
| `develop`| [![CI][develop Build Status Image]][develop Build Status] | [![Coverage Status][develop Code Coverage Image]][develop Code Coverage] |
87

9-
Installation
10-
============
8+
This repository provides custom Behat step definitions for working with Symfony Messenger transports. It includes functionality for checking messages in transports, validating them against expected JSON structures, and working with variable fields.
119

12-
Step 1: Download the Bundle
13-
----------------------------------
14-
Open a command console, enter your project directory and execute:
10+
## Installation
1511

16-
### Applications that use Symfony Flex [in progress](https://github.com/MacPaw/BehatRedisContext/issues/2)
12+
To install the MessengerContext and integrate it with your Behat setup, follow the instructions provided in the [Installation Guide](docs/install.md).
1713

18-
```console
19-
$ composer require --dev macpaw/behat-messenger-context
20-
```
14+
## Available Features
2115

22-
### Applications that don't use Symfony Flex
16+
### Check a Specific Message in a Transport
17+
You can verify if a specific message exists in a given transport.
18+
* Documentation: [Check Transport Message](docs/MessengerContext/check_transport_message.md)
2319

24-
Open a command console, enter your project directory and execute the
25-
following command to download the latest stable version of this bundle:
20+
### Check All Messages in a Transport
21+
Verify if all messages in a given transport match the expected JSON structure.
22+
* Documentation: [Check All Transport Messages](docs/MessengerContext/check_all_transport_message.md)
2623

27-
```console
28-
$ composer require --dev macpaw/behat-messenger-context
29-
```
24+
### Check Messages with Regular Expressions
25+
You can use regular expressions to validate messages that contain dynamic or variable data.
26+
* Documentation for specific message: [Check Transport Message with Regexp](docs/MessengerContext/check_transport_message_regexp.md)
27+
* Documentation for all messages: [Check All Transport Messages with Regexp](docs/MessengerContext/check_all_transport_message_regexp.md)
3028

31-
This command requires you to have Composer installed globally, as explained
32-
in the [installation chapter](https://getcomposer.org/doc/00-intro.md)
33-
of the Composer documentation.
29+
### Verify Message Count in a Transport
30+
Ensure that a specific number of messages exist in a given transport.
3431

35-
36-
Then, enable the bundle by adding it to the list of registered bundles
37-
in the `app/AppKernel.php` file of your project:
38-
39-
```php
40-
<?php
41-
// app/AppKernel.php
42-
43-
// ...
44-
class AppKernel extends Kernel
45-
{
46-
public function registerBundles()
47-
{
48-
$bundles = array(
49-
// ...
50-
BehatMessengerContext\BehatMessengerContextBundle::class => ['test' => true],
51-
);
52-
53-
// ...
54-
}
55-
56-
// ...
57-
}
58-
```
59-
60-
Step 2: Configure Messenger
61-
=============
62-
Copying `config/packages/dev/messenger.yaml` and pasting that into `config/packages/test/`. This gives us messenger configuration that will only be used in the test environment. Uncomment the code, and replace sync with in-memory. Do that for both of the transports.
63-
64-
```yaml
65-
framework:
66-
messenger:
67-
transports:
68-
async: 'in-memory://'
69-
async_priority_high: 'in-memory://'
70-
...
71-
...
72-
```
73-
74-
75-
Step 3: Configure Behat
76-
=============
77-
Go to `behat.yml`
78-
79-
```yaml
80-
...
81-
contexts:
82-
- BehatMessengerContext\Context\MessengerContext
83-
...
84-
```
32+
* Documentation: [Count Messages in Transport](docs/MessengerContext/count_message_transport.md)
8533

8634
[master Build Status]: https://github.com/macpaw/behat-messenger-context/actions?query=workflow%3ACI+branch%3Amaster
8735
[master Build Status Image]: https://github.com/macpaw/behat-messenger-context/workflows/CI/badge.svg?branch=master
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Behat Custom Step: Verify All Messages in a Transport
2+
3+
This documentation outlines the purpose and usage of the custom Behat step definition for verifying that all messages in a specific Symfony Messenger transport match a list of expected JSON structures.
4+
5+
## Purpose
6+
7+
This function is designed to validate that all messages in a specific transport (e.g., asynchronous queues) match a given set of expected JSON structures. It is useful in scenarios where you need to ensure that every message in the transport adheres to a specific structure or content format.
8+
9+
## Function Overview
10+
11+
### Signature:
12+
```gherkin
13+
Then all transport ":transportName" messages should be JSON:
14+
"""
15+
[
16+
{
17+
"event": "status_updated",
18+
"time": "123123123",
19+
"payload": "dqwdwdwdw"
20+
},
21+
{
22+
"event": "order_created",
23+
"time": "456456456",
24+
"payload": "otherPayload"
25+
}
26+
]
27+
"""
28+
29+
```
30+
31+
### Parameters:
32+
- `transportName` (string): The name of the transport (e.g., 'async') where the message is expected to be found.
33+
- `expectedMessage` PyStringNode): A JSON array representing the expected content of all messages that should be present in the transport.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Behat Custom Step: Verify All Messages in Transport with Variable Fields
2+
3+
This documentation outlines the purpose and usage of the custom Behat step definition for verifying that all messages in a specific Symfony Messenger transport match a list of expected JSON structures, while allowing certain fields to vary across the messages.
4+
## Purpose
5+
6+
This function is designed to validate that all messages in a specific transport (e.g., asynchronous queues) match a given set of expected JSON structures, while allowing certain fields (e.g., timestamps, unique IDs) to have variable values. This is useful in scenarios where specific fields in the messages may differ but the overall structure and format must remain consistent.## Function Overview
7+
8+
### Signature:
9+
```php
10+
/**
11+
* @Then all transport :transportName messages should be JSON with variable fields :variableFields:
12+
*/
13+
public function allTransportMessagesShouldBeJsonWithVariableFields(
14+
string $transportName,
15+
string $variableFields,
16+
PyStringNode $expectedMessageList
17+
): void
18+
```
19+
20+
### Parameters:
21+
- `transportName` (string): The name of the transport (e.g., 'webhook') where the message is expected to be found.
22+
- `variableFields` (string): A comma-separated list of fields where values may vary and should be compared using regular expressions.
23+
- `expectedMessage` (PyStringNode): The expected message content in JSON format, where fields marked with `~` in their values will be treated as regular expressions.
24+
25+
```gherkin
26+
Then all transport ":transportName" messages should be JSON with variable fields ":variableFields":
27+
"""
28+
[
29+
{
30+
"event": "status_updated",
31+
"time": "~^\\d{13}$",
32+
"payload": "dqwdwdwdw"
33+
},
34+
{
35+
"event": "order_created",
36+
"time": "~^\\d{13}$",
37+
"payload": "otherPayload"
38+
}
39+
]
40+
"""
41+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
# Behat Custom Step: Transport JSON Message Assertion
3+
4+
This documentation outlines the purpose and usage of the custom Behat step definition for verifying if a Symfony Messenger transport contains a message with a specific JSON structure.
5+
6+
## Purpose
7+
8+
This function is designed to check if a given transport (such as an asynchronous queue) contains a message that matches a particular JSON structure. It is useful in scenarios where you need to validate that a message was correctly dispatched with the expected content during Behat tests.
9+
10+
## Function Overview
11+
12+
### Signature:
13+
```gherkin
14+
Then transport ":transportName" should contain message with JSON:
15+
"""
16+
{
17+
"event": "status_updated",
18+
"time": "123123123",
19+
"payload": "dqwdwdwdw"
20+
}
21+
"""
22+
```
23+
24+
### Parameters:
25+
- `transportName` (string): The name of the transport (e.g., 'async') where the message is expected to be found.
26+
- `expectedMessage` (PyStringNode): The expected message content in JSON format that should be present in the transport.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
# Behat Custom Step: Transport JSON Message Assertion with Regexp Fields
3+
4+
This documentation outlines the purpose and usage of the custom Behat step definition for verifying if a Symfony Messenger transport contains a message with a specific JSON structure, using regular expressions to handle dynamic fields.
5+
## Purpose
6+
7+
This function is designed to check if a given transport (such as an asynchronous queue) contains a message that matches a particular JSON structure, while allowing certain fields to be validated using regular expressions. It is particularly useful when testing messages with dynamic data, such as timestamps, unique identifiers, or payloads, where the exact value cannot be guaranteed.
8+
## Function Overview
9+
10+
### Signature:
11+
```php
12+
/**
13+
* @Then transport :transportName should contain message with JSON and variable fields :variableFields:
14+
*/
15+
public function transportShouldContainMessageWithJsonAndVariableFields(
16+
string $transportName,
17+
string $variableFields,
18+
PyStringNode $expectedMessage
19+
): void
20+
```
21+
22+
### Parameters:
23+
- `transportName` (string): The name of the transport (e.g., 'webhook') where the message is expected to be found.
24+
- `variableFields` (string): A comma-separated list of field names where values should be matched using regular expressions.
25+
- `expectedMessage` (PyStringNode): The expected message content in JSON format, where fields marked with `~` in their values will be treated as regular expressions.
26+
27+
```gherkin
28+
And transport "webhook" should contain message with JSON and variable fields "time, payload":
29+
"""
30+
{
31+
"event": "customer_agreement_status_updated",
32+
"time": "~^\\d{13}$",
33+
"payload": "~^\\{.*\\}$"
34+
}
35+
"""
36+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Behat Custom Step: Verify Message Count in a Transport
2+
3+
This documentation outlines the purpose and usage of the custom Behat step definition for verifying that a specific number of messages exist in a given Symfony Messenger transport.
4+
5+
## Purpose
6+
7+
This function is designed to check that the number of messages in a specific transport (e.g., asynchronous queues) matches the expected count. It is useful in scenarios where you need to ensure that the correct number of messages have been dispatched into the transport during a Behat test.
8+
9+
## Function Overview
10+
11+
### Signature:
12+
```php
13+
/**
14+
* @Then there is :expectationMessageCount messages in transport :transportName
15+
*/
16+
public function thereIsCountMessagesInTransport(int $expectedMessageCount, string $transportName): void
17+
```
18+
19+
### Parameters:
20+
- `expectedMessageCount` (int): The expected number of messages that should be present in the transport.
21+
- `transportName` (string): The name of the transport (e.g., 'async') where the messages are expected to be found.
22+
23+
```gherkin
24+
Then there is 1 messages in transport "command"
25+
```

docs/install.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
# Installation Guide
3+
4+
## Step 1: Install the Bundle
5+
To begin, navigate to your project directory and use Composer to download the bundle.
6+
7+
### For applications using Symfony Flex:
8+
Simply run the following command:
9+
10+
```bash
11+
composer require --dev macpaw/behat-messenger-context
12+
```
13+
14+
### For applications without Symfony Flex:
15+
If your project doesn't use Symfony Flex, run the same command:
16+
17+
```bash
18+
composer require --dev macpaw/behat-messenger-context
19+
```
20+
21+
Make sure that Composer is installed globally on your machine. If not, refer to the [Composer installation guide](https://getcomposer.org/doc/00-intro.md) for assistance.
22+
23+
Next, you'll need to manually register the bundle in the `AppKernel.php` file. Add the following line to the `registerBundles` method:
24+
25+
```php
26+
// app/AppKernel.php
27+
28+
class AppKernel extends Kernel
29+
{
30+
public function registerBundles()
31+
{
32+
$bundles = [
33+
// Other bundles
34+
BehatMessengerContext\BehatMessengerContextBundle::class => ['test' => true],
35+
];
36+
}
37+
}
38+
```
39+
40+
## Step 2: Configure Symfony Messenger for Testing
41+
To ensure your messenger configuration is properly set up for testing, you need to copy the configuration from `config/packages/dev/messenger.yaml` to `config/packages/test/messenger.yaml`. Then, update the configuration to use in-memory transport for testing.
42+
43+
```yaml
44+
framework:
45+
messenger:
46+
transports:
47+
async: 'in-memory://'
48+
async_priority_high: 'in-memory://'
49+
```
50+
51+
OR in your global config file `config/packages/messenger.yaml`
52+
```yaml
53+
framework:
54+
messenger:
55+
transports:
56+
async: 'in-memory://'
57+
async_priority_high: 'in-memory://'
58+
```
59+
60+
This ensures that your tests use an in-memory transport rather than actual services.
61+
62+
## Step 3: Behat Configuration
63+
In your `behat.yml` file, make sure the Messenger context is registered:
64+
65+
```yaml
66+
default:
67+
suites:
68+
default:
69+
contexts:
70+
- BehatMessengerContext\Context\MessengerContext
71+
```
72+
73+
This completes the setup for integrating Behat with Symfony Messenger in your project. Now you're ready to run tests with the in-memory transport!

src/Context/MessengerContext.php

+2-7
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,10 @@
1414

1515
class MessengerContext extends SimilarArray implements Context
1616
{
17-
private ContainerInterface $container;
18-
private NormalizerInterface $normalizer;
19-
2017
public function __construct(
21-
ContainerInterface $container,
22-
NormalizerInterface $normalizer
18+
private readonly ContainerInterface $container,
19+
private readonly NormalizerInterface $normalizer
2320
) {
24-
$this->container = $container;
25-
$this->normalizer = $normalizer;
2621
}
2722

2823
/**

0 commit comments

Comments
 (0)