LastErrorTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 Carbon\Traits\Creator;
  14. use DateTime;
  15. use Tests\AbstractTestCase;
  16. class LastErrorTest extends AbstractTestCase
  17. {
  18. /**
  19. * @var array
  20. */
  21. protected $lastErrors;
  22. /**
  23. * @var array
  24. */
  25. protected $noErrors;
  26. protected function setUp(): void
  27. {
  28. parent::setUp();
  29. $this->noErrors = [
  30. 'warning_count' => 0,
  31. 'warnings' => [],
  32. 'error_count' => 0,
  33. 'errors' => [],
  34. ];
  35. $this->lastErrors = [
  36. 'warning_count' => 1,
  37. 'warnings' => ['11' => 'The parsed date was invalid'],
  38. 'error_count' => 0,
  39. 'errors' => [],
  40. ];
  41. }
  42. public function testCreateHandlesLastErrors()
  43. {
  44. $carbon = new Carbon('2017-02-30');
  45. $datetime = new DateTime('2017-02-30');
  46. $this->assertSame($this->lastErrors, $carbon->getLastErrors());
  47. $this->assertSame($carbon->getLastErrors(), $datetime->getLastErrors());
  48. $carbon = new Carbon('2017-02-15');
  49. $this->assertSame($this->noErrors, $carbon->getLastErrors());
  50. }
  51. public function testLastErrorsInitialization()
  52. {
  53. $obj = new class() {
  54. use Creator;
  55. /** @phpstan-ignore-next-line */
  56. public function __construct($time = null, $tz = null)
  57. {
  58. }
  59. public function triggerError()
  60. {
  61. self::setLastErrors(false);
  62. }
  63. };
  64. $this->assertNull($obj::getLastErrors());
  65. $obj->triggerError();
  66. $this->assertSame([
  67. 'warning_count' => 0,
  68. 'warnings' => [],
  69. 'error_count' => 0,
  70. 'errors' => [],
  71. ], $obj::getLastErrors());
  72. }
  73. }