Skip to content

Commit fa3b1c7

Browse files
committed
Create TestComponent Class to support Blade Components much better
1 parent 59b674a commit fa3b1c7

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/Illuminate/Foundation/Testing/Concerns/InteractsWithViews.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Support\Facades\View as ViewFacade;
66
use Illuminate\Support\MessageBag;
77
use Illuminate\Support\ViewErrorBag;
8+
use Illuminate\Testing\TestComponent;
89
use Illuminate\Testing\TestView;
910
use Illuminate\View\View;
1011

@@ -33,13 +34,13 @@ protected function blade(string $template, array $data = [])
3334
{
3435
$tempDirectory = sys_get_temp_dir();
3536

36-
if (! in_array($tempDirectory, ViewFacade::getFinder()->getPaths())) {
37+
if (!in_array($tempDirectory, ViewFacade::getFinder()->getPaths())) {
3738
ViewFacade::addLocation(sys_get_temp_dir());
3839
}
3940

4041
$tempFileInfo = pathinfo(tempnam($tempDirectory, 'laravel-blade'));
4142

42-
$tempFile = $tempFileInfo['dirname'].'/'.$tempFileInfo['filename'].'.blade.php';
43+
$tempFile = $tempFileInfo['dirname'] . '/' . $tempFileInfo['filename'] . '.blade.php';
4344

4445
file_put_contents($tempFile, $template);
4546

@@ -59,9 +60,11 @@ protected function component(string $componentClass, array $data = [])
5960

6061
$view = value($component->resolveView(), $data);
6162

62-
return $view instanceof View
63-
? new TestView($view->with($component->data()))
64-
: new TestView(view($view, $component->data()));
63+
$view = $view instanceof View
64+
? $view->with($component->data())
65+
: view($view, $component->data());
66+
67+
return (new TestComponent($component, $view));
6568
}
6669

6770
/**
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Illuminate\Testing;
4+
5+
use Illuminate\Testing\TestView;
6+
use Illuminate\View\Component;
7+
8+
9+
class TestComponent extends TestView
10+
{
11+
/**
12+
* The original view.
13+
*
14+
* @var \Illuminate\View\Component
15+
*/
16+
protected $component;
17+
18+
/**
19+
* The rendered view contents.
20+
*
21+
* @var string
22+
*/
23+
protected $rendered;
24+
25+
/**
26+
* Create a new test view instance.
27+
*
28+
* @param \Illuminate\View\Component $view
29+
* @return void
30+
*/
31+
public function __construct(Component $component, $view)
32+
{
33+
$this->component = $component;
34+
$this->rendered = $view->render();
35+
}
36+
37+
public function __get($attribute)
38+
{
39+
return $this->component->{$attribute};
40+
}
41+
}

0 commit comments

Comments
 (0)