DateIntervalFactoryTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 27
c 0
b 0
f 0
dl 0
loc 81
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCreateDateIntervalData() 0 7 1
A testCreateDateIntervalWillThrowDateIntervalFactoryException() 0 13 1
A getCreateDateIntervalWillThrowDateIntervalFactoryExceptionData() 0 5 1
A testImplementsDateIntervalFactoryInterface() 0 5 1
A testCreateDateInterval() 0 14 1
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
final class DateIntervalFactoryTest extends TestCase
16
{
17
    /**
18
     * Ensure that the DateIntervalFactory implements the DateIntervalFactoryInterface
19
     */
20
    public function testImplementsDateIntervalFactoryInterface(): void
21
    {
22
        $factory = new DateIntervalFactory();
23
24
        $this->assertInstanceOf(DateIntervalFactoryInterface::class, $factory);
25
    }
26
27
    /**
28
     * Ensure that the DateInterval is created in accordance with the provided $spec
29
     *
30
     * @dataProvider getCreateDateIntervalData
31
     *
32
     * @throws DateIntervalFactoryException
33
     * @throws \Exception
34
     */
35
    public function testCreateDateInterval(string $spec): void
36
    {
37
        $factory = new DateIntervalFactory();
38
39
        $dateInterval = $factory->createDateInterval($spec);
40
41
        $test = new \DateInterval($spec);
42
43
        $this->assertSame($test->y, $dateInterval->y);
44
        $this->assertSame($test->m, $dateInterval->m);
45
        $this->assertSame($test->d, $dateInterval->d);
46
        $this->assertSame($test->h, $dateInterval->h);
47
        $this->assertSame($test->i, $dateInterval->i);
48
        $this->assertSame($test->f, $dateInterval->f);
49
    }
50
51
    /**
52
     * @see https://www.php.net/manual/en/class.dateinterval.php
53
     *
54
     * @return array<mixed>
55
     */
56
    public function getCreateDateIntervalData(): array
57
    {
58
        return [
59
            ['P100D'],
60
            ['P4Y1DT9H11M3S'],
61
            ['P2Y4DT6H8M'],
62
            ['P7Y8M'],
63
        ];
64
    }
65
66
    /**
67
     * Ensure that createDateInterval() will throw a DateIntervalFactoryException if the provided $spec is invalid
68
     *
69
     * @dataProvider getCreateDateIntervalWillThrowDateIntervalFactoryExceptionData
70
     *
71
     * @throws DateIntervalFactoryException
72
     */
73
    public function testCreateDateIntervalWillThrowDateIntervalFactoryException(string $spec): void
74
    {
75
        $factory = new DateIntervalFactory();
76
77
        $this->expectException(DateIntervalFactoryException::class);
78
        $this->expectExceptionMessage(
79
            sprintf(
80
                'Failed to create a valid \DateInterval instance using \'%s\'',
81
                $spec
82
            )
83
        );
84
85
        $factory->createDateInterval($spec);
86
    }
87
88
    /**
89
     * @return array<mixed>
90
     */
91
    public function getCreateDateIntervalWillThrowDateIntervalFactoryExceptionData(): array
92
    {
93
        return [
94
            ['test'],
95
            ['invalid'],
96
        ];
97
    }
98
}
99