feat: Aggiungi funzionalità di eliminazione per clienti e preventivi con gestione delle dipendenze
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Mail\PreventivoCliente;
|
||||
use App\Models\Cliente;
|
||||
use App\Models\Preventivo;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ConfermaPreventivoTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/** Crea un preventivo in bozza con una riga, pronto per la conferma. */
|
||||
private function creaCarrelloBozza(): Preventivo
|
||||
{
|
||||
$preventivo = Preventivo::create([
|
||||
'token' => 'token-test-123',
|
||||
'stato' => 'bozza',
|
||||
'totale' => 0,
|
||||
]);
|
||||
|
||||
$preventivo->items()->create([
|
||||
'sistema' => 'pvc',
|
||||
'type' => 'finestra_1_anta',
|
||||
'nome_modello' => 'Finestra 1 anta',
|
||||
'larghezza' => 100,
|
||||
'altezza' => 100,
|
||||
'quantita' => 1,
|
||||
'prezzo_unitario' => 500,
|
||||
'prezzo_totale' => 500,
|
||||
'payload' => ['nome' => 'Finestra 1 anta'],
|
||||
]);
|
||||
|
||||
$preventivo->ricalcolaTotale();
|
||||
|
||||
return $preventivo;
|
||||
}
|
||||
|
||||
private function datiConferma(): array
|
||||
{
|
||||
return [
|
||||
'nome' => 'Mario',
|
||||
'cognome' => 'Rossi',
|
||||
'ragione_sociale' => null,
|
||||
'email' => 'mario.rossi@example.com',
|
||||
'telefono' => '3331234567',
|
||||
'indirizzo' => 'Via Roma 1',
|
||||
'cap' => '20100',
|
||||
'citta' => 'Milano',
|
||||
'provincia' => 'MI',
|
||||
'note' => null,
|
||||
];
|
||||
}
|
||||
|
||||
public function test_conferma_da_cliente_loggato_non_crea_un_nuovo_cliente(): void
|
||||
{
|
||||
Mockery::mock('alias:App\Support\PreventivoPdf')
|
||||
->shouldReceive('genera')
|
||||
->andReturn('preventivi/preventivo-1.pdf');
|
||||
Mail::fake();
|
||||
|
||||
$cliente = Cliente::create([
|
||||
'username' => 'user1234',
|
||||
'password' => 'Segreta123!',
|
||||
'nome' => 'Mario',
|
||||
'cognome' => 'Bianchi',
|
||||
'email' => 'mario.bianchi@example.com',
|
||||
'telefono' => '3330000000',
|
||||
]);
|
||||
$token = $cliente->createToken('configuratore', ['*'])->plainTextToken;
|
||||
|
||||
$preventivo = $this->creaCarrelloBozza();
|
||||
|
||||
$dati = $this->datiConferma();
|
||||
$dati['cognome'] = 'Rossi'; // diverso dal dato salvato: l'anagrafica deve aggiornarsi
|
||||
|
||||
$response = $this->postJson("/api/carrello/{$preventivo->token}/conferma", $dati, [
|
||||
'Authorization' => "Bearer {$token}",
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
|
||||
// Nessun nuovo utente creato: in database c'è ancora solo il cliente originale.
|
||||
$this->assertSame(1, Cliente::count());
|
||||
|
||||
$preventivo->refresh();
|
||||
$this->assertSame($cliente->id, $preventivo->cliente_id);
|
||||
$this->assertSame('inviato', $preventivo->stato);
|
||||
$this->assertNotNull($preventivo->numero);
|
||||
|
||||
// L'anagrafica del cliente loggato è aggiornata con i dati confermati nel form.
|
||||
$this->assertSame('Rossi', $cliente->fresh()->cognome);
|
||||
$this->assertSame('3331234567', $cliente->fresh()->telefono);
|
||||
|
||||
Mail::assertSent(PreventivoCliente::class);
|
||||
}
|
||||
|
||||
public function test_conferma_da_guest_crea_il_cliente(): void
|
||||
{
|
||||
Mockery::mock('alias:App\Support\PreventivoPdf')
|
||||
->shouldReceive('genera')
|
||||
->andReturn('preventivi/preventivo-1.pdf');
|
||||
Mail::fake();
|
||||
|
||||
$this->assertSame(0, Cliente::count());
|
||||
$preventivo = $this->creaCarrelloBozza();
|
||||
|
||||
$response = $this->postJson("/api/carrello/{$preventivo->token}/conferma", $this->datiConferma());
|
||||
|
||||
$response->assertOk();
|
||||
|
||||
$this->assertSame(1, Cliente::count());
|
||||
|
||||
$preventivo->refresh();
|
||||
$this->assertNotNull($preventivo->cliente_id);
|
||||
$this->assertSame('mario.rossi@example.com', $preventivo->cliente->email);
|
||||
}
|
||||
|
||||
public function test_conferma_con_token_non_valido_tratta_l_utente_come_guest(): void
|
||||
{
|
||||
Mockery::mock('alias:App\Support\PreventivoPdf')
|
||||
->shouldReceive('genera')
|
||||
->andReturn('preventivi/preventivo-1.pdf');
|
||||
Mail::fake();
|
||||
|
||||
$preventivo = $this->creaCarrelloBozza();
|
||||
|
||||
$response = $this->postJson("/api/carrello/{$preventivo->token}/conferma", $this->datiConferma(), [
|
||||
'Authorization' => 'Bearer token-non-esistente',
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
|
||||
$this->assertSame(1, Cliente::count());
|
||||
$this->assertNotNull($preventivo->fresh()->cliente_id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user