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:
Mekan1206
2026-02-08 02:24:43 +05:00
parent 2dfa3747b5
commit c46eccb24f
38 changed files with 257 additions and 257 deletions

View File

@@ -15,7 +15,8 @@ beforeEach(function () {
function createProductForFilterTest(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),
@@ -28,10 +29,10 @@ function createProductForFilterTest(array $attributes = []): Product
function createCategoryForFilterTest(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,
@@ -43,7 +44,8 @@ function createCategoryForFilterTest(array $attributes = []): Category
function createBrandForFilterTest(array $attributes = []): Brand
{
$name = $attributes['name'] ?? 'Test Brand ' . Str::random(5);
$name = $attributes['name'] ?? 'Test Brand '.Str::random(5);
return Brand::create(array_merge([
'name' => $name,
'slug' => Str::slug($name),
@@ -125,7 +127,7 @@ 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
@@ -140,13 +142,13 @@ test('can filter products by brand ids', function () {
$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'));
});
@@ -155,11 +157,11 @@ 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
@@ -174,13 +176,13 @@ test('can filter products by category ids', function () {
$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'));
});

View File

@@ -16,7 +16,8 @@ beforeEach(function () {
function createProductForReview(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),
@@ -31,7 +32,7 @@ test('can list product reviews', function () {
// Arrange
$product = createProductForReview();
$user = User::factory()->create();
$product->reviews()->create([
'user_id' => $user->id,
'rating' => 5,
@@ -39,7 +40,7 @@ test('can list product reviews', function () {
'is_visible' => true,
'is_recommended' => true,
]);
$product->reviews()->create([
'user_id' => $user->id,
'rating' => 4,
@@ -62,10 +63,10 @@ test('can list product reviews', function () {
'id',
'rating',
'content',
]
]
],
],
]);
$this->assertCount(2, $response->json('data'));
});
@@ -73,14 +74,14 @@ 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,
@@ -104,7 +105,7 @@ test('can store a review', function () {
// Arrange
$product = createProductForReview();
$user = User::factory()->create();
// Act
$response = $this->actingAs($user)
->withHeaders([
@@ -120,7 +121,7 @@ test('can store a review', function () {
// Assert
$response->assertStatus(201); // Created
$this->assertDatabaseHas('reviews', [
'product_id' => $product->id,
'user_id' => $user->id,
@@ -134,7 +135,7 @@ test('can store a review', function () {
test('guest cannot store a review', function () {
// Arrange
$product = createProductForReview();
// Act
$response = $this->withHeaders([
'Api-Token' => 'test-token',
@@ -152,7 +153,7 @@ test('validates review input', function () {
// Arrange
$product = createProductForReview();
$user = User::factory()->create();
// Act
$response = $this->actingAs($user)
->withHeaders([

View File

@@ -13,7 +13,8 @@ beforeEach(function () {
function createProductForSearch(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),
@@ -28,7 +29,7 @@ test('can search product by barcode', function () {
// Arrange
$product = createProductForSearch([
'name' => 'Barcode Product',
'barcode' => '1234567890123'
'barcode' => '1234567890123',
]);
// Act
@@ -44,7 +45,7 @@ test('can search product by barcode', function () {
'id' => $product->id,
'name' => 'Barcode Product',
'stock' => 10,
]
],
]);
});

View File

@@ -16,7 +16,7 @@ function createProductForSortTest(array $attributes = []): Product
$createdAt = $attributes['created_at'] ?? null;
unset($attributes['created_at']);
$name = $attributes['name'] ?? 'Test Product ' . Str::random(5);
$name = $attributes['name'] ?? 'Test Product '.Str::random(5);
$product = Product::create(array_merge([
'name' => $name,
'slug' => Str::slug($name),

View File

@@ -1,8 +1,8 @@
<?php
use App\Models\User;
use App\Models\Ecommerce\Product\Product\Product;
use App\Models\Ecommerce\Product\ProductView\ProductView;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
@@ -11,14 +11,15 @@ uses(RefreshDatabase::class);
beforeEach(function () {
Config::set('ecommerce.api.token', 'test-token');
$this->user = User::factory()->create([
'password' => 'password',
'phone_number' => 61929248,
]);
$this->createProduct = function (array $attributes = []) {
$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),
@@ -63,11 +64,11 @@ test('viewing the same product again updates the timestamp', function () {
$this->actingAs($this->user, 'sanctum')
->withHeaders(['Api-Token' => 'test-token'])
->getJson("/api/v1/products/{$product->id}");
$firstView = ProductView::where('user_id', $this->user->id)
->where('product_id', $product->id)
->first();
// Travel into the future
$this->travel(1)->hour();
@@ -111,10 +112,10 @@ test('authenticated user can list viewed products', function () {
'name',
'slug',
'price_amount',
]
]
],
],
]);
$this->assertCount(2, $response->json('data'));
});
@@ -130,7 +131,7 @@ test('viewed products are sorted by most recently viewed', function () {
$this->actingAs($this->user, 'sanctum')
->withHeaders(['Api-Token' => 'test-token'])
->getJson("/api/v1/products/{$product1->id}");
$this->travel(1)->minute();
// View 2
@@ -157,7 +158,7 @@ test('viewed products are sorted by most recently viewed', function () {
->getJson('/api/v1/products/viewed');
$data = $response->json('data');
expect($data[0]['id'])->toBe($product1->id)
->and($data[1]['id'])->toBe($product3->id)
->and($data[2]['id'])->toBe($product2->id);