From fa3b1c70129dacc7038684778dfba22eaa666f90 Mon Sep 17 00:00:00 2001 From: Sean <25021150+yob-yob@users.noreply.github.com> Date: Mon, 16 Aug 2021 13:51:01 +0800 Subject: [PATCH 1/4] Create TestComponent Class to support Blade Components much better --- .../Testing/Concerns/InteractsWithViews.php | 13 +++--- src/Illuminate/Testing/TestComponent.php | 41 +++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 src/Illuminate/Testing/TestComponent.php diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithViews.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithViews.php index 574effe21260..8eb932fcd0bb 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; @@ -33,13 +34,13 @@ protected function blade(string $template, array $data = []) { $tempDirectory = sys_get_temp_dir(); - if (! in_array($tempDirectory, ViewFacade::getFinder()->getPaths())) { + if (!in_array($tempDirectory, ViewFacade::getFinder()->getPaths())) { ViewFacade::addLocation(sys_get_temp_dir()); } $tempFileInfo = pathinfo(tempnam($tempDirectory, 'laravel-blade')); - $tempFile = $tempFileInfo['dirname'].'/'.$tempFileInfo['filename'].'.blade.php'; + $tempFile = $tempFileInfo['dirname'] . '/' . $tempFileInfo['filename'] . '.blade.php'; file_put_contents($tempFile, $template); @@ -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}; + } +} From ff9d29e455829bd404680b4f31be33b7dcbbaf23 Mon Sep 17 00:00:00 2001 From: Sean <25021150+yob-yob@users.noreply.github.com> Date: Mon, 16 Aug 2021 16:56:46 +0800 Subject: [PATCH 2/4] Create a test for TestComponent Class --- .../Testing/Concerns/InteractsWithViewsTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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); + } } From 81add9358d33b82edb0eba0af774f177dd749268 Mon Sep 17 00:00:00 2001 From: Sean <25021150+yob-yob@users.noreply.github.com> Date: Tue, 17 Aug 2021 18:15:58 +0800 Subject: [PATCH 3/4] Revert Style changes that are not related to my pull-request --- .../Foundation/Testing/Concerns/InteractsWithViews.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithViews.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithViews.php index 8eb932fcd0bb..7c24a51e26f9 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithViews.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithViews.php @@ -34,13 +34,13 @@ protected function blade(string $template, array $data = []) { $tempDirectory = sys_get_temp_dir(); - if (!in_array($tempDirectory, ViewFacade::getFinder()->getPaths())) { + if (! in_array($tempDirectory, ViewFacade::getFinder()->getPaths())) { ViewFacade::addLocation(sys_get_temp_dir()); } $tempFileInfo = pathinfo(tempnam($tempDirectory, 'laravel-blade')); - $tempFile = $tempFileInfo['dirname'] . '/' . $tempFileInfo['filename'] . '.blade.php'; + $tempFile = $tempFileInfo['dirname'].'/'.$tempFileInfo['filename'] . '.blade.php'; file_put_contents($tempFile, $template); @@ -64,7 +64,7 @@ protected function component(string $componentClass, array $data = []) ? $view->with($component->data()) : view($view, $component->data()); - return (new TestComponent($component, $view)); + return new TestComponent($component, $view); } /** From 1a37f3d1386da88e3cdce459d6b5bdcc1b91410c Mon Sep 17 00:00:00 2001 From: Sean <25021150+yob-yob@users.noreply.github.com> Date: Tue, 17 Aug 2021 18:16:16 +0800 Subject: [PATCH 4/4] Update InteractsWithViews.php --- .../Foundation/Testing/Concerns/InteractsWithViews.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithViews.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithViews.php index 7c24a51e26f9..6464edfdba02 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithViews.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithViews.php @@ -40,7 +40,7 @@ protected function blade(string $template, array $data = []) $tempFileInfo = pathinfo(tempnam($tempDirectory, 'laravel-blade')); - $tempFile = $tempFileInfo['dirname'].'/'.$tempFileInfo['filename'] . '.blade.php'; + $tempFile = $tempFileInfo['dirname'].'/'.$tempFileInfo['filename'].'.blade.php'; file_put_contents($tempFile, $template);