Skip to content

Commit 6078547

Browse files
committed
Merge branch 'yob-yob/8.x' into 8.x
2 parents b75a01f + 42a71fd commit 6078547

File tree

3 files changed

+197
-4
lines changed

3 files changed

+197
-4
lines changed

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

Lines changed: 7 additions & 4 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

@@ -51,17 +52,19 @@ protected function blade(string $template, array $data = [])
5152
*
5253
* @param string $componentClass
5354
* @param \Illuminate\Contracts\Support\Arrayable|array $data
54-
* @return \Illuminate\Testing\TestView
55+
* @return \Illuminate\Testing\TestComponent
5556
*/
5657
protected function component(string $componentClass, array $data = [])
5758
{
5859
$component = $this->app->make($componentClass, $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: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
3+
namespace Illuminate\Testing;
4+
5+
use Illuminate\Testing\Assert as PHPUnit;
6+
use Illuminate\Testing\Constraints\SeeInOrder;
7+
use Illuminate\View\Component;
8+
9+
class TestComponent
10+
{
11+
/**
12+
* The original component.
13+
*
14+
* @var \Illuminate\View\Component
15+
*/
16+
public $component;
17+
18+
/**
19+
* The rendered component contents.
20+
*
21+
* @var string
22+
*/
23+
protected $rendered;
24+
25+
/**
26+
* Create a new test component instance.
27+
*
28+
* @param \Illuminate\View\Component $component
29+
* @param \Illuminate\View\View $view
30+
* @return void
31+
*/
32+
public function __construct($component, $view)
33+
{
34+
$this->component = $component;
35+
36+
$this->rendered = $view->render();
37+
}
38+
39+
/**
40+
* Assert that the given string is contained within the rendered component.
41+
*
42+
* @param string $value
43+
* @param bool $escape
44+
* @return $this
45+
*/
46+
public function assertSee($value, $escape = true)
47+
{
48+
$value = $escape ? e($value) : $value;
49+
50+
PHPUnit::assertStringContainsString((string) $value, $this->rendered);
51+
52+
return $this;
53+
}
54+
55+
/**
56+
* Assert that the given strings are contained in order within the rendered component.
57+
*
58+
* @param array $values
59+
* @param bool $escape
60+
* @return $this
61+
*/
62+
public function assertSeeInOrder(array $values, $escape = true)
63+
{
64+
$values = $escape ? array_map('e', ($values)) : $values;
65+
66+
PHPUnit::assertThat($values, new SeeInOrder($this->rendered));
67+
68+
return $this;
69+
}
70+
71+
/**
72+
* Assert that the given string is contained within the rendered component text.
73+
*
74+
* @param string $value
75+
* @param bool $escape
76+
* @return $this
77+
*/
78+
public function assertSeeText($value, $escape = true)
79+
{
80+
$value = $escape ? e($value) : $value;
81+
82+
PHPUnit::assertStringContainsString((string) $value, strip_tags($this->rendered));
83+
84+
return $this;
85+
}
86+
87+
/**
88+
* Assert that the given strings are contained in order within the rendered component text.
89+
*
90+
* @param array $values
91+
* @param bool $escape
92+
* @return $this
93+
*/
94+
public function assertSeeTextInOrder(array $values, $escape = true)
95+
{
96+
$values = $escape ? array_map('e', ($values)) : $values;
97+
98+
PHPUnit::assertThat($values, new SeeInOrder(strip_tags($this->rendered)));
99+
100+
return $this;
101+
}
102+
103+
/**
104+
* Assert that the given string is not contained within the rendered component.
105+
*
106+
* @param string $value
107+
* @param bool $escape
108+
* @return $this
109+
*/
110+
public function assertDontSee($value, $escape = true)
111+
{
112+
$value = $escape ? e($value) : $value;
113+
114+
PHPUnit::assertStringNotContainsString((string) $value, $this->rendered);
115+
116+
return $this;
117+
}
118+
119+
/**
120+
* Assert that the given string is not contained within the rendered component text.
121+
*
122+
* @param string $value
123+
* @param bool $escape
124+
* @return $this
125+
*/
126+
public function assertDontSeeText($value, $escape = true)
127+
{
128+
$value = $escape ? e($value) : $value;
129+
130+
PHPUnit::assertStringNotContainsString((string) $value, strip_tags($this->rendered));
131+
132+
return $this;
133+
}
134+
135+
/**
136+
* Get the string contents of the rendered component.
137+
*
138+
* @return string
139+
*/
140+
public function __toString()
141+
{
142+
return $this->rendered;
143+
}
144+
145+
/**
146+
* Dynamically access properties on the underlying component.
147+
*
148+
* @param string $attribute
149+
* @return mixed
150+
*/
151+
public function __get($attribute)
152+
{
153+
return $this->component->{$attribute};
154+
}
155+
156+
/**
157+
* Dynamically call methods on the underlying component.
158+
*
159+
* @param string $method
160+
* @param array $parameters
161+
* @return mixed
162+
*/
163+
public function __call($method, $parameters)
164+
{
165+
return $this->component->{$method}(...$parameters);
166+
}
167+
}

tests/Foundation/Testing/Concerns/InteractsWithViewsTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Tests\Foundation\Testing\Concerns;
44

55
use Illuminate\Foundation\Testing\Concerns\InteractsWithViews;
6+
use Illuminate\View\Component;
67
use Orchestra\Testbench\TestCase;
78

89
class InteractsWithViewsTest extends TestCase
@@ -15,4 +16,26 @@ public function testBladeCorrectlyRendersString()
1516

1617
$this->assertEquals('test ', $string);
1718
}
19+
20+
public function testComponentCanAccessPublicProperties()
21+
{
22+
$exampleComponent = new class extends Component
23+
{
24+
public $foo = 'bar';
25+
public function speak()
26+
{
27+
return 'hello';
28+
}
29+
public function render()
30+
{
31+
return 'rendered content';
32+
}
33+
};
34+
35+
$component = $this->component(get_class($exampleComponent));
36+
37+
$this->assertEquals('bar', $component->foo);
38+
$this->assertEquals('hello', $component->speak());
39+
$component->assertSee('content');
40+
}
1841
}

0 commit comments

Comments
 (0)