Passed
Pull Request — 4.0 (#7725)
by Daniel
08:37
created

DBTimeTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testParse() 0 7 1
A dataTestParse() 0 11 1
A testShort() 0 4 1
A testNice() 0 4 1
A setUp() 0 4 1
A testTimeStamp() 0 6 1
1
<?php
2
3
namespace SilverStripe\ORM\Tests;
4
5
use SilverStripe\i18n\i18n;
6
use SilverStripe\ORM\FieldType\DBField;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\ORM\FieldType\DBTime;
9
use SilverStripe\Security\Member;
10
11
class DBTimeTest extends SapphireTest
12
{
13
    protected function setUp()
14
    {
15
        parent::setUp();
16
        i18n::set_locale('en_NZ');
17
    }
18
19
    public function dataTestParse()
20
    {
21
        return [
22
            // Test am-pm conversion
23
            ['11:01 pm', '23:01:00'],
24
            ['11:01 am', '11:01:00'],
25
            ['12:01 pm', '12:01:00'],
26
            ['12:01 am', '00:01:00'],
27
            // Test seconds
28
            ['11:01.01 pm', '23:01:01'],
29
            ['12:01.01', '12:01:01'],
30
        ];
31
    }
32
33
    /**
34
     * @dataProvider dataTestParse
35
     * @param string $input
36
     * @param string $expected
37
     */
38
    public function testParse($input, $expected)
39
    {
40
        $time = DBField::create_field('Time', $input);
41
        $this->assertEquals(
42
            $expected,
43
            $time->getValue(),
44
            "Date parsed from {$input} should be {$expected}"
45
        );
46
    }
47
48
    public function testNice()
49
    {
50
        $time = DBTime::create_field('Time', '17:15:55');
51
        $this->assertRegexp('#5:15:55 PM#i', $time->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

51
        $this->assertRegexp('#5:15:55 PM#i', $time->/** @scrutinizer ignore-call */ Nice());
Loading history...
52
    }
53
54
    public function testShort()
55
    {
56
        $time = DBTime::create_field('Time', '17:15:55');
57
        $this->assertRegexp('#5:15 PM#i', $time->Short());
0 ignored issues
show
Bug introduced by
The method Short() 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

57
        $this->assertRegexp('#5:15 PM#i', $time->/** @scrutinizer ignore-call */ Short());
Loading history...
58
    }
59
60
    public function testTimeStamp()
61
    {
62
        $time = DBTime::create_field('Time', '01:00:00');
63
        $this->assertEquals(3600, $time->getTimestamp());
0 ignored issues
show
Bug introduced by
The method getTimestamp() 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

63
        $this->assertEquals(3600, $time->/** @scrutinizer ignore-call */ getTimestamp());
Loading history...
64
        $time = DBTime::create_field('Time', '23:30:00');
65
        $this->assertEquals(3600*23.5, $time->getTimestamp());
66
    }
67
}
68