SupportMacroableTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace Illuminate\Tests\Support;
  3. use BadMethodCallException;
  4. use Illuminate\Support\Traits\Macroable;
  5. use PHPUnit\Framework\TestCase;
  6. class SupportMacroableTest extends TestCase
  7. {
  8. private $macroable;
  9. protected function setUp(): void
  10. {
  11. $this->macroable = $this->createObjectForTrait();
  12. }
  13. private function createObjectForTrait()
  14. {
  15. return new EmptyMacroable;
  16. }
  17. public function testRegisterMacro()
  18. {
  19. $macroable = $this->macroable;
  20. $macroable::macro(__CLASS__, function () {
  21. return 'Taylor';
  22. });
  23. $this->assertSame('Taylor', $macroable::{__CLASS__}());
  24. }
  25. public function testRegisterMacroAndCallWithoutStatic()
  26. {
  27. $macroable = $this->macroable;
  28. $macroable::macro(__CLASS__, function () {
  29. return 'Taylor';
  30. });
  31. $this->assertSame('Taylor', $macroable->{__CLASS__}());
  32. }
  33. public function testWhenCallingMacroClosureIsBoundToObject()
  34. {
  35. TestMacroable::macro('tryInstance', function () {
  36. return $this->protectedVariable;
  37. });
  38. TestMacroable::macro('tryStatic', function () {
  39. return static::getProtectedStatic();
  40. });
  41. $instance = new TestMacroable;
  42. $result = $instance->tryInstance();
  43. $this->assertSame('instance', $result);
  44. $result = TestMacroable::tryStatic();
  45. $this->assertSame('static', $result);
  46. }
  47. public function testClassBasedMacros()
  48. {
  49. TestMacroable::mixin(new TestMixin);
  50. $instance = new TestMacroable;
  51. $this->assertSame('instance-Adam', $instance->methodOne('Adam'));
  52. }
  53. public function testClassBasedMacrosNoReplace()
  54. {
  55. TestMacroable::macro('methodThree', function () {
  56. return 'bar';
  57. });
  58. TestMacroable::mixin(new TestMixin, false);
  59. $instance = new TestMacroable;
  60. $this->assertSame('bar', $instance->methodThree());
  61. TestMacroable::mixin(new TestMixin);
  62. $this->assertSame('foo', $instance->methodThree());
  63. }
  64. public function testFlushMacros()
  65. {
  66. TestMacroable::macro('flushMethod', function () {
  67. return 'flushMethod';
  68. });
  69. $instance = new TestMacroable;
  70. $this->assertSame('flushMethod', $instance->flushMethod());
  71. TestMacroable::flushMacros();
  72. $this->expectException(BadMethodCallException::class);
  73. $instance->flushMethod();
  74. }
  75. }
  76. class EmptyMacroable
  77. {
  78. use Macroable;
  79. }
  80. class TestMacroable
  81. {
  82. use Macroable;
  83. protected $protectedVariable = 'instance';
  84. protected static function getProtectedStatic()
  85. {
  86. return 'static';
  87. }
  88. }
  89. class TestMixin
  90. {
  91. public function methodOne()
  92. {
  93. return function ($value) {
  94. return $this->methodTwo($value);
  95. };
  96. }
  97. protected function methodTwo()
  98. {
  99. return function ($value) {
  100. return $this->protectedVariable.'-'.$value;
  101. };
  102. }
  103. protected function methodThree()
  104. {
  105. return function () {
  106. return 'foo';
  107. };
  108. }
  109. }