Skip to content

Commit

Permalink
[11.x] Allow TestComponent to be macroable (#54359)
Browse files Browse the repository at this point in the history
* macro test component

* test
  • Loading branch information
ziadoz authored Jan 26, 2025
1 parent ebd115f commit 4673387
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Illuminate/Testing/TestComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

namespace Illuminate\Testing;

use Illuminate\Support\Traits\Macroable;
use Illuminate\Testing\Assert as PHPUnit;
use Illuminate\Testing\Constraints\SeeInOrder;
use Stringable;

class TestComponent implements Stringable
{
use Macroable {
__call as macroCall;
}

/**
* The original component.
*
Expand Down Expand Up @@ -162,6 +167,10 @@ public function __get($attribute)
*/
public function __call($method, $parameters)
{
if (static::hasMacro($method)) {
return $this->macroCall($method, $parameters);
}

return $this->component->{$method}(...$parameters);
}
}
18 changes: 18 additions & 0 deletions tests/Foundation/Testing/Concerns/InteractsWithViewsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Tests\Foundation\Testing\Concerns;

use Illuminate\Foundation\Testing\Concerns\InteractsWithViews;
use Illuminate\Testing\TestComponent;
use Illuminate\View\Component;
use Orchestra\Testbench\TestCase;

Expand Down Expand Up @@ -40,4 +41,21 @@ public function render()
$this->assertSame('hello', $component->speak());
$component->assertSee('content');
}

public function testComponentMacroable()
{
TestComponent::macro('foo', fn (): string => 'bar');

$exampleComponent = new class extends Component
{
public function render()
{
return 'rendered content';
}
};

$component = $this->component(get_class($exampleComponent));

$this->assertSame('bar', $component->foo());
}
}

0 comments on commit 4673387

Please sign in to comment.