Extract and use helper methods

This commit is contained in:
Marcus Moore 2023-06-07 16:16:30 -07:00
parent 27d4d107bb
commit abaf59c990
No known key found for this signature in database
5 changed files with 75 additions and 91 deletions

View file

@ -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));
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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}));
}
}