File: FluidSettersTest.php

package info (click to toggle)
php-nesbot-carbon 2.73.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 19,940 kB
  • sloc: php: 158,741; xml: 99; makefile: 32; sh: 14
file content (134 lines) | stat: -rw-r--r-- 3,933 bytes parent folder | download | duplicates (3)
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
<?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\Carbon;

use Carbon\Carbon;
use Tests\AbstractTestCase;

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

    public function testFluidMonthSetter()
    {
        $d = Carbon::now();
        $this->assertInstanceOfCarbon($d->month(3));
        $this->assertSame(3, $d->month);
        // Can go to september 1 to 30 (but if it's the 31, it will overflow)
        $this->assertInstanceOfCarbon($d->startOfMonth()->setMonth(9));
        $this->assertSame(9, $d->month);
        $this->assertInstanceOfCarbon($d->months(3));
        $this->assertSame(3, $d->month);
        $this->assertInstanceOfCarbon($d->setMonths(9));
        $this->assertSame(9, $d->month);
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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