dstpabuaran.com/tests/Feature/Admin/Master/CustomerTest.php
Yoga Pangestu 5c1ef6a7b0 feat: add customer management functionality and update navigation
- Introduced CustomerController for handling customer operations.
- Added resourceful routes for customers in the web routes.
- Updated DatabaseSeeder to include CustomerSeeder for initial data population.
- Modified sidebar navigation to link to the customers index.
2026-07-29 01:30:29 +07:00

182 lines
5.1 KiB
PHP

<?php
use App\Models\Customer;
use App\Models\User;
use Inertia\Testing\AssertableInertia as Assert;
test('guests are redirected to the login page', function () {
$response = $this->get(route('admin.master.customers.index'));
$response->assertRedirect(route('login'));
});
test('authenticated users can visit the customer index page', function () {
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->get(route('admin.master.customers.index'));
$response->assertOk();
});
test('customer index page displays customers', function () {
$user = User::factory()->create();
$this->actingAs($user);
$customers = Customer::factory()->count(3)->create();
$response = $this->get(route('admin.master.customers.index'));
$response->assertOk();
$response->assertInertia(fn (Assert $page) => $page
->component('admin/master/customer/index')
->has('customers', 3)
);
});
test('customer can be created', function () {
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->post(route('admin.master.customers.store'), [
'name' => 'Customer Test',
'phone_number' => '08123456789',
'address' => 'Jl. Test No. 1',
]);
$response
->assertSessionHasNoErrors()
->assertRedirect(route('admin.master.customers.index'));
$this->assertDatabaseHas('customers', [
'name' => 'Customer Test',
'phone_number' => '08123456789',
'address' => 'Jl. Test No. 1',
]);
});
test('customer can be created without optional fields', function () {
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->post(route('admin.master.customers.store'), [
'name' => 'Customer Minimal',
]);
$response
->assertSessionHasNoErrors()
->assertRedirect(route('admin.master.customers.index'));
$this->assertDatabaseHas('customers', [
'name' => 'Customer Minimal',
]);
});
test('customer name is required', function () {
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->post(route('admin.master.customers.store'), [
'name' => '',
]);
$response->assertSessionHasErrors('name');
});
test('customer name must not exceed 200 characters', function () {
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->post(route('admin.master.customers.store'), [
'name' => str_repeat('a', 201),
]);
$response->assertSessionHasErrors('name');
});
test('customer name must be unique', function () {
$user = User::factory()->create();
$this->actingAs($user);
Customer::factory()->create(['name' => 'Existing Customer']);
$response = $this->post(route('admin.master.customers.store'), [
'name' => 'Existing Customer',
]);
$response->assertSessionHasErrors('name');
});
test('customer phone_number must be numeric', function () {
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->post(route('admin.master.customers.store'), [
'name' => 'Customer With Letters',
'phone_number' => 'abc123',
]);
$response->assertSessionHasErrors('phone_number');
});
test('customer can be updated', function () {
$user = User::factory()->create();
$this->actingAs($user);
$customer = Customer::factory()->create();
$response = $this->put(route('admin.master.customers.update', $customer), [
'name' => 'Customer Updated',
'phone_number' => '0987654321',
'address' => 'Jl. Updated No. 2',
]);
$response
->assertSessionHasNoErrors()
->assertRedirect(route('admin.master.customers.index'));
$customer->refresh();
expect($customer->name)->toBe('Customer Updated');
expect($customer->phone_number)->toBe('0987654321');
expect($customer->address)->toBe('Jl. Updated No. 2');
});
test('customer name can be updated to itself', function () {
$user = User::factory()->create();
$this->actingAs($user);
$customer = Customer::factory()->create(['name' => 'My Customer']);
$response = $this->put(route('admin.master.customers.update', $customer), [
'name' => 'My Customer',
]);
$response->assertSessionHasNoErrors();
});
test('customer update name must be unique excluding itself', function () {
$user = User::factory()->create();
$this->actingAs($user);
$customer = Customer::factory()->create(['name' => 'First']);
Customer::factory()->create(['name' => 'Second']);
$response = $this->put(route('admin.master.customers.update', $customer), [
'name' => 'Second',
]);
$response->assertSessionHasErrors('name');
});
test('customer can be deleted', function () {
$user = User::factory()->create();
$this->actingAs($user);
$customer = Customer::factory()->create();
$response = $this->delete(route('admin.master.customers.destroy', $customer));
$response
->assertSessionHasNoErrors()
->assertRedirect(route('admin.master.customers.index'));
$this->assertSoftDeleted('customers', ['id' => $customer->id]);
});