MacroContextNestingTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\CommonTraits;
  12. use Carbon\Carbon;
  13. use Carbon\CarbonImmutable;
  14. use Carbon\CarbonInterval;
  15. use Carbon\CarbonPeriod;
  16. use Generator;
  17. use Tests\AbstractTestCaseWithOldNow;
  18. class MacroContextNestingTest extends AbstractTestCaseWithOldNow
  19. {
  20. public static function dataForMacroableClasses(): Generator
  21. {
  22. yield [Carbon::class, Carbon::parse('2010-05-23'), null];
  23. yield [CarbonImmutable::class, CarbonImmutable::parse('2010-05-23'), null];
  24. yield [CarbonInterval::class, CarbonInterval::make('P1M6D'), (string) (CarbonInterval::second())];
  25. yield [CarbonPeriod::class, CarbonPeriod::create('2010-08-23', '2010-10-02'), null];
  26. }
  27. /**
  28. * @dataProvider \Tests\CommonTraits\MacroContextNestingTest::dataForMacroableClasses
  29. *
  30. * @param string $class
  31. * @param mixed $sample
  32. * @param string|null $reference
  33. */
  34. public function testMacroContextNesting($class, $sample, $reference)
  35. {
  36. $macro1 = 'macro'.(mt_rand(100, 999999) * 2);
  37. $class::macro($macro1, static function () {
  38. return self::this()->__toString();
  39. });
  40. $macro2 = 'macro'.(mt_rand(100, 999999) * 2 + 1);
  41. $class::macro($macro2, static function () use ($macro1, $sample) {
  42. $dates = [self::this()->$macro1()];
  43. $dates[] = $sample->$macro1();
  44. $dates[] = self::this()->$macro1();
  45. return $dates;
  46. });
  47. $dates = $class::$macro2();
  48. $this->assertSame([
  49. $reference ?: (string) (new $class()),
  50. (string) $sample,
  51. $reference ?: (string) (new $class()),
  52. ], $dates);
  53. }
  54. /**
  55. * @dataProvider \Tests\CommonTraits\MacroContextNestingTest::dataForMacroableClasses
  56. *
  57. * @param string $class
  58. * @param mixed $sample
  59. */
  60. public function testMacroContextDetectionNesting($class, $sample)
  61. {
  62. $macro1 = 'macro'.(mt_rand(100, 999999) * 2);
  63. $class::macro($macro1, static function () {
  64. $context = self::context();
  65. return $context ? \get_class($context) : 'null';
  66. });
  67. $macro2 = 'macro'.(mt_rand(100, 999999) * 2 + 1);
  68. $class::macro($macro2, static function () use ($macro1, $sample) {
  69. $dump = [self::$macro1(), self::this()->$macro1()];
  70. $dump[] = $sample->$macro1();
  71. $dump[] = self::$macro1();
  72. return $dump;
  73. });
  74. $dump = $class::$macro2();
  75. $this->assertSame([
  76. 'null',
  77. $class,
  78. $class,
  79. 'null',
  80. ], $dump);
  81. }
  82. }