FloatSettersEnabledTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of the Carbon package.
  5. *
  6. * (c) Brian Nesbitt <brian@nesbot.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Tests\CarbonInterval;
  12. use Carbon\CarbonInterval;
  13. use Carbon\CarbonPeriod;
  14. use RuntimeException;
  15. use Tests\AbstractTestCase;
  16. class FloatSettersEnabledTest extends AbstractTestCase
  17. {
  18. protected function setUp(): void
  19. {
  20. parent::setUp();
  21. CarbonInterval::enableFloatSetters();
  22. }
  23. protected function tearDown(): void
  24. {
  25. CarbonInterval::enableFloatSetters(false);
  26. parent::tearDown();
  27. }
  28. public function testInheritedConstruct()
  29. {
  30. $ci = new CarbonInterval('PT0S');
  31. $ci->hours(0.5);
  32. $this->assertSame('PT30M', $ci->spec());
  33. $ci = new CarbonInterval('P1D');
  34. $ci->hours(0.5);
  35. $this->assertSame('P1DT30M', $ci->spec());
  36. $ci = new CarbonInterval('PT4H');
  37. $ci->hours(0.5);
  38. $this->assertSame('PT30M', $ci->spec());
  39. $period = CarbonPeriod::since('2018-04-21 00:00:00')->hours(0.5)->until('2018-04-21 02:00:00');
  40. $this->assertSame('2018-04-21 00:30:00', $period->toArray()[1]->format('Y-m-d H:i:s'));
  41. CarbonInterval::enableFloatSetters(false);
  42. $ci = new CarbonInterval('PT4H');
  43. $ci->hours(0.5);
  44. $this->assertSame('PT0S', $ci->spec());
  45. }
  46. public function testOverridePrevention()
  47. {
  48. $this->expectExceptionObject(new RuntimeException(
  49. 'You cannot set hour to a float value as minute would be overridden, '.
  50. 'set it first to 0 explicitly if you really want to erase its value'
  51. ));
  52. $ci = new CarbonInterval('PT10M');
  53. $ci->hours(0.5);
  54. }
  55. }