diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithViews.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithViews.php index 574effe21260..6464edfdba02 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithViews.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithViews.php @@ -5,6 +5,7 @@ use Illuminate\Support\Facades\View as ViewFacade; use Illuminate\Support\MessageBag; use Illuminate\Support\ViewErrorBag; +use Illuminate\Testing\TestComponent; use Illuminate\Testing\TestView; use Illuminate\View\View; @@ -59,9 +60,11 @@ protected function component(string $componentClass, array $data = []) $view = value($component->resolveView(), $data); - return $view instanceof View - ? new TestView($view->with($component->data())) - : new TestView(view($view, $component->data())); + $view = $view instanceof View + ? $view->with($component->data()) + : view($view, $component->data()); + + return new TestComponent($component, $view); } /** diff --git a/src/Illuminate/Testing/TestComponent.php b/src/Illuminate/Testing/TestComponent.php new file mode 100644 index 000000000000..3d73a1ceafe8 --- /dev/null +++ b/src/Illuminate/Testing/TestComponent.php @@ -0,0 +1,41 @@ +component = $component; + $this->rendered = $view->render(); + } + + public function __get($attribute) + { + return $this->component->{$attribute}; + } +} diff --git a/tests/Foundation/Testing/Concerns/InteractsWithViewsTest.php b/tests/Foundation/Testing/Concerns/InteractsWithViewsTest.php index 42bc6c2ec28d..d03b8e28e901 100644 --- a/tests/Foundation/Testing/Concerns/InteractsWithViewsTest.php +++ b/tests/Foundation/Testing/Concerns/InteractsWithViewsTest.php @@ -3,6 +3,7 @@ namespace Illuminate\Tests\Foundation\Testing\Concerns; use Illuminate\Foundation\Testing\Concerns\InteractsWithViews; +use Illuminate\View\Component; use Orchestra\Testbench\TestCase; class InteractsWithViewsTest extends TestCase @@ -15,4 +16,19 @@ public function testBladeCorrectlyRendersString() $this->assertEquals('test ', $string); } + + public function testComponentCanAccessPublicProperties() + { + $exampleComponent = new class extends Component + { + public $foo = 'bar'; + public function render() + { + return ''; + } + }; + $component = $this->component(get_class($exampleComponent)); + + $this->assertEquals('bar', $component->foo); + } }