File: ToStringTest.php

package info (click to toggle)
php-nesbot-carbon 2.73.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,940 kB
  • sloc: php: 158,741; xml: 99; makefile: 32; sh: 14
file content (205 lines) | stat: -rw-r--r-- 6,332 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
<?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\CarbonPeriod;

use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterval;
use Carbon\CarbonPeriod;
use Generator;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\AbstractTestCase;

class ToStringTest extends AbstractTestCase
{
    #[DataProvider('dataForToString')]
    public function testToString($period, $expected)
    {
        $this->assertSame(
            $expected,
            $period->toString()
        );
    }

    public static function dataForToString(): Generator
    {
        $periodClass = static::$periodClass;
        Carbon::setTestNowAndTimezone(new Carbon('2015-09-01', 'America/Toronto'));

        yield [
                $periodClass::create('R4/2012-07-01T12:00:00/P7D'),
                '4 times every 1 week from 2012-07-01 12:00:00',
            ];
        yield [
                $periodClass::create(
                    Carbon::parse('2015-09-30'),
                    Carbon::parse('2015-10-03')
                ),
                'Every 1 day from 2015-09-30 to 2015-10-03',
            ];
        yield [
                $periodClass::create(
                    Carbon::parse('2015-09-30 12:50'),
                    CarbonInterval::days(3)->hours(5),
                    Carbon::parse('2015-10-03 19:00')
                ),
                'Every 3 days and 5 hours from 2015-09-30 12:50:00 to 2015-10-03 19:00:00',
            ];
        yield [
                $periodClass::create('2015-09-30 17:30'),
                'Every 1 day from 2015-09-30 17:30:00',
            ];
        yield [
                $periodClass::create('P1M14D'),
                'Every 1 month and 2 weeks from 2015-09-01',
            ];
        yield [
                $periodClass::create('2015-09-30 13:30', 'P17D')->setRecurrences(1),
                'Once every 2 weeks and 3 days from 2015-09-30 13:30:00',
            ];
        yield [
                $periodClass::create('2015-10-01', '2015-10-05', 'PT30M'),
                'Every 30 minutes from 2015-10-01 to 2015-10-05',
            ];

        Carbon::setTestNowAndTimezone();
    }

    public function testMagicToString()
    {
        $periodClass = static::$periodClass;
        $period = $periodClass::create(
            Carbon::parse('2015-09-30 12:50'),
            CarbonInterval::days(3)->hours(5),
            Carbon::parse('2015-10-03 19:00')
        );

        $this->assertSame(
            'Every 3 days and 5 hours from 2015-09-30 12:50:00 to 2015-10-03 19:00:00',
            (string) $period
        );
    }

    #[DataProvider('dataForToIso8601String')]
    public function testToIso8601String($period, $expected)
    {
        $this->assertSame(
            $expected,
            $period->toIso8601String()
        );
    }

    public static function dataForToIso8601String(): Generator
    {
        $periodClass = static::$periodClass;
        Carbon::setTestNowAndTimezone(new Carbon('2015-09-01', 'America/Toronto'));

        yield [
                $periodClass::create('R4/2012-07-01T00:00:00-04:00/P7D'),
                'R4/2012-07-01T00:00:00-04:00/P7D',
            ];
        yield [
                $periodClass::create(
                    Carbon::parse('2015-09-30', 'America/Toronto'),
                    Carbon::parse('2015-10-03', 'America/Toronto')
                ),
                '2015-09-30T00:00:00-04:00/P1D/2015-10-03T00:00:00-04:00',
            ];
        yield [
                $periodClass::create(
                    Carbon::parse('2015-09-30 12:50', 'America/Toronto'),
                    CarbonInterval::days(3)->hours(5),
                    Carbon::parse('2015-10-03 19:00', 'America/Toronto')
                ),
                '2015-09-30T12:50:00-04:00/P3DT5H/2015-10-03T19:00:00-04:00',
            ];
        yield [
                $periodClass::create(
                    Carbon::parse('2015-09-30 12:50', 'America/Toronto'),
                    CarbonInterval::days(3)
                ),
                '2015-09-30T12:50:00-04:00/P3D',
            ];
        yield [
                $periodClass::create(),
                '2015-09-01T00:00:00-04:00/P1D',
            ];
    }

    public function testSpec()
    {
        $periodClass = static::$periodClass;
        $period = $periodClass::create(
            Carbon::parse('2015-09-30'),
            CarbonInterval::days(3)->hours(5),
            Carbon::parse('2015-10-03')
        );

        $this->assertSame(
            '2015-09-30T00:00:00-04:00/P3DT5H/2015-10-03T00:00:00-04:00',
            $period->spec()
        );
    }

    public function testStartOfWeekForPeriod()
    {
        $periodClass = static::$periodClass;
        $sunday = CarbonImmutable::parse('2019-12-01');

        $period = $periodClass::create($sunday->startOfWeek(), '1 week', $sunday->endOfWeek())->toArray();

        $formattedSunday = $sunday->startOfWeek()->format('Y-m-d H:i:s');

        $this->assertSame(
            '2019-11-25 00:00:00',
            $formattedSunday
        );

        $this->assertSame(
            $formattedSunday,
            $period[0]->toImmutable()->startOfWeek()->format('Y-m-d H:i:s')
        );
    }

    public function testToStringCustomization()
    {
        $periodClass = static::$periodClass;
        $sunday = CarbonImmutable::parse('2019-12-01');

        $period = $periodClass::create($sunday->startOfWeek(), '1 week', $sunday->endOfWeek());

        $this->assertSame(
            'Every 1 week from 2019-11-25 00:00:00 to 2019-12-01 23:59:59!!',
            $period.'!!'
        );

        $periodClass::setToStringFormat('m/d');

        $this->assertSame(
            'Every 1 week from 11/25 to 12/01!!',
            $period.'!!'
        );

        $period->settings(['toStringFormat' => static function (CarbonPeriod $period) {
            return $period->toIso8601String();
        }]);

        $this->assertSame(
            '2019-11-25T00:00:00-05:00/P7D/2019-12-01T23:59:59-05:00!!',
            $period.'!!'
        );

        $periodClass::resetToStringFormat();
    }
}