96 lines
3.1 KiB
PHP
96 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Filament\Resources\ClienteResource;
|
|
use App\Filament\Resources\ClienteResource\Pages\ListClientes;
|
|
use App\Filament\Resources\PreventivoResource;
|
|
use App\Models\Cliente;
|
|
use App\Models\Preventivo;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Str;
|
|
use Livewire\Livewire;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* L'eliminazione di un cliente (singola o massiva) deve ridirezionare alla
|
|
* lista: il reload di pagina fa ri-renderizzare la sidebar, aggiornando il
|
|
* badge "Preventivi" nella navigazione.
|
|
*/
|
|
class EliminazioneClienteTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->actingAs(User::factory()->create());
|
|
}
|
|
|
|
private function creaCliente(string $nome, string $username): Cliente
|
|
{
|
|
return Cliente::create([
|
|
'username' => $username,
|
|
'password' => 'Segreta123!',
|
|
'nome' => $nome,
|
|
'email' => strtolower($username).'@example.com',
|
|
]);
|
|
}
|
|
|
|
private function creaPreventivoInviato(Cliente $cliente): Preventivo
|
|
{
|
|
return Preventivo::create([
|
|
'cliente_id' => $cliente->id,
|
|
'token' => Str::random(40),
|
|
'stato' => 'inviato',
|
|
'totale' => 100,
|
|
]);
|
|
}
|
|
|
|
public function test_eliminazione_singola_ridireziona_alla_lista(): void
|
|
{
|
|
$cliente = $this->creaCliente('Mario', 'mario-test');
|
|
$preventivo = $this->creaPreventivoInviato($cliente);
|
|
|
|
Livewire::test(ListClientes::class)
|
|
->callTableAction('delete', $cliente)
|
|
->assertRedirect(ClienteResource::getUrl('index'));
|
|
|
|
$this->assertDatabaseMissing(Cliente::class, ['id' => $cliente->id]);
|
|
$this->assertDatabaseMissing(Preventivo::class, ['id' => $preventivo->id]);
|
|
}
|
|
|
|
public function test_eliminazione_massiva_ridireziona_alla_lista(): void
|
|
{
|
|
$primo = $this->creaCliente('Mario', 'mario-bulk');
|
|
$secondo = $this->creaCliente('Luigi', 'luigi-bulk');
|
|
$this->creaPreventivoInviato($primo);
|
|
$this->creaPreventivoInviato($secondo);
|
|
|
|
Livewire::test(ListClientes::class)
|
|
->callTableBulkAction('delete', [$primo, $secondo])
|
|
->assertRedirect(ClienteResource::getUrl('index'));
|
|
|
|
$this->assertSame(0, Cliente::count());
|
|
$this->assertSame(0, Preventivo::count());
|
|
}
|
|
|
|
public function test_badge_preventivi_aggiornato_dopo_eliminazione(): void
|
|
{
|
|
$conPreventivo = $this->creaCliente('Anna', 'anna-test');
|
|
$senzaPreventivi = $this->creaCliente('Piero', 'pier-test');
|
|
$this->creaPreventivoInviato($conPreventivo);
|
|
$this->creaPreventivoInviato($senzaPreventivi);
|
|
|
|
$this->assertSame(2, (int) PreventivoResource::getNavigationBadge());
|
|
|
|
Livewire::test(ListClientes::class)
|
|
->callTableAction('delete', $conPreventivo)
|
|
->assertRedirect(ClienteResource::getUrl('index'));
|
|
|
|
// Dopo l'eliminazione il conteggio ricalcolato dal database è aggiornato.
|
|
$this->assertSame(1, (int) PreventivoResource::getNavigationBadge());
|
|
}
|
|
} |