Skip to content

Commit 53f76ca

Browse files
committed
Test the /api/products endpoint with PHPunit
1 parent 8333404 commit 53f76ca

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

database/factories/ProductFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function definition()
3131
'slug' => Str::slug($name),
3232
'description' => $this->faker->realText(320),
3333
'price' => $this->faker->numberBetween(100, 1000),
34+
'imageUrl' => $this->faker->imageUrl(400, 400),
3435
];
3536
}
3637
}

tests/Unit/ExampleTest.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/Unit/ProductTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use Illuminate\Foundation\Testing\RefreshDatabase;
6+
use Tests\TestCase;
7+
use App\Models\Product;
8+
9+
class ProductTest extends TestCase
10+
{
11+
use RefreshDatabase;
12+
13+
/**
14+
* Test the /api/products endpoint.
15+
*
16+
* This test sends a GET request to the /api/products endpoint and checks if
17+
* the response has a 200 status code and contains the correct product data.
18+
*
19+
* @return void
20+
*/
21+
public function testApiProductsEndpoint()
22+
{
23+
// Create a sample product
24+
$product = Product::factory()->create();
25+
26+
// Send a GET request to the /api/products endpoint
27+
$response = $this->get('/api/products');
28+
29+
// Check that the response has a 200 status code
30+
$response->assertStatus(200);
31+
32+
// Check that the response contains the sample product data
33+
$response->assertJsonPath('0.id', $product->id);
34+
$response->assertJsonPath('0.name', $product->name);
35+
$response->assertJsonPath('0.imageUrl', $product->imageUrl);
36+
$response->assertJsonPath('0.slug', $product->slug);
37+
$response->assertJsonPath('0.price', $product->price);
38+
}
39+
}

0 commit comments

Comments
 (0)