Skip to content

Commit

Permalink
Reset TestTransport on ensureKernelShutdown (#90)
Browse files Browse the repository at this point in the history
* Reset TestTransport on ensureKernelShutdown

* Test

* Fix typo

* Set lowest deps
  • Loading branch information
HypeMC authored Sep 26, 2024
1 parent f0088cc commit d9d711b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"require": {
"php": ">=8.1",
"symfony/deprecation-contracts": "^2.2|^3.0",
"symfony/framework-bundle": "^5.4|^6.0|^7.0",
"symfony/framework-bundle": "^5.4.44|^6.0|^7.0",
"symfony/messenger": "^5.4|^6.0|^7.0",
"zenstruck/assert": "^1.0"
},
Expand Down
22 changes: 22 additions & 0 deletions src/InteractsWithMessenger.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ final protected static function _enableMessagesCollection(): void
TestBus::enableMessagesCollection();
}

/**
* @internal
*
* @before
*/
#[Before]
final protected static function _disableResetOnKernelShutdown(): void
{
TestTransport::disableResetOnKernelShutdown();
}

/**
* @internal
*
Expand All @@ -71,6 +82,17 @@ final protected static function _resetMessengerTransports(): void
TestBus::resetAll();
}

/**
* @internal
*
* @after
*/
#[After]
final protected static function _enableResetOnKernelShutdown(): void
{
TestTransport::enableResetOnKernelShutdown();
}

/**
* @deprecated use transport() instead
*/
Expand Down
20 changes: 20 additions & 0 deletions src/Transport/TestTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ final class TestTransport implements TransportInterface, ListableReceiverInterfa

// this setting applies to all transports
private static bool $enableMessagesCollection = true;
private static bool $resetOnKernelShutdownEnabled = true;

/**
* @internal
Expand Down Expand Up @@ -411,6 +412,25 @@ public function supportsDelayStamp(): bool
return $this->clock && self::$supportDelayStamp[$this->name];
}

public function resetOnKernelShutdown(): void
{
if (!self::$resetOnKernelShutdownEnabled) {
return;
}

$this->reset();
}

public static function enableResetOnKernelShutdown(): void
{
self::$resetOnKernelShutdownEnabled = true;
}

public static function disableResetOnKernelShutdown(): void
{
self::$resetOnKernelShutdownEnabled = false;
}

/**
* @param array<string, Envelope[]> $messagesCollection
*/
Expand Down
5 changes: 4 additions & 1 deletion src/ZenstruckMessengerTestBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,18 @@ public function process(ContainerBuilder $container): void

foreach ($container->findTaggedServiceIds('messenger.receiver') as $id => $tags) {
$name = $id;
$transport = $container->getDefinition($name);

if (!$class = $container->getDefinition($name)->getClass()) {
if (!$class = $transport->getClass()) {
continue;
}

if (!\is_a($class, TransportInterface::class, true)) {
continue;
}

$transport->addTag('kernel.reset', ['method' => 'resetOnKernelShutdown', 'on_invalid' => 'ignore']);

foreach ($tags as $tag) {
if (isset($tag['alias'])) {
$name = $tag['alias'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Messenger\MessageBusInterface;
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageA;
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageAHandler;
use Zenstruck\Messenger\Test\Transport\TestTransport;
use Zenstruck\Messenger\Test\Transport\TestTransportRegistry;

/**
Expand All @@ -24,6 +25,12 @@
*/
class NotInteractsWithMessengerBeforeTest extends KernelTestCase
{
public static function setUpBeforeClass(): void
{
// Reset to default value to emulate first test in suite behavior
TestTransport::enableMessagesCollection();
}

/**
* @test
*/
Expand Down

0 comments on commit d9d711b

Please sign in to comment.