Passed
Pull Request — master (#2)
by Alex
07:25
created

testInvokeWillThrowServiceNotCreatedExceptionIfTheFormatOptionIsNotLoaded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 38
rs 9.584
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\DateTime\DateTimeFactory;
8
use Arp\LaminasDateTime\Factory\Hydrator\Strategy\DateTimeStrategyFactory;
9
use Arp\LaminasFactory\FactoryInterface;
10
use Interop\Container\ContainerInterface;
11
use Laminas\Hydrator\Strategy\DateTimeFormatterStrategy;
12
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
13
use PHPUnit\Framework\MockObject\MockObject;
14
use PHPUnit\Framework\TestCase;
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
     * Assert the factory class implements FactoryInterface
26
     */
27
    public function testImplementsFactoryInterface(): void
28
    {
29
        $factory = new DateTimeStrategyFactory();
30
31
        $this->assertInstanceOf(FactoryInterface::class, $factory);
32
    }
33
34
    /**
35
     * Assert that a ServiceNotCreatedException will be thrown if calling __invoke without a format option
36
     */
37
    public function testInvokeWillThrowServiceNotCreatedExceptionIfTheFormatOptionIsNotProvided(): void
38
    {
39
        $factory = new DateTimeStrategyFactory();
40
41
        /** @var ContainerInterface&MockObject $container */
42
        $container = $this->createMock(ContainerInterface::class);
43
44
        $requestedName = DateTimeFormatterStrategy::class;
45
46
        $this->expectException(ServiceNotCreatedException::class);
47
        $this->expectExceptionMessage(
48
            sprintf(
49
                'The required \'format\' configuration option is missing for service \'%s\'',
50
                $requestedName
51
            )
52
        );
53
54
        $factory($container, $requestedName, []);
55
    }
56
57
    /**
58
     * Assert that a ServiceNotCreatedException will be thrown if unable to load a 'format' option from
59
     * configuration
60
     */
61
    public function testInvokeWillThrowServiceNotCreatedExceptionIfTheFormatOptionIsNotLoaded(): void
62
    {
63
        $factory = new DateTimeStrategyFactory();
64
65
        $applicationConfig = [
66
            'arp' => [
67
                'services' => [
68
                    DateTimeFormatterStrategy::class => [
69
                        // Missing format option
70
                    ],
71
                ],
72
            ],
73
        ];
74
75
        /** @var ContainerInterface&MockObject $container */
76
        $container = $this->createMock(ContainerInterface::class);
77
78
        $requestedName = DateTimeFormatterStrategy::class;
79
80
        $container->expects($this->once())
81
            ->method('has')
82
            ->with('config')
83
            ->willReturn(true);
84
85
        $container->expects($this->once())
86
            ->method('get')
87
            ->with('config')
88
            ->willReturn($applicationConfig);
89
90
        $this->expectException(ServiceNotCreatedException::class);
91
        $this->expectExceptionMessage(
92
            sprintf(
93
                'The required \'format\' configuration option is missing for service \'%s\'',
94
                $requestedName
95
            )
96
        );
97
98
        $factory($container, $requestedName);
99
    }
100
101
    /**
102
     * Assert that the __invoke() method will return a configured DateTimeStrategy
103
     *
104
     * @param array<mixed>|null $options
105
     *
106
     * @dataProvider getInvokeReturnsDateTimeStrategyData
107
     */
108
    public function testInvokeReturnsDateTimeStrategy(?array $options): void
109
    {
110
        $factory = new DateTimeStrategyFactory();
111
112
        /** @var ContainerInterface&MockObject $container */
113
        $container = $this->createMock(ContainerInterface::class);
114
115
        $requestedName = DateTimeFormatterStrategy::class;
116
117
        if (null === $options) {
0 ignored issues
show
introduced by
The condition null === $options is always false.
Loading history...
118
            $applicationConfig = [
119
                'arp' => [
120
                    'services' => [
121
                        DateTimeFormatterStrategy::class => [
122
                            'format' => \DateTimeInterface::RFC3339,
123
                        ],
124
                    ],
125
                ],
126
            ];
127
128
            $container->expects($this->once())->method('has')->with('config')->willReturn(true);
129
            $container->expects($this->once())->method('get')->with('config')->willReturn($applicationConfig);
130
        }
131
132
        /** @noinspection UnnecessaryAssertionInspection */
133
        $this->assertInstanceOf(DateTimeFormatterStrategy::class, $factory($container, $requestedName, $options));
134
    }
135
136
    /**
137
     * @return array<mixed>
138
     */
139
    public function getInvokeReturnsDateTimeStrategyData(): array
140
    {
141
        return [
142
            [
143
                null,
144
            ],
145
            [
146
                [
147
                    'format' => \DateTimeInterface::ATOM,
148
                ],
149
            ],
150
            [
151
                [
152
                    'format' => \DateTimeInterface::RFC3339_EXTENDED,
153
                ],
154
            ],
155
        ];
156
    }
157
}
158