DateTimeTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 149
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 149
loc 149
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A isWeekDay() 5 5 1
A isWeekendDay() 5 5 1
A isSameDay() 8 8 1
A isDaylightSavings() 5 5 1
A isInRange() 7 7 1
A isInRangeWithInvalidRange() 7 7 1
A asAgoString() 4 4 1
A provideAgoStringData() 18 18 1
A asAgoStringWithFutureDate() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SubjectivePHPTestDateTime\Utilities;
4
5
use DateTime;
6
use DateTimeZone;
7
use PHPUnit\Framework\TestCase;
8
use SubjectivePHP\DateTime\Utilities\DateTimeUtil;
9
10
/**
11
 * Unit tests for the SubjectivePHP\UtilDateTime class.
12
 *
13
 * @coversDefaultClass \SubjectivePHP\DateTime\Utilities\DateTimeUtil
14
 * @covers ::<private>
15
 */
16 View Code Duplication
final class DateTimeTest extends TestCase
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
{
18
    /**
19
     * Verify basic behavior of isWeekDay().
20
     *
21
     * @test
22
     * @covers ::isWeekDay
23
     *
24
     * @return void
25
     */
26
    public function isWeekDay()
27
    {
28
        $this->assertTrue(DateTimeUtil::isWeekDay(new DateTime('2014-07-02')));
29
        $this->assertFalse(DateTimeUtil::isWeekDay(new DateTime('2014-07-05')));
30
    }
31
32
    /**
33
     * Verify basic behavior of isWeekendDay().
34
     *
35
     * @test
36
     * @covers ::isWeekendDay
37
     *
38
     * @return void
39
     */
40
    public function isWeekendDay()
41
    {
42
        $this->assertFalse(DateTimeUtil::isWeekendDay(new DateTime('2014-07-02')));
43
        $this->assertTrue(DateTimeUtil::isWeekendDay(new DateTime('2014-07-05')));
44
    }
45
46
    /**
47
     * Verify basic behavior of isSameDay().
48
     *
49
     * @test
50
     * @covers ::isSameDay
51
     *
52
     * @return void
53
     */
54
    public function isSameDay()
55
    {
56
        $thisDate = new DateTime('2015-01-01 12:00:00', new DateTimeZone('Pacific/Fiji'));
57
        $thatDate = new DateTime('2014-12-31 12:00:00', new DateTimeZone('America/New_York'));
58
59
        $this->assertNotEquals($thisDate->format('Y-m-d'), $thatDate->format('Y-m-d'));
60
        $this->assertTrue(DateTimeUtil::isSameDay($thisDate, $thatDate));
61
    }
62
63
    /**
64
     * Verify basic behavior of isDaylightSavings().
65
     *
66
     * @test
67
     * @covers ::isDaylightSavings
68
     *
69
     * @return void
70
     */
71
    public function isDaylightSavings()
72
    {
73
        $dateTime = new DateTime('now', new DateTimeZone('Pacific/Honolulu'));
74
        $this->assertFalse(DateTimeUtil::isDaylightSavings($dateTime));
75
    }
76
77
    /**
78
     * Verify basic behavior of isInRange().
79
     *
80
     * @test
81
     * @covers ::isInRange
82
     *
83
     * @return void
84
     */
85
    public function isInRange()
86
    {
87
        $currentDateTime = new DateTime('now');
88
        $startDateTime = new DateTime('last year');
89
        $endDateTime = new DateTime('next year');
90
        $this->assertTrue(DateTimeUtil::isInRange($currentDateTime, $startDateTime, $endDateTime));
91
    }
92
93
    /**
94
     * Verify error behavior of isInRange().
95
     *
96
     * @test
97
     * @covers ::isInRange
98
     * @expectedException \DomainException
99
     *
100
     * @return void
101
     */
102
    public function isInRangeWithInvalidRange()
103
    {
104
        $currentDateTime = new DateTime('now');
105
        $startDateTime = new DateTime('next year');
106
        $endDateTime = new DateTime('last year');
107
        DateTimeUtil::isInRange($currentDateTime, $startDateTime, $endDateTime);
108
    }
109
110
    /**
111
     * Verify basic behavior of asAgoString().
112
     *
113
     * @test
114
     * @covers ::asAgoString
115
     * @dataProvider provideAgoStringData
116
     *
117
     * @param string $dateTimeString    The date/time string.
118
     * @param string $expectedAgoString The expected ago string.
119
     *
120
     * @return void
121
     */
122
    public function asAgoString(string $dateTimeString, string $expectedAgoString)
123
    {
124
        $this->assertSame($expectedAgoString, DateTimeUtil::asAgoString(new DateTime($dateTimeString)));
125
    }
126
127
    /**
128
     * Returns data for the asAgoString test.
129
     *
130
     * @return array
131
     */
132
    public function provideAgoStringData()
133
    {
134
        return [
135
            [ '-1 minute', 'just now'],
136
            [ '-2 minutes', '2 minutes ago'],
137
            [ '-30 minutes', 'about an hour ago'],
138
            [ '-10 hours', 'about 10 hours ago'],
139
            [ '-25 hours', 'yesterday'],
140
            [ '-3 days', 'about 3 days ago'],
141
            [ '-8 days', 'last week'],
142
            [ '-3 weeks', 'about 3 weeks ago'],
143
            [ '-1 month', 'last month' ],
144
            [ '-2 months', 'about 2 months ago' ],
145
            [ '-12 months', 'last year' ],
146
            [ '-1 year', 'last year' ],
147
            [ '-4 years', 'about 4 years ago' ],
148
        ];
149
    }
150
151
    /**
152
     * Verify error behavior of asAgoString().
153
     *
154
     * @test
155
     * @covers ::asAgoString
156
     * @expectedException \DomainException
157
     *
158
     * @return void
159
     */
160
    public function asAgoStringWithFutureDate()
161
    {
162
        DateTimeUtil::asAgoString(new DateTime('tomorrow'));
163
    }
164
}
165