Réalisation du tp3, tp4, tp5 et tp6 avec documentation.
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Client;
|
||||
use App\Models\Product;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CrudTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_client_crud_is_available(): void
|
||||
{
|
||||
$this->get(route('clients.index'))->assertOk();
|
||||
$this->get(route('clients.create'))->assertOk();
|
||||
|
||||
$this->post(route('clients.store'), [
|
||||
'name' => 'Alice Martin',
|
||||
'email' => 'alice@example.com',
|
||||
'city' => 'Paris',
|
||||
])->assertRedirect(route('clients.index'));
|
||||
|
||||
$client = Client::where('email', 'alice@example.com')->firstOrFail();
|
||||
$this->get(route('clients.show', $client))->assertOk()->assertSee('Alice Martin');
|
||||
$this->get(route('clients.edit', $client))->assertOk();
|
||||
|
||||
$this->put(route('clients.update', $client), [
|
||||
'name' => 'Alice Martin',
|
||||
'email' => 'alice@example.com',
|
||||
'city' => 'Lyon',
|
||||
])->assertRedirect(route('clients.index'));
|
||||
$this->assertDatabaseHas('clients', ['id' => $client->id, 'city' => 'Lyon']);
|
||||
|
||||
$this->delete(route('clients.destroy', $client))->assertRedirect(route('clients.index'));
|
||||
$this->assertDatabaseMissing('clients', ['id' => $client->id]);
|
||||
}
|
||||
|
||||
public function test_product_crud_is_available(): void
|
||||
{
|
||||
$this->get(route('products.index'))->assertOk();
|
||||
$this->get(route('products.create'))->assertOk();
|
||||
|
||||
$this->post(route('products.store'), [
|
||||
'name' => 'Casque',
|
||||
'price' => 120,
|
||||
'stock' => 3,
|
||||
])->assertRedirect(route('products.index'));
|
||||
|
||||
$product = Product::where('name', 'Casque')->firstOrFail();
|
||||
$this->get(route('products.show', $product))->assertOk()->assertSee('Casque');
|
||||
$this->get(route('products.edit', $product))->assertOk();
|
||||
|
||||
$this->put(route('products.update', $product), [
|
||||
'name' => 'Casque Bluetooth',
|
||||
'price' => 150,
|
||||
'stock' => 8,
|
||||
])->assertRedirect(route('products.index'));
|
||||
$this->assertDatabaseHas('products', ['id' => $product->id, 'stock' => 8]);
|
||||
|
||||
$this->delete(route('products.destroy', $product))->assertRedirect(route('products.index'));
|
||||
$this->assertDatabaseMissing('products', ['id' => $product->id]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Client;
|
||||
use App\Models\Product;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class Tp6SearchPaginationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_client_search_checks_name_email_and_city(): void
|
||||
{
|
||||
Client::create(['name' => 'Jean Dupont', 'email' => 'jean@example.test', 'city' => 'Paris']);
|
||||
Client::create(['name' => 'Marie Martin', 'email' => 'marie@gmail.test', 'city' => 'Lyon']);
|
||||
Client::create(['name' => 'Paul Durand', 'email' => 'paul@example.test', 'city' => 'Bordeaux']);
|
||||
|
||||
$this->get(route('clients.index', ['search' => 'Jean']))
|
||||
->assertOk()->assertSee('Jean Dupont')->assertDontSee('Marie Martin');
|
||||
$this->get(route('clients.index', ['search' => 'gmail']))
|
||||
->assertOk()->assertSee('Marie Martin')->assertDontSee('Jean Dupont');
|
||||
$this->get(route('clients.index', ['search' => 'Bordeaux']))
|
||||
->assertOk()->assertSee('Paul Durand')->assertDontSee('Marie Martin');
|
||||
$this->get(route('clients.index'))
|
||||
->assertOk()->assertSee('Jean Dupont')->assertSee('Marie Martin')->assertSee('Paul Durand');
|
||||
}
|
||||
|
||||
public function test_client_pagination_keeps_the_search_on_page_two(): void
|
||||
{
|
||||
for ($number = 1; $number <= 12; $number++) {
|
||||
Client::create([
|
||||
'name' => sprintf('Jean %02d', $number),
|
||||
'email' => sprintf('jean%02d@example.test', $number),
|
||||
]);
|
||||
}
|
||||
Client::create(['name' => 'Autre Client', 'email' => 'autre@example.test']);
|
||||
|
||||
$first = $this->get(route('clients.index', ['search' => 'Jean']));
|
||||
$first->assertOk()->assertSee('Jean 01')->assertDontSee('Jean 11')
|
||||
->assertSee('Nombre total de clients : 12')
|
||||
->assertSee('search=Jean', false);
|
||||
$this->assertSame(10, $first->viewData('clients')->count());
|
||||
|
||||
$second = $this->get(route('clients.index', ['search' => 'Jean', 'page' => 2]));
|
||||
$second->assertOk()->assertSee('Jean 11')->assertSee('Jean 12')
|
||||
->assertDontSee('Jean 01')->assertDontSee('Autre Client');
|
||||
$this->assertSame(2, $second->viewData('clients')->count());
|
||||
}
|
||||
|
||||
public function test_product_search_by_name_or_price_is_paginated(): void
|
||||
{
|
||||
for ($number = 1; $number <= 12; $number++) {
|
||||
Product::create(['name' => sprintf('Clavier %02d', $number), 'price' => 50, 'stock' => 10]);
|
||||
}
|
||||
Product::create(['name' => 'Souris', 'price' => 15.50, 'stock' => 4]);
|
||||
|
||||
$first = $this->get(route('products.index', ['search' => 'Clavier']));
|
||||
$first->assertOk()->assertSee('Clavier 01')->assertDontSee('Clavier 11')
|
||||
->assertSee('Nombre total de produits : 12')
|
||||
->assertSee('search=Clavier', false);
|
||||
$this->assertSame(10, $first->viewData('products')->count());
|
||||
|
||||
$this->get(route('products.index', ['search' => 'Clavier', 'page' => 2]))
|
||||
->assertOk()->assertSee('Clavier 11')->assertDontSee('Clavier 01');
|
||||
$this->get(route('products.index', ['search' => '15']))
|
||||
->assertOk()->assertSee('Souris')->assertDontSee('Clavier 01');
|
||||
}
|
||||
|
||||
public function test_form_requests_keep_tp5_validation_and_messages(): void
|
||||
{
|
||||
$this->post(route('clients.store'), ['name' => '', 'email' => 'bonjour'])
|
||||
->assertSessionHasErrors(['name', 'email']);
|
||||
$this->post(route('products.store'), ['name' => 'Clavier', 'price' => -2, 'stock' => 1])
|
||||
->assertSessionHasErrors('price');
|
||||
|
||||
$client = Client::create(['name' => 'Jean Dupont', 'email' => 'jean@example.test']);
|
||||
$this->put(route('clients.update', $client), [
|
||||
'name' => 'Jean Dupont',
|
||||
'email' => 'jean@example.test',
|
||||
'city' => 'Paris',
|
||||
])->assertRedirect(route('clients.index'));
|
||||
$this->assertDatabaseHas('clients', ['id' => $client->id, 'city' => 'Paris']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Client;
|
||||
use App\Models\Product;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ValidationTp5Test extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_client_creation_rejects_invalid_values_and_keeps_old_input(): void
|
||||
{
|
||||
$this->from(route('clients.create'))->post(route('clients.store'), [
|
||||
'name' => '',
|
||||
'email' => 'bonjour',
|
||||
'city' => 'Paris',
|
||||
])->assertRedirect(route('clients.create'))
|
||||
->assertSessionHasErrors(['name', 'email'])
|
||||
->assertSessionHasInput('city', 'Paris');
|
||||
|
||||
$this->followingRedirects()->from(route('clients.create'))->post(route('clients.store'), [
|
||||
'name' => '',
|
||||
'email' => 'bonjour',
|
||||
'city' => 'Paris',
|
||||
])
|
||||
->assertSee('Le nom du client est obligatoire.')
|
||||
->assertSee('L’adresse e-mail n’est pas valide.')
|
||||
->assertSee('value="Paris"', false);
|
||||
|
||||
$this->assertDatabaseCount('clients', 0);
|
||||
}
|
||||
|
||||
public function test_client_limits_optional_fields_and_update_validation(): void
|
||||
{
|
||||
$valid = ['name' => 'Jean Dupont', 'email' => 'jean@example.test'];
|
||||
$this->post(route('clients.store'), $valid)->assertRedirect(route('clients.index'));
|
||||
$client = Client::firstOrFail();
|
||||
$this->assertNull($client->city);
|
||||
$this->assertNull($client->phone);
|
||||
$this->get(route('clients.index'))->assertOk()->assertSee('Jean Dupont');
|
||||
|
||||
$this->post(route('clients.store'), [...$valid, 'name' => str_repeat('A', 101), 'email' => 'autre@example.test'])
|
||||
->assertSessionHasErrors('name');
|
||||
$this->post(route('clients.store'), [...$valid, 'email' => 'autre@example.test', 'phone' => str_repeat('1', 21)])
|
||||
->assertSessionHasErrors('phone');
|
||||
$this->post(route('clients.store'), [...$valid, 'email' => 'autre@example.test', 'city' => str_repeat('V', 101)])
|
||||
->assertSessionHasErrors('city');
|
||||
$this->post(route('clients.store'), $valid)->assertSessionHasErrors('email');
|
||||
$this->post(route('clients.store'), ['name' => 'Autre', 'email' => ''])
|
||||
->assertSessionHasErrors('email');
|
||||
|
||||
$this->put(route('clients.update', $client), ['name' => '', 'email' => 'incorrect'])
|
||||
->assertSessionHasErrors(['name', 'email']);
|
||||
$this->assertDatabaseHas('clients', ['id' => $client->id, 'name' => 'Jean Dupont']);
|
||||
|
||||
$this->put(route('clients.update', $client), [
|
||||
'name' => 'Jean Dupont',
|
||||
'email' => 'jean@example.test',
|
||||
'phone' => '01 02 03 04 05',
|
||||
'city' => 'Paris',
|
||||
])->assertRedirect(route('clients.index'));
|
||||
$this->assertDatabaseHas('clients', ['id' => $client->id, 'phone' => '01 02 03 04 05', 'city' => 'Paris']);
|
||||
|
||||
// Le sujet demande une chaîne facultative, sans règle de chiffres uniquement.
|
||||
$this->put(route('clients.update', $client), [
|
||||
'name' => 'Jean Dupont',
|
||||
'email' => 'jean@example.test',
|
||||
'phone' => 'poste ABC',
|
||||
'city' => '',
|
||||
])->assertRedirect(route('clients.index'));
|
||||
$this->assertDatabaseHas('clients', ['id' => $client->id, 'phone' => 'poste ABC', 'city' => null]);
|
||||
}
|
||||
|
||||
public function test_product_creation_and_update_reject_invalid_values(): void
|
||||
{
|
||||
foreach ([
|
||||
['name' => '', 'price' => 15, 'stock' => 10, 'field' => 'name'],
|
||||
['name' => 'Clavier', 'price' => -10, 'stock' => 10, 'field' => 'price'],
|
||||
['name' => 'Clavier', 'price' => 15, 'stock' => -5, 'field' => 'stock'],
|
||||
['name' => 'Clavier', 'price' => 'quinze', 'stock' => 'dix', 'field' => 'price'],
|
||||
['name' => str_repeat('C', 151), 'price' => 15, 'stock' => 10, 'field' => 'name'],
|
||||
] as $case) {
|
||||
$field = $case['field'];
|
||||
unset($case['field']);
|
||||
$this->post(route('products.store'), $case)->assertSessionHasErrors($field);
|
||||
}
|
||||
$this->assertDatabaseCount('products', 0);
|
||||
$this->post(route('products.store'), ['name' => 'Clavier', 'price' => 'quinze', 'stock' => 'dix'])
|
||||
->assertSessionHasErrors(['price', 'stock']);
|
||||
|
||||
$this->post(route('products.store'), ['name' => 'Clavier', 'price' => 15.50, 'stock' => 10])
|
||||
->assertRedirect(route('products.index'));
|
||||
$product = Product::firstOrFail();
|
||||
$this->assertEquals(15.50, $product->price);
|
||||
|
||||
$this->put(route('products.update', $product), ['name' => 'Clavier', 'price' => -1, 'stock' => 10])
|
||||
->assertSessionHasErrors('price');
|
||||
$this->assertDatabaseHas('products', ['id' => $product->id, 'stock' => 10]);
|
||||
|
||||
$this->followingRedirects()->from(route('products.edit', $product))
|
||||
->put(route('products.update', $product), ['name' => 'Clavier', 'price' => -1, 'stock' => 10])
|
||||
->assertSee('Le prix doit être supérieur ou égal à zéro.')
|
||||
->assertSee('value="-1"', false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user