File: RoundingTest.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 (86 lines) | stat: -rw-r--r-- 4,552 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
<?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 InvalidArgumentException;
use Tests\AbstractTestCase;

class RoundingTest extends AbstractTestCase
{
    public function testThrowsExceptionForCompositeInterval()
    {
        $this->expectExceptionObject(new InvalidArgumentException(
            'Rounding is only possible with single unit intervals.'
        ));

        CarbonInterval::days(2)->round('2 hours 50 minutes');
    }

    public function testFloor()
    {
        $this->assertSame(21, CarbonInterval::days(21)->floorWeeks()->totalDays);
        $this->assertSame(21, CarbonInterval::days(24)->floorWeeks()->totalDays);
        $this->assertSame(21, CarbonInterval::days(25)->floorWeeks()->totalDays);
        $this->assertSame(21, CarbonInterval::days(27)->floorWeeks()->totalDays);
        $this->assertSame(28, CarbonInterval::days(28)->floorWeeks()->totalDays);

        $this->assertSame(1000, CarbonInterval::milliseconds(1234)->floor()->totalMilliseconds);
        $this->assertSame(1000, CarbonInterval::milliseconds(1834)->floor()->totalMilliseconds);
        $this->assertSame(20, CarbonInterval::days(21)->floor('2 days')->totalDays);
        $this->assertSame(18, CarbonInterval::days(21)->floor(CarbonInterval::days(6))->totalDays);
        $this->assertSame(18, CarbonInterval::days(22)->floorUnit('day', 6)->totalDays);
    }

    public function testRound()
    {
        $this->assertSame(21, CarbonInterval::days(21)->roundWeeks()->totalDays);
        $this->assertSame(21, CarbonInterval::days(24)->roundWeeks()->totalDays);
        $this->assertSame(28, CarbonInterval::days(25)->roundWeeks()->totalDays);
        $this->assertSame(28, CarbonInterval::days(27)->roundWeeks()->totalDays);
        $this->assertSame(28, CarbonInterval::days(28)->roundWeeks()->totalDays);
        $this->assertSame(-7, CarbonInterval::make('7 days 23 hours 34 minutes')->invert()->roundWeeks()->totalDays);
        $this->assertSame(-7, CarbonInterval::make('-7 days 23 hours 34 minutes')->roundWeeks()->totalDays);
        $this->assertSame(7, CarbonInterval::make('-7 days 23 hours 34 minutes')->invert()->roundWeeks()->totalDays);

        $this->assertSame(1000, CarbonInterval::milliseconds(1234)->round()->totalMilliseconds);
        $this->assertSame(2000, CarbonInterval::milliseconds(1834)->round()->totalMilliseconds);
        $this->assertSame(20, CarbonInterval::days(20)->round('2 days')->totalDays);
        $this->assertSame(18, CarbonInterval::days(20)->round(CarbonInterval::days(6))->totalDays);
        $this->assertSame(22, CarbonInterval::days(21)->round('2 days')->totalDays);
        $this->assertSame(24, CarbonInterval::days(21)->round(CarbonInterval::days(6))->totalDays);
        $this->assertSame(22, CarbonInterval::days(22)->round('2 days')->totalDays);
        $this->assertSame(24, CarbonInterval::days(22)->round(CarbonInterval::days(6))->totalDays);
        $this->assertSame(24, CarbonInterval::days(22)->roundUnit('day', 6)->totalDays);
    }

    public function testCeil()
    {
        $this->assertSame(21, CarbonInterval::days(21)->ceilWeeks()->totalDays);
        $this->assertSame(28, CarbonInterval::days(24)->ceilWeeks()->totalDays);
        $this->assertSame(28, CarbonInterval::days(25)->ceilWeeks()->totalDays);
        $this->assertSame(28, CarbonInterval::days(27)->ceilWeeks()->totalDays);
        $this->assertSame(28, CarbonInterval::days(28)->ceilWeeks()->totalDays);

        $this->assertSame(2000, CarbonInterval::milliseconds(1234)->ceil()->totalMilliseconds);
        $this->assertSame(2000, CarbonInterval::milliseconds(1834)->ceil()->totalMilliseconds);
        $this->assertSame(20, CarbonInterval::days(20)->ceil('2 days')->totalDays);
        $this->assertSame(24, CarbonInterval::days(20)->ceil(CarbonInterval::days(6))->totalDays);
        $this->assertSame(22, CarbonInterval::days(21)->ceil('2 days')->totalDays);
        $this->assertSame(24, CarbonInterval::days(21)->ceil(CarbonInterval::days(6))->totalDays);
        $this->assertSame(22, CarbonInterval::days(22)->ceil('2 days')->totalDays);
        $this->assertSame(24, CarbonInterval::days(22)->ceil(CarbonInterval::days(6))->totalDays);
        $this->assertSame(24, CarbonInterval::days(22)->ceilUnit('day', 6)->totalDays);
    }
}