Passed
Pull Request — master (#2)
by Alex
11:49
created

EventDispatcherFactoryTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\LaminasEvent\Factory;
6
7
use Arp\EventDispatcher\EventDispatcher;
8
use Arp\EventDispatcher\Listener\AddableListenerProviderInterface;
9
use Arp\EventDispatcher\Listener\ListenerProvider;
10
use Arp\LaminasEvent\Factory\EventDispatcherFactory;
11
use Arp\LaminasFactory\FactoryInterface;
12
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
13
use Laminas\ServiceManager\ServiceLocatorInterface;
14
use PHPUnit\Framework\MockObject\MockObject;
15
use PHPUnit\Framework\TestCase;
16
use Psr\Container\ContainerExceptionInterface;
17
use Psr\Container\NotFoundExceptionInterface;
18
use Psr\EventDispatcher\EventDispatcherInterface;
19
use Psr\EventDispatcher\ListenerProviderInterface;
20
21
/**
22
 * @covers \Arp\LaminasEvent\Factory\EventDispatcherFactory
23
 * @covers \Arp\LaminasEvent\Factory\AbstractEventDispatcherFactory
24
 */
25
final class EventDispatcherFactoryTest extends TestCase
26
{
27
    private ServiceLocatorInterface&MockObject $container;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected '&', expecting T_VARIABLE on line 27 at column 35
Loading history...
28
29
    public function setUp(): void
30
    {
31
        $this->container = $this->createMock(ServiceLocatorInterface::class);
32
    }
33
34
    public function testIsCallable(): void
35
    {
36
        $factory = new EventDispatcherFactory();
37
        $this->assertIsCallable($factory);
38
    }
39
40
    public function testImplementsFactoryInterface(): void
41
    {
42
        $factory = new EventDispatcherFactory();
43
        $this->assertInstanceOf(FactoryInterface::class, $factory);
44
    }
45
46
    /**
47
     * @throws ServiceNotCreatedException
48
     * @throws ContainerExceptionInterface
49
     * @throws NotFoundExceptionInterface
50
     */
51
    public function testInvalidListenerProviderWillThrowServiceNotCreatedException(): void
52
    {
53
        $factory = new EventDispatcherFactory();
54
55
        $serviceName = EventDispatcherInterface::class;
56
        $options = [
57
            'listener_provider' => false,
58
        ];
59
60
        $this->expectException(ServiceNotCreatedException::class);
61
        $this->expectExceptionMessage(
62
            sprintf(
63
                'The listener provider must be an object of type \'%s\'; \'%s\' provided for service \'%s\'',
64
                ListenerProviderInterface::class,
65
                'boolean',
66
                $serviceName
67
            )
68
        );
69
70
        $factory($this->container, $serviceName, $options);
71
    }
72
73
    /**
74
     * @throws ContainerExceptionInterface
75
     * @throws NotFoundExceptionInterface
76
     * @throws ServiceNotCreatedException
77
     */
78
    public function testNonAddableListenerProviderWillThrowServiceNotCreatedException(): void
79
    {
80
        $factory = new EventDispatcherFactory();
81
82
        $serviceName = EventDispatcherInterface::class;
83
        $listenerProviderName = ListenerProviderInterface::class;
84
        $options = [
85
            'listener_provider' => $listenerProviderName,
86
        ];
87
88
        $this->container->expects($this->once())
89
            ->method('has')
90
            ->with($listenerProviderName)
91
            ->willReturn(true);
92
93
        /** @var ListenerProviderInterface&MockObject $listenerProvider */
94
        $listenerProvider = $this->createMock(ListenerProviderInterface::class);
95
96
        $this->container->expects($this->once())
97
            ->method('get')
98
            ->with($listenerProviderName)
99
            ->willReturn($listenerProvider);
100
101
        $this->expectException(ServiceNotCreatedException::class);
102
        $this->expectExceptionMessage(
103
            sprintf(
104
                'The listener provider must be an object of type \'%s\'; \'%s\' provided for service \'%s\'',
105
                AddableListenerProviderInterface::class,
106
                get_class($listenerProvider),
107
                $serviceName
108
            )
109
        );
110
111
        $factory($this->container, $serviceName, $options);
112
    }
113
114
    /**
115
     * @dataProvider getEventDispatcherFactoryWillReturnConfiguredEventDispatcherData
116
     *
117
     * @param mixed $listenerProviderConfig
118
     *
119
     * @throws ContainerExceptionInterface
120
     * @throws NotFoundExceptionInterface
121
     * @throws ServiceNotCreatedException
122
     */
123
    public function testEventDispatcherFactoryWillReturnConfiguredEventDispatcher(mixed $listenerProviderConfig): void
124
    {
125
        $factory = new EventDispatcherFactory();
126
127
        $serviceName = EventDispatcherInterface::class;
128
        $options = [
129
            'listener_provider' => $listenerProviderConfig,
130
        ];
131
132
        /** @var AddableListenerProviderInterface&MockObject $listenerProvider */
133
        $listenerProvider = $this->createMock(AddableListenerProviderInterface::class);
134
135
        if (is_string($listenerProviderConfig)) {
136
            if (!class_exists($listenerProviderConfig)) {
137
                $this->container->expects($this->once())
138
                    ->method('has')
139
                    ->with($listenerProviderConfig)
140
                    ->willReturn(true);
141
            }
142
            $this->container->expects($this->once())
143
                ->method('get')
144
                ->with($listenerProviderConfig)
145
                ->willReturn($listenerProvider);
146
        } elseif (is_array($listenerProviderConfig)) {
147
            $this->container->expects($this->once())
148
                ->method('build')
149
                ->with(ListenerProviderInterface::class, $listenerProviderConfig)
150
                ->willReturn($listenerProvider);
151
        }
152
153
        $this->assertInstanceOf(EventDispatcher::class, $factory($this->container, $serviceName, $options));
154
    }
155
156
    /**
157
     * @return array<mixed>
158
     */
159
    public function getEventDispatcherFactoryWillReturnConfiguredEventDispatcherData(): array
160
    {
161
        return [
162
            [
163
                ListenerProvider::class,
164
            ],
165
166
            [
167
                [
168
                    'foo' => 'bar',
169
                    'test' => 'hello'
170
                ],
171
            ],
172
173
            [
174
                $this->createMock(AddableListenerProviderInterface::class),
175
            ]
176
        ];
177
    }
178
}
179