SupportCarbonTest.php 3.1 KB

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