StrictModeTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 BadMethodCallException;
  13. use Carbon\Carbon;
  14. use InvalidArgumentException;
  15. use Tests\AbstractTestCase;
  16. class StrictModeTest extends AbstractTestCase
  17. {
  18. public function testSafeCreateDateTimeZoneWithStrictMode1()
  19. {
  20. $this->expectExceptionObject(new InvalidArgumentException(
  21. 'Unknown or bad timezone (-15)'
  22. ));
  23. Carbon::createFromDate(2001, 1, 1, -15);
  24. }
  25. public function testSafeCreateDateTimeZoneWithStrictMode2()
  26. {
  27. $this->expectExceptionObject(new InvalidArgumentException(
  28. 'Unknown or bad timezone (foobar)'
  29. ));
  30. Carbon::createFromDate(2001, 1, 1, 'foobar');
  31. }
  32. public function testSafeCreateDateTimeZoneWithoutStrictMode()
  33. {
  34. Carbon::useStrictMode(false);
  35. $this->assertFalse(Carbon::createFromDate(2001, 1, 1, -15));
  36. $this->assertFalse(Carbon::createFromDate(2001, 1, 1, 'foobar'));
  37. }
  38. public function testSetWithStrictMode()
  39. {
  40. $this->expectExceptionObject(new InvalidArgumentException(
  41. 'Unknown setter \'foobar\''
  42. ));
  43. /** @var mixed $date */
  44. $date = Carbon::now();
  45. $date->foobar = 'biz';
  46. }
  47. public function testGetWithStrictMode()
  48. {
  49. $this->expectExceptionObject(new InvalidArgumentException(
  50. 'Unknown getter \'foobar\''
  51. ));
  52. /** @var mixed $date */
  53. $date = Carbon::now();
  54. $date->foobar;
  55. }
  56. public function testSetAndGetWithoutStrictMode()
  57. {
  58. Carbon::useStrictMode(false);
  59. /** @var mixed $date */
  60. $date = Carbon::now();
  61. @$date->foobar = 'biz';
  62. $this->assertSame('biz', $date->foobar);
  63. }
  64. public function testIsSameUnitWithStrictMode()
  65. {
  66. $this->expectExceptionObject(new InvalidArgumentException(
  67. 'Bad comparison unit: \'foobar\''
  68. ));
  69. Carbon::now()->isSameUnit('foobar');
  70. }
  71. public function testIsSameUnitWithoutStrictMode()
  72. {
  73. Carbon::useStrictMode(false);
  74. $this->assertFalse(Carbon::now()->isSameUnit('foobar'));
  75. }
  76. public function testAddRealUnitWithStrictMode()
  77. {
  78. $this->expectExceptionObject(new InvalidArgumentException(
  79. 'Invalid unit for real timestamp add/sub: \'foobar\''
  80. ));
  81. Carbon::now()->addRealUnit('foobar');
  82. }
  83. public function testAddRealUnitWithoutStrictMode()
  84. {
  85. Carbon::useStrictMode(false);
  86. $d = Carbon::create(2000, 1, 2, 3, 4, 5)->addRealUnit('foobar');
  87. $this->assertCarbon($d, 2000, 1, 2, 3, 4, 5);
  88. }
  89. public function testCallWithStrictMode()
  90. {
  91. $this->expectExceptionObject(new BadMethodCallException(
  92. 'Method foobar does not exist.'
  93. ));
  94. /** @var mixed $date */
  95. $date = Carbon::now();
  96. $date->foobar();
  97. }
  98. public function testCallWithoutStrictMode()
  99. {
  100. Carbon::useStrictMode(false);
  101. /** @var mixed $date */
  102. $date = Carbon::now();
  103. $this->assertNull($date->foobar());
  104. }
  105. public function testStaticCallWithStrictMode()
  106. {
  107. $this->expectExceptionObject(new BadMethodCallException(
  108. 'Method Carbon\Carbon::foobar does not exist.'
  109. ));
  110. Carbon::foobar();
  111. }
  112. public function testStaticCallWithoutStrictMode()
  113. {
  114. Carbon::useStrictMode(false);
  115. $this->assertNull(Carbon::foobar());
  116. }
  117. }