SettersTest::testDaySetterWithWrap()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace HMLB\Date\Tests\Date;
4
5
/*
6
 * This file is part of the Date package.
7
 *
8
 * (c) Hugues Maignol <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
use HMLB\Date\Date;
15
use HMLB\Date\Tests\AbstractTestCase;
16
use InvalidArgumentException;
17
18
class SettersTest extends AbstractTestCase
19
{
20
    public function testYearSetter()
21
    {
22
        $d = Date::now()->year(1995);
23
        $this->assertSame(1995, $d->getYear());
24
    }
25
26
    public function testImmutable()
27
    {
28
        $d = Date::now()->year(1989);
29
        $d2 = $d->year(1995);
30
        $this->assertSame(1989, $d->getYear());
31
        $this->assertSame(1995, $d2->getYear());
32
    }
33
34
    public function testMonthSetter()
35
    {
36
        $d = Date::now()->month(3);
37
        $this->assertSame(3, $d->getMonth());
38
    }
39
40
    public function testMonthSetterWithWrap()
41
    {
42
        $d = Date::now()->month(13);
43
        $this->assertSame(1, $d->getMonth());
44
    }
45
46
    public function testDaySetter()
47
    {
48
        $d = Date::now()->day(2);
49
        $this->assertSame(2, $d->getDay());
50
    }
51
52
    public function testDaySetterWithWrap()
53
    {
54
        $d = Date::createFromDate(2012, 8, 5)->day(32);
55
        $this->assertSame(1, $d->getDay());
56
    }
57
58
    public function testHourSetter()
59
    {
60
        $d = Date::now()->hour(2);
61
        $this->assertSame(2, $d->getHour());
62
    }
63
64
    public function testHourSetterWithWrap()
65
    {
66
        $d = Date::now()->hour(25);
67
        $this->assertSame(1, $d->getHour());
68
    }
69
70
    public function testMinuteSetter()
71
    {
72
        $d = Date::now()->minute(2);
73
        $this->assertSame(2, $d->getMinute());
74
    }
75
76
    public function testMinuteSetterWithWrap()
77
    {
78
        $d = Date::now()->minute(65);
79
        $this->assertSame(5, $d->getMinute());
80
    }
81
82
    public function testSecondSetter()
83
    {
84
        $d = Date::now()->second(2);
85
        $this->assertSame(2, $d->getSecond());
86
    }
87
88
    public function testTimeSetter()
89
    {
90
        $d = Date::now()->setTime(1, 1, 1);
91
        $this->assertSame(1, $d->getSecond());
92
        $d = $d->setTime(1, 1);
93
        $this->assertSame(0, $d->getSecond());
94
    }
95
96
    public function testTimeSetterWithChaining()
97
    {
98
        $d = Date::now();
99
        $d = $d->setTime(2, 2, 2)->setTime(1, 1, 1);
100
        $this->assertInstanceOf(Date::class, $d);
101
        $this->assertSame(1, $d->getSecond());
102
        $d = $d->setTime(2, 2, 2)->setTime(1, 1);
103
        $this->assertInstanceOf(Date::class, $d);
104
        $this->assertSame(0, $d->getSecond());
105
    }
106
107
    public function testTimeSetterWithZero()
108
    {
109
        $d = Date::now();
110
        $d = $d->setTime(1, 1);
111
        $this->assertSame(0, $d->getSecond());
112
    }
113
114
    public function testDateTimeSetter()
115
    {
116
        $d = Date::now();
117
        $d = $d->setDateTime($d->getYear(), $d->getMonth(), $d->getDay(), 1, 1, 1);
118
        $this->assertSame(1, $d->getSecond());
119
    }
120
121
    public function testDateTimeSetterWithZero()
122
    {
123
        $d = Date::now();
124
        $d = $d->setDateTime($d->getYear(), $d->getMonth(), $d->getDay(), 1, 1);
125
        $this->assertSame(0, $d->getSecond());
126
    }
127
128
    public function testDateTimeSetterWithChaining()
129
    {
130
        $d = Date::now();
131
        $d = $d->setDateTime(2013, 9, 24, 17, 4, 29);
132
        $this->assertInstanceOf(Date::class, $d);
133
        $d = $d->setDateTime(2014, 10, 25, 18, 5, 30);
134
        $this->assertInstanceOf(Date::class, $d);
135
        $this->assertDate($d, 2014, 10, 25, 18, 5, 30);
136
    }
137
138
    public function testSecondSetterWithWrap()
139
    {
140
        $d = Date::now()->second(65);
141
        $this->assertSame(5, $d->getSecond());
142
    }
143
144
    public function testTimestampSetter()
145
    {
146
        $d = Date::now()->timestamp(10);
147
        $this->assertSame(10, $d->getTimestamp());
148
149
        $d = $d->setTimestamp(11);
150
        $this->assertSame(11, $d->getTimestamp());
151
    }
152
153
    /**
154
     * @expectedException \InvalidArgumentException
155
     */
156
    public function testSetTimezoneWithInvalidTimezone()
157
    {
158
        $d = Date::now();
159
        $d->setTimezone('sdf');
160
    }
161
162
    public function testTimezoneWithInvalidTimezone()
163
    {
164
        $d = Date::now();
165
166
        try {
167
            $d->timezone('sdf');
168
            $this->fail('InvalidArgumentException has not been raised.');
169
        } catch (InvalidArgumentException $expected) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
170
        }
171
172
        try {
173
            $d->timezone('sdf');
174
            $this->fail('InvalidArgumentException has not been raised.');
175
        } catch (InvalidArgumentException $expected) {
176
            //
177
        }
178
    }
179
180
    public function testSetTimezoneUsingString()
181
    {
182
        $d = Date::now();
183
        $d = $d->setTimezone('America/Toronto');
184
        $this->assertSame('America/Toronto', $d->getTimezoneName());
185
    }
186
187 View Code Duplication
    public function testTimezoneUsingString()
0 ignored issues
show
Duplication introduced by
This method 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...
188
    {
189
        $d = Date::now();
190
        $d = $d->setTimezone('America/Toronto');
191
        $this->assertSame('America/Toronto', $d->getTimezoneName());
192
193
        $d = $d->timezone('America/Vancouver');
194
        $this->assertSame('America/Vancouver', $d->getTimezoneName());
195
    }
196
197
    public function testSetTimezoneUsingDateTimeZone()
198
    {
199
        $d = Date::now();
200
        $d = $d->setTimezone(new \DateTimeZone('America/Toronto'));
201
        $this->assertSame('America/Toronto', $d->getTimezoneName());
202
    }
203
204 View Code Duplication
    public function testTimezoneUsingDateTimeZone()
0 ignored issues
show
Duplication introduced by
This method 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...
205
    {
206
        $d = Date::now();
207
        $d = $d->setTimezone(new \DateTimeZone('America/Toronto'));
208
        $this->assertSame('America/Toronto', $d->getTimezoneName());
209
210
        $d = $d->timezone(new \DateTimeZone('America/Vancouver'));
211
        $this->assertSame('America/Vancouver', $d->getTimezoneName());
212
    }
213
214 View Code Duplication
    public function testSetTimeFromTimeString()
0 ignored issues
show
Duplication introduced by
This method 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...
215
    {
216
        $d = Date::now();
217
218
        $d = $d->setTimeFromTimeString('09:15:30');
219
220
        $this->assertSame(9, $d->getHour());
221
        $this->assertSame(15, $d->getMinute());
222
        $this->assertSame(30, $d->getSecond());
223
    }
224
}
225