CreateFromFormatTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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\Carbon;
  12. use Carbon\Carbon;
  13. use DateTime;
  14. use DateTimeZone;
  15. use Tests\AbstractTestCase;
  16. use Tests\Carbon\Fixtures\MyCarbon;
  17. class CreateFromFormatTest extends AbstractTestCase
  18. {
  19. /**
  20. * @var array
  21. */
  22. protected $lastErrors;
  23. /**
  24. * @var array
  25. */
  26. protected $noErrors;
  27. protected function setUp(): void
  28. {
  29. parent::setUp();
  30. $this->noErrors = [
  31. 'warning_count' => 0,
  32. 'warnings' => [],
  33. 'error_count' => 0,
  34. 'errors' => [],
  35. ];
  36. $this->lastErrors = [
  37. 'warning_count' => 1,
  38. 'warnings' => ['10' => 'The parsed date was invalid'],
  39. 'error_count' => 0,
  40. 'errors' => [],
  41. ];
  42. }
  43. public function testCreateFromFormatReturnsCarbon()
  44. {
  45. $d = Carbon::createFromFormat('Y-m-d H:i:s', '1975-05-21 22:32:11');
  46. $this->assertCarbon($d, 1975, 5, 21, 22, 32, 11);
  47. $this->assertInstanceOfCarbon($d);
  48. }
  49. public function testCreateFromFalseTimezone()
  50. {
  51. $d = Carbon::createFromFormat('Y-m-d H:i:s.u', '1975-05-21 22:32:11.123456', false);
  52. $this->assertInstanceOfCarbon($d);
  53. $this->assertSame('1975-05-21 22:32:11.123456 America/Toronto', $d->format('Y-m-d H:i:s.u e'));
  54. }
  55. /**
  56. * Work-around for https://bugs.php.net/bug.php?id=80141
  57. */
  58. public function testCFormat()
  59. {
  60. $d = Carbon::createFromFormat('c', Carbon::parse('2020-02-02')->format('c'));
  61. $this->assertCarbon($d, 2020, 2, 2, 0, 0, 0, 0);
  62. $d = Carbon::createFromFormat('c', '2020-02-02T23:45:12+09:00');
  63. $this->assertSame('+09:00', $d->tzName);
  64. $this->assertCarbon($d, 2020, 2, 2, 23, 45, 12, 0);
  65. $d = Carbon::createFromFormat('l c', 'Sunday 2020-02-02T23:45:12+09:00');
  66. $this->assertSame('+09:00', $d->tzName);
  67. $this->assertCarbon($d, 2020, 2, 2, 23, 45, 12, 0);
  68. $d = Carbon::createFromFormat('l \\\\c', 'Sunday \\2020-02-02T23:45:12+09:00');
  69. $this->assertSame('+09:00', $d->tzName);
  70. $this->assertCarbon($d, 2020, 2, 2, 23, 45, 12, 0);
  71. $d = Carbon::createFromFormat('Y-m-d\\cH:i:s', '2020-02-02c23:45:12');
  72. $this->assertCarbon($d, 2020, 2, 2, 23, 45, 12, 0);
  73. }
  74. public function testCreateFromFormatWithMillisecondsAlone()
  75. {
  76. $d = Carbon::createFromFormat('Y-m-d H:i:s.v', '1975-05-21 22:32:11.321');
  77. $this->assertCarbon($d, 1975, 5, 21, 22, 32, 11, 321000);
  78. $this->assertInstanceOfCarbon($d);
  79. }
  80. /**
  81. * @requires PHP >= 7.3
  82. *
  83. * Due to https://bugs.php.net/bug.php?id=75577, proper "v" format support can only works from PHP 7.3.0.
  84. */
  85. public function testCreateFromFormatWithMillisecondsMerged()
  86. {
  87. $d = Carbon::createFromFormat('Y-m-d H:s.vi', '1975-05-21 22:11.32132');
  88. $this->assertCarbon($d, 1975, 5, 21, 22, 32, 11, 321000);
  89. $this->assertInstanceOfCarbon($d);
  90. }
  91. public function testCreateFromFormatWithTimezoneString()
  92. {
  93. $d = Carbon::createFromFormat('Y-m-d H:i:s', '1975-05-21 22:32:11', 'Europe/London');
  94. $this->assertCarbon($d, 1975, 5, 21, 22, 32, 11);
  95. $this->assertSame('Europe/London', $d->tzName);
  96. }
  97. public function testCreateFromFormatWithTimezone()
  98. {
  99. $d = Carbon::createFromFormat('Y-m-d H:i:s', '1975-05-21 22:32:11', new DateTimeZone('Europe/London'));
  100. $this->assertCarbon($d, 1975, 5, 21, 22, 32, 11);
  101. $this->assertSame('Europe/London', $d->tzName);
  102. }
  103. public function testCreateFromFormatWithMillis()
  104. {
  105. $d = Carbon::createFromFormat('Y-m-d H:i:s.u', '1975-05-21 22:32:11.254687');
  106. $this->assertSame(254687, $d->micro);
  107. }
  108. public function testCreateFromFormatWithTestNow()
  109. {
  110. Carbon::setTestNow();
  111. $nativeDate = Carbon::createFromFormat('Y-m-d H:i:s', '1975-05-21 22:32:11');
  112. Carbon::setTestNow(Carbon::now());
  113. $mockedDate = Carbon::createFromFormat('Y-m-d H:i:s', '1975-05-21 22:32:11');
  114. $this->assertSame($mockedDate->micro === 0, $nativeDate->micro === 0);
  115. }
  116. public function testCreateLastErrorsCanBeAccessedByExtendingClass()
  117. {
  118. $this->assertSame([
  119. 'warning_count' => 0,
  120. 'warnings' => [],
  121. 'error_count' => 0,
  122. 'errors' => [],
  123. ], MyCarbon::getLastErrors());
  124. }
  125. public function testCreateFromFormatHandlesLastErrors()
  126. {
  127. $carbon = Carbon::createFromFormat('d/m/Y', '41/02/1900');
  128. $datetime = DateTime::createFromFormat('d/m/Y', '41/02/1900');
  129. $this->assertSame($this->lastErrors, $carbon->getLastErrors());
  130. $this->assertSame($carbon->getLastErrors(), $datetime->getLastErrors());
  131. }
  132. public function testCreateFromFormatResetLastErrors()
  133. {
  134. $carbon = Carbon::createFromFormat('d/m/Y', '41/02/1900');
  135. $this->assertSame($this->lastErrors, $carbon->getLastErrors());
  136. $carbon = Carbon::createFromFormat('d/m/Y', '11/03/2016');
  137. $this->assertSame($this->noErrors, $carbon->getLastErrors());
  138. }
  139. }