File: ToStringTest.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 (69 lines) | stat: -rw-r--r-- 2,195 bytes parent folder | download | duplicates (3)
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
<?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\CarbonInterval;

use Carbon\CarbonInterval;
use Tests\AbstractTestCase;

class ToStringTest extends AbstractTestCase
{
    public function testDefault()
    {
        CarbonInterval::setLocale('en');
        $ci = CarbonInterval::create(11, 1, 2, 5, 22, 33, 55);
        $this->assertSame('11 years 1 month 2 weeks 5 days 22 hours 33 minutes 55 seconds:abc', $ci.':abc');
    }

    public function testDefaultWithMicroseconds()
    {
        CarbonInterval::setLocale('en');
        $ci = CarbonInterval::create(11, 1, 2, 5, 22, 33, 55, 12345);
        $this->assertSame('11 years 1 month 2 weeks 5 days 22 hours 33 minutes 55 seconds:abc', $ci.':abc');
    }

    public function testOverrideSimple()
    {
        $ci = CarbonInterval::create(0, 0, 0, 0, 22, 33, 55);
        $ci->settings(['toStringFormat' => '%H:%I:%S']);
        $this->assertSame('22:33:55:abc', $ci.':abc');
    }

    public function testOverrideWithMicroseconds()
    {
        $ci = CarbonInterval::create(11, 1, 2, 5, 22, 33, 55, 12345);
        $ci->settings(['toStringFormat' => '%R%Y-%M-%D %H:%I:%S.%F']);
        $this->assertSame('+11-01-19 22:33:55.012345:abc', $ci.':abc');
    }

    public function testOverrideWithInvert()
    {
        $ci = CarbonInterval::create(11, 1, 2, 5, 22, 33, 55)->invert();
        $ci->settings(['toStringFormat' => '%R%Y-%M-%D %H:%I:%S']);
        $this->assertSame('-11-01-19 22:33:55:abc', $ci.':abc');
    }

    public function testClosure()
    {
        $ci = CarbonInterval::create(11);
        $this->assertSame('11 years:abc', $ci.':abc');
        CarbonInterval::setToStringFormat('%Y');
        $this->assertSame('11:abc', $ci.':abc');
        $ci->settings(['toStringFormat' => static function (CarbonInterval $interval) {
            return 'Y'.($interval->years * 2);
        }]);
        $this->assertSame('Y22:abc', $ci.':abc');

        CarbonInterval::resetToStringFormat();
    }
}