Passed
Pull Request — master (#7)
by Alex
07:24
created

testImplementsFactoryInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 2
c 2
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\DateTime\Factory;
6
7
use Arp\DateTime\DateFactoryInterface;
8
use Arp\DateTime\DateIntervalFactoryInterface;
9
use Arp\DateTime\DateTimeFactoryInterface;
10
use Arp\DateTime\Factory\DateFactoryFactory;
11
use Arp\Factory\Exception\FactoryException;
12
use Arp\Factory\FactoryInterface;
13
use PHPUnit\Framework\MockObject\MockObject;
14
use PHPUnit\Framework\TestCase;
15
16
/**
17
 * @covers  \Arp\DateTime\Factory\DateFactoryFactory
18
 *
19
 * @author  Alex Patterson <[email protected]>
20
 * @package ArpTest\DateTime\Factory
21
 */
22
final class DateFactoryFactoryTest extends TestCase
23
{
24
    /**
25
     * @var FactoryInterface|MockObject
26
     */
27
    private $dateTimeFactoryFactory;
28
29
    /**
30
     * @var FactoryInterface|MockObject
31
     */
32
    private $dateIntervalFactoryFactory;
33
34
    /**
35
     * Prepare test case dependencies
36
     */
37
    public function setUp(): void
38
    {
39
        $this->dateTimeFactoryFactory = $this->createMock(FactoryInterface::class);
40
41
        $this->dateIntervalFactoryFactory = $this->createMock(FactoryInterface::class);
42
    }
43
44
    /**
45
     * Assert that the class implements FactoryInterface
46
     */
47
    public function testImplementsFactoryInterface(): void
48
    {
49
        $factory = new DateFactoryFactory($this->dateTimeFactoryFactory, $this->dateIntervalFactoryFactory);
50
51
        $this->assertInstanceOf(FactoryInterface::class, $factory);
52
    }
53
54
    /**
55
     * Assert that a FactoryException is thrown then providing invalid 'date_time_factory' configuration
56
     *
57
     * @throws FactoryException
58
     */
59
    public function testCreateWillThrowFactoryExceptionIfDateTimeFactoryConfigIsInvalid(): void
60
    {
61
        $factory = new DateFactoryFactory($this->dateTimeFactoryFactory, $this->dateIntervalFactoryFactory);
62
63
        $config = [
64
            'date_time_factory' => new \stdClass(),
65
        ];
66
67
        $this->expectException(FactoryException::class);
68
        $this->expectExceptionMessage(
69
            sprintf(
70
                'The \'date_time_factory\' argument could not be resolved to an object of type \'%s\'',
71
                DateTimeFactoryInterface::class
72
            )
73
        );
74
75
        $factory->create($config);
76
    }
77
78
    /**
79
     * Assert that a FactoryException is thrown then providing invalid 'date_interval_factory' configuration
80
     *
81
     * @throws FactoryException
82
     */
83
    public function testCreateWillThrowFactoryExceptionIfDateIntervalFactoryConfigIsInvalid(): void
84
    {
85
        $factory = new DateFactoryFactory($this->dateTimeFactoryFactory, $this->dateIntervalFactoryFactory);
86
87
        $config = [
88
            'date_interval_factory' => new \stdClass(),
89
        ];
90
91
        $this->dateTimeFactoryFactory->expects($this->once())
92
            ->method('create')
93
            ->with([])
94
            ->willReturn($this->createMock(DateTimeFactoryInterface::class));
95
96
        $this->expectException(FactoryException::class);
97
        $this->expectExceptionMessage(
98
            sprintf(
99
                'The \'date_interval_factory\' argument could not be resolved to an object of type \'%s\'',
100
                DateIntervalFactoryInterface::class
101
            )
102
        );
103
104
        $factory->create($config);
105
    }
106
107
    /**
108
     * Assert that a DateTimeFactory instance and be create by calling create() with configuration options
109
     *
110
     * @param array $config
111
     *
112
     * @dataProvider getCreateData
113
     *
114
     * @throws FactoryException
115
     */
116
    public function testCreate(array $config): void
117
    {
118
        $factory = new DateFactoryFactory($this->dateTimeFactoryFactory, $this->dateIntervalFactoryFactory);
119
120
        $dateTimeFactoryConfig = $config['date_time_factory'] ?? [];
121
        if (is_array($dateTimeFactoryConfig)) {
122
            $dateTimeFactory = $this->createMock(DateTimeFactoryInterface::class);
123
            $this->dateTimeFactoryFactory->expects($this->once())
124
                ->method('create')
125
                ->with($dateTimeFactoryConfig)
126
                ->willReturn($dateTimeFactory);
127
        }
128
129
        $dateIntervalFactoryConfig = $config['date_interval_factory'] ?? [];
130
        if (is_array($dateIntervalFactoryConfig)) {
131
            $dateIntervalFactory = $this->createMock(DateIntervalFactoryInterface::class);
132
            $this->dateIntervalFactoryFactory->expects($this->once())
133
                ->method('create')
134
                ->with($dateIntervalFactoryConfig)
135
                ->willReturn($dateIntervalFactory);
136
        }
137
138
        $this->assertInstanceOf(DateFactoryInterface::class, $factory->create($config));
139
    }
140
141
    /**
142
     * @return array
143
     */
144
    public function getCreateData(): array
145
    {
146
        return [
147
            [
148
                []
149
            ],
150
            [
151
                [
152
                    'date_time_factory' => []
153
                ],
154
            ],
155
            [
156
                [
157
                    'date_interval_factory' => []
158
                ],
159
            ],
160
            [
161
                [
162
                    'date_time_factory' => [
163
                        'hello' => 'world',
164
                    ],
165
                    'date_interval_factory' => [
166
                        'foo' => 'bar',
167
                        'test' => 123,
168
                    ]
169
                ],
170
            ],
171
            [
172
                [
173
                    'date_time_factory' => $this->createMock(DateTimeFactoryInterface::class),
174
                ],
175
            ],
176
            [
177
                [
178
                    'date_interval_factory' => $this->createMock(DateIntervalFactoryInterface::class),
179
                ],
180
            ],
181
        ];
182
    }
183
}
184