1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Factory\Hydrator\Strategy; |
6
|
|
|
|
7
|
|
|
use Arp\LaminasDateTime\Factory\Hydrator\Strategy\DateTimeStrategyFactory; |
8
|
|
|
use Laminas\Hydrator\Strategy\DateTimeFormatterStrategy; |
9
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotCreatedException; |
10
|
|
|
use Laminas\ServiceManager\Factory\FactoryInterface; |
11
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Psr\Container\ContainerInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @covers \Arp\LaminasDateTime\Factory\Hydrator\Strategy\DateTimeStrategyFactory |
17
|
|
|
* |
18
|
|
|
* @author Alex Patterson <[email protected]> |
19
|
|
|
* @package Factory\Hydrator\Strategy |
20
|
|
|
*/ |
21
|
|
|
final class DateTimeStrategyFactoryTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Assert the factory class implements FactoryInterface |
25
|
|
|
*/ |
26
|
|
|
public function testImplementsFactoryInterface(): void |
27
|
|
|
{ |
28
|
|
|
$factory = new DateTimeStrategyFactory(); |
29
|
|
|
|
30
|
|
|
$this->assertInstanceOf(FactoryInterface::class, $factory); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Assert that a ServiceNotCreatedException will be thrown if calling __invoke without a format option |
35
|
|
|
*/ |
36
|
|
|
public function testInvokeWillThrowServiceNotCreatedExceptionIfTheFormatOptionIsNotProvided(): void |
37
|
|
|
{ |
38
|
|
|
$factory = new DateTimeStrategyFactory(); |
39
|
|
|
|
40
|
|
|
/** @var ContainerInterface|MockObject $container */ |
41
|
|
|
$container = $this->createMock(ContainerInterface::class); |
42
|
|
|
|
43
|
|
|
$requestedName = DateTimeFormatterStrategy::class; |
44
|
|
|
|
45
|
|
|
$this->expectException(ServiceNotCreatedException::class); |
46
|
|
|
$this->expectExceptionMessage( |
47
|
|
|
sprintf( |
48
|
|
|
'The required \'format\' configuration option is missing for service \'%s\'', |
49
|
|
|
$requestedName |
50
|
|
|
) |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
$factory($container, $requestedName, []); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Assert that a ServiceNotCreatedException will be thrown if unable to load a 'format' option from |
58
|
|
|
* configuration |
59
|
|
|
*/ |
60
|
|
|
public function testInvokeWillThrowServiceNotCreatedExceptionIfTheFormatOptionIsNotLoaded(): void |
61
|
|
|
{ |
62
|
|
|
$factory = new DateTimeStrategyFactory(); |
63
|
|
|
|
64
|
|
|
$applicationConfig = [ |
65
|
|
|
'arp' => [ |
66
|
|
|
'services' => [ |
67
|
|
|
DateTimeFormatterStrategy::class => [ |
68
|
|
|
// Missing format option |
69
|
|
|
], |
70
|
|
|
], |
71
|
|
|
] |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
/** @var ContainerInterface|MockObject $container */ |
75
|
|
|
$container = $this->createMock(ContainerInterface::class); |
76
|
|
|
|
77
|
|
|
$requestedName = DateTimeFormatterStrategy::class; |
78
|
|
|
|
79
|
|
|
$container->expects($this->once()) |
80
|
|
|
->method('has') |
81
|
|
|
->with('config') |
82
|
|
|
->willReturn(true); |
83
|
|
|
|
84
|
|
|
$container->expects($this->once()) |
85
|
|
|
->method('get') |
86
|
|
|
->with('config') |
87
|
|
|
->willReturn($applicationConfig); |
88
|
|
|
|
89
|
|
|
$this->expectException(ServiceNotCreatedException::class); |
90
|
|
|
$this->expectExceptionMessage( |
91
|
|
|
sprintf( |
92
|
|
|
'The required \'format\' configuration option is missing for service \'%s\'', |
93
|
|
|
$requestedName |
94
|
|
|
) |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$factory($container, $requestedName); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Assert that the __invoke() method will return a configured DateTimeStrategy |
102
|
|
|
* |
103
|
|
|
* @param array|null $options |
104
|
|
|
* |
105
|
|
|
* @dataProvider getInvokeReturnsDateTimeStrategyData |
106
|
|
|
*/ |
107
|
|
|
public function testInvokeReturnsDateTimeStrategy(?array $options): void |
108
|
|
|
{ |
109
|
|
|
$factory = new DateTimeStrategyFactory(); |
110
|
|
|
|
111
|
|
|
/** @var ContainerInterface|MockObject $container */ |
112
|
|
|
$container = $this->createMock(ContainerInterface::class); |
113
|
|
|
|
114
|
|
|
$requestedName = DateTimeFormatterStrategy::class; |
115
|
|
|
|
116
|
|
|
if (null === $options) { |
|
|
|
|
117
|
|
|
$applicationConfig = [ |
118
|
|
|
'arp' => [ |
119
|
|
|
'services' => [ |
120
|
|
|
DateTimeFormatterStrategy::class => [ |
121
|
|
|
'format' => \DateTime::RFC3339, |
122
|
|
|
], |
123
|
|
|
], |
124
|
|
|
] |
125
|
|
|
]; |
126
|
|
|
|
127
|
|
|
$container->expects($this->once())->method('has')->with('config')->willReturn(true); |
128
|
|
|
$container->expects($this->once())->method('get')->with('config')->willReturn($applicationConfig); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** @noinspection UnnecessaryAssertionInspection */ |
132
|
|
|
$this->assertInstanceOf(DateTimeFormatterStrategy::class, $factory($container, $requestedName, $options)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function getInvokeReturnsDateTimeStrategyData(): array |
139
|
|
|
{ |
140
|
|
|
return [ |
141
|
|
|
[ |
142
|
|
|
null, |
143
|
|
|
], |
144
|
|
|
[ |
145
|
|
|
[ |
146
|
|
|
'format' => \DateTime::ATOM, |
147
|
|
|
] |
148
|
|
|
], |
149
|
|
|
[ |
150
|
|
|
[ |
151
|
|
|
'format' => \DateTime::RFC3339_EXTENDED, |
152
|
|
|
] |
153
|
|
|
], |
154
|
|
|
]; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|