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
|
<?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 Carbon\CarbonPeriod;
use RuntimeException;
use Tests\AbstractTestCase;
class FloatSettersEnabledTest extends AbstractTestCase
{
protected function setUp(): void
{
parent::setUp();
CarbonInterval::enableFloatSetters();
}
protected function tearDown(): void
{
CarbonInterval::enableFloatSetters(false);
parent::tearDown();
}
public function testInheritedConstruct()
{
$ci = new CarbonInterval('PT0S');
$ci->hours(0.5);
$this->assertSame('PT30M', $ci->spec());
$ci = new CarbonInterval('P1D');
$ci->hours(0.5);
$this->assertSame('P1DT30M', $ci->spec());
$ci = new CarbonInterval('PT4H');
$ci->hours(0.5);
$this->assertSame('PT30M', $ci->spec());
$period = CarbonPeriod::since('2018-04-21 00:00:00')->hours(0.5)->until('2018-04-21 02:00:00');
$this->assertSame('2018-04-21 00:30:00', $period->toArray()[1]->format('Y-m-d H:i:s'));
CarbonInterval::enableFloatSetters(false);
$ci = new CarbonInterval('PT4H');
$ci->hours(0.5);
$this->assertSame('PT0S', $ci->spec());
}
public function testOverridePrevention()
{
$this->expectExceptionObject(new RuntimeException(
'You cannot set hour to a float value as minute would be overridden, '.
'set it first to 0 explicitly if you really want to erase its value'
));
$ci = new CarbonInterval('PT10M');
$ci->hours(0.5);
}
}
|