Skip to content

Commit 42a71fd

Browse files
committed
formatting
1 parent 1a37f3d commit 42a71fd

File tree

3 files changed

+168
-35
lines changed

3 files changed

+168
-35
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected function blade(string $template, array $data = [])
5252
*
5353
* @param string $componentClass
5454
* @param \Illuminate\Contracts\Support\Arrayable|array $data
55-
* @return \Illuminate\Testing\TestView
55+
* @return \Illuminate\Testing\TestComponent
5656
*/
5757
protected function component(string $componentClass, array $data = [])
5858
{

src/Illuminate/Testing/TestComponent.php

Lines changed: 159 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,166 @@
22

33
namespace Illuminate\Testing;
44

5-
use Illuminate\Testing\TestView;
5+
use Illuminate\Testing\Assert as PHPUnit;
6+
use Illuminate\Testing\Constraints\SeeInOrder;
67
use Illuminate\View\Component;
78

8-
9-
class TestComponent extends TestView
9+
class TestComponent
1010
{
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-
}
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+
}
41167
}

tests/Foundation/Testing/Concerns/InteractsWithViewsTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@ public function testComponentCanAccessPublicProperties()
2222
$exampleComponent = new class extends Component
2323
{
2424
public $foo = 'bar';
25+
public function speak()
26+
{
27+
return 'hello';
28+
}
2529
public function render()
2630
{
27-
return '';
31+
return 'rendered content';
2832
}
2933
};
34+
3035
$component = $this->component(get_class($exampleComponent));
3136

3237
$this->assertEquals('bar', $component->foo);
38+
$this->assertEquals('hello', $component->speak());
39+
$component->assertSee('content');
3340
}
3441
}

0 commit comments

Comments
 (0)