Skip to content

Commit a747b43

Browse files
committed
Add reset message queues before scenario with zentruck/messenger-test
1 parent ec8e8ef commit a747b43

12 files changed

+185
-27
lines changed

.github/workflows/ci.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ jobs:
1818
- '6.3.*'
1919
- '6.4.*'
2020
- '7.0.*'
21+
- '7.1.*'
2122
include:
2223
- php: '8.2'
2324
coverage: 'xdebug'
24-
symfony-versions: '7.0.*'
25+
symfony-versions: '7.1.*'
2526

2627

2728
name: PHP ${{ matrix.php }} Symfony ${{ matrix.symfony-versions }} ${{ matrix.description }}

composer.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@
3838
"phpstan/phpstan": "^1.9",
3939
"phpunit/phpunit": "^9.3",
4040
"slevomat/coding-standard": "^7.0",
41-
"squizlabs/php_codesniffer": "^3.6",
42-
"zenstruck/messenger-test": "^1.11"
41+
"squizlabs/php_codesniffer": "^3.6"
42+
},
43+
"suggest": {
44+
"zenstruck/messenger-test": "To use Zentruck messages clearing"
4345
},
4446
"autoload": {
4547
"psr-4": {

phpcs.xml.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@
4040
<rule ref="Squiz.Arrays.ArrayBracketSpacing"/>
4141

4242
<file>src/</file>
43-
<file>tests/</file>
43+
<file>tests/Unit</file>
4444
</ruleset>

phpstan.neon

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ parameters:
22
excludes_analyse:
33
paths:
44
- src
5+
- tests/Unit
56
level: 5
67
treatPhpDocTypesAsCertain: false

phpunit.xml.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
stopOnFailure="false">
1212
<testsuites>
1313
<testsuite name="Symfony Behat Context Test Suite">
14-
<directory>tests</directory>
14+
<directory>tests/Unit</directory>
1515
</testsuite>
1616
</testsuites>
1717
<coverage>

src/Context/MessengerContext.php

+21-18
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class MessengerContext extends SimilarArray implements Context
2121
{
2222
private ContainerInterface $container;
2323
private NormalizerInterface $normalizer;
24-
private TransportRetriever $transportRetriever;
24+
protected static TransportRetriever $transportRetriever;
2525

2626
public function __construct(
2727
ContainerInterface $container,
@@ -30,13 +30,13 @@ public function __construct(
3030
) {
3131
$this->container = $container;
3232
$this->normalizer = $normalizer;
33-
$this->transportRetriever = $transportRetriever;
33+
static::$transportRetriever = $transportRetriever;
3434
}
3535

3636
#[BeforeFeature]
3737
public static function startTrackMessages(): void
3838
{
39-
if (class_exists(TestTransport::class)) {
39+
if (class_exists(TestTransport::class) && class_exists(TestBus::class)) {
4040
TestTransport::resetAll();
4141
TestTransport::enableMessagesCollection();
4242
TestTransport::disableResetOnKernelShutdown();
@@ -47,25 +47,13 @@ public static function startTrackMessages(): void
4747
#[AfterFeature]
4848
public static function stopTrackMessages(): void
4949
{
50-
if (class_exists(TestTransport::class)) {
51-
TestTransport::resetAll();
52-
}
50+
self::clearMessenger();
5351
}
5452

5553
#[BeforeScenario]
56-
public function clearMessenger(): void
54+
public function clearMessengerBeforeScenario(): void
5755
{
58-
if (class_exists(TestTransport::class)) {
59-
TestTransport::resetAll();
60-
} else {
61-
$transports = $this->transportRetriever->getAllTransports();
62-
63-
foreach ($transports as $transport) {
64-
if ($transport instanceof InMemoryTransport) {
65-
$transport->reset();
66-
}
67-
}
68-
}
56+
self::clearMessenger();
6957
}
7058

7159
/**
@@ -245,4 +233,19 @@ private function getMessengerTransportByName(string $transportName): InMemoryTra
245233
'In memory transport ' . $fullName . ' not found',
246234
);
247235
}
236+
237+
private static function clearMessenger(): void
238+
{
239+
if (class_exists(TestTransport::class)) {
240+
TestTransport::resetAll();
241+
} else {
242+
$transports = static::$transportRetriever->getAllTransports();
243+
244+
foreach ($transports as $transport) {
245+
if ($transport instanceof InMemoryTransport) {
246+
$transport->reset();
247+
}
248+
}
249+
}
250+
}
248251
}

tests/Stub/Zentruck/TestBus.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Zenstruck\Messenger\Test\Bus;
6+
7+
class TestBus
8+
{
9+
public const ENABLE_MESSAGES_COLLECTION = 0x256;
10+
11+
private static int $callStacks = 0x1;
12+
13+
public static function enableMessagesCollection(): void
14+
{
15+
self::$callStacks = self::$callStacks | self::ENABLE_MESSAGES_COLLECTION;
16+
}
17+
18+
public static function getResult(): int
19+
{
20+
return self::$callStacks;
21+
}
22+
23+
public static function reset(): void
24+
{
25+
self::$callStacks = 0x1;
26+
}
27+
}

tests/Stub/Zentruck/TestTransport.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Zenstruck\Messenger\Test\Transport;
6+
7+
class TestTransport
8+
{
9+
public const RESET_ALL = 0x16;
10+
public const ENABLE_MESSAGES_COLLECTION = 0x256;
11+
public const DISABLE_RESET_ON_KERNEL_SHUTDOWN = 0x1024;
12+
private static int $callStacks = 0x1;
13+
14+
public static function resetAll(): void
15+
{
16+
self::$callStacks = self::$callStacks | self::RESET_ALL;
17+
}
18+
19+
public static function enableMessagesCollection(): void
20+
{
21+
self::$callStacks = self::$callStacks | self::ENABLE_MESSAGES_COLLECTION;
22+
}
23+
24+
public static function disableResetOnKernelShutdown(): void
25+
{
26+
self::$callStacks = self::$callStacks | self::DISABLE_RESET_ON_KERNEL_SHUTDOWN;
27+
}
28+
29+
public static function getResult(): int
30+
{
31+
return self::$callStacks;
32+
}
33+
34+
public static function reset(): void
35+
{
36+
self::$callStacks = 0x1;
37+
}
38+
}
39+

tests/DependencyInjection/BehatMessengerContextExtensionTest.php renamed to tests/Unit/DependencyInjection/BehatMessengerContextExtensionTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace BehatMessengerContext\Tests\DependencyInjection;
5+
namespace BehatMessengerContext\Tests\Unit\DependencyInjection;
66

77
use BehatMessengerContext\Context\MessengerContext;
88
use BehatMessengerContext\DependencyInjection\BehatMessengerContextExtension;

tests/DependencyInjection/ConfigurationTest.php renamed to tests/Unit/DependencyInjection/ConfigurationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace BehatMessengerContext\Tests\DependencyInjection;
5+
namespace BehatMessengerContext\Tests\Unit\DependencyInjection;
66

77
use BehatMessengerContext\DependencyInjection\Configuration;
88
use PHPUnit\Framework\TestCase;

tests/MessengerContextTest.php renamed to tests/Unit/MessengerContextTest.php

+86-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace BehatMessengerContext\Tests;
5+
namespace BehatMessengerContext\Tests\Unit;
66

77
use Behat\Gherkin\Node\PyStringNode;
88
use BehatMessengerContext\Context\MessengerContext;
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Messenger\Envelope;
1616
use Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport;
1717
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
18+
use Symfony\Contracts\Service\ServiceProviderInterface;
1819

1920
class MessengerContextTest extends TestCase
2021
{
@@ -447,4 +448,88 @@ public function testFailAllTransportMessagesShouldBeJsonWithVariableFields(): vo
447448
$expectedJson
448449
);
449450
}
451+
452+
public function testTransportWasReset(): void
453+
{
454+
$serviceProvider = $this->createMock(ServiceProviderInterface::class);
455+
$serviceProvider
456+
->expects($this->once())
457+
->method('getProvidedServices')
458+
->willReturn(['messenger.transport.test']);
459+
460+
$serviceProvider
461+
->expects(self::once())
462+
->method('get')
463+
->with('messenger.transport.test')
464+
->willReturn($this->inMemoryTransport);
465+
466+
$this->inMemoryTransport
467+
->expects($this->once())
468+
->method('reset');
469+
470+
(new MessengerContext(
471+
$this->container,
472+
$this->normalizer,
473+
new TransportRetriever($serviceProvider)
474+
))->clearMessengerBeforeScenario();
475+
}
476+
477+
public function testTransportWasResetWithZentruck(): void
478+
{
479+
require_once __DIR__ . '/../Stub/Zentruck/TestBus.php';
480+
require_once __DIR__ . '/../Stub/Zentruck/TestTransport.php';
481+
482+
$transportClass = 'Zenstruck\Messenger\Test\Transport\TestTransport';
483+
484+
$transportClass::reset();
485+
$this->messengerContext::stopTrackMessages();
486+
487+
$this->assertEquals(
488+
$transportClass::RESET_ALL,
489+
$transportClass::getResult() & $transportClass::RESET_ALL
490+
);
491+
492+
$this->assertNotEquals(
493+
$transportClass::DISABLE_RESET_ON_KERNEL_SHUTDOWN,
494+
$transportClass::getResult() & $transportClass::DISABLE_RESET_ON_KERNEL_SHUTDOWN
495+
);
496+
497+
$this->assertNotEquals(
498+
$transportClass::ENABLE_MESSAGES_COLLECTION,
499+
$transportClass::getResult() & $transportClass::ENABLE_MESSAGES_COLLECTION
500+
);
501+
}
502+
503+
public function testClearWithZentruck(): void
504+
{
505+
require_once __DIR__ . '/../Stub/Zentruck/TestBus.php';
506+
require_once __DIR__ . '/../Stub/Zentruck/TestTransport.php';
507+
508+
$transportClass = 'Zenstruck\Messenger\Test\Transport\TestTransport';
509+
$busClass = 'Zenstruck\Messenger\Test\Bus\TestBus';
510+
511+
$transportClass::reset();
512+
$busClass::reset();
513+
$this->messengerContext::startTrackMessages();
514+
515+
$this->assertEquals(
516+
$transportClass::RESET_ALL,
517+
$transportClass::getResult() & $transportClass::RESET_ALL
518+
);
519+
520+
$this->assertEquals(
521+
$transportClass::DISABLE_RESET_ON_KERNEL_SHUTDOWN,
522+
$transportClass::getResult() & $transportClass::DISABLE_RESET_ON_KERNEL_SHUTDOWN
523+
);
524+
525+
$this->assertEquals(
526+
$transportClass::ENABLE_MESSAGES_COLLECTION,
527+
$transportClass::getResult() & $transportClass::ENABLE_MESSAGES_COLLECTION
528+
);
529+
530+
$this->assertEquals(
531+
$busClass::ENABLE_MESSAGES_COLLECTION,
532+
$busClass::getResult() & $busClass::ENABLE_MESSAGES_COLLECTION
533+
);
534+
}
450535
}

tests/TransportRetrieverTest.php renamed to tests/Unit/TransportRetrieverTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace BehatMessengerContext\Tests;
5+
namespace BehatMessengerContext\Tests\Unit;
66

77
use BehatMessengerContext\Context\TransportRetriever;
88
use PHPUnit\Framework\TestCase;

0 commit comments

Comments
 (0)