Skip to content

Instantly share code, notes, and snippets.

@hkan
Last active Mar 8, 2019
Embed
What would you like to do?
TestResponse::assertViewHasDeep assertion
<?php
use PHPUnit\Framework\Assert as PHPUnit;
use Illuminate\Support\Arr;
use Illuminate\Foundation\Testing\TestResponse;
use Illuminate\Database\Eloquent\Model;
TestResponse::macro('assertViewHasDeep', function ($key, $value = null) {
$this->ensureResponseHasView();
$data = $this->original->getData();
$keys = explode('.', $key);
foreach ($keys as $key) {
if (Arr::accessible($data)) {
PHPUnit::assertTrue(Arr::exists($data, $key));
} elseif (is_object($data)) {
PHPUnit::assertTrue(isset($data->{$key}));
} else {
PHPUnit::fail('Data does not conform to given key.');
}
$data = data_get($data, $key);
}
if ($value instanceof Closure) {
PHPUnit::assertTrue($value($data));
} elseif ($value instanceof Model) {
PHPUnit::assertTrue($value->is($data));
} else {
PHPUnit::assertEquals($value, $data);
}
return $this;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment