Skip to content

Commit

Permalink
Merge pull request #917 from w3bdesign/dev
Browse files Browse the repository at this point in the history
Test the /api/products endpoint with PHPunit
  • Loading branch information
w3bdesign authored May 31, 2023
2 parents f13e426 + d164911 commit 4e72e0e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs: # a collection of steps
paths:
- node_modules
- run: touch database/testing.sqlite
- run: touch database/database.sqlite
- run: php artisan migrate --env=testing --database=sqlite_testing --force
- run: php artisan dusk:chrome-driver 70
- run: wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
Expand Down
1 change: 1 addition & 0 deletions database/factories/ProductFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function definition()
'slug' => Str::slug($name),
'description' => $this->faker->realText(320),
'price' => $this->faker->numberBetween(100, 1000),
'imageUrl' => $this->faker->imageUrl(400, 400),
];
}
}
18 changes: 0 additions & 18 deletions tests/Unit/ExampleTest.php

This file was deleted.

39 changes: 39 additions & 0 deletions tests/Unit/ProductTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Tests\Unit;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use App\Models\Product;

class ProductTest extends TestCase
{
use RefreshDatabase;

/**
* Test the /api/products endpoint.
*
* This test sends a GET request to the /api/products endpoint and checks if
* the response has a 200 status code and contains the correct product data.
*
* @return void
*/
public function testApiProductsEndpoint()
{
// Create a sample product
$product = Product::factory()->create();

// Send a GET request to the /api/products endpoint
$response = $this->get('/api/products');

// Check that the response has a 200 status code
$response->assertStatus(200);

// Check that the response contains the sample product data
$response->assertJsonPath('0.id', $product->id);
$response->assertJsonPath('0.name', $product->name);
$response->assertJsonPath('0.imageUrl', $product->imageUrl);
$response->assertJsonPath('0.slug', $product->slug);
$response->assertJsonPath('0.price', $product->price);
}
}

0 comments on commit 4e72e0e

Please sign in to comment.