ToDateIntervalTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 DateInterval;
  14. use DateTime;
  15. use InvalidArgumentException;
  16. use Tests\AbstractTestCase;
  17. class ToDateIntervalTest extends AbstractTestCase
  18. {
  19. public function testConvertToDateInterval()
  20. {
  21. $interval = CarbonInterval::days(5)->hours(3)->minutes(50)->microseconds(123456)->invert()->toDateInterval();
  22. $this->assertSame(DateInterval::class, \get_class($interval));
  23. $this->assertSame(1, $interval->invert);
  24. $this->assertSame(5, $interval->d);
  25. $this->assertSame(3, $interval->h);
  26. $this->assertSame(50, $interval->i);
  27. $this->assertSame(0, $interval->s);
  28. $this->assertSame(0.123456, $interval->f);
  29. }
  30. public function testCastToDateInterval()
  31. {
  32. $interval = CarbonInterval::days(5)->hours(3)->minutes(50)->microseconds(123456)->invert()->cast(DateInterval::class);
  33. $this->assertSame(DateInterval::class, \get_class($interval));
  34. $this->assertSame(1, $interval->invert);
  35. $this->assertSame(5, $interval->d);
  36. $this->assertSame(3, $interval->h);
  37. $this->assertSame(50, $interval->i);
  38. $this->assertSame(0, $interval->s);
  39. $this->assertSame(0.123456, $interval->f);
  40. }
  41. public function testBadCast()
  42. {
  43. $this->expectExceptionObject(new InvalidArgumentException(
  44. 'DateTime is not a sub-class of DateInterval.'
  45. ));
  46. CarbonInterval::days(5)->hours(3)->minutes(50)->microseconds(123456)->invert()->cast(DateTime::class);
  47. }
  48. }