<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ProductTest extends TestCase
{
use RefreshDatabase;
public function test_can_list_products(): void
{
// Arrange: create a user and authenticate
$user = User::factory()->create();
$token = auth()->login($user);
// Act: make the request
$response = $this->withHeaders([
'Authorization' => "Bearer $token",
])->getJson('/v1/catalog/products');
// Assert: check the response
$response->assertStatus(200)
->assertJsonStructure([
'data' => [['id', 'title', 'type', 'status']],
]);
}
public function test_can_create_product(): void
{
$user = User::factory()->create();
$token = auth()->login($user);
$response = $this->withHeaders([
'Authorization' => "Bearer $token",
])->postJson('/v1/catalog/products', [
'title' => 'Test Product',
'type' => 'physical',
'status' => 'active',
'variants' => [
['sku' => 'TEST-001', 'price' => 10000],
],
]);
$response->assertStatus(201)
->assertJson(['title' => 'Test Product']);
}
public function test_unauthorized_user_cannot_create_product(): void
{
$user = User::factory()->create(); // User without create permission
$token = auth()->login($user);
$response = $this->withHeaders([
'Authorization' => "Bearer $token",
])->postJson('/v1/catalog/products', [
'title' => 'Test',
'type' => 'physical',
]);
$response->assertStatus(403);
}
}