Guard against attempting to use invalid property
This commit is contained in:
parent
7672861b96
commit
65e8e4e163
1 changed files with 18 additions and 4 deletions
|
@ -4,16 +4,30 @@ namespace Tests\Support;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Testing\TestResponse;
|
use Illuminate\Testing\TestResponse;
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
trait InteractsWithResponses
|
trait InteractsWithResponses
|
||||||
{
|
{
|
||||||
private function assertResponseContainsInRows(TestResponse $response, Model $model, string $field = 'name')
|
protected function assertResponseContainsInRows(TestResponse $response, Model $model, string $property = 'name')
|
||||||
{
|
{
|
||||||
$this->assertTrue(collect($response['rows'])->pluck($field)->contains($model->{$field}));
|
$this->guardAgainstNullProperty($model, $property);
|
||||||
|
|
||||||
|
$this->assertTrue(collect($response['rows'])->pluck($property)->contains($model->{$property}));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function assertResponseDoesNotContainInRows(TestResponse $response, Model $model, string $field = 'name')
|
protected function assertResponseDoesNotContainInRows(TestResponse $response, Model $model, string $property = 'name')
|
||||||
{
|
{
|
||||||
$this->assertFalse(collect($response['rows'])->pluck($field)->contains($model->{$field}));
|
$this->guardAgainstNullProperty($model, $property);
|
||||||
|
|
||||||
|
$this->assertFalse(collect($response['rows'])->pluck($property)->contains($model->{$property}));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function guardAgainstNullProperty(Model $model, string $property): void
|
||||||
|
{
|
||||||
|
if (is_null($model->{$property})) {
|
||||||
|
throw new RuntimeException(
|
||||||
|
"The property ({$property}) is null on the model which isn't helpful for comparison."
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue