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
|
<?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\CarbonImmutable;
use Carbon\CarbonImmutable as Carbon;
use InvalidArgumentException;
use Tests\AbstractTestCase;
class RoundTest extends AbstractTestCase
{
public function testRoundWithDefaultUnit()
{
$dt = Carbon::create(2315, 7, 18, 22, 42, 17.643971);
$copy = $dt->copy();
$ref = $copy->round();
$this->assertNotEquals($ref, $copy);
$this->assertNotSame($ref, $copy);
$this->assertCarbon($ref, 2315, 7, 18, 22, 42, 18, 0);
$this->assertCarbon($dt->copy()->round(5), 2315, 7, 18, 22, 42, 20, 0);
$this->assertCarbon($dt->copy()->floor()->round(5), 2315, 7, 18, 22, 42, 15, 0);
$this->assertCarbon($dt->copy()->round(3), 2315, 7, 18, 22, 42, 18, 0);
$this->assertCarbon($dt->copy()->round(4), 2315, 7, 18, 22, 42, 16, 0);
$this->assertCarbon($dt->copy()->round(10), 2315, 7, 18, 22, 42, 20, 0);
$this->assertCarbon($dt->copy()->round(0.5), 2315, 7, 18, 22, 42, 17, 500000);
$this->assertCarbon($dt->copy()->round(0.25), 2315, 7, 18, 22, 42, 17, 750000);
$this->assertCarbon($dt->copy()->round(3.8), 2315, 7, 18, 22, 42, 19, 800000);
$this->assertCarbon($dt->copy()->floor(5), 2315, 7, 18, 22, 42, 15, 0);
$this->assertCarbon($dt->copy()->floor()->floor(5), 2315, 7, 18, 22, 42, 15, 0);
$this->assertCarbon($dt->copy()->floor(3), 2315, 7, 18, 22, 42, 15, 0);
$this->assertCarbon($dt->copy()->floor(4), 2315, 7, 18, 22, 42, 16, 0);
$this->assertCarbon($dt->copy()->floor(10), 2315, 7, 18, 22, 42, 10, 0);
$this->assertCarbon($dt->copy()->floor(0.5), 2315, 7, 18, 22, 42, 17, 500000);
$this->assertCarbon($dt->copy()->floor(0.25), 2315, 7, 18, 22, 42, 17, 500000);
$this->assertCarbon($dt->copy()->floor(3.8), 2315, 7, 18, 22, 42, 15, 0);
$this->assertCarbon($dt->copy()->ceil(5), 2315, 7, 18, 22, 42, 20, 0);
$this->assertCarbon($dt->copy()->floor()->ceil(5), 2315, 7, 18, 22, 42, 20, 0);
$this->assertCarbon($dt->copy()->ceil(3), 2315, 7, 18, 22, 42, 18, 0);
$this->assertCarbon($dt->copy()->ceil(4), 2315, 7, 18, 22, 42, 20, 0);
$this->assertCarbon($dt->copy()->ceil(10), 2315, 7, 18, 22, 42, 20, 0);
$this->assertCarbon($dt->copy()->ceil(0.5), 2315, 7, 18, 22, 42, 18, 0);
$this->assertCarbon($dt->copy()->ceil(0.25), 2315, 7, 18, 22, 42, 17, 750000);
$this->assertCarbon($dt->copy()->ceil(3.8), 2315, 7, 18, 22, 42, 19, 800000);
}
public function testRoundWithBaseUnit()
{
$dt = Carbon::create(2315, 7, 18, 22, 42, 17.643971);
$copy = $dt->copy();
$ref = $copy->roundSecond();
$this->assertNotEquals($ref, $copy);
$this->assertNotSame($ref, $copy);
$this->assertCarbon($ref, 2315, 7, 18, 22, 42, 18, 0);
$this->assertCarbon($dt->copy()->roundDay(), 2315, 7, 19, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->roundDay(5), 2315, 7, 21, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->ceilDay(), 2315, 7, 19, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->floorDay(), 2315, 7, 18, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->roundYear(), 2316, 1, 1, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->subMonths(2)->roundYear(), 2315, 1, 1, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->roundYear(2), 2315, 1, 1, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->floorYear(2), 2315, 1, 1, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->ceilYear(2), 2317, 1, 1, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->roundMonth(), 2315, 8, 1, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->floorMonth(), 2315, 7, 1, 0, 0, 0, 0);
}
public function testRoundWithMetaUnit()
{
$dt = Carbon::create(2315, 7, 18, 22, 42, 17.643971);
$copy = $dt->copy();
$ref = $copy->roundSecond();
$this->assertNotEquals($ref, $copy);
$this->assertNotSame($ref, $copy);
$this->assertCarbon($ref, 2315, 7, 18, 22, 42, 18, 0);
$this->assertCarbon($dt->copy()->roundMillisecond(), 2315, 7, 18, 22, 42, 17, 644000);
$this->assertCarbon($dt->copy()->roundMillennium(), 2001, 1, 1, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->roundQuarter(), 2315, 7, 1, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->roundQuarters(2), 2315, 7, 1, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->subMonth()->floorQuarter(), 2315, 4, 1, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->subMonth()->floorQuarters(2), 2315, 1, 1, 0, 0, 0, 0);
}
public function testRoundWeek()
{
$dt = Carbon::create(2315, 7, 18, 22, 42, 17.643971);
$copy = $dt->copy();
$ref = $copy->roundSecond();
$this->assertNotEquals($ref, $copy);
$this->assertNotSame($ref, $copy);
$this->assertCarbon($ref, 2315, 7, 18, 22, 42, 18, 0);
$this->assertCarbon($dt->copy()->floorWeek(), 2315, 7, 12, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->ceilWeek(), 2315, 7, 19, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->roundWeek(), 2315, 7, 19, 0, 0, 0, 0);
$dt = Carbon::create(2315, 7, 19, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->floorWeek(), 2315, 7, 19, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->ceilWeek(), 2315, 7, 19, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->roundWeek(), 2315, 7, 19, 0, 0, 0, 0);
Carbon::setWeekStartsAt(Carbon::SUNDAY);
Carbon::setWeekEndsAt(Carbon::SATURDAY);
$this->assertCarbon($dt->copy()->floorWeek(), 2315, 7, 18, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->ceilWeek(), 2315, 7, 25, 0, 0, 0, 0);
$this->assertCarbon($dt->copy()->roundWeek(), 2315, 7, 18, 0, 0, 0, 0);
Carbon::setWeekStartsAt(Carbon::MONDAY);
Carbon::setWeekEndsAt(Carbon::SUNDAY);
}
public function testCeilMonth()
{
$this->assertCarbon(Carbon::parse('2021-01-29')->ceilMonth(), 2021, 2, 1, 0, 0, 0);
$this->assertCarbon(Carbon::parse('2021-01-31')->ceilMonth(), 2021, 2, 1, 0, 0, 0);
$this->assertCarbon(Carbon::parse('2021-12-17')->ceilMonth(), 2022, 1, 1, 0, 0, 0);
}
public function testRoundInvalidArgument()
{
$this->expectExceptionObject(new InvalidArgumentException(
'Unknown unit \'foobar\'.'
));
Carbon::now()->roundUnit('foobar');
}
}
|