Skip to content

A free open source IT asset/license management system

Extract and use helper methods

Marcus Mooreabaf59c9902a
diff --git a/tests/Feature/Api/Components/ComponentIndexTest.php b/tests/Feature/Api/Components/ComponentIndexTest.php
index e459e9f8f6..978d4be10b 100644
--- a/tests/Feature/Api/Components/ComponentIndexTest.php
+++ b/tests/Feature/Api/Components/ComponentIndexTest.php
@@ -7,11 +7,13 @@ use App\Models\Component;
use App\Models\User;
use Illuminate\Testing\TestResponse;
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,49 +31,39 @@ class ComponentIndexTest extends TestCase
Passport::actingAs($superUser);
$response = $this->sendRequest();
- $this->assertResponseContains($response, $componentA);
- $this->assertResponseContains($response, $componentB);
+ $this->assertResponseContainsInRows($response, $componentA);
+ $this->assertResponseContainsInRows($response, $componentB);
Passport::actingAs($userInCompanyA);
$response = $this->sendRequest();
- $this->assertResponseContains($response, $componentA);
- $this->assertResponseContains($response, $componentB);
+ $this->assertResponseContainsInRows($response, $componentA);
+ $this->assertResponseContainsInRows($response, $componentB);
Passport::actingAs($userInCompanyB);
$response = $this->sendRequest();
- $this->assertResponseContains($response, $componentA);
- $this->assertResponseContains($response, $componentB);
+ $this->assertResponseContainsInRows($response, $componentA);
+ $this->assertResponseContainsInRows($response, $componentB);
$this->settings->enableMultipleFullCompanySupport();
Passport::actingAs($superUser);
$response = $this->sendRequest();
- $this->assertResponseContains($response, $componentA);
- $this->assertResponseContains($response, $componentB);
+ $this->assertResponseContainsInRows($response, $componentA);
+ $this->assertResponseContainsInRows($response, $componentB);
Passport::actingAs($userInCompanyA);
$response = $this->sendRequest();
- $this->assertResponseContains($response, $componentA);
- $this->assertResponseDoesNotContain($response, $componentB);
+ $this->assertResponseContainsInRows($response, $componentA);
+ $this->assertResponseDoesNotContainInRows($response, $componentB);
Passport::actingAs($userInCompanyB);
$response = $this->sendRequest();
- $this->assertResponseDoesNotContain($response, $componentA);
- $this->assertResponseContains($response, $componentB);
+ $this->assertResponseDoesNotContainInRows($response, $componentA);
+ $this->assertResponseContainsInRows($response, $componentB);
}
private function sendRequest(): TestResponse
{
return $this->getJson(route('api.components.index'));
}
-
- private function assertResponseContains(TestResponse $response, Component $component)
- {
- $this->assertTrue(collect($response['rows'])->pluck('name')->contains($component->name));
- }
-
- private function assertResponseDoesNotContain(TestResponse $response, Component $component)
- {
- $this->assertFalse(collect($response['rows'])->pluck('name')->contains($component->name));
- }
}
diff --git a/tests/Feature/Api/Consumables/ConsumablesIndexTest.php b/tests/Feature/Api/Consumables/ConsumablesIndexTest.php
index 1d1a627c65..11a4d79afa 100644
--- a/tests/Feature/Api/Consumables/ConsumablesIndexTest.php
+++ b/tests/Feature/Api/Consumables/ConsumablesIndexTest.php
@@ -5,13 +5,14 @@ namespace Tests\Feature\Api\Consumables;
use App\Models\Company;
use App\Models\Consumable;
use App\Models\User;
-use Illuminate\Testing\TestResponse;
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,44 +30,34 @@ class ConsumablesIndexTest extends TestCase
Passport::actingAs($superUser);
$response = $this->getJson(route('api.consumables.index'));
- $this->assertResponseContains($response, $consumableA);
- $this->assertResponseContains($response, $consumableB);
+ $this->assertResponseContainsInRows($response, $consumableA);
+ $this->assertResponseContainsInRows($response, $consumableB);
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('api.consumables.index'));
- $this->assertResponseContains($response, $consumableA);
- $this->assertResponseContains($response, $consumableB);
+ $this->assertResponseContainsInRows($response, $consumableA);
+ $this->assertResponseContainsInRows($response, $consumableB);
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('api.consumables.index'));
- $this->assertResponseContains($response, $consumableA);
- $this->assertResponseContains($response, $consumableB);
+ $this->assertResponseContainsInRows($response, $consumableA);
+ $this->assertResponseContainsInRows($response, $consumableB);
$this->settings->enableMultipleFullCompanySupport();
Passport::actingAs($superUser);
$response = $this->getJson(route('api.consumables.index'));
- $this->assertResponseContains($response, $consumableA);
- $this->assertResponseContains($response, $consumableB);
+ $this->assertResponseContainsInRows($response, $consumableA);
+ $this->assertResponseContainsInRows($response, $consumableB);
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('api.consumables.index'));
- $this->assertResponseContains($response, $consumableA);
- $this->assertResponseDoesNotContain($response, $consumableB);
+ $this->assertResponseContainsInRows($response, $consumableA);
+ $this->assertResponseDoesNotContainInRows($response, $consumableB);
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('api.consumables.index'));
- $this->assertResponseDoesNotContain($response, $consumableA);
- $this->assertResponseContains($response, $consumableB);
- }
-
- private function assertResponseContains(TestResponse $response, Consumable $consumable)
- {
- $this->assertTrue(collect($response['rows'])->pluck('name')->contains($consumable->name));
- }
-
- private function assertResponseDoesNotContain(TestResponse $response, Consumable $consumable)
- {
- $this->assertFalse(collect($response['rows'])->pluck('name')->contains($consumable->name));
+ $this->assertResponseDoesNotContainInRows($response, $consumableA);
+ $this->assertResponseContainsInRows($response, $consumableB);
}
}
diff --git a/tests/Feature/Api/Departments/DepartmentsIndexTest.php b/tests/Feature/Api/Departments/DepartmentsIndexTest.php
index a79a4f89ef..d791c79e93 100644
--- a/tests/Feature/Api/Departments/DepartmentsIndexTest.php
+++ b/tests/Feature/Api/Departments/DepartmentsIndexTest.php
@@ -5,13 +5,14 @@ namespace Tests\Feature\Api\Departments;
use App\Models\Company;
use App\Models\Department;
use App\Models\User;
-use Illuminate\Testing\TestResponse;
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()
@@ -31,44 +32,34 @@ class DepartmentsIndexTest extends TestCase
Passport::actingAs($superUser);
$response = $this->getJson(route('api.departments.index'));
- $this->assertResponseContains($response, $departmentA);
- $this->assertResponseContains($response, $departmentB);
+ $this->assertResponseContainsInRows($response, $departmentA);
+ $this->assertResponseContainsInRows($response, $departmentB);
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('api.departments.index'));
- $this->assertResponseContains($response, $departmentA);
- $this->assertResponseContains($response, $departmentB);
+ $this->assertResponseContainsInRows($response, $departmentA);
+ $this->assertResponseContainsInRows($response, $departmentB);
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('api.departments.index'));
- $this->assertResponseContains($response, $departmentA);
- $this->assertResponseContains($response, $departmentB);
+ $this->assertResponseContainsInRows($response, $departmentA);
+ $this->assertResponseContainsInRows($response, $departmentB);
$this->settings->enableMultipleFullCompanySupport();
Passport::actingAs($superUser);
$response = $this->getJson(route('api.departments.index'));
- $this->assertResponseContains($response, $departmentA);
- $this->assertResponseContains($response, $departmentB);
+ $this->assertResponseContainsInRows($response, $departmentA);
+ $this->assertResponseContainsInRows($response, $departmentB);
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('api.departments.index'));
- $this->assertResponseContains($response, $departmentA);
- $this->assertResponseDoesNotContain($response, $departmentB);
+ $this->assertResponseContainsInRows($response, $departmentA);
+ $this->assertResponseDoesNotContainInRows($response, $departmentB);
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('api.departments.index'));
- $this->assertResponseDoesNotContain($response, $departmentA);
- $this->assertResponseContains($response, $departmentB);
- }
-
- private function assertResponseContains(TestResponse $response, Department $department)
- {
- $this->assertTrue(collect($response['rows'])->pluck('name')->contains($department->name));
- }
-
- private function assertResponseDoesNotContain(TestResponse $response, Department $department)
- {
- $this->assertFalse(collect($response['rows'])->pluck('name')->contains($department->name));
+ $this->assertResponseDoesNotContainInRows($response, $departmentA);
+ $this->assertResponseContainsInRows($response, $departmentB);
}
}
diff --git a/tests/Feature/Api/Licenses/LicensesIndexTest.php b/tests/Feature/Api/Licenses/LicensesIndexTest.php
index 4deb6bbcbe..404eb31238 100644
--- a/tests/Feature/Api/Licenses/LicensesIndexTest.php
+++ b/tests/Feature/Api/Licenses/LicensesIndexTest.php
@@ -5,13 +5,14 @@ namespace Tests\Feature\Api\Licenses;
use App\Models\Company;
use App\Models\License;
use App\Models\User;
-use Illuminate\Testing\TestResponse;
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,44 +30,34 @@ class LicensesIndexTest extends TestCase
Passport::actingAs($superUser);
$response = $this->getJson(route('api.licenses.index'));
- $this->assertResponseContains($response, $licenseA);
- $this->assertResponseContains($response, $licenseB);
+ $this->assertResponseContainsInRows($response, $licenseA);
+ $this->assertResponseContainsInRows($response, $licenseB);
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('api.licenses.index'));
- $this->assertResponseContains($response, $licenseA);
- $this->assertResponseContains($response, $licenseB);
+ $this->assertResponseContainsInRows($response, $licenseA);
+ $this->assertResponseContainsInRows($response, $licenseB);
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('api.licenses.index'));
- $this->assertResponseContains($response, $licenseA);
- $this->assertResponseContains($response, $licenseB);
+ $this->assertResponseContainsInRows($response, $licenseA);
+ $this->assertResponseContainsInRows($response, $licenseB);
$this->settings->enableMultipleFullCompanySupport();
Passport::actingAs($superUser);
$response = $this->getJson(route('api.licenses.index'));
- $this->assertResponseContains($response, $licenseA);
- $this->assertResponseContains($response, $licenseB);
+ $this->assertResponseContainsInRows($response, $licenseA);
+ $this->assertResponseContainsInRows($response, $licenseB);
Passport::actingAs($userInCompanyA);
$response = $this->getJson(route('api.licenses.index'));
- $this->assertResponseContains($response, $licenseA);
- $this->assertResponseDoesNotContain($response, $licenseB);
+ $this->assertResponseContainsInRows($response, $licenseA);
+ $this->assertResponseDoesNotContainInRows($response, $licenseB);
Passport::actingAs($userInCompanyB);
$response = $this->getJson(route('api.licenses.index'));
- $this->assertResponseDoesNotContain($response, $licenseA);
- $this->assertResponseContains($response, $licenseB);
- }
-
- private function assertResponseContains(TestResponse $response, License $license)
- {
- $this->assertTrue(collect($response['rows'])->pluck('name')->contains($license->name));
- }
-
- private function assertResponseDoesNotContain(TestResponse $response, License $license)
- {
- $this->assertFalse(collect($response['rows'])->pluck('name')->contains($license->name));
+ $this->assertResponseDoesNotContainInRows($response, $licenseA);
+ $this->assertResponseContainsInRows($response, $licenseB);
}
}
diff --git a/tests/Support/InteractsWithResponses.php b/tests/Support/InteractsWithResponses.php
new file mode 100644
index 0000000000..3206aaea9d
--- /dev/null
+++ b/tests/Support/InteractsWithResponses.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace Tests\Support;
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Testing\TestResponse;
+
+trait InteractsWithResponses
+{
+ private function assertResponseContainsInRows(TestResponse $response, Model $model, string $field = 'name')
+ {
+ $this->assertTrue(collect($response['rows'])->pluck($field)->contains($model->{$field}));
+ }
+
+ private function assertResponseDoesNotContainInRows(TestResponse $response, Model $model, string $field = 'name')
+ {
+ $this->assertFalse(collect($response['rows'])->pluck($field)->contains($model->{$field}));
+ }
+}