CreateSafeTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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\CarbonImmutable;
  12. use Carbon\CarbonImmutable as Carbon;
  13. use Carbon\Exceptions\InvalidDateException;
  14. use Tests\AbstractTestCase;
  15. class CreateSafeTest extends AbstractTestCase
  16. {
  17. public function testInvalidDateExceptionProperties()
  18. {
  19. $e = new InvalidDateException('day', 'foo');
  20. $this->assertSame('day', $e->getField());
  21. $this->assertSame('foo', $e->getValue());
  22. }
  23. public function testCreateSafeThrowsExceptionForSecondLowerThanZero()
  24. {
  25. $this->expectExceptionObject(new InvalidDateException('second', -1));
  26. Carbon::createSafe(null, null, null, null, null, -1);
  27. }
  28. public function testCreateSafeThrowsExceptionForSecondGreaterThan59()
  29. {
  30. $this->expectExceptionObject(new InvalidDateException('second', 60));
  31. Carbon::createSafe(null, null, null, null, null, 60);
  32. }
  33. public function testCreateSafeThrowsExceptionForMinuteLowerThanZero()
  34. {
  35. $this->expectExceptionObject(new InvalidDateException('minute', -1));
  36. Carbon::createSafe(null, null, null, null, -1);
  37. }
  38. public function testCreateSafeThrowsExceptionForMinuteGreaterThan59()
  39. {
  40. $this->expectExceptionObject(new InvalidDateException('minute', 60));
  41. Carbon::createSafe(null, null, null, null, 60, 25);
  42. }
  43. public function testCreateSafeThrowsExceptionForHourLowerThanZero()
  44. {
  45. $this->expectExceptionObject(new InvalidDateException('hour', -6));
  46. Carbon::createSafe(null, null, null, -6);
  47. }
  48. public function testCreateSafeThrowsExceptionForHourGreaterThan24()
  49. {
  50. $this->expectExceptionObject(new InvalidDateException('hour', 25));
  51. Carbon::createSafe(null, null, null, 25, 16, 15);
  52. }
  53. public function testCreateSafeThrowsExceptionForDayLowerThanZero()
  54. {
  55. $this->expectExceptionObject(new InvalidDateException('day', -5));
  56. Carbon::createSafe(null, null, -5);
  57. }
  58. public function testCreateSafeThrowsExceptionForDayGreaterThan31()
  59. {
  60. $this->expectExceptionObject(new InvalidDateException('day', 32));
  61. Carbon::createSafe(null, null, 32, 17, 16, 15);
  62. }
  63. public function testCreateSafeThrowsExceptionForMonthLowerThanZero()
  64. {
  65. $this->expectExceptionObject(new InvalidDateException('month', -4));
  66. Carbon::createSafe(null, -4);
  67. }
  68. public function testCreateSafeThrowsExceptionForMonthGreaterThan12()
  69. {
  70. $this->expectExceptionObject(new InvalidDateException('month', 13));
  71. Carbon::createSafe(null, 13, 5, 17, 16, 15);
  72. }
  73. public function testCreateSafeThrowsExceptionForYearLowerThanZero()
  74. {
  75. $this->expectExceptionObject(new InvalidDateException('year', -5));
  76. Carbon::createSafe(-5);
  77. }
  78. public function testCreateSafeThrowsExceptionForYearGreaterThan12()
  79. {
  80. $this->expectExceptionObject(new InvalidDateException('year', 10000));
  81. Carbon::createSafe(10000, 12, 5, 17, 16, 15);
  82. }
  83. public function testCreateSafeThrowsExceptionForInvalidDayInShortMonth()
  84. {
  85. $this->expectExceptionObject(new InvalidDateException('day', 31));
  86. // 30 days in April
  87. Carbon::createSafe(2016, 4, 31, 17, 16, 15);
  88. }
  89. public function testCreateSafeThrowsExceptionForInvalidDayForFebruaryInLeapYear()
  90. {
  91. $this->expectExceptionObject(new InvalidDateException('day', 30));
  92. // 29 days in February for a leap year
  93. $this->assertTrue(Carbon::create(2016, 2)->isLeapYear());
  94. Carbon::createSafe(2016, 2, 30, 17, 16, 15);
  95. }
  96. public function testCreateSafePassesForFebruaryInLeapYear()
  97. {
  98. // 29 days in February for a leap year
  99. $this->assertSame(29, Carbon::createSafe(2016, 2, 29, 17, 16, 15)->day);
  100. }
  101. public function testCreateSafeThrowsExceptionForInvalidDayForFebruaryInNonLeapYear()
  102. {
  103. $this->expectExceptionObject(new InvalidDateException('day', 29));
  104. // 28 days in February for a non-leap year
  105. $this->assertFalse(Carbon::create(2015, 2)->isLeapYear());
  106. Carbon::createSafe(2015, 2, 29, 17, 16, 15);
  107. }
  108. public function testCreateSafePassesForInvalidDSTTime()
  109. {
  110. $message = '';
  111. $date = null;
  112. try {
  113. // 1h jumped to 2h because of the DST, so 1h30 is not a safe date in PHP 5.4+
  114. Carbon::createSafe(2014, 3, 30, 1, 30, 0, 'Europe/London');
  115. } catch (InvalidDateException $exception) {
  116. $message = $exception->getMessage();
  117. }
  118. $this->assertStringContainsString('hour : 1 is not a valid value.', $message);
  119. }
  120. public function testCreateSafePassesForValidDSTTime()
  121. {
  122. $this->assertSame(0, Carbon::createSafe(2014, 3, 30, 0, 30, 0, 'Europe/London')->hour);
  123. $this->assertSame(2, Carbon::createSafe(2014, 3, 30, 2, 30, 0, 'Europe/London')->hour);
  124. $this->assertSame(1, Carbon::createSafe(2014, 3, 30, 1, 30, 0, 'UTC')->hour);
  125. }
  126. public function testCreateSafeThrowsExceptionForWithNonIntegerValue()
  127. {
  128. $this->expectExceptionObject(new InvalidDateException('second', 15.1));
  129. Carbon::createSafe(2015, 2, 10, 17, 16, 15.1);
  130. }
  131. public function testCreateSafePassesForFebruaryInNonLeapYear()
  132. {
  133. // 28 days in February for a non-leap year
  134. $this->assertSame(28, Carbon::createSafe(2015, 2, 28, 17, 16, 15)->day);
  135. }
  136. public function testCreateSafePasses()
  137. {
  138. $sd = Carbon::createSafe(2015, 2, 15, 17, 16, 15);
  139. $d = Carbon::create(2015, 2, 15, 17, 16, 15);
  140. $this->assertEquals($d, $sd);
  141. $this->assertCarbon($sd, 2015, 2, 15, 17, 16, 15);
  142. }
  143. }