|
2 | 2 |
|
3 | 3 | namespace Illuminate\Testing;
|
4 | 4 |
|
| 5 | +use Closure; |
| 6 | +use Illuminate\Database\Eloquent\Collection; |
| 7 | +use Illuminate\Database\Eloquent\Model; |
| 8 | +use Illuminate\Support\Arr; |
5 | 9 | use Illuminate\Support\Traits\Macroable;
|
6 | 10 | use Illuminate\Testing\Assert as PHPUnit;
|
7 | 11 | use Illuminate\Testing\Constraints\SeeInOrder;
|
@@ -37,6 +41,71 @@ public function __construct(View $view)
|
37 | 41 | $this->rendered = $view->render();
|
38 | 42 | }
|
39 | 43 |
|
| 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 | + |
40 | 109 | /**
|
41 | 110 | * Assert that the given string is contained within the view.
|
42 | 111 | *
|
|
0 commit comments