From 2f6e810f261ae1c0e12f9d9b095ea942acc0d114 Mon Sep 17 00:00:00 2001 From: bruceoyugi Date: Tue, 6 Aug 2024 07:12:28 -0400 Subject: [PATCH] api-endpoint-admin-product --- .../Api/V1/Admin/ProductControllerTest.php | 154 ------------------ 1 file changed, 154 deletions(-) delete mode 100644 tests/Feature/Api/V1/Admin/ProductControllerTest.php diff --git a/tests/Feature/Api/V1/Admin/ProductControllerTest.php b/tests/Feature/Api/V1/Admin/ProductControllerTest.php deleted file mode 100644 index 829e1b47..00000000 --- a/tests/Feature/Api/V1/Admin/ProductControllerTest.php +++ /dev/null @@ -1,154 +0,0 @@ -admin = User::factory()->create([ - 'role' => 'admin', - 'is_active' => true, - ]); - } - - public function test_can_list_products() - { - Product::factory()->count(5)->create(); - $response = $this->actingAs($this->admin, 'api')->getJson('/api/v1/products'); - - $response->assertStatus(200) - ->assertJsonStructure([ - 'data' => [ - '*' => ['name', 'price', 'description', 'imageUrl', 'quantity', 'status', 'user_id', 'slug', 'tags'] - ], - 'links', - 'meta', - ]); - } - -public function test_can_show_product() -{ - $product = Product::factory()->create(); - $response = $this->actingAs($this->admin, 'api')->getJson("/api/v1/products/{$product->id}"); - - $response->assertStatus(200) - ->assertJson([ - 'data' => [ - [ - 'user_id' => $product->user_id, - 'name' => $product->name, - 'description' => $product->description, - 'price' => number_format($product->price, 2, '.', ''), - 'slug' => $product->slug, - 'tags' => $product->tags, - 'imageUrl' => $product->imageUrl, - 'status' => $product->status, - 'quantity' => (string)$product->quantity, - 'is_archived' => false, - 'created_at' => $product->created_at->toISOString(), - 'updated_at' => $product->updated_at->toISOString(), - ] - ] - ]); -} - - -public function test_can_update_product() -{ - $product = Product::factory()->create(); - $updatedData = [ - 'name' => 'Updated Product Name', - 'description' => 'Updated description.', - 'price' => 99.99, - 'imageUrl' => 'https://example.com/new-image.jpg', - 'quantity' => 10, - 'status' => 'active', - 'slug' => 'updated-product-name', - 'tags' => 'new,product', - ]; - - $response = $this->actingAs($this->admin, 'api') - ->putJson("/api/v1/products/{$product->product_id}", $updatedData); - - - $response->assertStatus(200) - ->assertJsonStructure([ - 'message', - 'data' => [ - 'product_id', 'user_id', 'name', 'description', 'price', 'imageUrl', - 'quantity', 'status', 'slug', 'tags', 'created_at', 'updated_at' - ] - ]); - - $this->assertDatabaseHas('products', [ - 'product_id' => $product->product_id, - 'name' => 'Updated Product Name', - ]); -} - - public function test_can_delete_product() - { - $product = Product::factory()->create(); - - - $response = $this->actingAs($this->admin, 'api') - ->deleteJson("/api/v1/products/{$product->product_id}"); - - - $response->assertStatus(204); - $this->assertDatabaseMissing('products', ['product_id' => $product->product_id]); - } - - public function test_can_edit_product() - { - $product = Product::factory()->create(); - $response = $this->actingAs($this->admin, 'api')->getJson("/api/v1/products/{$product->product_id}/edit"); - - $response->assertStatus(200) - ->assertJson([ - 'data' => $product->toArray() - ]); - } - - public function test_can_get_total_revenue() - { - Product::factory()->count(5)->create(['price' => 1000]); - $response = $this->actingAs($this->admin, 'api')->getJson('/api/v1/products/stats/total-revenue'); - - $response->assertStatus(200) - ->assertJson(['total_revenue' => 50.00]); - } - - public function test_can_get_total_price() - { - Product::factory()->count(5)->create(['price' => 1000]); - $response = $this->actingAs($this->admin, 'api')->getJson('/api/v1/products/stats/total-price'); - - $response->assertStatus(200) - ->assertJson(['total_price' => 50.00]); - } - - public function test_unauthorized_user_cannot_access_products() - { - $regularUser = User::factory()->create([ - 'role' => 'user', - 'is_active' => true, - ]); - - $response = $this->actingAs($regularUser, 'api')->getJson('/api/v1/products'); - $response->assertStatus(401); - } - - -}