MacroTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\CarbonImmutable;
  12. use BadMethodCallException;
  13. use Carbon\CarbonImmutable as Carbon;
  14. use CarbonTimezoneTrait;
  15. use Tests\AbstractTestCaseWithOldNow;
  16. use Tests\Carbon\Fixtures\FooBar;
  17. use Tests\CarbonImmutable\Fixtures\Mixin;
  18. class MacroTest extends AbstractTestCaseWithOldNow
  19. {
  20. public function testCarbonIsMacroableWhenNotCalledDynamically()
  21. {
  22. if (!\function_exists('easter_days')) {
  23. $this->markTestSkipped('This test requires ext-calendar to be enabled.');
  24. }
  25. Carbon::macro('easterDays', function ($year = 2019) {
  26. return easter_days($year);
  27. });
  28. /** @var mixed $now */
  29. $now = Carbon::now();
  30. $this->assertSame(22, $now->easterDays(2020));
  31. $this->assertSame(31, $now->easterDays());
  32. Carbon::macro('otherParameterName', function ($other = true) {
  33. return $other;
  34. });
  35. $this->assertTrue($now->otherParameterName());
  36. }
  37. public function testCarbonIsMacroableWhenNotCalledDynamicallyUsingThis()
  38. {
  39. if (!\function_exists('easter_days')) {
  40. $this->markTestSkipped('This test requires ext-calendar to be enabled.');
  41. }
  42. Carbon::macro('diffFromEaster', function ($year) {
  43. /** @var Carbon $date */
  44. $date = $this;
  45. return $date->diff(
  46. Carbon::create($year, 3, 21)
  47. ->setTimezone($date->getTimezone())
  48. ->addDays(easter_days($year))
  49. ->endOfDay()
  50. );
  51. });
  52. /** @var mixed $now */
  53. $now = Carbon::now();
  54. $this->assertSame(1020, $now->diffFromEaster(2020)->days);
  55. }
  56. public function testCarbonIsMacroableWhenCalledStatically()
  57. {
  58. if (!\function_exists('easter_days')) {
  59. $this->markTestSkipped('This test requires ext-calendar to be enabled.');
  60. }
  61. Carbon::macro('easterDate', function ($year) {
  62. return Carbon::create($year, 3, 21)->addDays(easter_days($year));
  63. });
  64. $this->assertSame('05/04', Carbon::easterDate(2015)->format('d/m'));
  65. }
  66. public function testCarbonIsMacroableWhithNonClosureCallables()
  67. {
  68. Carbon::macro('lower2', 'strtolower');
  69. /** @var mixed $now */
  70. $now = Carbon::now();
  71. $this->assertSame('abc', $now->lower2('ABC'));
  72. $this->assertSame('abc', Carbon::lower2('ABC'));
  73. }
  74. public function testCarbonIsMixinable()
  75. {
  76. include_once __DIR__.'/Fixtures/Mixin.php';
  77. $mixin = new Mixin();
  78. Carbon::mixin($mixin);
  79. Carbon::setUserTimezone('America/Belize');
  80. /** @var mixed $date */
  81. $date = Carbon::parse('2000-01-01 12:00:00', 'UTC');
  82. $this->assertSame('06:00 America/Belize', $date->userFormat('H:i e'));
  83. }
  84. public function testCarbonRaisesExceptionWhenStaticMacroIsNotFound()
  85. {
  86. $this->expectExceptionObject(new BadMethodCallException(
  87. 'Method Carbon\CarbonImmutable::nonExistingStaticMacro does not exist.'
  88. ));
  89. Carbon::nonExistingStaticMacro();
  90. }
  91. public function testCarbonRaisesExceptionWhenMacroIsNotFound()
  92. {
  93. $this->expectExceptionObject(new BadMethodCallException(
  94. 'Method nonExistingMacro does not exist.'
  95. ));
  96. /** @var mixed $date */
  97. $date = Carbon::now();
  98. $date->nonExistingMacro();
  99. }
  100. public function testTraitMixin()
  101. {
  102. Carbon::mixin(FooBar::class);
  103. Carbon::setTestNow('2019-07-19 00:00:00');
  104. $this->assertSame('supergirl / Friday / immutable', Carbon::super('girl'));
  105. $this->assertSame('superboy / Thursday / immutable', Carbon::parse('2019-07-18')->super('boy'));
  106. $this->assertInstanceOf(Carbon::class, Carbon::me());
  107. }
  108. /**
  109. * @requires PHP >= 8.0
  110. */
  111. public function testTraitWithNamedParameters()
  112. {
  113. require_once __DIR__.'/../Fixtures/CarbonTimezoneTrait.php';
  114. Carbon::mixin(CarbonTimezoneTrait::class);
  115. $now = Carbon::now();
  116. $now = eval("return \$now->toAppTz(tz: 'Europe/Paris');");
  117. $this->assertSame('Europe/Paris', $now->format('e'));
  118. }
  119. public function testSerializationAfterTraitChaining()
  120. {
  121. require_once __DIR__.'/../Fixtures/CarbonTimezoneTrait.php';
  122. Carbon::mixin(CarbonTimezoneTrait::class);
  123. Carbon::setTestNow('2023-05-24 14:49');
  124. $date = Carbon::toAppTz(false, 'Europe/Paris');
  125. $this->assertSame('2023-05-24 16:49 Europe/Paris', unserialize(serialize($date))->format('Y-m-d H:i e'));
  126. $date = Carbon::parse('2023-06-12 11:49')->toAppTz(false, 'Europe/Paris');
  127. $this->assertSame('2023-06-12 13:49 Europe/Paris', unserialize(serialize($date))->format('Y-m-d H:i e'));
  128. }
  129. }