SpecTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 Tests\AbstractTestCase;
  15. class SpecTest extends AbstractTestCase
  16. {
  17. public function testZeroInterval()
  18. {
  19. $ci = new CarbonInterval(0, 0, 0, 0, 0, 0, 0);
  20. $this->assertSame('PT0S', $ci->spec());
  21. }
  22. public function testYearInterval()
  23. {
  24. $ci = new CarbonInterval();
  25. $this->assertSame('P1Y', $ci->spec());
  26. }
  27. public function testMonthInterval()
  28. {
  29. $ci = new CarbonInterval(0, 1);
  30. $this->assertSame('P1M', $ci->spec());
  31. }
  32. public function testWeekInterval()
  33. {
  34. $ci = new CarbonInterval(0, 0, 1);
  35. $this->assertSame('P7D', $ci->spec());
  36. }
  37. public function testDayInterval()
  38. {
  39. $ci = new CarbonInterval(0, 0, 0, 1);
  40. $this->assertSame('P1D', $ci->spec());
  41. }
  42. public function testMixedDateInterval()
  43. {
  44. $ci = new CarbonInterval(1, 2, 0, 3);
  45. $this->assertSame('P1Y2M3D', $ci->spec());
  46. }
  47. public function testHourInterval()
  48. {
  49. $ci = new CarbonInterval(0, 0, 0, 0, 1);
  50. $this->assertSame('PT1H', $ci->spec());
  51. }
  52. public function testMinuteInterval()
  53. {
  54. $ci = new CarbonInterval(0, 0, 0, 0, 0, 1);
  55. $this->assertSame('PT1M', $ci->spec());
  56. }
  57. public function testSecondInterval()
  58. {
  59. $ci = new CarbonInterval(0, 0, 0, 0, 0, 0, 1);
  60. $this->assertSame('PT1S', $ci->spec());
  61. }
  62. public function testMicrosecondsInterval()
  63. {
  64. $ci = new CarbonInterval(0, 0, 0, 0, 0, 0, 0, 12300);
  65. $this->assertSame('PT0.012300S', $ci->spec(true));
  66. }
  67. public function testMixedTimeInterval()
  68. {
  69. $ci = new CarbonInterval(0, 0, 0, 0, 1, 2, 3);
  70. $this->assertSame('PT1H2M3S', $ci->spec());
  71. }
  72. public function testMixedDateAndTimeInterval()
  73. {
  74. $ci = new CarbonInterval(1, 2, 0, 3, 4, 5, 6);
  75. $this->assertSame('P1Y2M3DT4H5M6S', $ci->spec());
  76. }
  77. public function testCreatingInstanceEquals()
  78. {
  79. $ci = new CarbonInterval(1, 2, 0, 3, 4, 5, 6);
  80. $this->assertEquals($ci, CarbonInterval::instance(new DateInterval($ci->spec())));
  81. }
  82. }