Skip to content

Commit

Permalink
Merge pull request #15 from MacPaw/develop
Browse files Browse the repository at this point in the history
Release v1.1.0
  • Loading branch information
Yozhef authored Sep 8, 2021
2 parents b883cee + 49f2606 commit bf6f8cd
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 28 deletions.
55 changes: 55 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Contribute to BehatMessengerContext

Thank you for contributing!

Before we can merge your Pull-Request here are some guidelines that you need to follow.
These guidelines exist not to annoy you, but to keep the code base clean,
unified and future proof.

## Dependencies

We're using [`composer/composer`](https://github.com/composer/composer) to manage dependencies

## Coding Standard

This project uses [PHP CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) to enforce coding standards.
The coding standard rules are defined in the **phpcs.xml.dist** file (part of this repository).

Your Pull-Request must be compliant with the said standard.
To check your code you can run `composer run code-style`. This command will give you a list of violations in your code (if any).

The most common errors can be automatically fixed just by running `composer run code-style-fix`.

[coding standard homepage]: https://github.com/doctrine/coding-standard

## Static analysing tools

This project uses [PHPStan](https://github.com/phpstan/phpstan) to find errors in code without running it.
The analyser configuration is defined in the **phpstan.neon.dist** file (part of this repository).

Your Pull-Request must be compliant with PHPStan rules.
To check your code you can run `composer run phpstan`. This command will give you a list of violations in your code (if any).

If error can't be fixed you should add it to `ignoreErrors` in **phpstan.neon.dist**

## Unit-Tests

Please try to add a test for your pull-request. This project uses PHPUnit as testing framework.

You can run the unit-tests by calling `composer run phpunit`.

New features without tests can't be merged.

## Issues and Bugs

To create a new issue, you can use the GitHub issue tracking system.

## Getting merged

Please allow us time to review your pull requests. We will give our best to review
everything as fast as possible, but cannot always live up to our own expectations.

Pull requests without tests most probably will not be merged.
Documentation PRs obviously do not require tests.

Thank you very much again for your contribution!
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ In the `config/services_test.yaml` file of your project:
```
BehatMessengerContext\:
resource: '../vendor/macpaw/behat-messenger-context/src/*'
arguments:
- '@test.service_container'
```

Step 3: Configure Messenger
Expand Down
59 changes: 31 additions & 28 deletions src/Context/MessengerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@
use Exception;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Messenger\Transport\InMemoryTransport;
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class MessengerContext implements Context
{
use ArraySimilarTrait;

private ContainerInterface $container;
private NormalizableInterface $normalizable;
private NormalizerInterface $normalizer;

public function __construct(
ContainerInterface $container,
NormalizableInterface $normalizable
NormalizerInterface $normalizer
) {
$this->container = $container;
$this->normalizable = $normalizable;
$this->normalizer = $normalizer;
}

/**
* @Then bus :busName should contain message with JSON:
* @Then transport :transportName should contain message with JSON:
*/
public function busShouldContainMessageWithJson(string $busName, PyStringNode $expectedMessage): void
public function transportShouldContainMessageWithJson(string $transportName, PyStringNode $expectedMessage): void
{
$expectedMessage = $this->decodeExpectedJson($expectedMessage);

$transport = $this->getMessengerTransportByName($busName);
$transport = $this->getMessengerTransportByName($transportName);
$actualMessageList = [];
foreach ($transport->get() as $envelope) {
$actualMessage = $this->convertToArray($envelope->getMessage());
Expand All @@ -48,46 +48,49 @@ public function busShouldContainMessageWithJson(string $busName, PyStringNode $e
throw new Exception(
sprintf(
'The transport doesn\'t contain message with such JSON, actual messages: %s',
$this->getPrettyJson($expectedMessage)
$this->getPrettyJson($actualMessageList)
)
);
}

/**
* @Then bus :busName should contain message with JSON and variable fields :variableFields:
* @Then transport :transportName should contain message with JSON and variable fields :variableFields:
*/
public function busShouldContainMessageWithJsonAndVariableFields(
string $busName,
public function transportShouldContainMessageWithJsonAndVariableFields(
string $transportName,
string $variableFields,
PyStringNode $expectedMessage
): void {
$variableFields = $variableFields ? array_map('trim', explode(',', $variableFields)) : [];
$expectedMessage = $this->decodeExpectedJson($expectedMessage);

$transport = $this->getMessengerTransportByName($busName);
$transport = $this->getMessengerTransportByName($transportName);
$actualMessageList = [];
foreach ($transport->get() as $envelope) {
$actualMessage = $this->convertToArray($envelope->getMessage());
if ($this->isArraysSimilar($actualMessage, $expectedMessage, $variableFields)) {
return;
}

$actualMessageList[] = $actualMessage;
}

throw new Exception(
sprintf(
'The transport doesn\'t contain message with such JSON, actual messages: %s',
$this->getPrettyJson($expectedMessage)
$this->getPrettyJson($actualMessageList)
)
);
}

/**
* @Then all bus :busName messages should be JSON:
* @Then all transport :transportName messages should be JSON:
*/
public function allBusMessagesShouldBeJson(string $busName, PyStringNode $expectedMessageList): void
public function allTransportMessagesShouldBeJson(string $transportName, PyStringNode $expectedMessageList): void
{
$expectedMessageList = $this->decodeExpectedJson($expectedMessageList);

$transport = $this->getMessengerTransportByName($busName);
$transport = $this->getMessengerTransportByName($transportName);
$actualMessageList = [];
foreach ($transport->get() as $envelope) {
$actualMessageList[] = $this->convertToArray($envelope->getMessage());
Expand All @@ -96,25 +99,25 @@ public function allBusMessagesShouldBeJson(string $busName, PyStringNode $expect
if (!$this->isArraysSimilar($actualMessageList, $expectedMessageList)) {
throw new Exception(
sprintf(
'The expected bus messages doesn\'t match actual: %s',
'The expected transport messages doesn\'t match actual: %s',
$this->getPrettyJson($actualMessageList)
)
);
}
}

/**
* @Then all bus :busName messages should be JSON with variable fields :variableFields:
* @Then all transport :transportName messages should be JSON with variable fields :variableFields:
*/
public function allBusMessagesShouldBeJsonWithVariableFields(
string $busName,
public function allTransportMessagesShouldBeJsonWithVariableFields(
string $transportName,
string $variableFields,
PyStringNode $expectedMessageList
): void {
$variableFields = $variableFields ? array_map('trim', explode(',', $variableFields)) : [];
$expectedMessageList = $this->decodeExpectedJson($expectedMessageList);

$transport = $this->getMessengerTransportByName($busName);
$transport = $this->getMessengerTransportByName($transportName);
$actualMessageList = [];
foreach ($transport->get() as $envelope) {
$actualMessageList[] = $this->convertToArray($envelope->getMessage());
Expand All @@ -123,19 +126,19 @@ public function allBusMessagesShouldBeJsonWithVariableFields(
if (!$this->isArraysSimilar($actualMessageList, $expectedMessageList, $variableFields)) {
throw new Exception(
sprintf(
'The expected bus messages doesn\'t match actual: %s',
'The expected transport messages doesn\'t match actual: %s',
$this->getPrettyJson($actualMessageList)
)
);
}
}

/**
* @Then there is :expectationMessageCount messages in bus :busName
* @Then there is :expectationMessageCount messages in transport :transportName
*/
public function thereIsCountMessagesInBus(int $expectedMessageCount, string $busName): void
public function thereIsCountMessagesInTransport(int $expectedMessageCount, string $transportName): void
{
$transport = $this->getMessengerTransportByName($busName);
$transport = $this->getMessengerTransportByName($transportName);
$actualMessageCount = count($transport->get());

if ($actualMessageCount !== $expectedMessageCount) {
Expand Down Expand Up @@ -164,7 +167,7 @@ private function getPrettyJson(array $message)
*/
private function convertToArray($object): array
{
return (array) $this->normalizable->normalize($object);
return (array) $this->normalizer->normalize($object);
}

/**
Expand All @@ -180,9 +183,9 @@ private function decodeExpectedJson(PyStringNode $expectedJson): array
);
}

private function getMessengerTransportByName(string $busName): InMemoryTransport
private function getMessengerTransportByName(string $transportName): InMemoryTransport
{
$fullName = 'messenger.transport.' . $busName;
$fullName = 'messenger.transport.' . $transportName;
$hasTransport = $this->container->has($fullName);

if ($hasTransport === false) {
Expand Down

0 comments on commit bf6f8cd

Please sign in to comment.