PsrCacheReaderFactoryTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 252
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 123
c 1
b 0
f 0
dl 0
loc 252
rs 10
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testImplementsFactoryInterface() 0 4 1
A testIsCallable() 0 4 1
A testInvalidReaderServiceThrowsServiceNotCreatedException() 0 33 1
A testMissingCacheConfigThrowsServiceNotCreatedException() 0 40 1
A setUp() 0 3 1
A testInvoke() 0 38 1
A testInvalidCacheServiceThrowsServiceNotCreatedException() 0 33 1
A testMissingReaderConfigurationThrowsServiceNotCreatedException() 0 12 1
A testMissingCacheServiceClassThrowsServiceNotCreatedException() 0 16 1
A testMissingCacheConfigurationThrowsServiceNotCreatedException() 0 15 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\LaminasDoctrine\Factory\Annotation;
6
7
use Arp\LaminasDoctrine\Config\DoctrineConfigInterface;
8
use Arp\LaminasDoctrine\Factory\Annotation\PsrCacheReaderFactory;
9
use Arp\LaminasFactory\FactoryInterface;
10
use Doctrine\Common\Annotations\PsrCachedReader;
11
use Doctrine\Common\Annotations\Reader;
12
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
13
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
14
use Mockery\Adapter\Phpunit\MockeryTestCase;
15
use Mockery\MockInterface;
16
use Psr\Cache\CacheItemPoolInterface;
17
use Psr\Container\ContainerExceptionInterface;
18
use Psr\Container\ContainerInterface;
19
20
/**
21
 * @covers \Arp\LaminasDoctrine\Factory\Annotation\PsrCacheReaderFactory
22
 */
23
final class PsrCacheReaderFactoryTest extends MockeryTestCase
24
{
25
    /**
26
     * @var ContainerInterface&MockInterface
27
     */
28
    private ContainerInterface $container;
29
30
    public function setUp(): void
31
    {
32
        $this->container = \Mockery::mock(ContainerInterface::class);
33
    }
34
35
    public function testIsCallable(): void
36
    {
37
        $factory = new PsrCacheReaderFactory();
38
        $this->assertIsCallable($factory);
39
    }
40
41
    public function testImplementsFactoryInterface(): void
42
    {
43
        $factory = new PsrCacheReaderFactory();
44
        $this->assertInstanceOf(FactoryInterface::class, $factory);
45
    }
46
47
    /**
48
     * @throws ContainerExceptionInterface
49
     * @throws ServiceNotFoundException
50
     */
51
    public function testMissingReaderConfigurationThrowsServiceNotCreatedException(): void
52
    {
53
        $factory = new PsrCacheReaderFactory();
54
55
        $requestedName = 'FooServiceName';
56
57
        $this->expectException(ServiceNotCreatedException::class);
58
        $this->expectExceptionMessage(
59
            sprintf('The required \'reader\' configuration option is missing for service \'%s\'', $requestedName),
60
        );
61
62
        $factory($this->container, $requestedName, []);
63
    }
64
65
    /**
66
     * @throws ContainerExceptionInterface
67
     * @throws ServiceNotFoundException
68
     */
69
    public function testMissingCacheConfigurationThrowsServiceNotCreatedException(): void
70
    {
71
        $factory = new PsrCacheReaderFactory();
72
73
        $requestedName = 'FooServiceName';
74
        $options = [
75
            'reader' => Reader::class,
76
        ];
77
78
        $this->expectException(ServiceNotCreatedException::class);
79
        $this->expectExceptionMessage(
80
            sprintf('The required \'cache\' configuration option is missing for service \'%s\'', $requestedName),
81
        );
82
83
        $factory($this->container, $requestedName, $options);
84
    }
85
86
    /**
87
     * @throws ContainerExceptionInterface
88
     * @throws ServiceNotFoundException
89
     */
90
    public function testInvalidReaderServiceThrowsServiceNotCreatedException(): void
91
    {
92
        $factory = new PsrCacheReaderFactory();
93
94
        $requestedName = 'FooServiceName';
95
        $options = [
96
            'reader' => Reader::class,
97
            'cache' => CacheItemPoolInterface::class,
98
        ];
99
100
        $this->container->shouldReceive('has')
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not exist on Psr\Container\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
        $this->container->/** @scrutinizer ignore-call */ 
101
                          shouldReceive('has')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
            ->once()
102
            ->with($options['reader'])
103
            ->andReturn(true);
104
105
        $reader = new \stdClass(); // Invalid reader class
106
107
        $this->container->shouldReceive('get')
108
            ->once()
109
            ->with($options['reader'])
110
            ->andReturn($reader);
111
112
        $this->expectException(ServiceNotCreatedException::class);
113
        $this->expectExceptionMessage(
114
            sprintf(
115
                'The reader must be an object of type \'%s\'; \'%s\' provided for service \'%s\'',
116
                Reader::class,
117
                \stdClass::class,
118
                $requestedName,
119
            ),
120
        );
121
122
        $factory($this->container, $requestedName, $options);
123
    }
124
125
    /**
126
     * @throws ContainerExceptionInterface
127
     * @throws ServiceNotFoundException
128
     */
129
    public function testMissingCacheServiceClassThrowsServiceNotCreatedException(): void
130
    {
131
        $factory = new PsrCacheReaderFactory();
132
133
        $requestedName = 'FooServiceName';
134
        $options = [
135
            'reader' => \Mockery::mock(Reader::class),
136
            'cache' => [],
137
        ];
138
139
        $this->expectException(ServiceNotCreatedException::class);
140
        $this->expectExceptionMessage(
141
            sprintf('The required \'class\' configuration option is missing for service \'%s\'', $requestedName),
142
        );
143
144
        $factory($this->container, $requestedName, $options);
145
    }
146
147
    /**
148
     * @throws ContainerExceptionInterface
149
     * @throws ServiceNotFoundException
150
     */
151
    public function testInvalidCacheServiceThrowsServiceNotCreatedException(): void
152
    {
153
        $factory = new PsrCacheReaderFactory();
154
155
        $requestedName = 'FooServiceName';
156
157
        /** @var Reader&MockInterface $reader */
158
        $reader = \Mockery::mock(Reader::class);
159
160
        $options = [
161
            'reader' => $reader,
162
            'cache' => [
163
                'class' => \stdClass::class,
164
            ],
165
        ];
166
167
        $cache = new \stdClass();
168
        $this->container->shouldReceive('get')
169
            ->once()
170
            ->with($options['cache']['class'])
171
            ->andReturn($cache);
172
173
        $this->expectException(ServiceNotCreatedException::class);
174
        $this->expectExceptionMessage(
175
            sprintf(
176
                'The cache must be an object of type \'%s\'; \'%s\' provided for service \'%s\'',
177
                CacheItemPoolInterface::class,
178
                \stdClass::class,
179
                $requestedName,
180
            ),
181
        );
182
183
        $factory($this->container, $requestedName, $options);
184
    }
185
186
    /**
187
     * @throws ContainerExceptionInterface
188
     * @throws ServiceNotFoundException
189
     */
190
    public function testMissingCacheConfigThrowsServiceNotCreatedException(): void
191
    {
192
        $factory = new PsrCacheReaderFactory();
193
194
        $requestedName = 'FooServiceName';
195
        $cacheName = 'FooCacheName';
196
        $options = [
197
            'reader' => \Mockery::mock(Reader::class),
198
            'cache' => $cacheName,
199
        ];
200
201
        $this->container->shouldReceive('has')
202
            ->once()
203
            ->with($cacheName)
204
            ->andReturnFalse();
205
206
        $this->container->shouldReceive('has')
207
            ->once()
208
            ->with(DoctrineConfigInterface::class)
209
            ->andReturnTrue();
210
211
        /** @var DoctrineConfigInterface&MockInterface $doctrineConfig */
212
        $doctrineConfig = \Mockery::mock(DoctrineConfigInterface::class);
213
214
        $this->container->shouldReceive('get')
215
            ->once()
216
            ->with(DoctrineConfigInterface::class)
217
            ->andReturn($doctrineConfig);
218
219
        $doctrineConfig->shouldReceive('hasCacheConfig')
220
            ->once()
221
            ->with($cacheName)
222
            ->andReturnFalse();
0 ignored issues
show
Bug introduced by
The method andReturnFalse() does not exist on Mockery\ExpectationInterface. Did you maybe mean andReturn()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

222
            ->/** @scrutinizer ignore-call */ andReturnFalse();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
223
224
        $this->expectException(ServiceNotCreatedException::class);
225
        $this->expectExceptionMessage(
226
            sprintf('The cache configuration \'%s\' could not be found for service \'%s\'', $cacheName, $requestedName),
227
        );
228
229
        $factory($this->container, $requestedName, $options);
230
    }
231
232
    /**
233
     * @throws ContainerExceptionInterface
234
     * @throws \InvalidArgumentException
235
     * @throws ServiceNotFoundException
236
     */
237
    public function testInvoke(): void
238
    {
239
        $factory = new PsrCacheReaderFactory();
240
241
        $requestedName = 'FooServiceName';
242
243
        /** @var Reader&MockInterface $reader */
244
        $reader = \Mockery::mock(Reader::class);
245
246
        /** @var CacheItemPoolInterface&MockInterface $cache */
247
        $cache = \Mockery::mock(CacheItemPoolInterface::class);
248
249
        $options = [
250
            'reader' => Reader::class,
251
            'cache' => CacheItemPoolInterface::class,
252
        ];
253
254
        $this->container->shouldReceive('has')
255
            ->once()
256
            ->with($options['reader'])
257
            ->andReturnTrue();
258
259
        $this->container->shouldReceive('get')
260
            ->once()
261
            ->with($options['reader'])
262
            ->andReturn($reader);
263
264
        $this->container->shouldReceive('has')
265
            ->once()
266
            ->with($options['cache'])
267
            ->andReturnTrue();
268
269
        $this->container->shouldReceive('get')
270
            ->once()
271
            ->with($options['cache'])
272
            ->andReturn($cache);
273
274
        $this->assertInstanceOf(PsrCachedReader::class, $factory($this->container, $requestedName, $options));
275
    }
276
}
277