I have created a test, also the interfaces to be implemented for each test case based on user's role because I think it would make it easier to understand what the test case will and should do, also forces each test that has similar requirements to have consistent method naming. I want to make sure what I did is a best practice before I implement them to all my test cases.
To have a better sight of what am I doing, please have a look at this folder structure.
Based on that, I have implemented them in one class, the file looks like this. It applies to other test cases like Showing, Updating, Destroying, Uploading, and so on...
<?php
namespace Tests\Feature\Specialization;
use App\Models\Specialization;
use App\Models\User;
use Database\Seeders\SpecializationSeeder;
use Database\Seeders\RolesAndPermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Spatie\Permission\PermissionRegistrar;
use Tests\_Interfaces\Feature\Basic\Indexable\IndexableByCustomer;
use Tests\_Interfaces\Feature\Basic\Indexable\IndexableByEmployee;
use Tests\_Interfaces\Feature\Basic\Indexable\IndexableByInternal;
use Tests\_Interfaces\Feature\Basic\Indexable\IndexableByModerator;
use Tests\_Interfaces\Feature\Basic\NotIndexable\NotIndexableByGuest;
use Tests\TestCase;
class SpecializationIndexTest extends TestCase implements
IndexableByCustomer,
IndexableByEmployee,
IndexableByInternal,
IndexableByModerator,
NotIndexableByGuest
{
use RefreshDatabase;
public function setUp(): void
{
parent::setUp();
$this->app->make(PermissionRegistrar::class)->registerPermissions();
$this->seed(RolesAndPermissionSeeder::class);
$this->seed(SpecializationSeeder::class);
}
public function testIndexShouldBeInaccessibleByGuest()
{
$this
->getJson(route('specializations.index'))
->assertUnauthorized()
->assertJsonMissing(Specialization::first()->toArray());
}
public function testIndexShouldBeAccessibleByInternal()
{
$user = User::factory()->createOne()->assignRole('internal');
$this->actingAs($user, 'api')
->getJson(route('specializations.index'))
->assertOk()
->assertJsonFragment(Specialization::first()->toArray());
}
public function testIndexShouldBeAccessibleByModerator()
{
$user = User::factory()->createOne()->assignRole('moderator');
$this->actingAs($user, 'api')
->getJson(route('specializations.index'))
->assertOk()
->assertJsonFragment(Specialization::first()->toArray());
}
public function testIndexShouldBeAccessibleByCustomer()
{
$user = User::factory()->createOne()->assignRole('customer');
$this->actingAs($user, 'api')
->getJson(route('specializations.index'))
->assertOk()
->assertJsonFragment(Specialization::first()->toArray());
}
public function testIndexShouldBeAccessibleByEmployee()
{
$user = User::factory()->createOne()->assignRole('employee');
$this->actingAs($user, 'api')
->getJson(route('specializations.index'))
->assertOk()
->assertJsonFragment(Specialization::first()->toArray());
}
}
So the question is, is this a good idea to have interfaces for tests? Or should I avoid it?
