Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset TestTransport on ensureKernelShutdown #90

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
nikophil marked this conversation as resolved.
Show resolved Hide resolved
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
Loading