CreateFromFormatTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 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 testCreateFromFormatWithTimezoneString()
  50. {
  51. $d = Carbon::createFromFormat('Y-m-d H:i:s', '1975-05-21 22:32:11', 'Europe/London');
  52. $this->assertCarbon($d, 1975, 5, 21, 22, 32, 11);
  53. $this->assertSame('Europe/London', $d->tzName);
  54. }
  55. public function testCreateFromFormatWithTimezone()
  56. {
  57. $d = Carbon::createFromFormat('Y-m-d H:i:s', '1975-05-21 22:32:11', new DateTimeZone('Europe/London'));
  58. $this->assertCarbon($d, 1975, 5, 21, 22, 32, 11);
  59. $this->assertSame('Europe/London', $d->tzName);
  60. }
  61. public function testCreateFromFormatWithMillis()
  62. {
  63. $d = Carbon::createFromFormat('Y-m-d H:i:s.u', '1975-05-21 22:32:11.254687');
  64. $this->assertSame(254687, $d->micro);
  65. }
  66. public function testCreateFromFormatWithTestNow()
  67. {
  68. Carbon::setTestNow();
  69. $nativeDate = Carbon::createFromFormat('Y-m-d H:i:s', '1975-05-21 22:32:11');
  70. Carbon::setTestNow(Carbon::now());
  71. $mockedDate = Carbon::createFromFormat('Y-m-d H:i:s', '1975-05-21 22:32:11');
  72. $this->assertSame($mockedDate->micro === 0, $nativeDate->micro === 0);
  73. }
  74. public function testCreateLastErrorsCanBeAccessedByExtendingClass()
  75. {
  76. $this->assertSame([
  77. 'warning_count' => 0,
  78. 'warnings' => [],
  79. 'error_count' => 0,
  80. 'errors' => [],
  81. ], MyCarbon::getLastErrors());
  82. }
  83. public function testCreateFromFormatHandlesLastErrors()
  84. {
  85. $carbon = Carbon::createFromFormat('d/m/Y', '41/02/1900');
  86. $datetime = DateTime::createFromFormat('d/m/Y', '41/02/1900');
  87. $this->assertSame($this->lastErrors, $carbon->getLastErrors());
  88. $this->assertSame($carbon->getLastErrors(), $datetime->getLastErrors());
  89. }
  90. public function testCreateFromFormatResetLastErrors()
  91. {
  92. $carbon = Carbon::createFromFormat('d/m/Y', '41/02/1900');
  93. $this->assertSame($this->lastErrors, $carbon->getLastErrors());
  94. $carbon = Carbon::createFromFormat('d/m/Y', '11/03/2016');
  95. $this->assertSame($this->noErrors, $carbon->getLastErrors());
  96. }
  97. public function testCreateFromFormatWithDollar()
  98. {
  99. $d = Carbon::createFromFormat('$c', '$1975-05-21T22:32:11+01:00');
  100. $this->assertCarbon($d, 1975, 5, 21, 22, 32, 11);
  101. $this->assertInstanceOfCarbon($d);
  102. }
  103. }