From 65e8e4e1632cc009ff54b13b1d359f5a50595ca3 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 21 Jun 2023 16:18:09 -0700 Subject: [PATCH] Guard against attempting to use invalid property --- tests/Support/InteractsWithResponses.php | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/tests/Support/InteractsWithResponses.php b/tests/Support/InteractsWithResponses.php index 3206aaea9d..9a1965eefe 100644 --- a/tests/Support/InteractsWithResponses.php +++ b/tests/Support/InteractsWithResponses.php @@ -4,16 +4,30 @@ namespace Tests\Support; use Illuminate\Database\Eloquent\Model; use Illuminate\Testing\TestResponse; +use RuntimeException; 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." + ); + } } }