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:
@@ -14,7 +14,8 @@ beforeEach(function () {
|
||||
|
||||
function createProductForTest(array $attributes = []): Product
|
||||
{
|
||||
$name = $attributes['name'] ?? 'Test Product ' . Str::random(5);
|
||||
$name = $attributes['name'] ?? 'Test Product '.Str::random(5);
|
||||
|
||||
return Product::create(array_merge([
|
||||
'name' => $name,
|
||||
'slug' => Str::slug($name),
|
||||
@@ -27,10 +28,10 @@ function createProductForTest(array $attributes = []): Product
|
||||
|
||||
function createCategoryForTest(array $attributes = []): Category
|
||||
{
|
||||
$name = $attributes['name'] ?? 'Test Category ' . Str::random(5);
|
||||
$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,
|
||||
@@ -61,16 +62,16 @@ test('can list products', function () {
|
||||
'name',
|
||||
'slug',
|
||||
'price_amount',
|
||||
]
|
||||
]
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
|
||||
$this->assertCount(3, $response->json('data'));
|
||||
});
|
||||
|
||||
test('can paginate products', function () {
|
||||
// Arrange
|
||||
collect(range(1, 10))->each(fn() => createProductForTest());
|
||||
collect(range(1, 10))->each(fn () => createProductForTest());
|
||||
|
||||
// Act
|
||||
$response = $this->withHeaders([
|
||||
@@ -150,7 +151,7 @@ test('can show a specific product', function () {
|
||||
'data' => [
|
||||
'id' => $product->id,
|
||||
'name' => 'Test Product',
|
||||
]
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -179,18 +180,18 @@ test('returns 404 for invalid product id format', function () {
|
||||
test('can list related products', function () {
|
||||
// Arrange
|
||||
$category = createCategoryForTest();
|
||||
|
||||
|
||||
$mainProduct = createProductForTest(['name' => 'Main Product']);
|
||||
$relatedProduct1 = createProductForTest(['name' => 'Related 1']);
|
||||
$relatedProduct2 = createProductForTest(['name' => 'Related 2']);
|
||||
$unrelatedProduct = createProductForTest(['name' => 'Unrelated']);
|
||||
|
||||
|
||||
// Attach products to category
|
||||
// Note: ProductRelatedController looks for products in the same category
|
||||
$category->products()->attach([
|
||||
$mainProduct->id,
|
||||
$relatedProduct1->id,
|
||||
$relatedProduct2->id
|
||||
$mainProduct->id,
|
||||
$relatedProduct1->id,
|
||||
$relatedProduct2->id,
|
||||
]);
|
||||
|
||||
// Act
|
||||
@@ -206,10 +207,10 @@ test('can list related products', function () {
|
||||
'*' => [
|
||||
'id',
|
||||
'name',
|
||||
]
|
||||
]
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
|
||||
// Should contain related products but not the main product itself (usually)
|
||||
// The query in ProductRelatedController:
|
||||
// where('product_id', '=', $product->id) -> This selects categories WHERE the main product is present
|
||||
@@ -221,10 +222,10 @@ test('can list related products', function () {
|
||||
// However, `unique()` is used.
|
||||
// If the main product is returned, count would be 3. If excluded, 2.
|
||||
// Let's assert that we get some products back.
|
||||
|
||||
|
||||
$data = $response->json('data');
|
||||
$this->assertGreaterThanOrEqual(2, count($data));
|
||||
|
||||
|
||||
// Check if unrelated product is NOT in the list
|
||||
$ids = collect($data)->pluck('id');
|
||||
$this->assertNotContains($unrelatedProduct->id, $ids);
|
||||
@@ -235,7 +236,7 @@ test('returns empty related products if no shared categories', function () {
|
||||
// Arrange
|
||||
$mainProduct = createProductForTest(['name' => 'Main Product']);
|
||||
$otherProduct = createProductForTest(['name' => 'Other Product']);
|
||||
|
||||
|
||||
// No categories attached
|
||||
|
||||
// Act
|
||||
|
||||
Reference in New Issue
Block a user