Skip to content

Commit 4e72e0e

Browse files
authored
Merge pull request #917 from w3bdesign/dev
Test the /api/products endpoint with PHPunit
2 parents f13e426 + d164911 commit 4e72e0e

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ jobs: # a collection of steps
3535
paths:
3636
- node_modules
3737
- run: touch database/testing.sqlite
38+
- run: touch database/database.sqlite
3839
- run: php artisan migrate --env=testing --database=sqlite_testing --force
3940
- run: php artisan dusk:chrome-driver 70
4041
- run: wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

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)