CreateStrictTest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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\Exceptions\OutOfRangeException;
  14. use Tests\AbstractTestCase;
  15. use TypeError;
  16. class CreateStrictTest extends AbstractTestCase
  17. {
  18. public function testCreateStrictThrowsExceptionForSecondLowerThanZero()
  19. {
  20. $this->expectExceptionObject(new OutOfRangeException('second', 0, 99, -1));
  21. Carbon::createStrict(null, null, null, null, null, -1);
  22. }
  23. public function testCreateStrictThrowsExceptionForMonthOverRange()
  24. {
  25. $this->expectExceptionObject(new OutOfRangeException('month', 0, 99, 9001));
  26. Carbon::createStrict(null, 9001);
  27. }
  28. public function testCreateStrictDoesNotAllowFormatString()
  29. {
  30. $this->expectException(TypeError::class);
  31. Carbon::createStrict('2021-05-25', 'Y-m-d');
  32. }
  33. public function testCreateStrictResetsStrictModeOnSuccess()
  34. {
  35. Carbon::useStrictMode(false);
  36. $this->assertInstanceOfCarbon(Carbon::createStrict());
  37. $this->assertFalse(Carbon::isStrictModeEnabled());
  38. }
  39. public function testCreateStrictResetsStrictModeOnFailure()
  40. {
  41. Carbon::useStrictMode(false);
  42. $exception = null;
  43. try {
  44. Carbon::createStrict(null, -1);
  45. } catch (OutOfRangeException $e) {
  46. $exception = $e;
  47. }
  48. $this->assertInstanceOf(OutOfRangeException::class, $exception);
  49. $this->assertFalse(Carbon::isStrictModeEnabled());
  50. }
  51. }