testImplementsFactoryInterface()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

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