Skip to content

Commit e07e005

Browse files
[9.x] Add view data assertions to TestView (#43923)
* add view data assertions to `TestView` - `assertViewHas()` - `assertViewHasAll()` - `assertViewMissing()` these methods are all available on the `TestResponse`, but not currently `TestView`. These assertions can be more helpful to test the workings of the component, and not reliant on how the data actually gets displayed on the view side of things. * Update TestView.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent 0ec2c41 commit e07e005

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

src/Illuminate/Testing/TestView.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace Illuminate\Testing;
44

5+
use Closure;
6+
use Illuminate\Database\Eloquent\Collection;
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Support\Arr;
59
use Illuminate\Support\Traits\Macroable;
610
use Illuminate\Testing\Assert as PHPUnit;
711
use Illuminate\Testing\Constraints\SeeInOrder;
@@ -37,6 +41,71 @@ public function __construct(View $view)
3741
$this->rendered = $view->render();
3842
}
3943

44+
/**
45+
* Assert that the response view has a given piece of bound data.
46+
*
47+
* @param string|array $key
48+
* @param mixed $value
49+
* @return $this
50+
*/
51+
public function assertViewHas($key, $value = null)
52+
{
53+
if (is_array($key)) {
54+
return $this->assertViewHasAll($key);
55+
}
56+
57+
if (is_null($value)) {
58+
PHPUnit::assertTrue(Arr::has($this->view->gatherData(), $key));
59+
} elseif ($value instanceof Closure) {
60+
PHPUnit::assertTrue($value(Arr::get($this->view->gatherData(), $key)));
61+
} elseif ($value instanceof Model) {
62+
PHPUnit::assertTrue($value->is(Arr::get($this->view->gatherData(), $key)));
63+
} elseif ($value instanceof Collection) {
64+
$actual = Arr::get($this->view->gatherData(), $key);
65+
66+
PHPUnit::assertInstanceOf(Collection::class, $actual);
67+
PHPUnit::assertSameSize($value, $actual);
68+
69+
$value->each(fn ($item, $index) => PHPUnit::assertTrue($actual->get($index)->is($item)));
70+
} else {
71+
PHPUnit::assertEquals($value, Arr::get($this->view->gatherData(), $key));
72+
}
73+
74+
return $this;
75+
}
76+
77+
/**
78+
* Assert that the response view has a given list of bound data.
79+
*
80+
* @param array $bindings
81+
* @return $this
82+
*/
83+
public function assertViewHasAll(array $bindings)
84+
{
85+
foreach ($bindings as $key => $value) {
86+
if (is_int($key)) {
87+
$this->assertViewHas($value);
88+
} else {
89+
$this->assertViewHas($key, $value);
90+
}
91+
}
92+
93+
return $this;
94+
}
95+
96+
/**
97+
* Assert that the response view is missing a piece of bound data.
98+
*
99+
* @param string $key
100+
* @return $this
101+
*/
102+
public function assertViewMissing($key)
103+
{
104+
PHPUnit::assertFalse(Arr::has($this->view->gatherData(), $key));
105+
106+
return $this;
107+
}
108+
40109
/**
41110
* Assert that the given string is contained within the view.
42111
*

0 commit comments

Comments
 (0)