Passed
Push — master ( 15bdaa...c2dfbc )
by Saulius
10:02
created

DateTimeTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 126
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 1
1
<?php
2
/**
3
 * This file is part of the sauls/helpers package.
4
 *
5
 * @author    Saulius Vaičeliūnas <[email protected]>
6
 * @link      http://saulius.vaiceliunas.lt
7
 * @copyright 2018
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sauls\Component\Helper;
14
15
use PHPUnit\Framework\TestCase;
16
17
class DateTimeTest extends TestCase
18
{
19
    /**
20
     * @test
21
     * @dataProvider getCountdownDatesData
22
     *
23
     * @param $from
24
     * @param $to
25
     * @param $result
26
     */
27
    public function should_return_correct_countdown_by_given_format($from, $to, $result)
28
    {
29
        $this->assertEquals($result, countdown($from, $to));
30
    }
31
32
    /**
33
     * @return array
34
     */
35
    public function getCountdownDatesData(): array
36
    {
37
        return [
38
            ['2016-01-01 00:00:00', '2016-01-01 00:00:01', '00:00:01'],
39
            ['2016-01-01 00:00:00', '2016-01-02 00:00:01', '24:00:01'],
40
            ['2016-01-01 00:00:00', '2016-01-03 00:00:01', '48:00:01'],
41
            ['2016-01-01 00:00:00', '2016-01-04 00:00:01', '72:00:01'],
42
            ['2016-01-01 00:00:00', '2016-01-05 00:00:01', '96:00:01'],
43
            ['2016-01-01 00:00:00', '2016-01-03 22:35:00', '70:35:00'],
44
            ['2016-01-02 00:01:00', '2016-01-01 01:23:59', '-22:37:01'],
45
        ];
46
    }
47
48
    /**
49
     * @test
50
     * @dataProvider getPrintElapsedTimeShortStringsData
51
     *
52
     * @param string|\DateTime $dateTime
53
     */
54
    public function should_print_elapsed_time_short_strings($dateTime, string $expected, array $labels = [])
55
    {
56
        $this->assertContains($expected, print_elapsed_time_short($dateTime, $labels));
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    public function getPrintElapsedTimeShortStringsData(): array
63
    {
64
        $now = new \DateTime();
65
66
        return [
67
            [(clone $now)->modify('-3 month -1 hour -29 minutes -26 seconds')->format('Y-m-d H:i:s'), '3mo'],
68
            [(new \DateTime())->modify('-7 second'), 's'],
69
            [(new \DateTime())->modify('-1 second'), 's'],
70
            [(new \DateTime())->modify('-7 minute'), 'm'],
71
            [(new \DateTime())->modify('-1 hour'), 'h'],
72
            [(new \DateTime())->modify('-7 days'), 'w'],
73
            [(new \DateTime())->modify('-7 days'), 'savaite', ['single' => ['{week}' => 'savaite']]],
74
            [(new \DateTime())->modify('-1 day'), 'd'],
75
            [(new \DateTime())->modify('-1 month'), 'mo'],
76
            [(new \DateTime())->modify('-1 year'), 'mo'],
77
        ];
78
    }
79
80
    /**
81
     * @test
82
     * @dataProvider getPrintElapsedTimeLongStringsData
83
     *
84
     * @param string|\DateTime $dateTime
85
     */
86
    public function should_print_elapsed_time_long_strings($dateTime, string $expected, array $labels = [])
87
    {
88
        $this->assertContains($expected, print_elapsed_time_long($dateTime, $labels));
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    public function getPrintElapsedTimeLongStringsData(): array
95
    {
96
        $now = new \DateTime();
97
98
        return [
99
            [(clone $now)->modify('-3 month -1 hour -29 minutes -26 seconds')->format('Y-m-d H:i:s'), '3mo 2d 1h 29m'],
100
            [(new \DateTime())->modify('-1 year -1 month'), 'yr'],
101
            [(new \DateTime())->modify('-1 year -1 month'), 'years', ['single' => ['{year}' => 'years']]],
102
            [
103
                (new \DateTime())->modify('-1 year -1 month'),
104
                '1metai 1menesis 18valandu',
105
                ['single' => ['{year}' => 'metai', '{month}' => 'menesis'], 'plural' => ['{hours}' => 'valandu']],
106
            ],
107
        ];
108
    }
109
110
    /**
111
     * @test
112
     * @dataProvider getFormatElapsedTimeStringData
113
     */
114
    public function should_format_elapsed_time_strings(
115
        $expected,
116
        int $timeLeft,
117
        array $elapsedTimeLabels,
118
        array $timeStrings
119
    ) {
120
        $this->assertSame($expected, format_elapsed_time_string($timeLeft, $elapsedTimeLabels, $timeStrings));
121
    }
122
123
    public function getFormatElapsedTimeStringData(): array
124
    {
125
        return [
126
            ['1s', 1, DEFAULT_ELAPSED_TIME_LABELS, ['single' => '{second}']],
127
            ['2s', 2, DEFAULT_ELAPSED_TIME_LABELS, ['plural' => '{seconds}']],
128
            [
129
                '1-eri metai',
130
                1,
131
                array_merge(DEFAULT_ELAPSED_TIME_LABELS, ['single' => ['{year}' => '-eri metai']]),
132
                ['single' => '{year}'],
133
            ],
134
            [
135
                '23menesiai',
136
                23,
137
                array_merge(DEFAULT_ELAPSED_TIME_LABELS, ['plural' => ['{months}' => 'menesiai']]),
138
                ['plural' => '{months}'],
139
            ]
140
        ];
141
    }
142
}
143