Completed
Push — 4.0 ( 1a421d...f764ab )
by Damian
31s
created

DBDatetimeTest::testSetNullAndZeroValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ORM\Tests;
4
5
use SilverStripe\i18n\i18n;
6
use SilverStripe\ORM\FieldType\DBDatetime;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\Security\Member;
9
10
/**
11
 * Tests for {@link Datetime} class.
12
 */
13
class DBDatetimeTest extends SapphireTest
14
{
15
    protected function setUp()
16
    {
17
        parent::setUp();
18
        i18n::set_locale('en_NZ');
19
    }
20
21
    public function testNowWithSystemDate()
22
    {
23
        $systemDatetime = DBDatetime::create_field('Datetime', date('Y-m-d H:i:s'));
24
        $nowDatetime = DBDatetime::now();
25
26
        $this->assertEquals($systemDatetime->Date(), $nowDatetime->Date());
0 ignored issues
show
Bug introduced by
The method Date() does not exist on SilverStripe\ORM\FieldType\DBField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
        $this->assertEquals($systemDatetime->/** @scrutinizer ignore-call */ Date(), $nowDatetime->Date());
Loading history...
27
    }
28
29
    public function testNowWithMockDate()
30
    {
31
        // Test setting
32
        $mockDate = '2001-12-31 22:10:59';
33
        DBDatetime::set_mock_now($mockDate);
34
        $systemDatetime = DBDatetime::create_field('Datetime', date('Y-m-d H:i:s'));
35
        $nowDatetime = DBDatetime::now();
36
        $this->assertNotEquals($systemDatetime->Date(), $nowDatetime->Date());
37
        $this->assertEquals($nowDatetime->getValue(), $mockDate);
38
39
        // Test clearing
40
        DBDatetime::clear_mock_now();
41
        $systemDatetime = DBDatetime::create_field('Datetime', date('Y-m-d H:i:s'));
42
        $nowDatetime = DBDatetime::now();
43
        $this->assertEquals($systemDatetime->Date(), $nowDatetime->Date());
44
    }
45
46
    public function testSetNullAndZeroValues()
47
    {
48
        $date = DBDatetime::create_field('Datetime', '');
49
        $this->assertNull($date->getValue(), 'Empty string evaluates to NULL');
50
51
        $date = DBDatetime::create_field('Datetime', null);
52
        $this->assertNull($date->getValue(), 'NULL is set as NULL');
53
54
        $date = DBDatetime::create_field('Datetime', false);
55
        $this->assertNull($date->getValue(), 'Boolean FALSE evaluates to NULL');
56
57
        $date = DBDatetime::create_field('Datetime', '0');
58
        $this->assertEquals('1970-01-01 00:00:00', $date->getValue(), 'String zero is UNIX epoch time');
59
60
        $date = DBDatetime::create_field('Datetime', 0);
61
        $this->assertEquals('1970-01-01 00:00:00', $date->getValue(), 'Numeric zero is UNIX epoch time');
62
    }
63
64
    public function testExtendedDateTimes()
65
    {
66
        $date = DBDatetime::create_field('Datetime', '1600-10-10 15:32:24');
67
        $this->assertEquals('10 Oct 1600 15 32 24', $date->Format('d MMM y H m s'));
0 ignored issues
show
Bug introduced by
The method Format() does not exist on SilverStripe\ORM\FieldType\DBField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
        $this->assertEquals('10 Oct 1600 15 32 24', $date->/** @scrutinizer ignore-call */ Format('d MMM y H m s'));
Loading history...
68
69
        $date = DBDatetime::create_field('Datetime', '3000-10-10 15:32:24');
70
        $this->assertEquals('10 Oct 3000 15 32 24', $date->Format('d MMM y H m s'));
71
    }
72
73
    public function testNice()
74
    {
75
        $date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');
76
        // note: Some localisation packages exclude the ',' in default medium format
77
        $this->assertRegExp('#31/12/2001(,)? 10:10:59 PM#i', $date->Nice());
0 ignored issues
show
Bug introduced by
The method Nice() does not exist on SilverStripe\ORM\FieldType\DBField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
        $this->assertRegExp('#31/12/2001(,)? 10:10:59 PM#i', $date->/** @scrutinizer ignore-call */ Nice());
Loading history...
78
    }
79
80
    public function testDate()
81
    {
82
        $date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');
83
        $this->assertEquals('31/12/2001', $date->Date());
84
    }
85
86
    public function testTime()
87
    {
88
        $date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');
89
        $this->assertRegexp('#10:10:59 PM#i', $date->Time());
0 ignored issues
show
Bug introduced by
The method Time() does not exist on SilverStripe\ORM\FieldType\DBField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
        $this->assertRegexp('#10:10:59 PM#i', $date->/** @scrutinizer ignore-call */ Time());
Loading history...
90
    }
91
92
    public function testTime24()
93
    {
94
        $date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');
95
        $this->assertEquals('22:10', $date->Time24());
0 ignored issues
show
Bug introduced by
The method Time24() does not exist on SilverStripe\ORM\FieldType\DBField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
        $this->assertEquals('22:10', $date->/** @scrutinizer ignore-call */ Time24());
Loading history...
96
    }
97
98
    public function testURLDateTime()
99
    {
100
        $date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');
101
        $this->assertEquals('2001-12-31%2022%3A10%3A59', $date->URLDateTime());
0 ignored issues
show
Bug introduced by
The method URLDateTime() does not exist on SilverStripe\ORM\FieldType\DBField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
        $this->assertEquals('2001-12-31%2022%3A10%3A59', $date->/** @scrutinizer ignore-call */ URLDateTime());
Loading history...
102
    }
103
104
    public function testAgoInPast()
105
    {
106
        DBDatetime::set_mock_now('2000-12-31 12:00:00');
107
108
        $this->assertEquals(
109
            '10 years ago',
110
            DBDatetime::create_field('Datetime', '1990-12-31 12:00:00')->Ago(),
0 ignored issues
show
Bug introduced by
The method Ago() does not exist on SilverStripe\ORM\FieldType\DBField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
            DBDatetime::create_field('Datetime', '1990-12-31 12:00:00')->/** @scrutinizer ignore-call */ Ago(),
Loading history...
111
            'Exact past match on years'
112
        );
113
114
        $this->assertEquals(
115
            '10 years ago',
116
            DBDatetime::create_field('Datetime', '1990-12-30 12:00:00')->Ago(),
117
            'Approximate past match on years'
118
        );
119
120
        $this->assertEquals(
121
            '1 year ago',
122
            DBDatetime::create_field('Datetime', '1999-12-30 12:00:12')->Ago(true, 1),
123
            'Approximate past match in singular, significance=1'
124
        );
125
126
        $this->assertEquals(
127
            '12 months ago',
128
            DBDatetime::create_field('Datetime', '1999-12-30 12:00:12')->Ago(),
129
            'Approximate past match in singular'
130
        );
131
132
        $this->assertEquals(
133
            '50 mins ago',
134
            DBDatetime::create_field('Datetime', '2000-12-31 11:10:11')->Ago(),
135
            'Approximate past match on minutes'
136
        );
137
138
        $this->assertEquals(
139
            '59 secs ago',
140
            DBDatetime::create_field('Datetime', '2000-12-31 11:59:01')->Ago(),
141
            'Approximate past match on seconds'
142
        );
143
144
        $this->assertEquals(
145
            'less than a minute ago',
146
            DBDatetime::create_field('Datetime', '2000-12-31 11:59:01')->Ago(false),
147
            'Approximate past match on seconds with $includeSeconds=false'
148
        );
149
150
        $this->assertEquals(
151
            '1 min ago',
152
            DBDatetime::create_field('Datetime', '2000-12-31 11:58:50')->Ago(false),
153
            'Test between 1 and 2 minutes with includeSeconds=false'
154
        );
155
156
        $this->assertEquals(
157
            '70 secs ago',
158
            DBDatetime::create_field('Datetime', '2000-12-31 11:58:50')->Ago(true),
159
            'Test between 1 and 2 minutes with includeSeconds=true'
160
        );
161
162
        $this->assertEquals(
163
            '4 mins ago',
164
            DBDatetime::create_field('Datetime', '2000-12-31 11:55:50')->Ago(),
165
            'Past match on minutes'
166
        );
167
168
        $this->assertEquals(
169
            '1 hour ago',
170
            DBDatetime::create_field('Datetime', '2000-12-31 10:50:58')->Ago(true, 1),
171
            'Past match on hours, significance=1'
172
        );
173
174
        $this->assertEquals(
175
            '3 hours ago',
176
            DBDatetime::create_field('Datetime', '2000-12-31 08:50:58')->Ago(),
177
            'Past match on hours'
178
        );
179
180
        DBDatetime::clear_mock_now();
181
    }
182
183
    public function testAgoInFuture()
184
    {
185
        DBDatetime::set_mock_now('2000-12-31 00:00:00');
186
187
        $this->assertEquals(
188
            'in 10 years',
189
            DBDatetime::create_field('Datetime', '2010-12-31 12:00:00')->Ago(),
190
            'Exact past match on years'
191
        );
192
193
        $this->assertEquals(
194
            'in 1 hour',
195
            DBDatetime::create_field('Datetime', '2000-12-31 1:01:05')->Ago(true, 1),
196
            'Approximate past match on minutes, significance=1'
197
        );
198
199
        $this->assertEquals(
200
            'in 61 mins',
201
            DBDatetime::create_field('Datetime', '2000-12-31 1:01:05')->Ago(),
202
            'Approximate past match on minutes'
203
        );
204
205
        DBDatetime::clear_mock_now();
206
    }
207
208
    public function testRfc3999()
209
    {
210
        // Dates should be formatted as: 2018-01-24T14:05:53+00:00
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
211
        $date = DBDatetime::create_field('Datetime', '2010-12-31 16:58:59');
212
        $this->assertEquals('2010-12-31T16:58:59+00:00', $date->Rfc3339());
0 ignored issues
show
Bug introduced by
The method Rfc3339() does not exist on SilverStripe\ORM\FieldType\DBField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

212
        $this->assertEquals('2010-12-31T16:58:59+00:00', $date->/** @scrutinizer ignore-call */ Rfc3339());
Loading history...
213
    }
214
}
215