2023-06-22 14:41:56 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Support;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Testing\TestResponse;
|
|
|
|
use PHPUnit\Framework\Assert;
|
|
|
|
use RuntimeException;
|
|
|
|
|
|
|
|
trait CustomTestMacros
|
|
|
|
{
|
|
|
|
protected function registerCustomMacros()
|
|
|
|
{
|
|
|
|
$guardAgainstNullProperty = function (Model $model, string $property) {
|
|
|
|
if (is_null($model->{$property})) {
|
|
|
|
throw new RuntimeException(
|
|
|
|
"The property ({$property}) either does not exist or is null on the model which isn't helpful for comparison."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TestResponse::macro(
|
|
|
|
'assertResponseContainsInRows',
|
|
|
|
function (Model $model, string $property = 'name') use ($guardAgainstNullProperty) {
|
|
|
|
$guardAgainstNullProperty($model, $property);
|
|
|
|
|
2023-08-09 17:09:31 -07:00
|
|
|
Assert::assertTrue(
|
|
|
|
collect($this['rows'])->pluck($property)->contains(e($model->{$property})),
|
|
|
|
"Response did not contain the expected value: {$model->{$property}}"
|
|
|
|
);
|
2023-06-22 14:41:56 -07:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
TestResponse::macro(
|
|
|
|
'assertResponseDoesNotContainInRows',
|
|
|
|
function (Model $model, string $property = 'name') use ($guardAgainstNullProperty) {
|
|
|
|
$guardAgainstNullProperty($model, $property);
|
|
|
|
|
2023-08-09 17:09:31 -07:00
|
|
|
Assert::assertFalse(
|
|
|
|
collect($this['rows'])->pluck($property)->contains(e($model->{$property})),
|
|
|
|
"Response contained unexpected value: {$model->{$property}}"
|
|
|
|
);
|
2023-06-22 14:41:56 -07:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
TestResponse::macro(
|
|
|
|
'assertResponseContainsInResults',
|
|
|
|
function (Model $model, string $property = 'id') use ($guardAgainstNullProperty) {
|
|
|
|
$guardAgainstNullProperty($model, $property);
|
|
|
|
|
2023-08-09 17:09:31 -07:00
|
|
|
Assert::assertTrue(
|
|
|
|
collect($this->json('results'))->pluck('id')->contains(e($model->{$property})),
|
|
|
|
"Response did not contain the expected value: {$model->{$property}}"
|
|
|
|
);
|
2023-06-22 14:41:56 -07:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
TestResponse::macro(
|
|
|
|
'assertResponseDoesNotContainInResults',
|
|
|
|
function (Model $model, string $property = 'id') use ($guardAgainstNullProperty) {
|
|
|
|
$guardAgainstNullProperty($model, $property);
|
|
|
|
|
2023-08-09 17:09:31 -07:00
|
|
|
Assert::assertFalse(
|
|
|
|
collect($this->json('results'))->pluck('id')->contains(e($model->{$property})),
|
|
|
|
"Response contained unexpected value: {$model->{$property}}"
|
|
|
|
);
|
2023-06-22 14:41:56 -07:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
);
|
2023-11-28 13:17:46 -08:00
|
|
|
|
|
|
|
TestResponse::macro(
|
|
|
|
'assertStatusMessageIs',
|
|
|
|
function (string $message) {
|
|
|
|
Assert::assertEquals(
|
|
|
|
$message,
|
|
|
|
$this['status'],
|
|
|
|
"Response status message was not {$message}"
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
);
|
2024-04-15 10:46:11 -07:00
|
|
|
|
|
|
|
TestResponse::macro(
|
|
|
|
'assertMessagesAre',
|
|
|
|
function (string $message) {
|
|
|
|
Assert::assertEquals(
|
|
|
|
$message,
|
|
|
|
$this['messages'],
|
|
|
|
"Response messages was not {$message}"
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
);
|
2024-09-18 11:47:59 -07:00
|
|
|
|
|
|
|
TestResponse::macro(
|
|
|
|
'assertMessagesContains',
|
|
|
|
function (array|string $keys) {
|
|
|
|
Assert::assertArrayHasKey('messages', $this, 'Response did not contain any messages');
|
|
|
|
|
|
|
|
if (is_string($keys)) {
|
|
|
|
$keys = [$keys];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
Assert::assertArrayHasKey(
|
|
|
|
$key,
|
|
|
|
$this['messages'],
|
|
|
|
"Response messages did not contain the key: {$key}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
);
|
2023-06-22 14:41:56 -07:00
|
|
|
}
|
|
|
|
}
|