Migrate to response macros for readability

This commit is contained in:
Marcus Moore 2023-06-22 14:41:56 -07:00
parent 8aae5beaba
commit a35d83d14a
No known key found for this signature in database
11 changed files with 218 additions and 211 deletions

View file

@ -7,13 +7,11 @@ use App\Models\Company;
use App\Models\User;
use Illuminate\Testing\Fluent\AssertableJson;
use Laravel\Passport\Passport;
use Tests\Support\InteractsWithResponses;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
class AssetIndexTest extends TestCase
{
use InteractsWithResponses;
use InteractsWithSettings;
public function testAssetIndexReturnsExpectedAssets()
@ -50,35 +48,35 @@ class AssetIndexTest extends TestCase
$this->settings->disableMultipleFullCompanySupport();
Passport::actingAs($superUser);
$response = $this->getJson(route('api.assets.index'));
$this->assertResponseContainsInRows($response, $assetA, 'asset_tag');
$this->assertResponseContainsInRows($response, $assetB, 'asset_tag');
$this->getJson(route('api.assets.index'))
->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag');
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('api.assets.index'));
$this->assertResponseContainsInRows($response, $assetA, 'asset_tag');
$this->assertResponseContainsInRows($response, $assetB, 'asset_tag');
$this->getJson(route('api.assets.index'))
->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag');
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('api.assets.index'));
$this->assertResponseContainsInRows($response, $assetA, 'asset_tag');
$this->assertResponseContainsInRows($response, $assetB, 'asset_tag');
$this->getJson(route('api.assets.index'))
->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag');
$this->settings->enableMultipleFullCompanySupport();
Passport::actingAs($superUser);
$response = $this->getJson(route('api.assets.index'));
$this->assertResponseContainsInRows($response, $assetA, 'asset_tag');
$this->assertResponseContainsInRows($response, $assetB, 'asset_tag');
$this->getJson(route('api.assets.index'))
->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag');
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('api.assets.index'));
$this->assertResponseContainsInRows($response, $assetA, 'asset_tag');
$this->assertResponseDoesNotContainInRows($response, $assetB, 'asset_tag');
$this->getJson(route('api.assets.index'))
->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseDoesNotContainInRows($assetB, 'asset_tag');
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('api.assets.index'));
$this->assertResponseDoesNotContainInRows($response, $assetA, 'asset_tag');
$this->assertResponseContainsInRows($response, $assetB, 'asset_tag');
$this->getJson(route('api.assets.index'))
->assertResponseDoesNotContainInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag');
}
}

View file

@ -6,13 +6,11 @@ use App\Models\Asset;
use App\Models\Company;
use App\Models\User;
use Laravel\Passport\Passport;
use Tests\Support\InteractsWithResponses;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
class AssetsForSelectListTest extends TestCase
{
use InteractsWithResponses;
use InteractsWithSettings;
public function testAssetsCanBeSearchedForByAssetTag()
@ -45,35 +43,35 @@ class AssetsForSelectListTest extends TestCase
$this->settings->disableMultipleFullCompanySupport();
Passport::actingAs($superUser);
$response = $this->getJson(route('assets.selectlist', ['search' => '000']));
$this->assertResponseContainsInResults($response, $assetA);
$this->assertResponseContainsInResults($response, $assetB);
$this->getJson(route('assets.selectlist', ['search' => '000']))
->assertResponseContainsInResults($assetA)
->assertResponseContainsInResults($assetB);
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('assets.selectlist', ['search' => '000']));
$this->assertResponseContainsInResults($response, $assetA);
$this->assertResponseContainsInResults($response, $assetB);
$this->getJson(route('assets.selectlist', ['search' => '000']))
->assertResponseContainsInResults($assetA)
->assertResponseContainsInResults($assetB);
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('assets.selectlist', ['search' => '000']));
$this->assertResponseContainsInResults($response, $assetA);
$this->assertResponseContainsInResults($response, $assetB);
$this->getJson(route('assets.selectlist', ['search' => '000']))
->assertResponseContainsInResults($assetA)
->assertResponseContainsInResults($assetB);
$this->settings->enableMultipleFullCompanySupport();
Passport::actingAs($superUser);
$response = $this->getJson(route('assets.selectlist', ['search' => '000']));
$this->assertResponseContainsInResults($response, $assetA);
$this->assertResponseContainsInResults($response, $assetB);
$this->getJson(route('assets.selectlist', ['search' => '000']))
->assertResponseContainsInResults($assetA)
->assertResponseContainsInResults($assetB);
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('assets.selectlist', ['search' => '000']));
$this->assertResponseContainsInResults($response, $assetA);
$this->assertResponseDoesNotContainInResults($response, $assetB);
$this->getJson(route('assets.selectlist', ['search' => '000']))
->assertResponseContainsInResults($assetA)
->assertResponseDoesNotContainInResults($assetB);
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('assets.selectlist', ['search' => '000']));
$this->assertResponseDoesNotContainInResults($response, $assetA);
$this->assertResponseContainsInResults($response, $assetB);
$this->getJson(route('assets.selectlist', ['search' => '000']))
->assertResponseDoesNotContainInResults($assetA)
->assertResponseContainsInResults($assetB);
}
}

View file

@ -6,13 +6,11 @@ use App\Models\Asset;
use App\Models\Company;
use App\Models\User;
use Laravel\Passport\Passport;
use Tests\Support\InteractsWithResponses;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
class RequestableAssetsTest extends TestCase
{
use InteractsWithResponses;
use InteractsWithSettings;
public function testViewingRequestableAssetsRequiresCorrectPermission()
@ -27,10 +25,10 @@ class RequestableAssetsTest extends TestCase
$nonRequestableAsset = Asset::factory()->nonrequestable()->create(['asset_tag' => 'non-requestable']);
Passport::actingAs(User::factory()->viewRequestableAssets()->create());
$response = $this->getJson(route('api.assets.requestable'))->assertOk();
$this->assertResponseContainsInRows($response, $requestableAsset, 'asset_tag');
$this->assertResponseDoesNotContainInRows($response, $nonRequestableAsset, 'asset_tag');
$this->getJson(route('api.assets.requestable'))
->assertOk()
->assertResponseContainsInRows($requestableAsset, 'asset_tag')
->assertResponseDoesNotContainInRows($nonRequestableAsset, 'asset_tag');
}
public function testRequestableAssetsAreScopedToCompanyWhenMultipleCompanySupportEnabled()
@ -47,35 +45,35 @@ class RequestableAssetsTest extends TestCase
$this->settings->disableMultipleFullCompanySupport();
Passport::actingAs($superUser);
$response = $this->getJson(route('api.assets.requestable'));
$this->assertResponseContainsInRows($response, $assetA, 'asset_tag');
$this->assertResponseContainsInRows($response, $assetB, 'asset_tag');
$this->getJson(route('api.assets.requestable'))
->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag');
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('api.assets.requestable'));
$this->assertResponseContainsInRows($response, $assetA, 'asset_tag');
$this->assertResponseContainsInRows($response, $assetB, 'asset_tag');
$this->getJson(route('api.assets.requestable'))
->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag');
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('api.assets.requestable'));
$this->assertResponseContainsInRows($response, $assetA, 'asset_tag');
$this->assertResponseContainsInRows($response, $assetB, 'asset_tag');
$this->getJson(route('api.assets.requestable'))
->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag');
$this->settings->enableMultipleFullCompanySupport();
Passport::actingAs($superUser);
$response = $this->getJson(route('api.assets.requestable'));
$this->assertResponseContainsInRows($response, $assetA, 'asset_tag');
$this->assertResponseContainsInRows($response, $assetB, 'asset_tag');
$this->getJson(route('api.assets.requestable'))
->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag');
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('api.assets.requestable'));
$this->assertResponseContainsInRows($response, $assetA, 'asset_tag');
$this->assertResponseDoesNotContainInRows($response, $assetB, 'asset_tag');
$this->getJson(route('api.assets.requestable'))
->assertResponseContainsInRows($assetA, 'asset_tag')
->assertResponseDoesNotContainInRows($assetB, 'asset_tag');
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('api.assets.requestable'));
$this->assertResponseDoesNotContainInRows($response, $assetA, 'asset_tag');
$this->assertResponseContainsInRows($response, $assetB, 'asset_tag');
$this->getJson(route('api.assets.requestable'))
->assertResponseDoesNotContainInRows($assetA, 'asset_tag')
->assertResponseContainsInRows($assetB, 'asset_tag');
}
}

View file

@ -6,13 +6,11 @@ use App\Models\Company;
use App\Models\Component;
use App\Models\User;
use Laravel\Passport\Passport;
use Tests\Support\InteractsWithResponses;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
class ComponentIndexTest extends TestCase
{
use InteractsWithResponses;
use InteractsWithSettings;
public function testComponentIndexAdheresToCompanyScoping()
@ -29,35 +27,35 @@ class ComponentIndexTest extends TestCase
$this->settings->disableMultipleFullCompanySupport();
Passport::actingAs($superUser);
$response = $this->getJson(route('api.components.index'));
$this->assertResponseContainsInRows($response, $componentA);
$this->assertResponseContainsInRows($response, $componentB);
$this->getJson(route('api.components.index'))
->assertResponseContainsInRows($componentA)
->assertResponseContainsInRows($componentB);
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('api.components.index'));
$this->assertResponseContainsInRows($response, $componentA);
$this->assertResponseContainsInRows($response, $componentB);
$this->getJson(route('api.components.index'))
->assertResponseContainsInRows($componentA)
->assertResponseContainsInRows($componentB);
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('api.components.index'));
$this->assertResponseContainsInRows($response, $componentA);
$this->assertResponseContainsInRows($response, $componentB);
$this->getJson(route('api.components.index'))
->assertResponseContainsInRows($componentA)
->assertResponseContainsInRows($componentB);
$this->settings->enableMultipleFullCompanySupport();
Passport::actingAs($superUser);
$response = $this->getJson(route('api.components.index'));
$this->assertResponseContainsInRows($response, $componentA);
$this->assertResponseContainsInRows($response, $componentB);
$this->getJson(route('api.components.index'))
->assertResponseContainsInRows($componentA)
->assertResponseContainsInRows($componentB);
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('api.components.index'));
$this->assertResponseContainsInRows($response, $componentA);
$this->assertResponseDoesNotContainInRows($response, $componentB);
$this->getJson(route('api.components.index'))
->assertResponseContainsInRows($componentA)
->assertResponseDoesNotContainInRows($componentB);
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('api.components.index'));
$this->assertResponseDoesNotContainInRows($response, $componentA);
$this->assertResponseContainsInRows($response, $componentB);
$this->getJson(route('api.components.index'))
->assertResponseDoesNotContainInRows($componentA)
->assertResponseContainsInRows($componentB);
}
}

View file

@ -6,13 +6,11 @@ use App\Models\Company;
use App\Models\Consumable;
use App\Models\User;
use Laravel\Passport\Passport;
use Tests\Support\InteractsWithResponses;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
class ConsumablesIndexTest extends TestCase
{
use InteractsWithResponses;
use InteractsWithSettings;
public function testConsumableIndexAdheresToCompanyScoping()
@ -29,35 +27,35 @@ class ConsumablesIndexTest extends TestCase
$this->settings->disableMultipleFullCompanySupport();
Passport::actingAs($superUser);
$response = $this->getJson(route('api.consumables.index'));
$this->assertResponseContainsInRows($response, $consumableA);
$this->assertResponseContainsInRows($response, $consumableB);
$this->getJson(route('api.consumables.index'))
->assertResponseContainsInRows($consumableA)
->assertResponseContainsInRows($consumableB);
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('api.consumables.index'));
$this->assertResponseContainsInRows($response, $consumableA);
$this->assertResponseContainsInRows($response, $consumableB);
$this->getJson(route('api.consumables.index'))
->assertResponseContainsInRows($consumableA)
->assertResponseContainsInRows($consumableB);
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('api.consumables.index'));
$this->assertResponseContainsInRows($response, $consumableA);
$this->assertResponseContainsInRows($response, $consumableB);
$this->getJson(route('api.consumables.index'))
->assertResponseContainsInRows($consumableA)
->assertResponseContainsInRows($consumableB);
$this->settings->enableMultipleFullCompanySupport();
Passport::actingAs($superUser);
$response = $this->getJson(route('api.consumables.index'));
$this->assertResponseContainsInRows($response, $consumableA);
$this->assertResponseContainsInRows($response, $consumableB);
$this->getJson(route('api.consumables.index'))
->assertResponseContainsInRows($consumableA)
->assertResponseContainsInRows($consumableB);
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('api.consumables.index'));
$this->assertResponseContainsInRows($response, $consumableA);
$this->assertResponseDoesNotContainInRows($response, $consumableB);
$this->getJson(route('api.consumables.index'))
->assertResponseContainsInRows($consumableA)
->assertResponseDoesNotContainInRows($consumableB);
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('api.consumables.index'));
$this->assertResponseDoesNotContainInRows($response, $consumableA);
$this->assertResponseContainsInRows($response, $consumableB);
$this->getJson(route('api.consumables.index'))
->assertResponseDoesNotContainInRows($consumableA)
->assertResponseContainsInRows($consumableB);
}
}

View file

@ -6,13 +6,11 @@ use App\Models\Company;
use App\Models\Department;
use App\Models\User;
use Laravel\Passport\Passport;
use Tests\Support\InteractsWithResponses;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
class DepartmentsIndexTest extends TestCase
{
use InteractsWithResponses;
use InteractsWithSettings;
public function testDepartmentsIndexAdheresToCompanyScoping()
@ -33,35 +31,35 @@ class DepartmentsIndexTest extends TestCase
$this->settings->disableMultipleFullCompanySupport();
Passport::actingAs($superUser);
$response = $this->getJson(route('api.departments.index'));
$this->assertResponseContainsInRows($response, $departmentA);
$this->assertResponseContainsInRows($response, $departmentB);
$this->getJson(route('api.departments.index'))
->assertResponseContainsInRows($departmentA)
->assertResponseContainsInRows($departmentB);
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('api.departments.index'));
$this->assertResponseContainsInRows($response, $departmentA);
$this->assertResponseContainsInRows($response, $departmentB);
$this->getJson(route('api.departments.index'))
->assertResponseContainsInRows($departmentA)
->assertResponseContainsInRows($departmentB);
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('api.departments.index'));
$this->assertResponseContainsInRows($response, $departmentA);
$this->assertResponseContainsInRows($response, $departmentB);
$this->getJson(route('api.departments.index'))
->assertResponseContainsInRows($departmentA)
->assertResponseContainsInRows($departmentB);
$this->settings->enableMultipleFullCompanySupport();
Passport::actingAs($superUser);
$response = $this->getJson(route('api.departments.index'));
$this->assertResponseContainsInRows($response, $departmentA);
$this->assertResponseContainsInRows($response, $departmentB);
$this->getJson(route('api.departments.index'))
->assertResponseContainsInRows($departmentA)
->assertResponseContainsInRows($departmentB);
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('api.departments.index'));
$this->assertResponseContainsInRows($response, $departmentA);
$this->assertResponseDoesNotContainInRows($response, $departmentB);
$this->getJson(route('api.departments.index'))
->assertResponseContainsInRows($departmentA)
->assertResponseDoesNotContainInRows($departmentB);
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('api.departments.index'));
$this->assertResponseDoesNotContainInRows($response, $departmentA);
$this->assertResponseContainsInRows($response, $departmentB);
$this->getJson(route('api.departments.index'))
->assertResponseDoesNotContainInRows($departmentA)
->assertResponseContainsInRows($departmentB);
}
}

View file

@ -6,13 +6,11 @@ use App\Models\Company;
use App\Models\License;
use App\Models\User;
use Laravel\Passport\Passport;
use Tests\Support\InteractsWithResponses;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
class LicensesIndexTest extends TestCase
{
use InteractsWithResponses;
use InteractsWithSettings;
public function testLicensesIndexAdheresToCompanyScoping()
@ -29,35 +27,35 @@ class LicensesIndexTest extends TestCase
$this->settings->disableMultipleFullCompanySupport();
Passport::actingAs($superUser);
$response = $this->getJson(route('api.licenses.index'));
$this->assertResponseContainsInRows($response, $licenseA);
$this->assertResponseContainsInRows($response, $licenseB);
$this->getJson(route('api.licenses.index'))
->assertResponseContainsInRows($licenseA)
->assertResponseContainsInRows($licenseB);
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('api.licenses.index'));
$this->assertResponseContainsInRows($response, $licenseA);
$this->assertResponseContainsInRows($response, $licenseB);
$this->getJson(route('api.licenses.index'))
->assertResponseContainsInRows($licenseA)
->assertResponseContainsInRows($licenseB);
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('api.licenses.index'));
$this->assertResponseContainsInRows($response, $licenseA);
$this->assertResponseContainsInRows($response, $licenseB);
$this->getJson(route('api.licenses.index'))
->assertResponseContainsInRows($licenseA)
->assertResponseContainsInRows($licenseB);
$this->settings->enableMultipleFullCompanySupport();
Passport::actingAs($superUser);
$response = $this->getJson(route('api.licenses.index'));
$this->assertResponseContainsInRows($response, $licenseA);
$this->assertResponseContainsInRows($response, $licenseB);
$this->getJson(route('api.licenses.index'))
->assertResponseContainsInRows($licenseA)
->assertResponseContainsInRows($licenseB);
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('api.licenses.index'));
$this->assertResponseContainsInRows($response, $licenseA);
$this->assertResponseDoesNotContainInRows($response, $licenseB);
$this->getJson(route('api.licenses.index'))
->assertResponseContainsInRows($licenseA)
->assertResponseDoesNotContainInRows($licenseB);
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('api.licenses.index'));
$this->assertResponseDoesNotContainInRows($response, $licenseA);
$this->assertResponseContainsInRows($response, $licenseB);
$this->getJson(route('api.licenses.index'))
->assertResponseDoesNotContainInRows($licenseA)
->assertResponseContainsInRows($licenseB);
}
}

View file

@ -5,13 +5,11 @@ namespace Tests\Feature\Api\Users;
use App\Models\Company;
use App\Models\User;
use Laravel\Passport\Passport;
use Tests\Support\InteractsWithResponses;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
class UsersIndexTest extends TestCase
{
use InteractsWithResponses;
use InteractsWithSettings;
public function testUsersIndexAdheresToCompanyScoping()
@ -32,35 +30,35 @@ class UsersIndexTest extends TestCase
$this->settings->disableMultipleFullCompanySupport();
Passport::actingAs($superUser);
$response = $this->getJson(route('api.users.index'));
$this->assertResponseContainsInRows($response, $userA, 'first_name');
$this->assertResponseContainsInRows($response, $userB, 'first_name');
$this->getJson(route('api.users.index'))
->assertResponseContainsInRows($userA, 'first_name')
->assertResponseContainsInRows($userB, 'first_name');
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('api.users.index'));
$this->assertResponseContainsInRows($response, $userA, 'first_name');
$this->assertResponseContainsInRows($response, $userB, 'first_name');
$this->getJson(route('api.users.index'))
->assertResponseContainsInRows($userA, 'first_name')
->assertResponseContainsInRows($userB, 'first_name');
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('api.users.index'));
$this->assertResponseContainsInRows($response, $userA, 'first_name');
$this->assertResponseContainsInRows($response, $userB, 'first_name');
$this->getJson(route('api.users.index'))
->assertResponseContainsInRows($userA, 'first_name')
->assertResponseContainsInRows($userB, 'first_name');
$this->settings->enableMultipleFullCompanySupport();
Passport::actingAs($superUser);
$response = $this->getJson(route('api.users.index'));
$this->assertResponseContainsInRows($response, $userA, 'first_name');
$this->assertResponseContainsInRows($response, $userB, 'first_name');
$this->getJson(route('api.users.index'))
->assertResponseContainsInRows($userA, 'first_name')
->assertResponseContainsInRows($userB, 'first_name');
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('api.users.index'));
$this->assertResponseContainsInRows($response, $userA, 'first_name');
$this->assertResponseDoesNotContainInRows($response, $userB, 'first_name');
$this->getJson(route('api.users.index'))
->assertResponseContainsInRows($userA, 'first_name')
->assertResponseDoesNotContainInRows($userB, 'first_name');
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('api.users.index'));
$this->assertResponseDoesNotContainInRows($response, $userA, 'first_name');
$this->assertResponseContainsInRows($response, $userB, 'first_name');
$this->getJson(route('api.users.index'))
->assertResponseDoesNotContainInRows($userA, 'first_name')
->assertResponseContainsInRows($userB, 'first_name');
}
}

View file

@ -0,0 +1,66 @@
<?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);
Assert::assertTrue(collect($this['rows'])->pluck($property)->contains($model->{$property}));
return $this;
}
);
TestResponse::macro(
'assertResponseDoesNotContainInRows',
function (Model $model, string $property = 'name') use ($guardAgainstNullProperty) {
$guardAgainstNullProperty($model, $property);
Assert::assertFalse(collect($this['rows'])->pluck($property)->contains($model->{$property}));
return $this;
}
);
TestResponse::macro(
'assertResponseContainsInResults',
function (Model $model, string $property = 'id') use ($guardAgainstNullProperty) {
$guardAgainstNullProperty($model, $property);
Assert::assertTrue(collect($this->json('results'))->pluck('id')->contains($model->{$property}));
return $this;
}
);
TestResponse::macro(
'assertResponseDoesNotContainInResults',
function (Model $model, string $property = 'id') use ($guardAgainstNullProperty) {
$guardAgainstNullProperty($model, $property);
Assert::assertFalse(collect($this->json('results'))->pluck('id')->contains($model->{$property}));
return $this;
}
);
}
}

View file

@ -1,47 +0,0 @@
<?php
namespace Tests\Support;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Testing\TestResponse;
use RuntimeException;
trait InteractsWithResponses
{
protected function assertResponseContainsInRows(TestResponse $response, Model $model, string $property = 'name')
{
$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}));
}
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
{
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."
);
}
}
}

View file

@ -5,11 +5,13 @@ namespace Tests;
use App\Http\Middleware\SecurityHeaders;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Tests\Support\CustomTestMacros;
use Tests\Support\InteractsWithSettings;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
use CustomTestMacros;
use LazilyRefreshDatabase;
private array $globallyDisabledMiddleware = [
@ -25,5 +27,7 @@ abstract class TestCase extends BaseTestCase
if (collect(class_uses_recursive($this))->contains(InteractsWithSettings::class)) {
$this->initializeSettings();
}
$this->registerCustomMacros();
}
}