Passed
Pull Request — master (#2)
by Alex
04:42 queued 02:11
created

testInvokeWillThrowServiceNotCreatedExceptionIfTheFormatOptionIsNotLoaded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 20
c 2
b 0
f 0
dl 0
loc 35
rs 9.6
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\LaminasDateTime\Factory\Hydrator\Strategy;
6
7
use Arp\LaminasDateTime\Factory\Hydrator\Strategy\DateTimeStrategyFactory;
8
use Arp\LaminasFactory\FactoryInterface;
9
use Interop\Container\ContainerInterface;
10
use Laminas\Hydrator\Strategy\DateTimeFormatterStrategy;
11
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
12
use PHPUnit\Framework\MockObject\MockObject;
13
use PHPUnit\Framework\TestCase;
14
use Psr\Container\ContainerExceptionInterface;
15
16
/**
17
 * @covers  \Arp\LaminasDateTime\Factory\Hydrator\Strategy\DateTimeStrategyFactory
18
 *
19
 * @author  Alex Patterson <[email protected]>
20
 * @package ArpTest\LaminasDateTime\Factory\Hydrator\Strategy
21
 */
22
final class DateTimeStrategyFactoryTest extends TestCase
23
{
24
    /**
25
     * @var ContainerInterface&MockObject
26
     */
27
    private ContainerInterface $container;
28
29
    public function setUp(): void
30
    {
31
        $this->container = $this->createMock(ContainerInterface::class);
32
    }
33
34
    /**
35
     * Assert the factory class implements FactoryInterface
36
     */
37
    public function testImplementsFactoryInterface(): void
38
    {
39
        $factory = new DateTimeStrategyFactory();
40
41
        $this->assertInstanceOf(FactoryInterface::class, $factory);
42
    }
43
44
    /**
45
     * Assert that a ServiceNotCreatedException will be thrown if calling __invoke without a format option
46
     *
47
     * @throws ContainerExceptionInterface
48
     */
49
    public function testInvokeWillThrowServiceNotCreatedExceptionIfTheFormatOptionIsNotProvided(): void
50
    {
51
        $factory = new DateTimeStrategyFactory();
52
53
        $requestedName = DateTimeFormatterStrategy::class;
54
55
        $this->expectException(ServiceNotCreatedException::class);
56
        $this->expectExceptionMessage(
57
            sprintf(
58
                'The required \'format\' configuration option is missing for service \'%s\'',
59
                $requestedName
60
            )
61
        );
62
63
        $factory($this->container, $requestedName, []);
64
    }
65
66
    /**
67
     * Assert that a ServiceNotCreatedException will be thrown if unable to load a 'format' option from
68
     * configuration
69
     *
70
     * @throws ContainerExceptionInterface
71
     */
72
    public function testInvokeWillThrowServiceNotCreatedExceptionIfTheFormatOptionIsNotLoaded(): void
73
    {
74
        $factory = new DateTimeStrategyFactory();
75
76
        $applicationConfig = [
77
            'arp' => [
78
                'services' => [
79
                    DateTimeFormatterStrategy::class => [
80
                        // Missing format option
81
                    ],
82
                ],
83
            ],
84
        ];
85
86
        $requestedName = DateTimeFormatterStrategy::class;
87
88
        $this->container->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Interop\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

88
        $this->container->/** @scrutinizer ignore-call */ 
89
                          expects($this->once())

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...
89
            ->method('has')
90
            ->with('config')
91
            ->willReturn(true);
92
93
        $this->container->expects($this->once())
94
            ->method('get')
95
            ->with('config')
96
            ->willReturn($applicationConfig);
97
98
        $this->expectException(ServiceNotCreatedException::class);
99
        $this->expectExceptionMessage(
100
            sprintf(
101
                'The required \'format\' configuration option is missing for service \'%s\'',
102
                $requestedName
103
            )
104
        );
105
106
        $factory($this->container, $requestedName);
107
    }
108
109
    /**
110
     * Assert that the __invoke() method will return a configured DateTimeStrategy
111
     *
112
     * @param array<mixed>|null $options
113
     *
114
     * @dataProvider getInvokeReturnsDateTimeStrategyData
115
     *
116
     * @throws ContainerExceptionInterface
117
     */
118
    public function testInvokeReturnsDateTimeStrategy(?array $options): void
119
    {
120
        $factory = new DateTimeStrategyFactory();
121
122
        $requestedName = DateTimeFormatterStrategy::class;
123
124
        if (null === $options) {
0 ignored issues
show
introduced by
The condition null === $options is always false.
Loading history...
125
            $applicationConfig = [
126
                'arp' => [
127
                    'services' => [
128
                        DateTimeFormatterStrategy::class => [
129
                            'format' => \DateTimeInterface::RFC3339,
130
                        ],
131
                    ],
132
                ],
133
            ];
134
135
            $this->container->expects($this->once())
136
                ->method('has')
137
                ->with('config')
138
                ->willReturn(true);
139
140
            $this->container->expects($this->once())
141
                ->method('get')
142
                ->with('config')
143
                ->willReturn($applicationConfig);
144
        }
145
146
        /** @noinspection UnnecessaryAssertionInspection */
147
        $this->assertInstanceOf(
148
            DateTimeFormatterStrategy::class,
149
            $factory($this->container, $requestedName, $options)
150
        );
151
    }
152
153
    /**
154
     * @return array<mixed>
155
     */
156
    public function getInvokeReturnsDateTimeStrategyData(): array
157
    {
158
        return [
159
            [
160
                null,
161
            ],
162
            [
163
                [
164
                    'format' => \DateTimeInterface::ATOM,
165
                ],
166
            ],
167
            [
168
                [
169
                    'format' => \DateTimeInterface::RFC3339_EXTENDED,
170
                ],
171
            ],
172
        ];
173
    }
174
}
175