SupportCarbonTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace Illuminate\Tests\Support;
  3. use BadMethodCallException;
  4. use Carbon\Carbon as BaseCarbon;
  5. use Carbon\CarbonImmutable as BaseCarbonImmutable;
  6. use DateTime;
  7. use DateTimeInterface;
  8. use Illuminate\Support\Carbon;
  9. use PHPUnit\Framework\TestCase;
  10. class SupportCarbonTest extends TestCase
  11. {
  12. /**
  13. * @var \Illuminate\Support\Carbon
  14. */
  15. protected $now;
  16. protected function setUp(): void
  17. {
  18. parent::setUp();
  19. Carbon::setTestNow($this->now = Carbon::create(2017, 6, 27, 13, 14, 15, 'UTC'));
  20. }
  21. protected function tearDown(): void
  22. {
  23. Carbon::setTestNow();
  24. Carbon::serializeUsing(null);
  25. parent::tearDown();
  26. }
  27. public function testInstance()
  28. {
  29. $this->assertInstanceOf(DateTime::class, $this->now);
  30. $this->assertInstanceOf(DateTimeInterface::class, $this->now);
  31. $this->assertInstanceOf(BaseCarbon::class, $this->now);
  32. $this->assertInstanceOf(Carbon::class, $this->now);
  33. }
  34. public function testCarbonIsMacroableWhenNotCalledStatically()
  35. {
  36. Carbon::macro('diffInDecades', function (Carbon $dt = null, $abs = true) {
  37. return (int) ($this->diffInYears($dt, $abs) / 10);
  38. });
  39. $this->assertSame(2, $this->now->diffInDecades(Carbon::now()->addYears(25)));
  40. }
  41. public function testCarbonIsMacroableWhenCalledStatically()
  42. {
  43. Carbon::macro('twoDaysAgoAtNoon', function () {
  44. return Carbon::now()->subDays(2)->setTime(12, 0, 0);
  45. });
  46. $this->assertSame('2017-06-25 12:00:00', Carbon::twoDaysAgoAtNoon()->toDateTimeString());
  47. }
  48. public function testCarbonRaisesExceptionWhenStaticMacroIsNotFound()
  49. {
  50. $this->expectException(BadMethodCallException::class);
  51. $this->expectExceptionMessage('nonExistingStaticMacro does not exist.');
  52. Carbon::nonExistingStaticMacro();
  53. }
  54. public function testCarbonRaisesExceptionWhenMacroIsNotFound()
  55. {
  56. $this->expectException(BadMethodCallException::class);
  57. $this->expectExceptionMessage('nonExistingMacro does not exist.');
  58. Carbon::now()->nonExistingMacro();
  59. }
  60. public function testCarbonAllowsCustomSerializer()
  61. {
  62. Carbon::serializeUsing(function (Carbon $carbon) {
  63. return $carbon->getTimestamp();
  64. });
  65. $result = json_decode(json_encode($this->now), true);
  66. $this->assertSame(1498569255, $result);
  67. }
  68. public function testCarbonCanSerializeToJson()
  69. {
  70. $this->assertSame('2017-06-27T13:14:15.000000Z', $this->now->jsonSerialize());
  71. }
  72. public function testSetStateReturnsCorrectType()
  73. {
  74. $carbon = Carbon::__set_state([
  75. 'date' => '2017-06-27 13:14:15.000000',
  76. 'timezone_type' => 3,
  77. 'timezone' => 'UTC',
  78. ]);
  79. $this->assertInstanceOf(Carbon::class, $carbon);
  80. }
  81. public function testDeserializationOccursCorrectly()
  82. {
  83. $carbon = new Carbon('2017-06-27 13:14:15.000000');
  84. $serialized = 'return '.var_export($carbon, true).';';
  85. $deserialized = eval($serialized);
  86. $this->assertInstanceOf(Carbon::class, $deserialized);
  87. }
  88. public function testSetTestNowWillPersistBetweenImmutableAndMutableInstance()
  89. {
  90. Carbon::setTestNow(new Carbon('2017-06-27 13:14:15.000000'));
  91. $this->assertSame('2017-06-27 13:14:15', Carbon::now()->toDateTimeString());
  92. $this->assertSame('2017-06-27 13:14:15', BaseCarbon::now()->toDateTimeString());
  93. $this->assertSame('2017-06-27 13:14:15', BaseCarbonImmutable::now()->toDateTimeString());
  94. }
  95. }