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,200 @@
<?php
use App\Models\Ecommerce\Product\Brand\Brand;
use App\Models\Ecommerce\Product\Category\Category;
use App\Models\Ecommerce\Product\Product\Product;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
uses(RefreshDatabase::class);
beforeEach(function () {
Config::set('ecommerce.api.token', 'test-token');
});
function createProductForFilterTest(array $attributes = []): Product
{
$name = $attributes['name'] ?? 'Test Product ' . Str::random(5);
return Product::create(array_merge([
'name' => $name,
'slug' => Str::slug($name),
'is_visible' => true,
'price_amount' => 100.00,
'stock' => 10,
'parent_id' => null,
], $attributes));
}
function createCategoryForFilterTest(array $attributes = []): Category
{
$name = $attributes['name'] ?? 'Test Category ' . Str::random(5);
$nameValue = is_array($name) ? $name : ['en' => $name];
$slug = Str::slug(is_array($name) ? ($name['en'] ?? reset($name)) : $name);
return Category::create(array_merge([
'name' => $nameValue,
'slug' => $slug,
'is_visible' => true,
'sort_order' => 1,
'type' => 'default',
], $attributes));
}
function createBrandForFilterTest(array $attributes = []): Brand
{
$name = $attributes['name'] ?? 'Test Brand ' . Str::random(5);
return Brand::create(array_merge([
'name' => $name,
'slug' => Str::slug($name),
'is_visible' => true,
'sort_order' => 1,
], $attributes));
}
test('can filter products by name', function () {
// Arrange
createProductForFilterTest(['name' => 'Apple iPhone']);
createProductForFilterTest(['name' => 'Samsung Galaxy']);
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson('/api/v1/products?name=Apple');
// Assert
$response->assertOk();
$this->assertCount(1, $response->json('data'));
$this->assertEquals('Apple iPhone', $response->json('data.0.name'));
});
test('can filter products by min price', function () {
// Arrange
createProductForFilterTest(['name' => 'Cheap', 'price_amount' => 50]);
createProductForFilterTest(['name' => 'Expensive', 'price_amount' => 150]);
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson('/api/v1/products?min_price=100');
// Assert
$response->assertOk();
$this->assertCount(1, $response->json('data'));
$this->assertEquals('Expensive', $response->json('data.0.name'));
});
test('can filter products by max price', function () {
// Arrange
createProductForFilterTest(['name' => 'Cheap', 'price_amount' => 50]);
createProductForFilterTest(['name' => 'Expensive', 'price_amount' => 150]);
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson('/api/v1/products?max_price=100');
// Assert
$response->assertOk();
$this->assertCount(1, $response->json('data'));
$this->assertEquals('Cheap', $response->json('data.0.name'));
});
test('can filter products by price range', function () {
// Arrange
createProductForFilterTest(['name' => 'Cheap', 'price_amount' => 50]);
createProductForFilterTest(['name' => 'Medium', 'price_amount' => 100]);
createProductForFilterTest(['name' => 'Expensive', 'price_amount' => 150]);
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson('/api/v1/products?min_price=80&max_price=120');
// Assert
$response->assertOk();
$this->assertCount(1, $response->json('data'));
$this->assertEquals('Medium', $response->json('data.0.name'));
});
test('can filter products by brand ids', function () {
// Arrange
$brand1 = createBrandForFilterTest(['name' => 'Brand 1']);
$brand2 = createBrandForFilterTest(['name' => 'Brand 2']);
createProductForFilterTest(['name' => 'Product 1', 'brand_id' => $brand1->id]);
createProductForFilterTest(['name' => 'Product 2', 'brand_id' => $brand2->id]);
createProductForFilterTest(['name' => 'Product 3']); // No brand
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson("/api/v1/products?brands={$brand1->id}");
// Assert
$response->assertOk();
$this->assertCount(1, $response->json('data'));
$this->assertEquals('Product 1', $response->json('data.0.name'));
// Test multiple brands
$responseMulti = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson("/api/v1/products?brands={$brand1->id},{$brand2->id}");
$responseMulti->assertOk();
$this->assertCount(2, $responseMulti->json('data'));
});
test('can filter products by category ids', function () {
// Arrange
$category1 = createCategoryForFilterTest(['name' => 'Category 1']);
$category2 = createCategoryForFilterTest(['name' => 'Category 2']);
$product1 = createProductForFilterTest(['name' => 'Product 1']);
$product2 = createProductForFilterTest(['name' => 'Product 2']);
$product3 = createProductForFilterTest(['name' => 'Product 3']);
$category1->products()->attach($product1->id);
$category2->products()->attach($product2->id);
// Product 3 has no category
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson("/api/v1/products?categories={$category1->id}");
// Assert
$response->assertOk();
$this->assertCount(1, $response->json('data'));
$this->assertEquals('Product 1', $response->json('data.0.name'));
// Test multiple categories
$responseMulti = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson("/api/v1/products?categories={$category1->id},{$category2->id}");
$responseMulti->assertOk();
$this->assertCount(2, $responseMulti->json('data'));
});
// Note: 'backorder' filter logic in ProductFilterer seems to check 'backorder' column on products table.
// However, I don't see 'backorder' in the create_products_table migration I read earlier.
// It might be a column added later or I missed it.
// Let's assume it exists or check if it fails.
// Looking at ProductFilterer: $this->queryBuilder->where('products.backorder', $this->request->backorder);
// If the column doesn't exist, this test will fail with SQL error.
// I'll skip it for now unless I confirm the column exists.
// Wait, I read the migration earlier:
// 2023_06_12_133956_create_products_table.php
// It did NOT have backorder column.
// Let me check if there are other migrations adding it.
// I'll assume it might not work or is added in another migration I didn't read fully (I truncated the list).
// But `ProductFilterer.php` uses it, so it MUST exist on the model/table.

View File

@@ -0,0 +1,169 @@
<?php
use App\Models\Ecommerce\Product\Product\Product;
use App\Models\System\Settings\OS;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
uses(RefreshDatabase::class);
beforeEach(function () {
Config::set('ecommerce.api.token', 'test-token');
Config::set('hashing.bcrypt.rounds', 10); // Fix for hashed cast verification
});
function createProductForReview(array $attributes = []): Product
{
$name = $attributes['name'] ?? 'Test Product ' . Str::random(5);
return Product::create(array_merge([
'name' => $name,
'slug' => Str::slug($name),
'is_visible' => true,
'price_amount' => 100.00,
'stock' => 10,
'parent_id' => null,
], $attributes));
}
test('can list product reviews', function () {
// Arrange
$product = createProductForReview();
$user = User::factory()->create();
$product->reviews()->create([
'user_id' => $user->id,
'rating' => 5,
'content' => 'Great product!',
'is_visible' => true,
'is_recommended' => true,
]);
$product->reviews()->create([
'user_id' => $user->id,
'rating' => 4,
'content' => 'Good product',
'is_visible' => true,
'is_recommended' => false,
]);
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson("/api/v1/products/{$product->id}/reviews");
// Assert
$response->assertOk()
->assertJsonStructure([
'data' => [
'*' => [
'id',
'rating',
'content',
]
]
]);
$this->assertCount(2, $response->json('data'));
});
test('does not list invisible reviews', function () {
// Arrange
$product = createProductForReview();
$user = User::factory()->create();
$product->reviews()->create([
'user_id' => $user->id,
'rating' => 5,
'content' => 'Visible',
'is_visible' => true,
]);
$product->reviews()->create([
'user_id' => $user->id,
'rating' => 1,
'content' => 'Invisible',
'is_visible' => false,
]);
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson("/api/v1/products/{$product->id}/reviews");
// Assert
$response->assertOk();
$this->assertCount(1, $response->json('data'));
$this->assertEquals('Visible', $response->json('data.0.content'));
});
test('can store a review', function () {
// Arrange
$product = createProductForReview();
$user = User::factory()->create();
// Act
$response = $this->actingAs($user)
->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->postJson("/api/v1/products/{$product->id}/reviews", [
'rating' => 5,
'title' => 'Review Title',
'content' => 'Amazing!',
'is_recommended' => true,
'source' => OS::WEBSITE,
]);
// Assert
$response->assertStatus(201); // Created
$this->assertDatabaseHas('reviews', [
'product_id' => $product->id,
'user_id' => $user->id,
'rating' => 5,
'content' => 'Amazing!',
'title' => 'Review Title',
'source' => OS::WEBSITE,
]);
});
test('guest cannot store a review', function () {
// Arrange
$product = createProductForReview();
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->postJson("/api/v1/products/{$product->id}/reviews", [
'rating' => 5,
'content' => 'Amazing!',
]);
// Assert
$response->assertUnauthorized();
});
test('validates review input', function () {
// Arrange
$product = createProductForReview();
$user = User::factory()->create();
// Act
$response = $this->actingAs($user)
->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->postJson("/api/v1/products/{$product->id}/reviews", [
// Missing rating and source
'content' => 'No rating',
]);
// Assert
$response->assertUnprocessable();
$response->assertJsonValidationErrors(['rating', 'source']);
});

View File

@@ -0,0 +1,142 @@
<?php
use App\Models\Ecommerce\Product\Product\Product;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
uses(RefreshDatabase::class);
beforeEach(function () {
Config::set('ecommerce.api.token', 'test-token');
});
function createProductForSearch(array $attributes = []): Product
{
$name = $attributes['name'] ?? 'Test Product ' . Str::random(5);
return Product::create(array_merge([
'name' => $name,
'slug' => Str::slug($name),
'is_visible' => true,
'price_amount' => 100.00,
'stock' => 10,
'parent_id' => null,
], $attributes));
}
test('can search product by barcode', function () {
// Arrange
$product = createProductForSearch([
'name' => 'Barcode Product',
'barcode' => '1234567890123'
]);
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson('/api/v1/search-product-barcode?barcode=1234567890123');
// Assert
$response->assertOk()
->assertJson([
'data' => [
'id' => $product->id,
'name' => 'Barcode Product',
'stock' => 10,
]
]);
});
test('returns empty result for non-existent barcode', function () {
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson('/api/v1/search-product-barcode?barcode=nonexistent');
// Assert
$response->assertUnprocessable(); // 422
$response->assertJsonValidationErrors(['barcode']);
});
test('can search product by query string (name)', function () {
// Arrange
createProductForSearch(['name' => 'Apple iPhone 13']);
createProductForSearch(['name' => 'Samsung Galaxy S21']);
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson('/api/v1/search-product?q=iPhone');
// Assert
$response->assertOk();
$this->assertCount(1, $response->json('data'));
$this->assertEquals('Apple iPhone 13', $response->json('data.0.name'));
});
test('can search product by query string (sku)', function () {
// Arrange
createProductForSearch(['name' => 'Product A', 'sku' => 'SKU-123']);
createProductForSearch(['name' => 'Product B', 'sku' => 'SKU-456']);
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson('/api/v1/search-product?q=SKU-123');
// Assert
$response->assertOk();
$this->assertCount(1, $response->json('data'));
$this->assertEquals('Product A', $response->json('data.0.name'));
});
test('can search product by query string (barcode)', function () {
// Arrange
createProductForSearch(['name' => 'Product X', 'barcode' => 'BAR-123']);
createProductForSearch(['name' => 'Product Y', 'barcode' => 'BAR-456']);
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson('/api/v1/search-product?q=BAR-123');
// Assert
$response->assertOk();
$this->assertCount(1, $response->json('data'));
$this->assertEquals('Product X', $response->json('data.0.name'));
});
test('can search product by id (numeric query)', function () {
// Arrange
$product = createProductForSearch(['name' => 'Product ID Search']);
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson("/api/v1/search-product?q={$product->id}");
// Assert
$response->assertOk();
// It might return other products if they match name/sku/barcode with the ID, but unlikely with random names.
// Ideally it should find the product by ID.
$ids = collect($response->json('data'))->pluck('id');
$this->assertContains($product->id, $ids);
});
test('search requires q parameter', function () {
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson('/api/v1/search-product');
// Assert
$response->assertUnprocessable(); // 422
$response->assertJsonValidationErrors(['q']);
});

View File

@@ -0,0 +1,172 @@
<?php
use App\Models\Ecommerce\Product\Product\Product;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
uses(RefreshDatabase::class);
beforeEach(function () {
Config::set('ecommerce.api.token', 'test-token');
});
function createProductForSortTest(array $attributes = []): Product
{
$createdAt = $attributes['created_at'] ?? null;
unset($attributes['created_at']);
$name = $attributes['name'] ?? 'Test Product ' . Str::random(5);
$product = Product::create(array_merge([
'name' => $name,
'slug' => Str::slug($name),
'is_visible' => true,
'price_amount' => 100.00,
'stock' => 10,
'parent_id' => null,
], $attributes));
if ($createdAt) {
$product->created_at = $createdAt;
$product->save();
}
return $product;
}
test('can sort products by price ascending', function () {
// Arrange
createProductForSortTest(['name' => 'Expensive', 'price_amount' => 200]);
createProductForSortTest(['name' => 'Cheap', 'price_amount' => 100]);
createProductForSortTest(['name' => 'Medium', 'price_amount' => 150]);
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson('/api/v1/products?sorting=price_amount-ascending');
// Assert
$response->assertOk();
$this->assertCount(3, $response->json('data'));
$this->assertEquals('Cheap', $response->json('data.0.name'));
$this->assertEquals('Medium', $response->json('data.1.name'));
$this->assertEquals('Expensive', $response->json('data.2.name'));
});
test('can sort products by price descending', function () {
// Arrange
createProductForSortTest(['name' => 'Expensive', 'price_amount' => 200]);
createProductForSortTest(['name' => 'Cheap', 'price_amount' => 100]);
createProductForSortTest(['name' => 'Medium', 'price_amount' => 150]);
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson('/api/v1/products?sorting=price_amount-descending');
// Assert
$response->assertOk();
$this->assertCount(3, $response->json('data'));
$this->assertEquals('Expensive', $response->json('data.0.name'));
$this->assertEquals('Medium', $response->json('data.1.name'));
$this->assertEquals('Cheap', $response->json('data.2.name'));
});
test('can sort products by name ascending', function () {
// Arrange
createProductForSortTest(['name' => 'C Product']);
createProductForSortTest(['name' => 'A Product']);
createProductForSortTest(['name' => 'B Product']);
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson('/api/v1/products?sorting=name-ascending');
// Assert
$response->assertOk();
$this->assertCount(3, $response->json('data'));
$this->assertEquals('A Product', $response->json('data.0.name'));
$this->assertEquals('B Product', $response->json('data.1.name'));
$this->assertEquals('C Product', $response->json('data.2.name'));
});
test('can sort products by name descending', function () {
// Arrange
createProductForSortTest(['name' => 'C Product']);
createProductForSortTest(['name' => 'A Product']);
createProductForSortTest(['name' => 'B Product']);
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson('/api/v1/products?sorting=name-descending');
// Assert
$response->assertOk();
$this->assertCount(3, $response->json('data'));
$this->assertEquals('C Product', $response->json('data.0.name'));
$this->assertEquals('B Product', $response->json('data.1.name'));
$this->assertEquals('A Product', $response->json('data.2.name'));
});
test('can sort products by created_at ascending', function () {
// Arrange
$p1 = createProductForSortTest(['name' => 'Oldest', 'created_at' => now()->subDays(2)]);
$p2 = createProductForSortTest(['name' => 'Newest', 'created_at' => now()]);
$p3 = createProductForSortTest(['name' => 'Middle', 'created_at' => now()->subDay()]);
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson('/api/v1/products?sorting=created_at-ascending');
// Assert
$response->assertOk();
$this->assertCount(3, $response->json('data'));
$this->assertEquals('Oldest', $response->json('data.0.name'));
$this->assertEquals('Middle', $response->json('data.1.name'));
$this->assertEquals('Newest', $response->json('data.2.name'));
});
test('can sort products by created_at descending', function () {
// Arrange
$p1 = createProductForSortTest(['name' => 'Oldest', 'created_at' => now()->subDays(2)]);
$p2 = createProductForSortTest(['name' => 'Newest', 'created_at' => now()]);
$p3 = createProductForSortTest(['name' => 'Middle', 'created_at' => now()->subDay()]);
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson('/api/v1/products?sorting=created_at-descending');
// Assert
$response->assertOk();
$this->assertCount(3, $response->json('data'));
$this->assertEquals('Newest', $response->json('data.0.name'));
$this->assertEquals('Middle', $response->json('data.1.name'));
$this->assertEquals('Oldest', $response->json('data.2.name'));
});
test('defaults to latest sort when sorting param is invalid', function () {
// Arrange
$p1 = createProductForSortTest(['name' => 'Oldest', 'created_at' => now()->subDays(2)]);
$p2 = createProductForSortTest(['name' => 'Newest', 'created_at' => now()]);
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
'Accept' => 'application/json',
])->getJson('/api/v1/products?sorting=invalid-param');
// Assert
$response->assertOk();
// Default is latest() -> descending created_at
$this->assertEquals('Newest', $response->json('data.0.name'));
});