File: AddMonthsTest.php

package info (click to toggle)
php-nesbot-carbon 3.10.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,132 kB
  • sloc: php: 163,896; xml: 119; makefile: 32; sh: 14
file content (207 lines) | stat: -rw-r--r-- 6,423 bytes parent folder | download
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?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 PHPUnit\Framework\Attributes\DataProvider;
use Tests\AbstractTestCase;

class AddMonthsTest extends AbstractTestCase
{
    private ?Carbon $carbon = null;

    protected function setUp(): void
    {
        parent::setUp();

        /** @var Carbon $date */
        $date = Carbon::create(2016, 1, 31);

        $this->carbon = $date;
    }

    public static function dataForTestAddMonthNoOverflow()
    {
        return [
            [-2, 2015, 11, 30],
            [-1, 2015, 12, 31],
            [0, 2016, 1, 31],
            [1, 2016, 2, 29],
            [2, 2016, 3, 31],
        ];
    }

    #[DataProvider('dataForTestAddMonthNoOverflow')]
    public function testAddMonthNoOverflow(int $months, int $y, int $m, int $d)
    {
        $this->assertCarbon($this->carbon->addMonthNoOverflow($months), $y, $m, $d);
    }

    #[DataProvider('dataForTestAddMonthNoOverflow')]
    public function testAddMonthsNoOverflow(int $months, int $y, int $m, int $d)
    {
        $this->assertCarbon($this->carbon->addMonthsNoOverflow($months), $y, $m, $d);
    }

    public static function dataForTestSubMonthNoOverflow(): array
    {
        return [
            [-2, 2016, 3, 31],
            [-1, 2016, 2, 29],
            [0, 2016, 1, 31],
            [1, 2015, 12, 31],
            [2, 2015, 11, 30],
        ];
    }

    #[DataProvider('dataForTestSubMonthNoOverflow')]
    public function testSubMonthNoOverflow(int $months, int $y, int $m, int $d)
    {
        $this->assertCarbon($this->carbon->subMonthNoOverflow($months), $y, $m, $d);
    }

    #[DataProvider('dataForTestSubMonthNoOverflow')]
    public function testSubMonthsNoOverflow(int $months, int $y, int $m, int $d)
    {
        $this->assertCarbon($this->carbon->subMonthsNoOverflow($months), $y, $m, $d);
    }

    public static function dataForTestAddMonthWithOverflow(): array
    {
        return [
            [-2, 2015, 12, 1],
            [-1, 2015, 12, 31],
            [0, 2016, 1, 31],
            [1, 2016, 3, 2],
            [2, 2016, 3, 31],
        ];
    }

    #[DataProvider('dataForTestAddMonthWithOverflow')]
    public function testAddMonthWithOverflow(int $months, int $y, int $m, int $d)
    {
        $this->assertCarbon($this->carbon->addMonthWithOverflow($months), $y, $m, $d);
    }

    #[DataProvider('dataForTestAddMonthWithOverflow')]
    public function testAddMonthsWithOverflow(int $months, int $y, int $m, int $d)
    {
        $this->assertCarbon($this->carbon->addMonthsWithOverflow($months), $y, $m, $d);
    }

    public static function dataForTestSubMonthWithOverflow(): array
    {
        return [
            [-2, 2016, 3, 31],
            [-1, 2016, 3, 2],
            [0, 2016, 1, 31],
            [1, 2015, 12, 31],
            [2, 2015, 12, 1],
        ];
    }

    #[DataProvider('dataForTestSubMonthWithOverflow')]
    public function testSubMonthWithOverflow(int $months, int $y, int $m, int $d)
    {
        $this->assertCarbon($this->carbon->subMonthWithOverflow($months), $y, $m, $d);
    }

    #[DataProvider('dataForTestSubMonthWithOverflow')]
    public function testSubMonthsWithOverflow(int $months, int $y, int $m, int $d)
    {
        $this->assertCarbon($this->carbon->subMonthsWithOverflow($months), $y, $m, $d);
    }

    public function testSetOverflowIsTrue()
    {
        Carbon::useMonthsOverflow(true);
        $this->assertTrue(Carbon::shouldOverflowMonths());
    }

    public function testSetOverflowIsFalse()
    {
        Carbon::useMonthsOverflow(false);
        $this->assertFalse(Carbon::shouldOverflowMonths());
    }

    public function testSetOverflowIsResetInTests()
    {
        $this->assertTrue(Carbon::shouldOverflowMonths());
    }

    public function testSetOverflowIsReset()
    {
        Carbon::useMonthsOverflow(false);
        $this->assertFalse(Carbon::shouldOverflowMonths());

        Carbon::resetMonthsOverflow();
        $this->assertTrue(Carbon::shouldOverflowMonths());
    }

    #[DataProvider('dataForTestAddMonthWithOverflow')]
    public function testUseOverflowAddMonth(int $months, int $y, int $m, int $d)
    {
        Carbon::useMonthsOverflow(true);
        $this->assertCarbon($this->carbon->addMonth($months), $y, $m, $d);
    }

    #[DataProvider('dataForTestAddMonthWithOverflow')]
    public function testUseOverflowAddMonths(int $months, int $y, int $m, int $d)
    {
        Carbon::useMonthsOverflow(true);
        $this->assertCarbon($this->carbon->addMonths($months), $y, $m, $d);
    }

    #[DataProvider('dataForTestSubMonthWithOverflow')]
    public function testUseOverflowSubMonth(int $months, int $y, int $m, int $d)
    {
        Carbon::useMonthsOverflow(true);
        $this->assertCarbon($this->carbon->subMonth($months), $y, $m, $d);
    }

    #[DataProvider('dataForTestSubMonthWithOverflow')]
    public function testUseOverflowSubMonths(int $months, int $y, int $m, int $d)
    {
        Carbon::useMonthsOverflow(true);
        $this->assertCarbon($this->carbon->subMonths($months), $y, $m, $d);
    }

    #[DataProvider('dataForTestAddMonthNoOverflow')]
    public function testSkipOverflowAddMonth(int $months, int $y, int $m, int $d)
    {
        Carbon::useMonthsOverflow(false);
        $this->assertCarbon($this->carbon->addMonth($months), $y, $m, $d);
    }

    #[DataProvider('dataForTestAddMonthNoOverflow')]
    public function testSkipOverflowAddMonths(int $months, int $y, int $m, int $d)
    {
        Carbon::useMonthsOverflow(false);
        $this->assertCarbon($this->carbon->addMonths($months), $y, $m, $d);
    }

    #[DataProvider('dataForTestSubMonthNoOverflow')]
    public function testSkipOverflowSubMonth(int $months, int $y, int $m, int $d)
    {
        Carbon::useMonthsOverflow(false);
        $this->assertCarbon($this->carbon->subMonth($months), $y, $m, $d);
    }

    #[DataProvider('dataForTestSubMonthNoOverflow')]
    public function testSkipOverflowSubMonths(int $months, int $y, int $m, int $d)
    {
        Carbon::useMonthsOverflow(false);
        $this->assertCarbon($this->carbon->subMonths($months), $y, $m, $d);
    }
}