Skip to content

Commit

Permalink
Check for model in assertViewHas (#26012)
Browse files Browse the repository at this point in the history
  • Loading branch information
FatBoyXPC authored and taylorotwell committed Oct 9, 2018
1 parent 4a09215 commit 5b9f4c1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Illuminate/Foundation/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/Foundation/FoundationTestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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([
Expand Down

0 comments on commit 5b9f4c1

Please sign in to comment.