Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Allow TestComponent to be macroable #54359

Merged
merged 2 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
Loading