File: ToDatePeriodTest.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 (148 lines) | stat: -rw-r--r-- 5,499 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
<?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\Translator;
use DatePeriod;
use DateTime;
use DateTimeImmutable;
use ReflectionMethod;
use Tests\AbstractTestCase;

class ToDatePeriodTest extends AbstractTestCase
{
    public function testToArrayIsNotEmptyArray()
    {
        $periodClass = static::$periodClass;
        $period = $periodClass::create('2021-01-05', '2021-02-15');
        $result = $period->toDatePeriod();

        $this->assertFalse($period->isEndExcluded());
        $this->assertSame(DatePeriod::class, \get_class($result));
        $this->assertSame('2021-01-05', $result->getStartDate()->format('Y-m-d'));
        $this->assertSame('2021-02-15', $result->getEndDate()->format('Y-m-d'));
        // CarbonPeriod includes end date by default while DatePeriod will always exclude it
        $dates = iterator_to_array($result);
        $this->assertSame('2021-02-14', end($dates)->format('Y-m-d'));
        $this->assertTrue($period->equalTo($periodClass::instance($result)));

        $period = $periodClass::create('2021-01-05', '2021-02-15', $periodClass::EXCLUDE_END_DATE);
        $result = $period->toDatePeriod();
        $newInstance = $periodClass::instance($result);

        $this->assertTrue($period->isEndExcluded());
        $this->assertSame(DatePeriod::class, \get_class($result));
        $this->assertSame('2021-01-05', $result->getStartDate()->format('Y-m-d'));
        $this->assertSame('2021-02-14', $result->getEndDate()->format('Y-m-d'));
        $dates = iterator_to_array($result);
        $this->assertSame('2021-02-13', end($dates)->format('Y-m-d'));
        $this->assertSame('2021-01-05', $newInstance->getStartDate()->format('Y-m-d'));
        $this->assertSame('2021-02-14', $newInstance->getEndDate()->format('Y-m-d'));

        $period = $periodClass::create('2021-01-05', 3);
        $result = $period->toDatePeriod();
        $newInstance = $periodClass::instance($result);

        $this->assertSame(DatePeriod::class, \get_class($result));
        $this->assertSame('2021-01-05', $result->getStartDate()->format('Y-m-d'));
        $this->assertNull($result->getEndDate());
        $dates = iterator_to_array($result);
        $this->assertSame('2021-01-08', end($dates)->format('Y-m-d'));
        $this->assertSame('2021-01-05', $newInstance->getStartDate()->format('Y-m-d'));
        $this->assertSame(3, $newInstance->getRecurrences());
    }

    public function testWithIntervalLocalized()
    {
        CarbonInterval::setLocale('fr');
        $periodClass = static::$periodClass;
        $period = $periodClass::create('2021-01-05', 3);
        $result = $period->floor()->toDatePeriod();

        $this->assertSame(DatePeriod::class, \get_class($result));
        $this->assertSame('2021-01-05', $result->getStartDate()->format('Y-m-d'));
        $this->assertNull($result->getEndDate());

        if (method_exists($result, 'getRecurrences')) {
            $this->assertSame(3, $result->getRecurrences());
        }

        CarbonInterval::setLocale('en');
    }

    public function testWithModifiedEnglish()
    {
        $periodClass = static::$periodClass;
        $translator = Translator::get('en');
        $translator->setTranslations([
            'day' => ':count boring day|:count boring days',
        ]);
        $period = $periodClass::create('2021-01-05', 3);
        $result = $period->floor()->toDatePeriod();

        $this->assertSame(DatePeriod::class, \get_class($result));
        $this->assertSame('2021-01-05', $result->getStartDate()->format('Y-m-d'));
        $this->assertNull($result->getEndDate());

        if (method_exists($result, 'getRecurrences')) {
            $this->assertSame(3, $result->getRecurrences());
        }

        $translator->resetMessages();
    }

    public function testRawDate()
    {
        $periodClass = static::$periodClass;
        $period = new $periodClass();
        $method = new ReflectionMethod($periodClass, 'rawDate');
        $method->setAccessible(true);

        $this->assertNull($method->invoke($period, false));
        $this->assertNull($method->invoke($period, null));

        $date = new DateTime();
        $this->assertSame($date, $method->invoke($period, $date));

        $date = new DateTimeImmutable();
        $this->assertSame($date, $method->invoke($period, $date));

        $date = new Carbon();
        $raw = $method->invoke($period, $date);
        $this->assertInstanceOf(DateTime::class, $raw);
        $this->assertEquals($date, $raw);

        $date = new CarbonImmutable();
        $raw = $method->invoke($period, $date);
        $this->assertInstanceOf(DateTimeImmutable::class, $raw);
        $this->assertEquals($date, $raw);

        $date = new class() extends DateTime {
            // void
        };
        $raw = $method->invoke($period, $date);
        $this->assertInstanceOf(DateTime::class, $raw);
        $this->assertEquals($date, $raw);

        $date = new class() extends DateTimeImmutable {
            // void
        };
        $raw = $method->invoke($period, $date);
        $this->assertInstanceOf(DateTimeImmutable::class, $raw);
        $this->assertEquals($date, $raw);
    }
}