Passed
Pull Request — master (#11)
by Alex
02:52
created

DateIntervalFactoryTest::testDiffWillReturnValidDateInterval()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 19
rs 9.9666
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\DateTime;
6
7
use Arp\DateTime\DateIntervalFactory;
8
use Arp\DateTime\DateIntervalFactoryInterface;
9
use Arp\DateTime\Exception\DateIntervalFactoryException;
10
use PHPUnit\Framework\TestCase;
11
12
/**
13
 * @covers \Arp\DateTime\DateIntervalFactory
14
 *
15
 * @author  Alex Patterson <[email protected]>
16
 * @package ArpTest\DateTime
17
 */
18
final class DateIntervalFactoryTest extends TestCase
19
{
20
    /**
21
     * Ensure that the DateIntervalFactory implements the DateIntervalFactoryInterface
22
     */
23
    public function testImplementsDateIntervalFactoryInterface(): void
24
    {
25
        $factory = new DateIntervalFactory();
26
27
        $this->assertInstanceOf(DateIntervalFactoryInterface::class, $factory);
28
    }
29
30
    /**
31
     * Ensure that the DateInterval is created in accordance with the provided $spec
32
     *
33
     * @param string $spec The \DateInterval specification
34
     *
35
     * @dataProvider getCreateDateIntervalData
36
     *
37
     * @throws DateIntervalFactoryException
38
     * @throws \Exception
39
     */
40
    public function testCreateDateInterval(string $spec): void
41
    {
42
        $factory = new DateIntervalFactory();
43
44
        $dateInterval = $factory->createDateInterval($spec);
45
46
        $test = new \DateInterval($spec);
47
48
        $this->assertSame($test->y, $dateInterval->y);
49
        $this->assertSame($test->m, $dateInterval->m);
50
        $this->assertSame($test->d, $dateInterval->d);
51
        $this->assertSame($test->h, $dateInterval->h);
52
        $this->assertSame($test->i, $dateInterval->i);
53
        $this->assertSame($test->f, $dateInterval->f);
54
    }
55
56
    /**
57
     * @see https://www.php.net/manual/en/class.dateinterval.php
58
     *
59
     * @return array<mixed>
60
     */
61
    public function getCreateDateIntervalData(): array
62
    {
63
        return [
64
            ['P100D'],
65
            ['P4Y1DT9H11M3S'],
66
            ['P2Y4DT6H8M'],
67
            ['P7Y8M'],
68
        ];
69
    }
70
71
    /**
72
     * Ensure that createDateInterval() will throw a DateIntervalFactoryException if the provided $spec is invalid
73
     *
74
     * @param string $spec
75
     *
76
     * @dataProvider getCreateDateIntervalWillThrowDateIntervalFactoryExceptionData
77
     *
78
     * @throws DateIntervalFactoryException
79
     */
80
    public function testCreateDateIntervalWillThrowDateIntervalFactoryException(string $spec): void
81
    {
82
        $factory = new DateIntervalFactory();
83
84
        $exceptionMessage = sprintf('DateInterval::__construct(): Unknown or bad format (%s)', $spec);
85
86
        $this->expectException(DateIntervalFactoryException::class);
87
        $this->expectExceptionMessage(
88
            sprintf(
89
                'Failed to create a valid \DateInterval instance using \'%s\': %s',
90
                $spec,
91
                $exceptionMessage
92
            )
93
        );
94
95
        $factory->createDateInterval($spec);
96
    }
97
98
    /**
99
     * @return array<mixed>
100
     */
101
    public function getCreateDateIntervalWillThrowDateIntervalFactoryExceptionData(): array
102
    {
103
        return [
104
            ['test'],
105
            ['invalid'],
106
        ];
107
    }
108
}
109