File: FactoryTest.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 (149 lines) | stat: -rw-r--r-- 5,458 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
<?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\Factory;

use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Carbon\Factory;
use Carbon\FactoryImmutable;
use Tests\AbstractTestCase;
use Tests\Carbon\Fixtures\MyCarbon;

class FactoryTest extends AbstractTestCase
{
    public function testFactory()
    {
        $factory = new Factory();

        $this->assertInstanceOf(Carbon::class, $factory->parse('2018-01-01'));
        $this->assertSame('01/01/2018', $factory->parse('2018-01-01')->format('d/m/Y'));

        $factory = new Factory([
            'locale' => 'fr',
        ]);

        $this->assertSame('fr', $factory->parse('2018-01-01')->locale);

        $factory = new Factory([
            'locale' => 'fr',
        ], MyCarbon::class);

        $this->assertInstanceOf(MyCarbon::class, $factory->parse('2018-01-01'));
        $this->assertSame('01/01/2018', $factory->parse('2018-01-01')->format('d/m/Y'));

        $factory = new FactoryImmutable([
            'locale' => 'fr',
        ]);

        $this->assertInstanceOf(CarbonImmutable::class, $factory->parse('2018-01-01'));
        $this->assertSame('01/01/2018', $factory->parse('2018-01-01')->format('d/m/Y'));
    }

    public function testFactoryModification()
    {
        $factory = new Factory();

        $this->assertSame(Carbon::class, $factory->className());
        $this->assertSame($factory, $factory->className(MyCarbon::class));
        $this->assertSame(MyCarbon::class, $factory->className());

        $this->assertSame([], $factory->settings());
        $this->assertSame($factory, $factory->settings([
            'locale' => 'fr',
        ]));
        $this->assertSame([
            'locale' => 'fr',
        ], $factory->settings());

        $this->assertSame($factory, $factory->mergeSettings([
            'timezone' => 'Europe/Paris',
        ]));
        $this->assertSame([
            'locale' => 'fr',
            'timezone' => 'Europe/Paris',
        ], $factory->settings());

        $this->assertSame($factory, $factory->settings([
            'timezone' => 'Europe/Paris',
        ]));
        $this->assertSame([
            'timezone' => 'Europe/Paris',
        ], $factory->settings());
    }

    public function testFactoryTimezone()
    {
        Carbon::setTestNowAndTimezone(Carbon::parse('2020-09-04 03:39:04.123456', 'UTC'));

        $factory = new Factory();

        $date = $factory->now();
        $this->assertInstanceOf(Carbon::class, $date);
        $this->assertSame('2020-09-04 03:39:04.123456 UTC', $date->format('Y-m-d H:i:s.u e'));

        $factory = new Factory([
            'timezone' => 'Europe/Paris',
        ]);

        $this->assertSame('2020-09-04 05:39:04.123456 Europe/Paris', $factory->now()->format('Y-m-d H:i:s.u e'));
        $this->assertSame('2020-09-04 00:00:00.000000 Europe/Paris', $factory->today()->format('Y-m-d H:i:s.u e'));
        $this->assertSame('2020-09-05 00:00:00.000000 Europe/Paris', $factory->tomorrow()->format('Y-m-d H:i:s.u e'));
        $this->assertSame('2020-09-04 09:39:04.123456 Europe/Paris', $factory->parse('2020-09-04 09:39:04.123456')->format('Y-m-d H:i:s.u e'));

        $factory = new Factory([
            'timezone' => 'America/Toronto',
        ]);

        $this->assertSame('2020-09-03 23:39:04.123456 America/Toronto', $factory->now()->format('Y-m-d H:i:s.u e'));
        $this->assertSame('2020-09-03 00:00:00.000000 America/Toronto', $factory->today()->format('Y-m-d H:i:s.u e'));
        $this->assertSame('2020-09-04 00:00:00.000000 America/Toronto', $factory->tomorrow()->format('Y-m-d H:i:s.u e'));
        $this->assertSame('2020-09-04 09:39:04.123456 America/Toronto', $factory->parse('2020-09-04 09:39:04.123456')->format('Y-m-d H:i:s.u e'));

        $factory = new Factory([
            'timezone' => 'Asia/Shanghai',
        ]);

        $baseDate = Carbon::parse('2021-08-01 08:00:00', 'UTC');

        $date = $factory->createFromTimestamp($baseDate->getTimestamp());
        $this->assertSame('2021-08-01T16:00:00+08:00', $date->format('c'));

        $date = $factory->make('2021-08-01 08:00:00');
        $this->assertSame('2021-08-01T08:00:00+08:00', $date->format('c'));

        $date = $factory->make($baseDate);
        $this->assertSame('2021-08-01T16:00:00+08:00', $date->format('c'));

        $date = $factory->create($baseDate);
        $this->assertSame('2021-08-01T16:00:00+08:00', $date->format('c'));

        $date = $factory->parse($baseDate);
        $this->assertSame('2021-08-01T16:00:00+08:00', $date->format('c'));

        $date = $factory->instance($baseDate);
        $this->assertSame('2021-08-01T16:00:00+08:00', $date->format('c'));

        $date = $factory->make('2021-08-01 08:00:00+00:20');
        $this->assertSame('2021-08-01T08:00:00+00:20', $date->format('c'));

        $date = $factory->parse('2021-08-01T08:00:00Z');
        $this->assertSame('2021-08-01T08:00:00+00:00', $date->format('c'));

        $date = $factory->create('2021-08-01 08:00:00 UTC');
        $this->assertSame('2021-08-01T08:00:00+00:00', $date->format('c'));

        $date = $factory->make('2021-08-01 08:00:00 Europe/Paris');
        $this->assertSame('2021-08-01T08:00:00+02:00', $date->format('c'));
    }
}