snipe-it/tests/Support/InteractsWithResponses.php

48 lines
1.6 KiB
PHP
Raw Normal View History

2023-06-07 16:16:30 -07:00
<?php
namespace Tests\Support;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Testing\TestResponse;
use RuntimeException;
2023-06-07 16:16:30 -07:00
trait InteractsWithResponses
{
protected function assertResponseContainsInRows(TestResponse $response, Model $model, string $property = 'name')
2023-06-07 16:16:30 -07:00
{
$this->guardAgainstNullProperty($model, $property);
$this->assertTrue(collect($response['rows'])->pluck($property)->contains($model->{$property}));
}
protected function assertResponseDoesNotContainInRows(TestResponse $response, Model $model, string $property = 'name')
{
$this->guardAgainstNullProperty($model, $property);
$this->assertFalse(collect($response['rows'])->pluck($property)->contains($model->{$property}));
2023-06-07 16:16:30 -07:00
}
protected function assertResponseContainsInResults(TestResponse $response, Model $model, string $property = 'id')
{
$this->guardAgainstNullProperty($model, $property);
$this->assertTrue(collect($response->json('results'))->pluck('id')->contains($model->{$property}));
}
protected function assertResponseDoesNotContainInResults(TestResponse $response, Model $model, string $property = 'id')
{
$this->guardAgainstNullProperty($model, $property);
$this->assertFalse(collect($response->json('results'))->pluck('id')->contains($model->{$property}));
}
private function guardAgainstNullProperty(Model $model, string $property): void
2023-06-07 16:16:30 -07:00
{
if (is_null($model->{$property})) {
throw new RuntimeException(
"The property ({$property}) is null on the model which isn't helpful for comparison."
);
}
2023-06-07 16:16:30 -07:00
}
}