Skip to content

Commit

Permalink
[8.x] Added a method to the Macroable trait that removes all configur…
Browse files Browse the repository at this point in the history
…ed macros. (#39633)

* Added a method to the Macroable trait that removes all configured macros.

* Added a test case for flushMacros.

* Changed the comment on the flushMacros method to be clearer.
  • Loading branch information
mad-briller authored Nov 16, 2021
1 parent 23912e7 commit b2669a8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Illuminate/Macroable/Traits/Macroable.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ public static function hasMacro($name)
return isset(static::$macros[$name]);
}

/**
* Flush the existing macros.
*
* @return void
*/
public static function flushMacros()
{
static::$macros = [];
}

/**
* Dynamically handle calls to the class.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/Support/SupportMacroableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Support;

use BadMethodCallException;
use Illuminate\Support\Traits\Macroable;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -73,6 +74,23 @@ public function testClassBasedMacrosNoReplace()
TestMacroable::mixin(new TestMixin);
$this->assertSame('foo', $instance->methodThree());
}

public function testFlushMacros()
{
TestMacroable::macro('flushMethod', function () {
return 'flushMethod';
});

$instance = new TestMacroable;

$this->assertSame('flushMethod', $instance->flushMethod());

TestMacroable::flushMacros();

$this->expectException(BadMethodCallException::class);

$instance->flushMethod();
}
}

class EmptyMacroable
Expand Down

0 comments on commit b2669a8

Please sign in to comment.