Passed
Push — master ( b2b84f...109e7c )
by Thomas Mauro
03:02
created

FailedMessagesRetryCommandFactoryTest::testFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 9.1781
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\Laminas\Messenger\Test\Factory\Command;
6
7
use PHPUnit\Framework\TestCase;
8
use Prophecy\PhpUnit\ProphecyTrait;
9
use Psr\Container\ContainerInterface;
10
use Psr\Log\LoggerInterface;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
use Symfony\Component\Messenger\Command\FailedMessagesRetryCommand;
13
use Symfony\Component\Messenger\RoutableMessageBus;
14
use Symfony\Component\Messenger\Transport\TransportInterface;
15
use Symfony\Contracts\Service\ServiceProviderInterface;
16
use TMV\Laminas\Messenger\Exception\InvalidArgumentException;
17
use TMV\Laminas\Messenger\Factory\Command\FailedMessagesRetryCommandFactory;
18
19
class FailedMessagesRetryCommandFactoryTest extends TestCase
20
{
21
    use ProphecyTrait;
22
23
    public function testFactory(): void
24
    {
25
        $container = $this->prophesize(ContainerInterface::class);
26
27
        $container->has('config')->willReturn(true);
28
        $container->get('config')->willReturn([
29
            'messenger' => [
30
                'failure_transport' => 'failed',
31
                'event_dispatcher' => 'my.event_dispatcher',
32
                'logger' => 'my.logger',
33
                'transports' => [],
34
            ],
35
        ]);
36
37
        $receiversLocator = $this->prophesize(ServiceProviderInterface::class);
38
        $failedTransport = $this->prophesize(TransportInterface::class);
39
        $routableMessageBus = $this->prophesize(RoutableMessageBus::class);
40
        $logger = $this->prophesize(LoggerInterface::class);
41
        $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
42
43
        $container->get('messenger.receivers_locator')
44
            ->shouldBeCalled()
45
            ->willReturn($receiversLocator->reveal());
46
47
        $container->get('messenger.routable_message_bus')
48
            ->shouldBeCalled()
49
            ->willReturn($routableMessageBus->reveal());
50
51
        $container->get('my.logger')
52
            ->shouldBeCalled()
53
            ->willReturn($logger->reveal());
54
55
        $container->get('my.event_dispatcher')
56
            ->shouldBeCalled()
57
            ->willReturn($eventDispatcher->reveal());
58
59
        $factory = new FailedMessagesRetryCommandFactory();
60
61
        $service = $factory($container->reveal());
62
63
        $this->assertInstanceOf(FailedMessagesRetryCommand::class, $service);
64
    }
65
66
    public function testFactoryWithoutOptionalDependencies(): void
67
    {
68
        $container = $this->prophesize(ContainerInterface::class);
69
70
        $container->has('config')->willReturn(true);
71
        $container->get('config')->willReturn([
72
            'messenger' => [
73
                'failure_transport' => 'failed',
74
                'event_dispatcher' => 'messenger.event_dispatcher',
75
                'logger' => null,
76
                'transports' => [],
77
            ],
78
        ]);
79
80
        $receiversLocator = $this->prophesize(ServiceProviderInterface::class);
81
        $failedTransport = $this->prophesize(TransportInterface::class);
82
        $routableMessageBus = $this->prophesize(RoutableMessageBus::class);
83
        $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
84
85
        $container->get('messenger.receivers_locator')
86
            ->shouldBeCalled()
87
            ->willReturn($receiversLocator->reveal());
88
89
        $container->get('messenger.routable_message_bus')
90
            ->shouldBeCalled()
91
            ->willReturn($routableMessageBus->reveal());
92
93
        $container->get('messenger.logger')
94
            ->shouldNotBeCalled();
95
96
        $container->get('messenger.event_dispatcher')
97
            ->shouldBeCalled()
98
            ->willReturn($eventDispatcher->reveal());
99
100
        $factory = new FailedMessagesRetryCommandFactory();
101
102
        $service = $factory($container->reveal());
103
104
        $this->assertInstanceOf(FailedMessagesRetryCommand::class, $service);
105
    }
106
107
    public function testFactoryWithNoFailureTransportShouldThrowException(): void
108
    {
109
        $this->expectException(InvalidArgumentException::class);
110
        $this->expectExceptionMessage('Invalid failure_transport name');
111
112
        $container = $this->prophesize(ContainerInterface::class);
113
114
        $container->has('config')->willReturn(true);
115
        $container->get('config')->willReturn([
116
            'messenger' => [
117
                'failure_transport' => null,
118
            ],
119
        ]);
120
121
        $factory = new FailedMessagesRetryCommandFactory();
122
123
        $factory($container->reveal());
124
    }
125
}
126