File: FluidSettersTest.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 (163 lines) | stat: -rw-r--r-- 5,205 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?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 Tests\AbstractTestCase;

class FluidSettersTest extends AbstractTestCase
{
    public function testFluidYearSetter()
    {
        $d = Carbon::now();
        $d2 = $d->year(1995);
        $this->assertInstanceOfCarbon($d2);
        $this->assertInstanceOf(Carbon::class, $d2);
        $this->assertSame($this->immutableNow->year, $d->year);
        $this->assertSame(1995, $d2->year);
    }

    public function testFluidMonthSetter()
    {
        $d = Carbon::now();
        $d2 = $d->month(3);
        $this->assertInstanceOfCarbon($d2);
        $this->assertInstanceOf(Carbon::class, $d2);
        $this->assertSame($this->immutableNow->month, $d->month);
        $this->assertSame(3, $d2->month);
    }

    public function testFluidMonthSetterWithWrap()
    {
        $d = Carbon::createFromDate(2012, 8, 21);
        $d2 = $d->month(13);
        $this->assertInstanceOfCarbon($d2);
        $this->assertInstanceOf(Carbon::class, $d2);
        $this->assertSame(8, $d->month);
        $this->assertSame(1, $d2->month);
    }

    public function testFluidDaySetter()
    {
        $d = Carbon::now();
        $d2 = $d->day(2);
        $this->assertInstanceOfCarbon($d2);
        $this->assertInstanceOf(Carbon::class, $d2);
        $this->assertSame($this->immutableNow->day, $d->day);
        $this->assertSame(2, $d2->day);
    }

    public function testFluidDaySetterWithWrap()
    {
        $d = Carbon::createFromDate(2000, 1, 3);
        $d2 = $d->day(32);
        $this->assertInstanceOfCarbon($d2);
        $this->assertInstanceOf(Carbon::class, $d2);
        $this->assertSame(3, $d->day);
        $this->assertSame(1, $d2->day);
    }

    public function testFluidSetDate()
    {
        $d = Carbon::createFromDate(2000, 1, 1);
        $d2 = $d->setDate(1995, 13, 32);
        $this->assertInstanceOfCarbon($d2);
        $this->assertInstanceOf(Carbon::class, $d2);
        $this->assertCarbon($d, 2000, 1, 1);
        $this->assertCarbon($d2, 1996, 2, 1);
    }

    public function testFluidHourSetter()
    {
        $d = Carbon::now();
        $d2 = $d->hour(2);
        $this->assertInstanceOfCarbon($d2);
        $this->assertInstanceOf(Carbon::class, $d2);
        $this->assertSame($this->immutableNow->hour, $d->hour);
        $this->assertSame(2, $d2->hour);
    }

    public function testFluidHourSetterWithWrap()
    {
        $d = Carbon::now();
        $d2 = $d->hour(25);
        $this->assertInstanceOfCarbon($d2);
        $this->assertInstanceOf(Carbon::class, $d2);
        $this->assertSame($this->immutableNow->hour, $d->hour);
        $this->assertSame(1, $d2->hour);
    }

    public function testFluidMinuteSetter()
    {
        $d = Carbon::now();
        $d2 = $d->minute(2);
        $this->assertInstanceOfCarbon($d2);
        $this->assertInstanceOf(Carbon::class, $d2);
        $this->assertSame($this->immutableNow->minute, $d->minute);
        $this->assertSame(2, $d2->minute);
    }

    public function testFluidMinuteSetterWithWrap()
    {
        $d = Carbon::now();
        $d2 = $d->minute(61);
        $this->assertInstanceOfCarbon($d2);
        $this->assertInstanceOf(Carbon::class, $d2);
        $this->assertSame($this->immutableNow->minute, $d->minute);
        $this->assertSame(1, $d2->minute);
    }

    public function testFluidSecondSetter()
    {
        $d = Carbon::now();
        $d2 = $d->second(2);
        $this->assertInstanceOfCarbon($d2);
        $this->assertInstanceOf(Carbon::class, $d2);
        $this->assertSame($this->immutableNow->second, $d->second);
        $this->assertSame(2, $d2->second);
    }

    public function testFluidSecondSetterWithWrap()
    {
        $d = Carbon::now();
        $d2 = $d->second(62);
        $this->assertInstanceOfCarbon($d2);
        $this->assertInstanceOf(Carbon::class, $d2);
        $this->assertSame($this->immutableNow->second, $d->second);
        $this->assertSame(2, $d2->second);
    }

    public function testFluidSetTime()
    {
        $d = Carbon::createFromDate(2000, 1, 1);
        $this->assertInstanceOfCarbon($d2 = $d->setTime(25, 61, 61));
        $this->assertCarbon($d2, 2000, 1, 2, 2, 2, 1);
    }

    public function testFluidTimestampSetter()
    {
        $d = Carbon::now();
        $this->assertInstanceOfCarbon($d2 = $d->timestamp(10));
        $this->assertSame(10, $d2->timestamp);

        $this->assertInstanceOfCarbon($d2 = $d->timestamp(1600887164.88952298));
        $this->assertSame('2020-09-23 14:52:44.889523', $d2->format('Y-m-d H:i:s.u'));

        $this->assertInstanceOfCarbon($d2 = $d->timestamp('0.88951247 1600887164'));
        $this->assertSame('2020-09-23 14:52:44.889512', $d2->format('Y-m-d H:i:s.u'));

        $this->assertInstanceOfCarbon($d2 = $d->timestamp('0.88951247/1600887164/12.56'));
        $this->assertSame('2020-09-23 14:52:57.449512', $d2->format('Y-m-d H:i:s.u'));
    }
}