| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace Tightenco\Collect\Tests\Support;
- use PHPUnit\Framework\TestCase;
- use Tightenco\Collect\Support\Traits\Macroable;
- class SupportMacroableTest extends TestCase
- {
- private $macroable;
- public function setUp()
- {
- $this->macroable = $this->createObjectForTrait();
- }
- private function createObjectForTrait()
- {
- return new EmptyMacroable;
- }
- public function testRegisterMacro()
- {
- $macroable = $this->macroable;
- $macroable::macro(__CLASS__, function () {
- return 'Taylor';
- });
- $this->assertEquals('Taylor', $macroable::{__CLASS__}());
- }
- public function testRegisterMacroAndCallWithoutStatic()
- {
- $macroable = $this->macroable;
- $macroable::macro(__CLASS__, function () {
- return 'Taylor';
- });
- $this->assertEquals('Taylor', $macroable->{__CLASS__}());
- }
- public function testWhenCallingMacroClosureIsBoundToObject()
- {
- TestMacroable::macro('tryInstance', function () {
- return $this->protectedVariable;
- });
- TestMacroable::macro('tryStatic', function () {
- return static::getProtectedStatic();
- });
- $instance = new TestMacroable;
- $result = $instance->tryInstance();
- $this->assertEquals('instance', $result);
- $result = TestMacroable::tryStatic();
- $this->assertEquals('static', $result);
- }
- public function testClassBasedMacros()
- {
- TestMacroable::mixin(new TestMixin);
- $instance = new TestMacroable;
- $this->assertEquals('instance-Adam', $instance->methodOne('Adam'));
- }
- }
- class EmptyMacroable
- {
- use Macroable;
- }
- class TestMacroable
- {
- use Macroable;
- protected $protectedVariable = 'instance';
- protected static function getProtectedStatic()
- {
- return 'static';
- }
- }
- class TestMixin
- {
- public function methodOne()
- {
- return function ($value) {
- return $this->methodTwo($value);
- };
- }
- protected function methodTwo()
- {
- return function ($value) {
- return $this->protectedVariable.'-'.$value;
- };
- }
- }
|