Skip to content

Commit

Permalink
fix: symfony container wiring fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
IiiigorGG committed Sep 5, 2021
1 parent 1e83e34 commit 9e18b35
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
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 9e18b35

Please sign in to comment.