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

Remove DoctrineClearEntityManagerWorkerSubscriber during process #44

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 25 additions & 3 deletions src/Transport/TestTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Zenstruck\Messenger\Test\Transport;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
use Symfony\Component\Messenger\Event\WorkerRunningEvent;
Expand Down Expand Up @@ -32,6 +33,9 @@ final class TestTransport implements TransportInterface
private MessageBusInterface $bus;
private SerializerInterface $serializer;

/** @var EventSubscriberInterface[] */
private array $subscribersToRemove;

/** @var array<string, bool> */
private static array $intercept = [];

Expand Down Expand Up @@ -59,16 +63,24 @@ final class TestTransport implements TransportInterface
/**
* @internal
*
* @param array<string,bool> $options
* @param EventSubscriberInterface[] $subscribersToRemove
* @param array<string,bool> $options
*/
public function __construct(string $name, MessageBusInterface $bus, EventDispatcherInterface $dispatcher, SerializerInterface $serializer, array $options = [])
{
public function __construct(
string $name,
MessageBusInterface $bus,
EventDispatcherInterface $dispatcher,
SerializerInterface $serializer,
array $subscribersToRemove,
array $options = []
) {
$options = \array_merge(self::DEFAULT_OPTIONS, $options);

$this->name = $name;
$this->dispatcher = $dispatcher;
$this->bus = $bus;
$this->serializer = $serializer;
$this->subscribersToRemove = $subscribersToRemove;

self::$intercept[$name] ??= $options['intercept'];
self::$catchExceptions[$name] ??= $options['catch_exceptions'];
Expand Down Expand Up @@ -131,6 +143,11 @@ public function process(int $number = -1): self
$listeners = [];
$subscribers = [];

// remove default subscribers
foreach ($this->subscribersToRemove as $subscriber) {
$this->dispatcher->removeSubscriber($subscriber);
}

$this->dispatcher->addListener(
WorkerRunningEvent::class,
$listeners[WorkerRunningEvent::class] = static function(WorkerRunningEvent $event) use (&$processCount) {
Expand Down Expand Up @@ -171,6 +188,11 @@ public function process(int $number = -1): self
$this->dispatcher->removeSubscriber($subscriber);
}

// re-add default subscribers
foreach ($this->subscribersToRemove as $subscriber) {
$this->dispatcher->addSubscriber($subscriber);
}

if ($number > 0) {
Assert::that($processCount)->is($number, 'Expected to process {expected} messages but only processed {actual}.');
}
Expand Down
18 changes: 17 additions & 1 deletion src/Transport/TestTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Zenstruck\Messenger\Test\Transport;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\TransportFactoryInterface;
Expand All @@ -18,6 +19,9 @@ final class TestTransportFactory implements TransportFactoryInterface
private MessageBusInterface $bus;
private EventDispatcherInterface $dispatcher;

/** @var EventSubscriberInterface[] */
private array $subscribersToRemove = [];

public function __construct(MessageBusInterface $bus, EventDispatcherInterface $dispatcher)
{
$this->bus = $bus;
Expand All @@ -26,14 +30,26 @@ public function __construct(MessageBusInterface $bus, EventDispatcherInterface $

public function createTransport(string $dsn, array $options, SerializerInterface $serializer): TransportInterface // @phpstan-ignore-line
{
return new TestTransport($options['transport_name'], $this->bus, $this->dispatcher, $serializer, $this->parseDsn($dsn));
return new TestTransport(
$options['transport_name'],
$this->bus,
$this->dispatcher,
$serializer,
$this->subscribersToRemove,
$this->parseDsn($dsn)
);
}

public function supports(string $dsn, array $options): bool // @phpstan-ignore-line
{
return 0 === \mb_strpos($dsn, 'test://');
}

public function registerSubscriberToRemove(EventSubscriberInterface $subscriber): void
{
$this->subscribersToRemove[] = $subscriber;
}

/**
* @return array<string,bool>
*/
Expand Down
7 changes: 7 additions & 0 deletions src/ZenstruckMessengerTestBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,12 @@ public function process(ContainerBuilder $container): void

$registry->addMethodCall('register', [$name, new Reference($id)]);
}

$factory = $container->getDefinition('zenstruck_messenger_test.transport_factory');

if ($container->hasDefinition($id = 'doctrine.orm.messenger.event_subscriber.doctrine_clear_entity_manager')) {
// we want to remove this subscriber so the em isn't reset when processing messages
$factory->addMethodCall('registerSubscriberToRemove', [new Reference($id)]);
}
}
}