feat: introduce feature tests for Studio Catalog Bottle and Product management, including CRUD and audit functionalities.

This commit is contained in:
Yoga Pangestu 2026-01-02 19:37:18 +07:00
parent 6a0271d9e1
commit 32eef4acfe
8 changed files with 430 additions and 0 deletions

View File

@ -0,0 +1,38 @@
<?php
use App\Livewire\Studio\Catalog\Bottle\Audit;
use App\Models\Bottle;
use App\Models\Outlet;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
beforeEach(function () {
$this->setupUser();
$role = Role::firstOrCreate(['name' => 'Owner']);
Permission::firstOrCreate(['name' => 'view bottle']);
Permission::firstOrCreate(['name' => 'audit bottle']);
$role->givePermissionTo(['view bottle', 'audit bottle']);
$this->user->assignRole($role);
});
it('renders the bottle audit page correctly', function () {
$outlet = Outlet::factory()->create();
$bottle = Bottle::factory()->create();
$this->actingAs($this->user)
->get(route('studio.catalog.bottle.audit', ['outlet' => $outlet, 'bottle' => $bottle]))
->assertOk()
->assertSeeLivewire(Audit::class);
});
it('cannot access bottle audit without permission', function () {
$outlet = Outlet::factory()->create();
$bottle = Bottle::factory()->create();
$this->user->roles()->detach();
$this->user->permissions()->detach();
$this->actingAs($this->user)
->get(route('studio.catalog.bottle.audit', ['outlet' => $outlet, 'bottle' => $bottle]))
->assertForbidden();
});

View File

@ -0,0 +1,68 @@
<?php
use App\Livewire\Studio\Catalog\Bottle\Create;
use App\Models\Outlet;
use Livewire\Livewire;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
beforeEach(function () {
$this->setupUser();
$role = Role::firstOrCreate(['name' => 'Owner']);
Permission::firstOrCreate(['name' => 'view bottle']);
Permission::firstOrCreate(['name' => 'create bottle']);
$role->givePermissionTo(['view bottle', 'create bottle']);
$this->user->assignRole($role);
});
it('renders the bottle create page correctly', function () {
$this->actingAs($this->user)
->get(route('studio.catalog.bottle.create'))
->assertOk()
->assertSeeLivewire(Create::class);
});
it('shows validation errors when fields are empty', function () {
Livewire::actingAs($this->user)
->test(Create::class)
->call('save')
->assertHasErrors([
'form.name' => 'required',
'form.size' => 'required',
'form.cost_price' => 'required',
'form.sale_price' => 'required',
'form.outlet_ids' => 'required',
]);
});
it('can store a new bottle', function () {
$outlet = Outlet::factory()->create();
Livewire::actingAs($this->user)
->test(Create::class)
->set('form.name', 'Bottle C')
->set('form.size', '100')
->set('form.cost_price', '5000')
->set('form.sale_price', '15000')
->set('form.outlet_ids', [$outlet->id])
->call('save')
->assertHasNoErrors()
->assertRedirect(route('studio.catalog.bottle.index', ['notification' => 'Botol berhasil ditambahkan.']));
$this->assertDatabaseHas('bottles', [
'name' => 'Bottle C',
'size' => 100,
'cost_price' => 5000,
'sale_price' => 15000,
]);
});
it('cannot access bottle create without permission', function () {
$this->user->roles()->detach();
$this->user->permissions()->detach();
$this->actingAs($this->user)
->get(route('studio.catalog.bottle.create'))
->assertForbidden();
});

View File

@ -0,0 +1,64 @@
<?php
use App\Livewire\Studio\Catalog\Bottle\Edit;
use App\Models\Bottle;
use App\Models\Outlet;
use Livewire\Livewire;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
beforeEach(function () {
$this->setupUser();
$role = Role::firstOrCreate(['name' => 'Owner']);
Permission::firstOrCreate(['name' => 'view bottle']);
Permission::firstOrCreate(['name' => 'update bottle']);
$role->givePermissionTo(['view bottle', 'update bottle']);
$this->user->assignRole($role);
});
it('renders the bottle edit page correctly', function () {
$bottle = Bottle::factory()->create();
$this->actingAs($this->user)
->get(route('studio.catalog.bottle.edit', ['bottle' => $bottle]))
->assertOk()
->assertSeeLivewire(Edit::class);
});
it('can load data correctly into edit form', function () {
$bottle = Bottle::factory()->create(['name' => 'Old Bottle Name']);
Livewire::actingAs($this->user)
->test(Edit::class, ['bottle' => $bottle])
->assertSet('form.name', 'Old Bottle Name')
->assertSet('form.bottle.id', $bottle->id);
});
it('can update a bottle', function () {
$bottle = Bottle::factory()->create();
$outlet = Outlet::factory()->create();
Livewire::actingAs($this->user)
->test(Edit::class, ['bottle' => $bottle])
->set('form.name', 'Updated Bottle Name')
->set('form.outlet_ids', [$outlet->id])
->call('save')
->assertHasNoErrors()
->assertRedirect(route('studio.catalog.bottle.index', ['notification' => 'Botol berhasil diperbarui.']));
$this->assertDatabaseHas('bottles', [
'id' => $bottle->id,
'name' => 'Updated Bottle Name',
]);
});
it('cannot access bottle edit without permission', function () {
$bottle = Bottle::factory()->create();
$this->user->roles()->detach();
$this->user->permissions()->detach();
$this->actingAs($this->user)
->get(route('studio.catalog.bottle.edit', ['bottle' => $bottle]))
->assertForbidden();
});

View File

@ -0,0 +1,45 @@
<?php
use App\Livewire\Studio\Catalog\Bottle\Index;
use App\Models\Bottle;
use Livewire\Livewire;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
beforeEach(function () {
$this->setupUser();
$role = Role::firstOrCreate(['name' => 'Owner']);
Permission::firstOrCreate(['name' => 'view bottle']);
Permission::firstOrCreate(['name' => 'delete bottle']);
$role->givePermissionTo(['view bottle', 'delete bottle']);
$this->user->assignRole($role);
});
it('renders the bottle index page correctly', function () {
$this->actingAs($this->user)
->get(route('studio.catalog.bottle.index'))
->assertOk()
->assertSeeLivewire(Index::class);
});
it('can delete a bottle', function () {
$bottle = Bottle::factory()->create();
Livewire::actingAs($this->user)
->test(Index::class)
->call('delete', $bottle->id)
->assertHasNoErrors()
->assertDispatched('refreshDatatable');
$this->assertSoftDeleted('bottles', ['id' => $bottle->id]);
});
it('cannot access bottle index without permission', function () {
$this->user->roles()->detach();
$this->user->permissions()->detach();
$this->actingAs($this->user)
->get(route('studio.catalog.bottle.index'))
->assertForbidden();
});

View File

@ -0,0 +1,38 @@
<?php
use App\Livewire\Studio\Catalog\Product\Audit;
use App\Models\Outlet;
use App\Models\Product;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
beforeEach(function () {
$this->setupUser();
$role = Role::firstOrCreate(['name' => 'Owner']);
Permission::firstOrCreate(['name' => 'view product']);
Permission::firstOrCreate(['name' => 'audit product']);
$role->givePermissionTo(['view product', 'audit product']);
$this->user->assignRole($role);
});
it('renders the product audit page correctly', function () {
$outlet = Outlet::factory()->create();
$product = Product::factory()->create();
$this->actingAs($this->user)
->get(route('studio.catalog.product.audit', ['outlet' => $outlet, 'product' => $product]))
->assertOk()
->assertSeeLivewire(Audit::class);
});
it('cannot access product audit without permission', function () {
$outlet = Outlet::factory()->create();
$product = Product::factory()->create();
$this->user->roles()->detach();
$this->user->permissions()->detach();
$this->actingAs($this->user)
->get(route('studio.catalog.product.audit', ['outlet' => $outlet, 'product' => $product]))
->assertForbidden();
});

View File

@ -0,0 +1,68 @@
<?php
use App\Livewire\Studio\Catalog\Product\Create;
use App\Models\Outlet;
use Livewire\Livewire;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
beforeEach(function () {
$this->setupUser();
$role = Role::firstOrCreate(['name' => 'Owner']);
Permission::firstOrCreate(['name' => 'view product']);
Permission::firstOrCreate(['name' => 'create product']);
$role->givePermissionTo(['view product', 'create product']);
$this->user->assignRole($role);
});
it('renders the product create page correctly', function () {
$this->actingAs($this->user)
->get(route('studio.catalog.product.create'))
->assertOk()
->assertSeeLivewire(Create::class);
});
it('shows validation errors when fields are empty', function () {
Livewire::actingAs($this->user)
->test(Create::class)
->call('save')
->assertHasErrors([
'form.name' => 'required',
'form.sku' => 'required',
'form.cost_price' => 'required',
'form.sale_price' => 'required',
'form.outlet_ids' => 'required',
]);
});
it('can store a new product', function () {
$outlet = Outlet::factory()->create();
Livewire::actingAs($this->user)
->test(Create::class)
->set('form.name', 'Product B')
->set('form.sku', 'SKU-Product-B')
->set('form.cost_price', '20000')
->set('form.sale_price', '40000')
->set('form.outlet_ids', [$outlet->id])
->call('save')
->assertHasNoErrors()
->assertRedirect(route('studio.catalog.product.index', ['notification' => 'Produk berhasil ditambahkan.']));
$this->assertDatabaseHas('products', [
'name' => 'Product B',
'sku' => 'SKU-Product-B',
'cost_price' => 20000,
'sale_price' => 40000,
]);
});
it('cannot access product create without permission', function () {
$this->user->roles()->detach();
$this->user->permissions()->detach();
$this->actingAs($this->user)
->get(route('studio.catalog.product.create'))
->assertForbidden();
});

View File

@ -0,0 +1,64 @@
<?php
use App\Livewire\Studio\Catalog\Product\Edit;
use App\Models\Outlet;
use App\Models\Product;
use Livewire\Livewire;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
beforeEach(function () {
$this->setupUser();
$role = Role::firstOrCreate(['name' => 'Owner']);
Permission::firstOrCreate(['name' => 'view product']);
Permission::firstOrCreate(['name' => 'update product']);
$role->givePermissionTo(['view product', 'update product']);
$this->user->assignRole($role);
});
it('renders the product edit page correctly', function () {
$product = Product::factory()->create();
$this->actingAs($this->user)
->get(route('studio.catalog.product.edit', ['product' => $product]))
->assertOk()
->assertSeeLivewire(Edit::class);
});
it('can load data correctly into edit form', function () {
$product = Product::factory()->create(['name' => 'Old Product Name']);
Livewire::actingAs($this->user)
->test(Edit::class, ['product' => $product])
->assertSet('form.name', 'Old Product Name')
->assertSet('form.product.id', $product->id);
});
it('can update a product', function () {
$product = Product::factory()->create();
$outlet = Outlet::factory()->create();
Livewire::actingAs($this->user)
->test(Edit::class, ['product' => $product])
->set('form.name', 'Updated Product Name')
->set('form.outlet_ids', [$outlet->id])
->call('save')
->assertHasNoErrors()
->assertRedirect(route('studio.catalog.product.index', ['notification' => 'Produk berhasil diperbarui.']));
$this->assertDatabaseHas('products', [
'id' => $product->id,
'name' => 'Updated Product Name',
]);
});
it('cannot access product edit without permission', function () {
$product = Product::factory()->create();
$this->user->roles()->detach();
$this->user->permissions()->detach();
$this->actingAs($this->user)
->get(route('studio.catalog.product.edit', ['product' => $product]))
->assertForbidden();
});

View File

@ -0,0 +1,45 @@
<?php
use App\Livewire\Studio\Catalog\Product\Index;
use App\Models\Product;
use Livewire\Livewire;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
beforeEach(function () {
$this->setupUser();
$role = Role::firstOrCreate(['name' => 'Owner']);
Permission::firstOrCreate(['name' => 'view product']);
Permission::firstOrCreate(['name' => 'delete product']);
$role->givePermissionTo(['view product', 'delete product']);
$this->user->assignRole($role);
});
it('renders the product index page correctly', function () {
$this->actingAs($this->user)
->get(route('studio.catalog.product.index'))
->assertOk()
->assertSeeLivewire(Index::class);
});
it('can delete a product', function () {
$product = Product::factory()->create();
Livewire::actingAs($this->user)
->test(Index::class)
->call('delete', $product->id)
->assertHasNoErrors()
->assertDispatched('refreshDatatable');
$this->assertSoftDeleted('products', ['id' => $product->id]);
});
it('cannot access product index without permission', function () {
$this->user->roles()->detach();
$this->user->permissions()->detach();
$this->actingAs($this->user)
->get(route('studio.catalog.product.index'))
->assertForbidden();
});