Passed
Pull Request — master (#5)
by Alex
02:16
created

testInvokeWithServiceOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 26
rs 9.7333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\LaminasDoctrine\Factory\Cache\Adapter;
6
7
use Arp\LaminasDoctrine\Factory\Cache\Adapter\ArrayAdapterFactory;
8
use Arp\LaminasFactory\FactoryInterface;
9
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
10
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
11
use Mockery\Adapter\Phpunit\MockeryTestCase;
12
use Mockery\MockInterface;
13
use Psr\Container\ContainerExceptionInterface;
14
use Psr\Container\ContainerInterface;
15
use Symfony\Component\Cache\Adapter\ArrayAdapter;
16
17
/**
18
 * @covers \Arp\LaminasDoctrine\Factory\Cache\Adapter\ArrayAdapterFactory
19
 */
20
final class ArrayAdapterFactoryTest extends MockeryTestCase
21
{
22
    /**
23
     * @var ContainerInterface&MockInterface
24
     */
25
    private ContainerInterface $container;
26
27
    public function setUp(): void
28
    {
29
        $this->container = \Mockery::mock(ContainerInterface::class);
30
    }
31
32
    public function testIsInvokable(): void
33
    {
34
        $factory = new ArrayAdapterFactory();
35
        $this->assertIsCallable($factory);
36
    }
37
38
    public function testImplementsFactoryInterface(): void
39
    {
40
        $factory = new ArrayAdapterFactory();
41
        $this->assertInstanceOf(FactoryInterface::class, $factory);
42
    }
43
44
    /**
45
     * @throws ContainerExceptionInterface
46
     * @throws ServiceNotFoundException
47
     */
48
    public function testServiceNotCreatedExceptionIfProvidedWithInvalidArguments(): void
49
    {
50
        $factory = new ArrayAdapterFactory();
51
52
        $requestedName = ArrayAdapter::class;
53
        $options = [
54
            'max_lifetime' => -1,
55
        ];
56
57
        $this->expectException(ServiceNotCreatedException::class);
58
        $this->expectExceptionMessage(
59
            sprintf('Failed to create cache adapter \'%s\' due to invalid configuration options', $requestedName),
60
        );
61
62
        $factory($this->container, $requestedName, $options);
63
    }
64
65
    public function testInvokeWithProvidedOptions(): void
66
    {
67
        $factory = new ArrayAdapterFactory();
68
69
        $requestedName = ArrayAdapter::class;
70
        $options = [
71
            'max_lifetime' => 3600,
72
            'max_items' => 10,
73
            'store_serialized' => false,
74
        ];
75
76
        $this->assertInstanceOf(ArrayAdapter::class, $factory($this->container, $requestedName, $options));
77
    }
78
79
    public function testInvokeWithServiceOptions(): void
80
    {
81
        $factory = new ArrayAdapterFactory();
82
83
        $requestedName = ArrayAdapter::class;
84
        $config = [
85
            'arp' => [
86
                'cache' => [
87
                    $requestedName => [
88
                        'max_lifetime' => 3600,
89
                    ],
90
                ],
91
            ],
92
        ];
93
94
        $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

94
        $this->container->/** @scrutinizer ignore-call */ 
95
                          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...
95
            ->once()
96
            ->with('config')
97
            ->andReturn(true);
98
99
        $this->container->shouldReceive('get')
100
            ->once()
101
            ->with('config')
102
            ->andReturn($config);
103
104
        $this->assertInstanceOf(ArrayAdapter::class, $factory($this->container, $requestedName));
105
    }
106
}
107