Skip to content

Commit

Permalink
fix: allows to use options configuration (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
aegypius committed Apr 18, 2024
1 parent 60bc93d commit 7164fce
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 19 deletions.
36 changes: 18 additions & 18 deletions src/Transport/TestTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,47 +352,47 @@ public static function disableMessagesCollection(): void
self::$enableMessagesCollection = false;
}

/**
* @param array<string, Envelope[]> $messagesCollection
*/
private function collectMessage(array &$messagesCollection, Envelope $envelope, bool $force = false): void
{
if (!self::$enableMessagesCollection && !$force) {
return;
}

$messagesCollection[$this->name] ??= [];
$messagesCollection[$this->name][] = $envelope;
}

private function isIntercepting(): bool
public function isIntercepting(): bool
{
return self::$intercept[$this->name];
}

private function isCatchingExceptions(): bool
public function isCatchingExceptions(): bool
{
return self::$catchExceptions[$this->name];
}

private function shouldTestSerialization(): bool
public function shouldTestSerialization(): bool
{
return self::$testSerialization[$this->name];
}

private function isRetriesDisabled(): bool
public function isRetriesDisabled(): bool
{
return self::$disableRetries[$this->name];
}

/**
* @phpstan-assert-if-true !null $this->clock
*/
private function supportsDelayStamp(): bool
public function supportsDelayStamp(): bool
{
return $this->clock && self::$supportDelayStamp[$this->name];
}

/**
* @param array<string, Envelope[]> $messagesCollection
*/
private function collectMessage(array &$messagesCollection, Envelope $envelope, bool $force = false): void
{
if (!self::$enableMessagesCollection && !$force) {
return;
}

$messagesCollection[$this->name] ??= [];
$messagesCollection[$this->name][] = $envelope;
}

private function hasMessagesToProcess(): bool
{
return !empty(self::$queue[$this->name] ?? []);
Expand Down
2 changes: 1 addition & 1 deletion src/Transport/TestTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(private MessageBusInterface $bus, private EventDispa

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->clock, $this->parseDsn($dsn));
return new TestTransport($options['transport_name'], $this->bus, $this->dispatcher, $serializer, $this->clock, \array_merge($this->parseDsn($dsn), $options));
}

public function supports(string $dsn, array $options): bool // @phpstan-ignore-line
Expand Down
129 changes: 129 additions & 0 deletions tests/Transport/TestTransportFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

declare(strict_types=1);

/*
* This file is part of the zenstruck/messenger-test package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Messenger\Test\Tests\Transport;

use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;
use Psr\Clock\ClockInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Zenstruck\Messenger\Test\Transport\TestTransport;
use Zenstruck\Messenger\Test\Transport\TestTransportFactory;

/** @covers \Zenstruck\Messenger\Test\Transport\TestTransportFactory */
final class TestTransportFactoryTest extends TestCase
{
private Stub&MessageBusInterface $bus;
private Stub&EventDispatcherInterface $dispatcher;
private Stub&ClockInterface $clock;
private Stub&SerializerInterface $serializer;

protected function setUp(): void
{
// Reset statics
TestTransport::resetAll();

$this->bus = $this->createStub(MessageBusInterface::class);
$this->dispatcher = $this->createStub(EventDispatcherInterface::class);
$this->clock = $this->createStub(ClockInterface::class);
$this->serializer = $this->createStub(SerializerInterface::class);
}

/**
* @dataProvider provideCreateTransportCases
*
* @param array<string, bool> $options
* @param array<string, bool> $expectedOptions
*
* @covers \Zenstruck\Messenger\Test\Transport\TestTransport::isIntercepting()
* @covers \Zenstruck\Messenger\Test\Transport\TestTransport::isCatchingExceptions()
* @covers \Zenstruck\Messenger\Test\Transport\TestTransport::shouldTestSerialization()
* @covers \Zenstruck\Messenger\Test\Transport\TestTransport::isRetriesDisabled()
* @covers \Zenstruck\Messenger\Test\Transport\TestTransport::supportsDelayStamp()
*
* @test
*/
public function create_transport(string $dsn, array $options, array $expectedOptions): void
{
$factory = new TestTransportFactory(
$this->bus,
$this->dispatcher,
$this->clock
);

$transport = $factory->createTransport($dsn, [
'transport_name' => 'some-transport-name',
] + $options, $this->serializer);
self::assertInstanceOf(TestTransport::class, $transport);
self::assertEquals($expectedOptions, [
'intercept' => $transport->isIntercepting(),
'catch_exceptions' => $transport->isCatchingExceptions(),
'test_serialization' => $transport->shouldTestSerialization(),
'disable_retries' => $transport->isRetriesDisabled(),
'support_delay_stamp' => $transport->supportsDelayStamp(),
]);
}

/**
* @return iterable<array{string, array<string, bool>, array<string, bool>}>
*/
public static function provideCreateTransportCases(): iterable
{
$defaults = [
'intercept' => true,
'catch_exceptions' => true,
'test_serialization' => true,
'disable_retries' => true,
'support_delay_stamp' => false,
];

yield 'pass options by dsn only' => ['test://?intercept=false&support_delay_stamp=true', [], [
'intercept' => false,
'support_delay_stamp' => true,
] + $defaults];

yield 'pass options by options only' => ['test://', [
'intercept' => false,
'support_delay_stamp' => true,
], [
'intercept' => false,
'support_delay_stamp' => true,
] + $defaults];

yield 'pass options by dns and options only' => ['test://?catch_exceptions=false&support_delay_stamp=false', [
'catch_exceptions' => true,
'support_delay_stamp' => true,
], [
'catch_exceptions' => true,
'support_delay_stamp' => true,
] + $defaults];
}

/**
* @testWith ["test://", true]
* ["another-test://", false]
*
* @test
*/
public function support(string $dsn, bool $expectedSupport): void
{
$factory = new TestTransportFactory(
$this->bus,
$this->dispatcher,
$this->clock
);
self::assertSame($expectedSupport, $factory->supports($dsn, []));
}
}

0 comments on commit 7164fce

Please sign in to comment.