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 Nov 18, 2024
1 parent ec8e8ef commit a747b43
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ jobs:
- '6.3.*'
- '6.4.*'
- '7.0.*'
- '7.1.*'
include:
- php: '8.2'
coverage: 'xdebug'
symfony-versions: '7.0.*'
symfony-versions: '7.1.*'


name: PHP ${{ matrix.php }} Symfony ${{ matrix.symfony-versions }} ${{ matrix.description }}
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
"phpstan/phpstan": "^1.9",
"phpunit/phpunit": "^9.3",
"slevomat/coding-standard": "^7.0",
"squizlabs/php_codesniffer": "^3.6",
"zenstruck/messenger-test": "^1.11"
"squizlabs/php_codesniffer": "^3.6"
},
"suggest": {
"zenstruck/messenger-test": "To use Zentruck messages clearing"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@
<rule ref="Squiz.Arrays.ArrayBracketSpacing"/>

<file>src/</file>
<file>tests/</file>
<file>tests/Unit</file>
</ruleset>
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ parameters:
excludes_analyse:
paths:
- src
- tests/Unit
level: 5
treatPhpDocTypesAsCertain: false
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
stopOnFailure="false">
<testsuites>
<testsuite name="Symfony Behat Context Test Suite">
<directory>tests</directory>
<directory>tests/Unit</directory>
</testsuite>
</testsuites>
<coverage>
Expand Down
39 changes: 21 additions & 18 deletions src/Context/MessengerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class MessengerContext extends SimilarArray implements Context
{
private ContainerInterface $container;
private NormalizerInterface $normalizer;
private TransportRetriever $transportRetriever;
protected static TransportRetriever $transportRetriever;

public function __construct(
ContainerInterface $container,
Expand All @@ -30,13 +30,13 @@ public function __construct(
) {
$this->container = $container;
$this->normalizer = $normalizer;
$this->transportRetriever = $transportRetriever;
static::$transportRetriever = $transportRetriever;
}

#[BeforeFeature]
public static function startTrackMessages(): void
{
if (class_exists(TestTransport::class)) {
if (class_exists(TestTransport::class) && class_exists(TestBus::class)) {
TestTransport::resetAll();
TestTransport::enableMessagesCollection();
TestTransport::disableResetOnKernelShutdown();
Expand All @@ -47,25 +47,13 @@ public static function startTrackMessages(): void
#[AfterFeature]
public static function stopTrackMessages(): void
{
if (class_exists(TestTransport::class)) {
TestTransport::resetAll();
}
self::clearMessenger();
}

#[BeforeScenario]
public function clearMessenger(): void
public function clearMessengerBeforeScenario(): void
{
if (class_exists(TestTransport::class)) {
TestTransport::resetAll();
} else {
$transports = $this->transportRetriever->getAllTransports();

foreach ($transports as $transport) {
if ($transport instanceof InMemoryTransport) {
$transport->reset();
}
}
}
self::clearMessenger();
}

/**
Expand Down Expand Up @@ -245,4 +233,19 @@ private function getMessengerTransportByName(string $transportName): InMemoryTra
'In memory transport ' . $fullName . ' not found',

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

View check run for this annotation

Codecov / codecov/patch

src/Context/MessengerContext.php#L233

Added line #L233 was not covered by tests
);
}

private static function clearMessenger(): void
{
if (class_exists(TestTransport::class)) {
TestTransport::resetAll();
} else {
$transports = static::$transportRetriever->getAllTransports();

foreach ($transports as $transport) {
if ($transport instanceof InMemoryTransport) {
$transport->reset();
}
}
}
}
}
27 changes: 27 additions & 0 deletions tests/Stub/Zentruck/TestBus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Zenstruck\Messenger\Test\Bus;

class TestBus
{
public const ENABLE_MESSAGES_COLLECTION = 0x256;

private static int $callStacks = 0x1;

public static function enableMessagesCollection(): void
{
self::$callStacks = self::$callStacks | self::ENABLE_MESSAGES_COLLECTION;
}

public static function getResult(): int
{
return self::$callStacks;
}

public static function reset(): void
{
self::$callStacks = 0x1;
}
}
39 changes: 39 additions & 0 deletions tests/Stub/Zentruck/TestTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Zenstruck\Messenger\Test\Transport;

class TestTransport
{
public const RESET_ALL = 0x16;
public const ENABLE_MESSAGES_COLLECTION = 0x256;
public const DISABLE_RESET_ON_KERNEL_SHUTDOWN = 0x1024;
private static int $callStacks = 0x1;

public static function resetAll(): void
{
self::$callStacks = self::$callStacks | self::RESET_ALL;
}

public static function enableMessagesCollection(): void
{
self::$callStacks = self::$callStacks | self::ENABLE_MESSAGES_COLLECTION;
}

public static function disableResetOnKernelShutdown(): void
{
self::$callStacks = self::$callStacks | self::DISABLE_RESET_ON_KERNEL_SHUTDOWN;
}

public static function getResult(): int
{
return self::$callStacks;
}

public static function reset(): void
{
self::$callStacks = 0x1;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BehatMessengerContext\Tests\DependencyInjection;
namespace BehatMessengerContext\Tests\Unit\DependencyInjection;

use BehatMessengerContext\Context\MessengerContext;
use BehatMessengerContext\DependencyInjection\BehatMessengerContextExtension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BehatMessengerContext\Tests\DependencyInjection;
namespace BehatMessengerContext\Tests\Unit\DependencyInjection;

use BehatMessengerContext\DependencyInjection\Configuration;
use PHPUnit\Framework\TestCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BehatMessengerContext\Tests;
namespace BehatMessengerContext\Tests\Unit;

use Behat\Gherkin\Node\PyStringNode;
use BehatMessengerContext\Context\MessengerContext;
Expand All @@ -15,6 +15,7 @@
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Contracts\Service\ServiceProviderInterface;

class MessengerContextTest extends TestCase
{
Expand Down Expand Up @@ -447,4 +448,88 @@ public function testFailAllTransportMessagesShouldBeJsonWithVariableFields(): vo
$expectedJson
);
}

public function testTransportWasReset(): 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)
))->clearMessengerBeforeScenario();
}

public function testTransportWasResetWithZentruck(): void
{
require_once __DIR__ . '/../Stub/Zentruck/TestBus.php';
require_once __DIR__ . '/../Stub/Zentruck/TestTransport.php';

$transportClass = 'Zenstruck\Messenger\Test\Transport\TestTransport';

$transportClass::reset();
$this->messengerContext::stopTrackMessages();

$this->assertEquals(
$transportClass::RESET_ALL,
$transportClass::getResult() & $transportClass::RESET_ALL
);

$this->assertNotEquals(
$transportClass::DISABLE_RESET_ON_KERNEL_SHUTDOWN,
$transportClass::getResult() & $transportClass::DISABLE_RESET_ON_KERNEL_SHUTDOWN
);

$this->assertNotEquals(
$transportClass::ENABLE_MESSAGES_COLLECTION,
$transportClass::getResult() & $transportClass::ENABLE_MESSAGES_COLLECTION
);
}

public function testClearWithZentruck(): void
{
require_once __DIR__ . '/../Stub/Zentruck/TestBus.php';
require_once __DIR__ . '/../Stub/Zentruck/TestTransport.php';

$transportClass = 'Zenstruck\Messenger\Test\Transport\TestTransport';
$busClass = 'Zenstruck\Messenger\Test\Bus\TestBus';

$transportClass::reset();
$busClass::reset();
$this->messengerContext::startTrackMessages();

$this->assertEquals(
$transportClass::RESET_ALL,
$transportClass::getResult() & $transportClass::RESET_ALL
);

$this->assertEquals(
$transportClass::DISABLE_RESET_ON_KERNEL_SHUTDOWN,
$transportClass::getResult() & $transportClass::DISABLE_RESET_ON_KERNEL_SHUTDOWN
);

$this->assertEquals(
$transportClass::ENABLE_MESSAGES_COLLECTION,
$transportClass::getResult() & $transportClass::ENABLE_MESSAGES_COLLECTION
);

$this->assertEquals(
$busClass::ENABLE_MESSAGES_COLLECTION,
$busClass::getResult() & $busClass::ENABLE_MESSAGES_COLLECTION
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BehatMessengerContext\Tests;
namespace BehatMessengerContext\Tests\Unit;

use BehatMessengerContext\Context\TransportRetriever;
use PHPUnit\Framework\TestCase;
Expand Down

0 comments on commit a747b43

Please sign in to comment.