| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Foundation\Testing\Concerns;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Support\Facades\View as ViewFacade;
|
|
|
6 |
use Illuminate\Support\MessageBag;
|
|
|
7 |
use Illuminate\Support\ViewErrorBag;
|
|
|
8 |
use Illuminate\Testing\TestComponent;
|
|
|
9 |
use Illuminate\Testing\TestView;
|
|
|
10 |
use Illuminate\View\View;
|
|
|
11 |
|
|
|
12 |
trait InteractsWithViews
|
|
|
13 |
{
|
|
|
14 |
/**
|
|
|
15 |
* Create a new TestView from the given view.
|
|
|
16 |
*
|
|
|
17 |
* @param string $view
|
|
|
18 |
* @param \Illuminate\Contracts\Support\Arrayable|array $data
|
|
|
19 |
* @return \Illuminate\Testing\TestView
|
|
|
20 |
*/
|
|
|
21 |
protected function view(string $view, $data = [])
|
|
|
22 |
{
|
|
|
23 |
return new TestView(view($view, $data));
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Render the contents of the given Blade template string.
|
|
|
28 |
*
|
|
|
29 |
* @param string $template
|
|
|
30 |
* @param \Illuminate\Contracts\Support\Arrayable|array $data
|
|
|
31 |
* @return \Illuminate\Testing\TestView
|
|
|
32 |
*/
|
|
|
33 |
protected function blade(string $template, $data = [])
|
|
|
34 |
{
|
|
|
35 |
$tempDirectory = sys_get_temp_dir();
|
|
|
36 |
|
|
|
37 |
if (! in_array($tempDirectory, ViewFacade::getFinder()->getPaths())) {
|
|
|
38 |
ViewFacade::addLocation(sys_get_temp_dir());
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
$tempFileInfo = pathinfo(tempnam($tempDirectory, 'laravel-blade'));
|
|
|
42 |
|
|
|
43 |
$tempFile = $tempFileInfo['dirname'].'/'.$tempFileInfo['filename'].'.blade.php';
|
|
|
44 |
|
|
|
45 |
file_put_contents($tempFile, $template);
|
|
|
46 |
|
|
|
47 |
return new TestView(view($tempFileInfo['filename'], $data));
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Render the given view component.
|
|
|
52 |
*
|
|
|
53 |
* @param string $componentClass
|
|
|
54 |
* @param \Illuminate\Contracts\Support\Arrayable|array $data
|
|
|
55 |
* @return \Illuminate\Testing\TestComponent
|
|
|
56 |
*/
|
|
|
57 |
protected function component(string $componentClass, $data = [])
|
|
|
58 |
{
|
|
|
59 |
$component = $this->app->make($componentClass, $data);
|
|
|
60 |
|
|
|
61 |
$view = value($component->resolveView(), $data);
|
|
|
62 |
|
|
|
63 |
$view = $view instanceof View
|
|
|
64 |
? $view->with($component->data())
|
|
|
65 |
: view($view, $component->data());
|
|
|
66 |
|
|
|
67 |
return new TestComponent($component, $view);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Populate the shared view error bag with the given errors.
|
|
|
72 |
*
|
|
|
73 |
* @param array $errors
|
|
|
74 |
* @param string $key
|
|
|
75 |
* @return $this
|
|
|
76 |
*/
|
|
|
77 |
protected function withViewErrors(array $errors, $key = 'default')
|
|
|
78 |
{
|
|
|
79 |
ViewFacade::share('errors', (new ViewErrorBag)->put($key, new MessageBag($errors)));
|
|
|
80 |
|
|
|
81 |
return $this;
|
|
|
82 |
}
|
|
|
83 |
}
|