Passed
Pull Request — master (#7)
by Alex
01:46
created

DateFactoryFactoryTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
eloc 50
c 3
b 0
f 0
dl 0
loc 140
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testCreateWillThrowFactoryExceptionIfDateIntervalFactoryConfigIsInvalid() 0 22 1
A testCreateWillThrowFactoryExceptionIfDateTimeFactoryConfigIsInvalid() 0 17 1
A testImplementsFactoryInterface() 0 5 1
A setUp() 0 5 1
A getCreateData() 0 20 1
A testCreate() 0 23 3
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
     * @param array $config
109
     *
110
     * @dataProvider getCreateData
111
     *
112
     * @throws \Arp\Factory\Exception\FactoryException
113
     */
114
    public function testCreate(array $config): void
115
    {
116
        $factory = new DateFactoryFactory($this->dateTimeFactoryFactory, $this->dateIntervalFactoryFactory);
117
118
        $dateTimeFactoryConfig = $config['date_time_factory'] ?? [];
119
        if (is_array($dateTimeFactoryConfig)) {
120
            $dateTimeFactory = $this->createMock(DateTimeFactoryInterface::class);
121
            $this->dateTimeFactoryFactory->expects($this->once())
122
                ->method('create')
123
                ->with($dateTimeFactoryConfig)
124
                ->willReturn($dateTimeFactory);
125
        }
126
127
        $dateIntervalFactoryConfig = $config['date_interval_factory'] ?? [];
128
        if (is_array($dateIntervalFactoryConfig)) {
129
            $dateIntervalFactory = $this->createMock(DateIntervalFactoryInterface::class);
130
            $this->dateIntervalFactoryFactory->expects($this->once())
131
                ->method('create')
132
                ->with($dateIntervalFactoryConfig)
133
                ->willReturn($dateIntervalFactory);
134
        }
135
136
        $this->assertInstanceOf(DateFactoryInterface::class, $factory->create($config));
137
    }
138
139
    /**
140
     * @return array
141
     */
142
    public function getCreateData(): array
143
    {
144
        return [
145
            [
146
                []
147
            ],
148
            [
149
                [
150
                    'date_time_factory' => []
151
                ],
152
            ],
153
            [
154
                [
155
                    'date_interval_factory' => []
156
                ],
157
            ],
158
            [
159
                [
160
                    'date_time_factory' => [],
161
                    'date_interval_factory' => []
162
                ],
163
            ],
164
        ];
165
    }
166
}
167