Last active
March 8, 2019 21:06
-
-
Save hkan/fa9f992b5fc09b677b4a4e3066ac1879 to your computer and use it in GitHub Desktop.
TestResponse::assertViewHasDeep assertion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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