File: NowAndOtherStaticHelpersTest.php

package info (click to toggle)
php-nesbot-carbon 2.65.0-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 18,280 kB
  • sloc: php: 157,346; xml: 42; makefile: 26; sh: 12
file content (122 lines) | stat: -rw-r--r-- 4,125 bytes parent folder | download | duplicates (2)
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
31
32
33
34
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php

declare(strict_types=1);

/**
 * This file is part of the Carbon package.
 *
 * (c) Brian Nesbitt <brian@nesbot.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Tests\CarbonImmutable;

use Carbon\CarbonImmutable as Carbon;
use DateTime;
use DateTimeZone;
use Tests\AbstractTestCase;

class NowAndOtherStaticHelpersTest extends AbstractTestCase
{
    public function testNow()
    {
        $dt = Carbon::now();
        $this->assertSame($this->immutableNow->getTimestamp(), $dt->timestamp);

        Carbon::setTestNow();
        $before = $this->getTimestamp();
        $dt = Carbon::now();
        $after = $this->getTimestamp();
        $this->assertGreaterThanOrEqual($before, $dt->timestamp);
        $this->assertLessThanOrEqual($after, $dt->timestamp);
    }

    public function testGetPreciseTimestamp()
    {
        $dt = Carbon::parse('2018-01-06 12:34:10.987126');
        $this->assertSame(1515260.0, $dt->getPreciseTimestamp(-3));
        $this->assertSame(151526005.0, $dt->getPreciseTimestamp(-1));
        $this->assertSame(1515260051.0, $dt->getPreciseTimestamp(0));
        $this->assertSame(15152600510.0, $dt->getPreciseTimestamp(1));
        $this->assertSame(151526005099.0, $dt->getPreciseTimestamp(2));
        $this->assertSame(1515260050987.0, $dt->valueOf());
        $this->assertSame(15152600509871.0, $dt->getPreciseTimestamp(4));
        $this->assertSame(151526005098713.0, $dt->getPreciseTimestamp(5));
        $this->assertSame(1515260050987126.0, $dt->getPreciseTimestamp(6));
        $this->assertSame(151526005098712600.0, $dt->getPreciseTimestamp(8));
        $this->assertSame(1515260050987126000.0, $dt->getPreciseTimestamp(9));
    }

    public function testGetTimestampMs()
    {
        $dt = Carbon::parse('2018-01-06 12:34:10.987126');
        $this->assertSame(1515260050987, $dt->getTimestampMs());
    }

    public function testNowWithTimezone()
    {
        $dt = Carbon::now('Europe/London');
        $this->assertSame($this->immutableNow->getTimestamp(), $dt->timestamp);

        Carbon::setTestNow();
        $before = $this->getTimestamp();
        $dt = Carbon::now('Europe/London');
        $after = $this->getTimestamp();
        $this->assertGreaterThanOrEqual($before, $dt->timestamp);
        $this->assertLessThanOrEqual($after, $dt->timestamp);
        $this->assertSame('Europe/London', $dt->tzName);
    }

    public function testToday()
    {
        $dt = Carbon::today();
        $this->assertSame(date('Y-m-d 00:00:00'), $dt->toDateTimeString());
    }

    public function testTodayWithTimezone()
    {
        $dt = Carbon::today('Europe/London');
        $dt2 = new DateTime('now', new DateTimeZone('Europe/London'));
        $this->assertSame($dt2->format('Y-m-d 00:00:00'), $dt->toDateTimeString());
    }

    public function testTomorrow()
    {
        $dt = Carbon::tomorrow();
        $dt2 = new DateTime('tomorrow');
        $this->assertSame($dt2->format('Y-m-d 00:00:00'), $dt->toDateTimeString());
    }

    public function testTomorrowWithTimezone()
    {
        $dt = Carbon::tomorrow('Europe/London');
        $dt2 = new DateTime('tomorrow', new DateTimeZone('Europe/London'));
        $this->assertSame($dt2->format('Y-m-d 00:00:00'), $dt->toDateTimeString());
    }

    public function testYesterday()
    {
        $dt = Carbon::yesterday();
        $dt2 = new DateTime('yesterday');
        $this->assertSame($dt2->format('Y-m-d 00:00:00'), $dt->toDateTimeString());
    }

    public function testYesterdayWithTimezone()
    {
        $dt = Carbon::yesterday('Europe/London');
        $dt2 = new DateTime('yesterday', new DateTimeZone('Europe/London'));
        $this->assertSame($dt2->format('Y-m-d 00:00:00'), $dt->toDateTimeString());
    }

    public function testMinValue()
    {
        $this->assertLessThanOrEqual(-2147483647, Carbon::minValue()->getTimestamp());
    }

    public function testMaxValue()
    {
        $this->assertGreaterThanOrEqual(2147483647, Carbon::maxValue()->getTimestamp());
    }
}