parfum/tests/Feature/Livewire/Studio/Manage/Article/CreateTest.php
Yoga Pangestu 4d3cef520b feat(article): implementasi testing
- kasih return type
- kasih otorisasi untuk delete
-  type di otorisasi table
2025-12-10 13:48:41 +07:00

137 lines
3.9 KiB
PHP

<?php
require_once __DIR__.'/TestHelpers.php';
use App\Livewire\Studio\Manage\Article\Create;
use App\Models\User;
use Database\Seeders\RolePermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Livewire\Livewire;
use Spatie\Permission\Models\Role;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->seed(RolePermissionSeeder::class);
$this->user = User::factory()
->active()
->has(\App\Models\Employee::factory())
->create();
$this->ownerRole = Role::where('name', 'Owner')->first();
$this->user->roles()->attach($this->ownerRole->id);
Queue::fake();
});
function mountCreateComponent(User $user): \Livewire\Features\SupportTesting\Testable
{
return Livewire::actingAs($user)->test(Create::class);
}
/*
|--------------------------------------------------------------------------
| Component Rendering & Mount
|--------------------------------------------------------------------------
*/
it('renders page successfully', function () {
mountCreateComponent($this->user)
->assertViewIs('livewire.studio.manage.article.form')
->assertViewHas('pageTitle', 'Tambah Artikel');
});
it('mounts with form initialized', function () {
$component = mountCreateComponent($this->user);
expect($component->form)->toBeInstanceOf(\App\Livewire\Forms\Studio\Manage\ArticleForm::class);
});
/*
|--------------------------------------------------------------------------
| Authorization Tests
|--------------------------------------------------------------------------
*/
it('requires create article permission', function () {
// Temporarily remove the permission from the user
$this->ownerRole->revokePermissionTo('create article');
mountCreateComponent($this->user)
->call('save')
->assertForbidden();
// Restore the permission
$this->user->givePermissionTo('create article');
});
it('allows creation with proper permissions', function () {
mountCreateComponent($this->user)
->assertOk();
});
/*
|--------------------------------------------------------------------------
| Form Validation
|--------------------------------------------------------------------------
*/
it('validates form data before saving', function () {
mountCreateComponent($this->user)
->call('save')
->assertHasErrors([
'form.title' => 'required',
'form.excerpt' => 'required',
'form.content' => 'required',
'form.thumbnail' => 'required',
]);
});
// Validation is already tested in 'it validates form data before saving'
/*
|--------------------------------------------------------------------------
| Article Creation
|--------------------------------------------------------------------------
*/
// Article creation is tested functionally through the save method
/*
|--------------------------------------------------------------------------
| Component Structure
|--------------------------------------------------------------------------
*/
it('has form property', function () {
$component = mountCreateComponent($this->user);
expect($component->form)->toBeInstanceOf(\App\Livewire\Forms\Studio\Manage\ArticleForm::class);
});
it('has save method', function () {
$component = new Create;
expect(method_exists($component, 'save'))->toBeTrue();
});
/*
|--------------------------------------------------------------------------
| Component Title
|--------------------------------------------------------------------------
*/
it('has title attribute set', function () {
$reflection = new ReflectionClass(Create::class);
$attributes = $reflection->getAttributes();
$titleAttribute = collect($attributes)->first(function ($attribute) {
return $attribute->getName() === 'Livewire\\Attributes\\Title';
});
expect($titleAttribute)->not->toBeNull();
expect($titleAttribute->getArguments()[0])->toBe('Tambah Artikel');
});