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

fix: allows to use options configuration #80

Merged
merged 5 commits into from
Apr 18, 2024
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
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, []));
}
}
Loading