This commit is contained in:
2026-02-03 15:31:29 +05:00
commit 326c677e8d
2800 changed files with 1489388 additions and 0 deletions

View File

@@ -0,0 +1,123 @@
<?php
use App\Models\User;
use App\Models\Ecommerce\Product\Product\Product;
use App\Models\Ecommerce\Product\Favorite\Favorite;
beforeEach(function () {
$this->user = User::factory()->create([
'password' => 'password',
'phone_number' => 61929248,
]);
// Helper to create product
$this->createProduct = function () {
return Product::create([
'name' => 'Test Product',
'slug' => 'test-product-' . uniqid(),
'stock' => 10,
'price_amount' => 100,
'is_visible' => true,
]);
};
});
test('unauthenticated user cannot access favorites', function () {
$this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
->getJson('/api/v1/favorites')
->assertStatus(401);
});
test('authenticated user can view empty favorites list', function () {
$response = $this->actingAs($this->user, 'sanctum')
->withHeaders(['Api-Token' => config('ecommerce.api.token')])
->getJson('/api/v1/favorites');
$response->assertStatus(200)
->assertJson(['data' => []]);
});
test('authenticated user can add item to favorites', function () {
$product = ($this->createProduct)();
$response = $this->actingAs($this->user, 'sanctum')
->withHeaders(['Api-Token' => config('ecommerce.api.token')])
->postJson('/api/v1/favorites', [
'product_id' => $product->id
]);
$response->assertStatus(200)
->assertJson([
'message' => 'Added'
]);
$this->assertDatabaseHas('favorites', [
'user_id' => $this->user->id,
'product_id' => $product->id
]);
});
test('authenticated user can remove item from favorites (toggle)', function () {
$product = ($this->createProduct)();
// Add first
$this->user->favorites()->create([
'product_id' => $product->id
]);
// Request to toggle (remove)
$response = $this->actingAs($this->user, 'sanctum')
->withHeaders(['Api-Token' => config('ecommerce.api.token')])
->postJson('/api/v1/favorites', [
'product_id' => $product->id
]);
$response->assertStatus(200)
->assertJson([
'message' => 'Removed'
]);
$this->assertDatabaseMissing('favorites', [
'user_id' => $this->user->id,
'product_id' => $product->id
]);
});
test('favorite toggle validation fails if product does not exist', function () {
$response = $this->actingAs($this->user, 'sanctum')
->withHeaders(['Api-Token' => config('ecommerce.api.token')])
->postJson('/api/v1/favorites', [
'product_id' => 99999
]);
$response->assertStatus(422)
->assertJsonValidationErrors(['product_id']);
});
test('authenticated user can view favorites list with items', function () {
$product1 = ($this->createProduct)();
$product2 = ($this->createProduct)();
$this->user->favorites()->create(['product_id' => $product1->id]);
$this->user->favorites()->create(['product_id' => $product2->id]);
$response = $this->actingAs($this->user, 'sanctum')
->withHeaders(['Api-Token' => config('ecommerce.api.token')])
->getJson('/api/v1/favorites');
$response->assertStatus(200)
->assertJsonCount(2, 'data')
->assertJsonStructure([
'data' => [
'*' => [
'created_at',
'product' => [
'id',
'name',
'slug',
// Add other product fields as expected by ProductResource
]
]
]
]);
});