Files
BUT2/mini-crm/tests/Feature/ValidationTp5Test.php
T

109 lines
5.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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);
}
}