File: MonthTest.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 (62 lines) | stat: -rw-r--r-- 2,057 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
<?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\Unit;

use Carbon\CarbonImmutable;
use Carbon\Exceptions\InvalidFormatException;
use Carbon\Month;
use Tests\AbstractTestCase;

class MonthTest extends AbstractTestCase
{
    public function testFromName(): void
    {
        $this->assertSame(Month::January, Month::fromName('jan'));
        $this->assertSame(Month::February, Month::fromName('FEBRUARY'));
        $this->assertSame(Month::February, Month::fromName('févr', 'fr'));
        $this->assertSame(Month::March, Month::fromName('Mars', 'fr'));
    }

    public function testFromNumber(): void
    {
        $this->assertSame(Month::May, Month::fromNumber(5));
        $this->assertSame(Month::October, Month::fromNumber(-2));
        $this->assertSame(Month::September, Month::fromNumber(9));
        $this->assertSame(Month::November, Month::fromNumber(-1));
        $this->assertSame(Month::July, Month::fromNumber(7));
        $this->assertSame(Month::December, Month::fromNumber(0));
        $this->assertSame(Month::December, Month::fromNumber(12));
        $this->assertSame(Month::January, Month::fromNumber(13));
    }

    public function testOfTheYear(): void
    {
        $date = Month::October->ofTheYear(2020);
        $this->assertInstanceOf(CarbonImmutable::class, $date);
        $this->assertSame('2020-10-01 00:00:00.000000 America/Toronto', $date->format('Y-m-d H:i:s.u e'));
    }

    public function testLocale(): void
    {
        $this->assertSame('ottobre', Month::October->locale('it')->monthName);
        $this->assertSame('diciembre', Month::January->locale('es')->subMonth()->monthName);
    }

    public function testFromNameFailure(): void
    {
        $this->expectExceptionObject(new InvalidFormatException("Could not parse 'pr 1'"));

        Month::fromName('pr', 'fr');
    }
}