DateUtilsTest.php 4.65 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
//    Pasteque server testing
//
//    Copyright (C) 
//			2012 Scil (http://scil.coop)
//			2017 Karamel, Association Pastèque (karamel@creativekara.fr, https://pasteque.org)
//
//    This file is part of Pasteque.
//
//    Pasteque is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Pasteque is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Pasteque.  If not, see <http://www.gnu.org/licenses/>.
namespace Pasteque\Server;

use \Pasteque\Server\System\DateUtils;
use \PHPUnit\Framework\TestCase;

require_once(dirname(dirname(__FILE__)) . "/common_load.php");

class DateUtilsTest extends TestCase {

31
    protected function setUp(): void {
32
33
    }

34
    protected function tearDown(): void {
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    }

    public function testToTimestampNull() {
        $this->assertNull(DateUtils::toTimestamp(null));
    }

    public function testToTimestampTimestamp() {
        $timestamp =  1171502725;
        $this->assertEquals($timestamp, DateUtils::toTimestamp($timestamp));
    }

    public function testToTimestampDateTime() {
        $timestamp =  1171502725;
        $date = new \DateTime();
        $date->setTimestamp($timestamp);
        $this->assertEquals($timestamp, DateUtils::toTimestamp($date));
    }

    public function testToTimestampInvalid() {
        $this->assertNull(DateUtils::toTimestamp("blabla"));
    }

    public function testReadTimestamp() {
        $timestamp = 1171502725;
        $date = DateUtils::readTimestamp($timestamp);
        $this->assertNotEquals(false, $date);
        $this->assertEquals($timestamp, $date->getTimestamp());
    }

    public function testReadTimestampNok() {
        $date = 'date';
        $this->assertFalse(DateUtils::readTimestamp($date));
    }

    /** @depends testReadTimestamp */
    public function testReadDateTimestamp() {
        $timestamp = 1171502725;
        $date = DateUtils::readDate($timestamp);
        $this->assertNotEquals(false, $date);
        $this->assertEquals($timestamp, $date->getTimestamp());
    }

77
78
79
80
81
82
83
    public function testReadDateTimestampString() {
        $timestamp = '1171502725';
        $date = DateUtils::readDate($timestamp);
        $this->assertNotEquals(false, $date);
        $this->assertEquals($timestamp, $date->getTimestamp()); 
    }

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    public function testReadDateDateTime() {
        $timestamp =  1171502725;
        $date = new \DateTime();
        $date->setTimestamp($timestamp);
        $d2 = DateUtils::readDate($date);
        $this->assertNotEquals(false, $d2);
        $this->assertEquals($timestamp, $d2->getTimestamp());
    }

    public function testReadDateNull() {
        $input = null;
        $this->assertNull(DateUtils::readDate($input));
    }

    public function testReadDateNok() {
        $input = 'not a date';
        $this->assertFalse(DateUtils::readDate($input));
    }
102

103
104
105
106
107
108
109
110
111
112
113
    public function testReadDateString() {
        $date = DateUtils::readDate('2017-10-01');
        $this->assertNotEquals(false, $date);
        $this->assertEquals('2017-10-01 00:00:00',
                $date->format('Y-m-d H:i:s'));
        $date = DateUtils::readDate('2017-10-01 11:30');
        $this->assertNotEquals(false, $date);
        $this->assertEquals('2017-10-01 11:30:00',
                $date->format('Y-m-d H:i:s'));
    }

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    /** @depends testReadDateTimestamp
     * @depends testReadDateDateTime */
    public function testEquals() {
        $t1 = 1171502725;
        $t2 = 1171502727;
        $dt1 = $date = new \DateTime();
        $date->setTimestamp($t1);
        $dt2 = $date = new \DateTime();
        $dt2->setTimestamp($t2);
        $this->assertFalse(DateUtils::equals("a", $t1));
        $this->assertFalse(DateUtils::equals("a", null));
        $this->assertFalse(DateUtils::equals(null, "a"));
        $this->assertTrue(DateUtils::equals(null, null));
        $this->assertTrue(DateUtils::equals($t1, $t1));
        $this->assertTrue(DateUtils::equals($t1, $dt1));
        $this->assertTrue(DateUtils::equals($dt1, $t1));
        $this->assertTrue(DateUtils::equals($dt1, $dt1));
        $this->assertFalse(DateUtils::equals($t1, $t2));
        $this->assertFalse(DateUtils::equals($t1, $dt2));
        $this->assertFalse(DateUtils::equals($dt2, $t1));
        $this->assertFalse(DateUtils::equals($dt1, $dt2));
    }
136
}