Files

125 lines
5.1 KiB
PHP

<?php
namespace Tests\Feature\Http\Admin;
use App\Enums\PetGenderEnum;
use App\Enums\PetTypeEnum;
use App\Models\Pet;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Inertia\Testing\AssertableInertia as Assert;
use Maatwebsite\Excel\Facades\Excel;
use Tests\Concerns\InteractsWithAdmin;
use Tests\TestCase;
class PetControllerTest extends TestCase
{
use InteractsWithAdmin, RefreshDatabase;
protected function payload(array $overrides = []): array
{
return array_merge([
'name' => 'Rex',
'type' => PetTypeEnum::Dog->value,
'gender' => PetGenderEnum::Male->value,
'breed' => 'ژرمن',
'weight' => '20',
'sterilized' => true,
'birthday' => '2020-01-01',
], $overrides);
}
public function test_guest_is_redirected(): void
{
$this->get(route('admin.pets.index'))->assertRedirect(route('login'));
}
public function test_admin_without_permission_is_forbidden(): void
{
$admin = $this->adminWithoutPermissions();
$pet = Pet::factory()->create();
$this->actingAs($admin)->get(route('admin.pets.index'))->assertForbidden();
$this->actingAs($admin)->post(route('admin.pets.store'), $this->payload())->assertForbidden();
$this->actingAs($admin)->put(route('admin.pets.update', $pet), $this->payload())->assertForbidden();
$this->actingAs($admin)->delete(route('admin.pets.destroy', $pet))->assertForbidden();
$this->actingAs($admin)->get('/admin/pets-options')->assertForbidden();
}
public function test_index_renders_with_filters(): void
{
$admin = $this->adminWithAllPermissions();
Pet::factory()->dog()->male()->create(['name' => 'Rex']);
Pet::factory()->cat()->female()->create(['name' => 'Kitty']);
$this->actingAs($admin)->get(route('admin.pets.index'))->assertOk()->assertInertia(fn (Assert $page) => $page->component('Pets/index')->has('pets.data', 2));
$this->actingAs($admin)->get(route('admin.pets.index', ['type' => [PetTypeEnum::Cat->value]]))->assertInertia(fn (Assert $page) => $page->has('pets.data', 1));
$this->actingAs($admin)->get(route('admin.pets.index', ['gender' => PetGenderEnum::Male->value]))->assertInertia(fn (Assert $page) => $page->has('pets.data', 1));
$this->actingAs($admin)->get(route('admin.pets.index', ['search' => 'Kit']))->assertInertia(fn (Assert $page) => $page->has('pets.data', 1));
}
public function test_create_and_edit_pages_render(): void
{
$admin = $this->adminWithAllPermissions();
$pet = Pet::factory()->create();
$this->actingAs($admin)->get(route('admin.pets.create'))->assertInertia(fn (Assert $page) => $page->component('Pets/create_and_edit'));
$this->actingAs($admin)->get(route('admin.pets.edit', $pet))->assertInertia(fn (Assert $page) => $page->component('Pets/create_and_edit')->where('is_edit', true)->where('pet.id', $pet->id));
}
public function test_store_creates_pet_owned_by_admin(): void
{
$admin = $this->adminWithAllPermissions();
$this->actingAs($admin)->post(route('admin.pets.store'), $this->payload(['weight' => '۲۰']))->assertOk()->assertSessionHas('success');
$this->assertDatabaseHas('pets', ['name' => 'Rex', 'type' => PetTypeEnum::Dog->value, 'weight' => 20, 'owner_id' => $admin->id]);
}
public function test_store_validates_input(): void
{
$this->actingAs($this->adminWithAllPermissions())->postJson(route('admin.pets.store'), [])->assertUnprocessable()->assertJsonValidationErrors(['name', 'type', 'weight']);
}
public function test_update_changes_pet(): void
{
$admin = $this->adminWithAllPermissions();
$pet = Pet::factory()->create();
$this->actingAs($admin)->put(route('admin.pets.update', $pet), $this->payload(['name' => 'Max']))->assertOk();
$this->assertDatabaseHas('pets', ['id' => $pet->id, 'name' => 'Max']);
}
public function test_destroy_and_bulk_delete_soft_delete_pets(): void
{
$admin = $this->adminWithAllPermissions();
$pets = Pet::factory()->count(3)->create();
$this->actingAs($admin)->delete(route('admin.pets.destroy', $pets[0]))->assertOk();
$this->actingAs($admin)->post('/admin/pets-bulk-delete', ['ids' => [$pets[1]->id, $pets[2]->id]])->assertOk();
$this->assertSame(0, Pet::count());
$this->assertSame(3, Pet::withTrashed()->count());
}
public function test_get_options_returns_label_value_pairs(): void
{
$pet = Pet::factory()->create(['name' => 'Rex']);
Pet::factory()->create(['name' => 'Kitty']);
$this->actingAs($this->adminWithAllPermissions())->getJson('/admin/pets-options?search=Rex')
->assertOk()
->assertJsonCount(1, 'data')
->assertJsonPath('data.0', ['label' => 'Rex', 'value' => $pet->id]);
}
public function test_export_data_downloads_excel(): void
{
Excel::fake();
$this->actingAs($this->adminWithAllPermissions())->get('/admin/pets-export-data')->assertOk();
Excel::assertDownloaded('pets.xlsx');
}
}