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

TransportFactoryTest::testStaticFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 9.28
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\Transport;
6
7
use PHPUnit\Framework\TestCase;
8
use Prophecy\PhpUnit\ProphecyTrait;
9
use Psr\Container\ContainerInterface;
10
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
11
use Symfony\Component\Messenger\Transport\TransportFactory as SFTransportFactory;
12
use Symfony\Component\Messenger\Transport\TransportInterface;
13
use TMV\Laminas\Messenger\Exception\InvalidArgumentException;
14
use TMV\Laminas\Messenger\Factory\Transport\TransportFactory;
15
16
class TransportFactoryTest extends TestCase
17
{
18
    use ProphecyTrait;
19
20
    public function testStaticFactory(): void
21
    {
22
        $factory = [TransportFactory::class, 'foo'];
23
        $dsn = 'foo://bar';
24
        $options = ['foo2' => 'bar'];
25
        $serializerName = 'transport_serializer';
26
        $defaultSerializerName = 'serializer';
27
28
        $container = $this->prophesize(ContainerInterface::class);
29
        $container->has('config')->willReturn(true);
30
        $container->get('config')->willReturn([
31
            'messenger' => [
32
                'default_serializer' => $defaultSerializerName,
33
                'transports' => [
34
                    'foo' => [
35
                        'dsn' => $dsn,
36
                        'options' => $options,
37
                        'serializer' => $serializerName,
38
                    ],
39
                ],
40
            ],
41
        ]);
42
43
        $sfTransportFactory = $this->prophesize(SFTransportFactory::class);
44
        $transport = $this->prophesize(TransportInterface::class);
45
        $serializer = $this->prophesize(SerializerInterface::class);
46
47
        $container->get(SFTransportFactory::class)
48
            ->willReturn($sfTransportFactory->reveal());
49
        $container->get($serializerName)
50
            ->willReturn($serializer->reveal());
51
52
        $sfTransportFactory->createTransport($dsn, $options, $serializer->reveal())
53
            ->willReturn($transport->reveal());
54
55
        /** @var TransportInterface $service */
56
        $service = $factory($container->reveal());
57
58
        $this->assertSame($transport->reveal(), $service);
59
    }
60
61
    public function testStaticFactoryWithDsnStringAndDefaultSerializer(): void
62
    {
63
        $factory = [TransportFactory::class, 'foo'];
64
        $dsn = 'foo://bar';
65
        $options = [];
66
        $defaultSerializerName = 'serializer';
67
68
        $container = $this->prophesize(ContainerInterface::class);
69
        $container->has('config')->willReturn(true);
70
        $container->get('config')->willReturn([
71
            'messenger' => [
72
                'default_serializer' => $defaultSerializerName,
73
                'transports' => [
74
                    'foo' => $dsn,
75
                ],
76
            ],
77
        ]);
78
79
        $sfTransportFactory = $this->prophesize(SFTransportFactory::class);
80
        $transport = $this->prophesize(TransportInterface::class);
81
        $serializer = $this->prophesize(SerializerInterface::class);
82
83
        $container->get(SFTransportFactory::class)
84
            ->willReturn($sfTransportFactory->reveal());
85
        $container->get($defaultSerializerName)
86
            ->willReturn($serializer->reveal());
87
88
        $sfTransportFactory->createTransport($dsn, $options, $serializer->reveal())
89
            ->willReturn($transport->reveal());
90
91
        /** @var TransportInterface $service */
92
        $service = $factory($container->reveal());
93
94
        $this->assertSame($transport->reveal(), $service);
95
    }
96
97
    public function testStaticFactoryWithOnlyDsnString(): void
98
    {
99
        $dsn = 'foo://bar';
100
        $factory = [TransportFactory::class, $dsn];
101
        $options = [];
102
        $defaultSerializerName = 'serializer';
103
104
        $container = $this->prophesize(ContainerInterface::class);
105
        $container->has('config')->willReturn(true);
106
        $container->get('config')->willReturn([
107
            'messenger' => [
108
                'default_serializer' => $defaultSerializerName,
109
            ],
110
        ]);
111
112
        $sfTransportFactory = $this->prophesize(SFTransportFactory::class);
113
        $transport = $this->prophesize(TransportInterface::class);
114
        $serializer = $this->prophesize(SerializerInterface::class);
115
116
        $container->get(SFTransportFactory::class)
117
            ->willReturn($sfTransportFactory->reveal());
118
        $container->get($defaultSerializerName)
119
            ->willReturn($serializer->reveal());
120
121
        $sfTransportFactory->createTransport($dsn, $options, $serializer->reveal())
122
            ->willReturn($transport->reveal());
123
124
        /** @var TransportInterface $service */
125
        $service = $factory($container->reveal());
126
127
        $this->assertSame($transport->reveal(), $service);
128
    }
129
130
    public function testInvalidCall(): void
131
    {
132
        $this->expectException(InvalidArgumentException::class);
133
134
        $factory = [TransportFactory::class, 'foo://'];
135
        $factory('foo');
136
    }
137
}
138