From 5b9f4c10e4db2c5d5bd5fc80b0b70f9359752ae2 Mon Sep 17 00:00:00 2001 From: James LaChance Date: Tue, 9 Oct 2018 09:01:37 -0400 Subject: [PATCH] Check for model in assertViewHas (#26012) --- .../Foundation/Testing/TestResponse.php | 3 +++ .../Foundation/FoundationTestResponseTest.php | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index 9969574e2141..dfe9e9f031dd 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -7,6 +7,7 @@ use Illuminate\Support\Str; use Illuminate\Support\Carbon; use Illuminate\Contracts\View\View; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Traits\Macroable; use PHPUnit\Framework\Assert as PHPUnit; use Symfony\Component\HttpFoundation\StreamedResponse; @@ -743,6 +744,8 @@ public function assertViewHas($key, $value = null) PHPUnit::assertArrayHasKey($key, $this->original->getData()); } elseif ($value instanceof Closure) { PHPUnit::assertTrue($value($this->original->$key)); + } elseif ($value instanceof Model) { + PHPUnit::assertTrue($value->is($this->original->$key)); } else { PHPUnit::assertEquals($value, $this->original->$key); } diff --git a/tests/Foundation/FoundationTestResponseTest.php b/tests/Foundation/FoundationTestResponseTest.php index 05abc1692291..92e7407da258 100644 --- a/tests/Foundation/FoundationTestResponseTest.php +++ b/tests/Foundation/FoundationTestResponseTest.php @@ -8,6 +8,7 @@ use PHPUnit\Framework\TestCase; use Illuminate\Contracts\View\View; use Illuminate\Filesystem\Filesystem; +use Illuminate\Database\Eloquent\Model; use PHPUnit\Framework\AssertionFailedError; use Illuminate\Foundation\Testing\TestResponse; use Symfony\Component\HttpFoundation\BinaryFileResponse; @@ -35,6 +36,25 @@ public function testAssertViewHas() $response->assertViewHas('foo'); } + public function testAssertViewHasModel() + { + $model = new class extends Model { + public function is($model) + { + return $this == $model; + } + }; + + $response = $this->makeMockResponse([ + 'render' => 'hello world', + 'getData' => ['foo' => $model], + ]); + + $response->original->foo = $model; + + $response->assertViewHas('foo', $model); + } + public function testAssertSeeInOrder() { $response = $this->makeMockResponse([