New-Thanasoft/thanasoft-back/tests/Feature/WarehouseApiTest.php

90 lines
2.4 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\User;
use App\Models\Warehouse;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class WarehouseApiTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Sanctum::actingAs(User::factory()->create());
}
public function test_can_list_warehouses(): void
{
Warehouse::factory()->count(3)->create();
$response = $this->getJson('/api/warehouses');
$response->assertStatus(200)
->assertJsonCount(3, 'data');
}
public function test_can_create_warehouse(): void
{
$data = [
'name' => 'Main Warehouse',
'city' => 'Paris',
'country_code' => 'FR',
];
$response = $this->postJson('/api/warehouses', $data);
$response->assertStatus(201)
->assertJsonPath('data.name', 'Main Warehouse');
$this->assertDatabaseHas('warehouses', ['name' => 'Main Warehouse']);
}
public function test_can_show_warehouse(): void
{
$warehouse = Warehouse::factory()->create();
$response = $this->getJson('/api/warehouses/' . $warehouse->id);
$response->assertStatus(200)
->assertJsonPath('data.id', $warehouse->id);
}
public function test_can_update_warehouse(): void
{
$warehouse = Warehouse::factory()->create(['name' => 'Old Name']);
$response = $this->putJson('/api/warehouses/' . $warehouse->id, [
'name' => 'New Name'
]);
$response->assertStatus(200)
->assertJsonPath('data.name', 'New Name');
$this->assertDatabaseHas('warehouses', ['id' => $warehouse->id, 'name' => 'New Name']);
}
public function test_can_delete_warehouse(): void
{
$warehouse = Warehouse::factory()->create();
$response = $this->deleteJson('/api/warehouses/' . $warehouse->id);
$response->assertStatus(200);
$this->assertDatabaseMissing('warehouses', ['id' => $warehouse->id]);
}
public function test_valide_french_messages(): void
{
$response = $this->postJson('/api/warehouses', []);
$response->assertStatus(422)
->assertJsonValidationErrors(['name'])
->assertJsonFragment(['name' => ['Le nom de l\'entrepôt est requis.']]);
}
}