Refactor code for improved readability and consistency
- Removed unnecessary blank lines in various files to enhance code clarity. - Updated comments for consistency and clarity across multiple classes and methods. - Adjusted spacing in test files for better formatting and readability.
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\Ecommerce\Product\Product\Product;
|
||||
use App\Models\Ecommerce\Product\Cart\CartItem;
|
||||
use App\Models\Ecommerce\Product\Product\Product;
|
||||
use App\Models\User;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->user = User::factory()->create([
|
||||
'password' => 'password',
|
||||
'phone_number' => 61929248,
|
||||
]);
|
||||
|
||||
|
||||
// Helper to create product with stock
|
||||
$this->createProductWithStock = function ($stock = 10) {
|
||||
// Since we don't have factories for Channel/Inventory, and the Controller logic
|
||||
@@ -17,10 +17,10 @@ beforeEach(function () {
|
||||
// (based on CartController::index logic: if ($cartItem->product->stock < ...)),
|
||||
// we can just create a product with the stock attribute.
|
||||
// If Product factory doesn't exist, we'll create it manually.
|
||||
|
||||
|
||||
return Product::create([
|
||||
'name' => 'Test Product',
|
||||
'slug' => 'test-product-' . uniqid(),
|
||||
'slug' => 'test-product-'.uniqid(),
|
||||
'stock' => $stock,
|
||||
'price_amount' => 100,
|
||||
'is_visible' => true,
|
||||
@@ -50,7 +50,7 @@ test('authenticated user can store item in cart', function () {
|
||||
->withHeaders(['Api-Token' => config('ecommerce.api.token')])
|
||||
->postJson('/api/v1/carts', [
|
||||
'product_id' => $product->id,
|
||||
'product_quantity' => 2
|
||||
'product_quantity' => 2,
|
||||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
@@ -58,18 +58,18 @@ test('authenticated user can store item in cart', function () {
|
||||
$this->assertDatabaseHas('cart_items', [
|
||||
'user_id' => $this->user->id,
|
||||
'product_id' => $product->id,
|
||||
'product_quantity' => 2
|
||||
'product_quantity' => 2,
|
||||
]);
|
||||
});
|
||||
|
||||
test('storing item updates quantity if already in cart', function () {
|
||||
$product = ($this->createProductWithStock)(10);
|
||||
|
||||
|
||||
// Initial add
|
||||
CartItem::create([
|
||||
'user_id' => $this->user->id,
|
||||
'product_id' => $product->id,
|
||||
'product_quantity' => 1
|
||||
'product_quantity' => 1,
|
||||
]);
|
||||
|
||||
// Update quantity
|
||||
@@ -77,7 +77,7 @@ test('storing item updates quantity if already in cart', function () {
|
||||
->withHeaders(['Api-Token' => config('ecommerce.api.token')])
|
||||
->postJson('/api/v1/carts', [
|
||||
'product_id' => $product->id,
|
||||
'product_quantity' => 5
|
||||
'product_quantity' => 5,
|
||||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
@@ -85,7 +85,7 @@ test('storing item updates quantity if already in cart', function () {
|
||||
$this->assertDatabaseHas('cart_items', [
|
||||
'user_id' => $this->user->id,
|
||||
'product_id' => $product->id,
|
||||
'product_quantity' => 5 // Should update to new quantity, not add
|
||||
'product_quantity' => 5, // Should update to new quantity, not add
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -94,10 +94,10 @@ test('cart validation fails if product does not exist', function () {
|
||||
->withHeaders(['Api-Token' => config('ecommerce.api.token')])
|
||||
->postJson('/api/v1/carts', [
|
||||
'product_id' => 99999,
|
||||
'product_quantity' => 1
|
||||
'product_quantity' => 1,
|
||||
]);
|
||||
|
||||
// Expecting validation error.
|
||||
// Expecting validation error.
|
||||
// Note: The custom rule ProductStockIsAvailable might handle existence check or it might fail before that.
|
||||
// The request doesn't use 'exists' rule explicitly but checks stock.
|
||||
$response->assertStatus(422)
|
||||
@@ -111,7 +111,7 @@ test('cart validation fails if quantity is invalid', function () {
|
||||
->withHeaders(['Api-Token' => config('ecommerce.api.token')])
|
||||
->postJson('/api/v1/carts', [
|
||||
'product_id' => $product->id,
|
||||
'product_quantity' => 0 // Min is 1
|
||||
'product_quantity' => 0, // Min is 1
|
||||
]);
|
||||
|
||||
$response->assertStatus(422)
|
||||
@@ -125,7 +125,7 @@ test('cart validation fails if stock is insufficient', function () {
|
||||
->withHeaders(['Api-Token' => config('ecommerce.api.token')])
|
||||
->postJson('/api/v1/carts', [
|
||||
'product_id' => $product->id,
|
||||
'product_quantity' => 10
|
||||
'product_quantity' => 10,
|
||||
]);
|
||||
|
||||
$response->assertStatus(422)
|
||||
@@ -153,8 +153,8 @@ test('authenticated user can list cart items', function () {
|
||||
'product_id',
|
||||
'product_quantity',
|
||||
'product',
|
||||
]
|
||||
]
|
||||
],
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -170,12 +170,12 @@ test('index adjusts quantity if stock decreased', function () {
|
||||
->getJson('/api/v1/carts');
|
||||
|
||||
$response->assertStatus(200);
|
||||
|
||||
|
||||
// Check DB
|
||||
$this->assertDatabaseHas('cart_items', [
|
||||
'user_id' => $this->user->id,
|
||||
'product_id' => $product->id,
|
||||
'product_quantity' => 5
|
||||
'product_quantity' => 5,
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -192,7 +192,7 @@ test('index removes item if out of stock', function () {
|
||||
|
||||
$response->assertStatus(200)
|
||||
->assertJsonCount(0, 'data');
|
||||
|
||||
|
||||
// Check DB
|
||||
$this->assertDatabaseMissing('cart_items', [
|
||||
'user_id' => $this->user->id,
|
||||
@@ -207,14 +207,14 @@ test('authenticated user can remove specific item from cart', function () {
|
||||
$response = $this->actingAs($this->user, 'sanctum')
|
||||
->withHeaders(['Api-Token' => config('ecommerce.api.token')])
|
||||
->patchJson('/api/v1/carts', [
|
||||
'product_id' => $product->id
|
||||
'product_id' => $product->id,
|
||||
]);
|
||||
|
||||
$response->assertStatus(200);
|
||||
|
||||
$this->assertDatabaseMissing('cart_items', [
|
||||
'user_id' => $this->user->id,
|
||||
'product_id' => $product->id
|
||||
'product_id' => $product->id,
|
||||
]);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user