From a35d83d14a07d3bc1e7044be36c2e4af25f32c79 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 22 Jun 2023 14:41:56 -0700 Subject: [PATCH] Migrate to response macros for readability --- tests/Feature/Api/Assets/AssetIndexTest.php | 38 +++++------ .../Api/Assets/AssetsForSelectListTest.php | 38 +++++------ .../Api/Assets/RequestableAssetsTest.php | 46 +++++++------ .../Api/Components/ComponentIndexTest.php | 38 +++++------ .../Api/Consumables/ConsumablesIndexTest.php | 38 +++++------ .../Api/Departments/DepartmentsIndexTest.php | 38 +++++------ .../Api/Licenses/LicensesIndexTest.php | 38 +++++------ tests/Feature/Api/Users/UsersIndexTest.php | 38 +++++------ tests/Support/CustomTestMacros.php | 66 +++++++++++++++++++ tests/Support/InteractsWithResponses.php | 47 ------------- tests/TestCase.php | 4 ++ 11 files changed, 218 insertions(+), 211 deletions(-) create mode 100644 tests/Support/CustomTestMacros.php delete mode 100644 tests/Support/InteractsWithResponses.php diff --git a/tests/Feature/Api/Assets/AssetIndexTest.php b/tests/Feature/Api/Assets/AssetIndexTest.php index 9ca415c74..0fe3a4d30 100644 --- a/tests/Feature/Api/Assets/AssetIndexTest.php +++ b/tests/Feature/Api/Assets/AssetIndexTest.php @@ -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'); } } diff --git a/tests/Feature/Api/Assets/AssetsForSelectListTest.php b/tests/Feature/Api/Assets/AssetsForSelectListTest.php index fbc5a1e33..a3210b988 100644 --- a/tests/Feature/Api/Assets/AssetsForSelectListTest.php +++ b/tests/Feature/Api/Assets/AssetsForSelectListTest.php @@ -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); } } diff --git a/tests/Feature/Api/Assets/RequestableAssetsTest.php b/tests/Feature/Api/Assets/RequestableAssetsTest.php index 7c9b4e120..7ecddeaa0 100644 --- a/tests/Feature/Api/Assets/RequestableAssetsTest.php +++ b/tests/Feature/Api/Assets/RequestableAssetsTest.php @@ -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'); } } diff --git a/tests/Feature/Api/Components/ComponentIndexTest.php b/tests/Feature/Api/Components/ComponentIndexTest.php index 845ce29b9..276f22e6f 100644 --- a/tests/Feature/Api/Components/ComponentIndexTest.php +++ b/tests/Feature/Api/Components/ComponentIndexTest.php @@ -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); } } diff --git a/tests/Feature/Api/Consumables/ConsumablesIndexTest.php b/tests/Feature/Api/Consumables/ConsumablesIndexTest.php index 11a4d79af..6e6e809c6 100644 --- a/tests/Feature/Api/Consumables/ConsumablesIndexTest.php +++ b/tests/Feature/Api/Consumables/ConsumablesIndexTest.php @@ -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); } } diff --git a/tests/Feature/Api/Departments/DepartmentsIndexTest.php b/tests/Feature/Api/Departments/DepartmentsIndexTest.php index 7e813ad9c..9570e8530 100644 --- a/tests/Feature/Api/Departments/DepartmentsIndexTest.php +++ b/tests/Feature/Api/Departments/DepartmentsIndexTest.php @@ -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); } } diff --git a/tests/Feature/Api/Licenses/LicensesIndexTest.php b/tests/Feature/Api/Licenses/LicensesIndexTest.php index 404eb3123..fd8523992 100644 --- a/tests/Feature/Api/Licenses/LicensesIndexTest.php +++ b/tests/Feature/Api/Licenses/LicensesIndexTest.php @@ -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); } } diff --git a/tests/Feature/Api/Users/UsersIndexTest.php b/tests/Feature/Api/Users/UsersIndexTest.php index b48da4483..ed91976cc 100644 --- a/tests/Feature/Api/Users/UsersIndexTest.php +++ b/tests/Feature/Api/Users/UsersIndexTest.php @@ -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'); } } diff --git a/tests/Support/CustomTestMacros.php b/tests/Support/CustomTestMacros.php new file mode 100644 index 000000000..0a30d7c24 --- /dev/null +++ b/tests/Support/CustomTestMacros.php @@ -0,0 +1,66 @@ +{$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; + } + ); + } +} diff --git a/tests/Support/InteractsWithResponses.php b/tests/Support/InteractsWithResponses.php deleted file mode 100644 index 03faf0ef2..000000000 --- a/tests/Support/InteractsWithResponses.php +++ /dev/null @@ -1,47 +0,0 @@ -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." - ); - } - } -} diff --git a/tests/TestCase.php b/tests/TestCase.php index 28051c7c7..4a5c3da69 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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(); } }