150 lines
4.8 KiB
PHP
150 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Livewire\Home;
|
|
use App\Models\AnnoTeocratico;
|
|
use App\Models\Assegnazione;
|
|
use App\Models\Proclamatore;
|
|
use App\Models\Setting;
|
|
use App\Models\Territorio;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Livewire\Livewire;
|
|
use Spatie\Permission\Models\Role;
|
|
use Spatie\Permission\PermissionRegistrar;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* "Media percorrenza congregazione": la durata media di un'assegnazione chiusa,
|
|
* in mesi, sull'anno di servizio corrente.
|
|
*
|
|
* Il calcolo è stato spostato da SQL a PHP perché usava DATEDIFF() e YEAR(),
|
|
* funzioni MySQL che rendevano la dashboard l'unica pagina non verificabile
|
|
* dalla suite. Questi test fissano il comportamento che lo spostamento doveva
|
|
* lasciare intatto — compresi i due filtri, che sono la parte facile da perdere
|
|
* riscrivendo: senza, una data assurda ereditata dalla vecchia applicazione o
|
|
* un rientro nello stesso giorno spostano la media senza volerlo dire.
|
|
*/
|
|
class HomeMediaDurataTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private User $utente;
|
|
private Proclamatore $proclamatore;
|
|
private AnnoTeocratico $anno;
|
|
private int $prossimoTerritorio = 1;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->withoutVite();
|
|
|
|
Role::findOr('amministratore', fn () => Role::create(['name' => 'amministratore', 'guard_name' => 'web']));
|
|
app(PermissionRegistrar::class)->forgetCachedPermissions();
|
|
Setting::firstOrCreate([], ['congregazione_nome' => 'Congregazione di prova']);
|
|
|
|
$this->utente = User::create([
|
|
'name' => 'Amministratore',
|
|
'email' => 'admin@example.test',
|
|
'password' => 'password-di-prova',
|
|
]);
|
|
$this->utente->assignRole('amministratore');
|
|
$this->actingAs($this->utente);
|
|
|
|
$this->proclamatore = Proclamatore::create(['nome' => 'Mario', 'cognome' => 'Rossi', 'attivo' => true]);
|
|
$this->anno = AnnoTeocratico::corrente();
|
|
}
|
|
|
|
public function test_senza_assegnazioni_chiuse_la_media_e_zero(): void
|
|
{
|
|
$this->assertSame(0, $this->media());
|
|
}
|
|
|
|
public function test_un_assegnazione_ancora_aperta_non_entra_nella_media(): void
|
|
{
|
|
$this->assegnazione($this->anno->start_date->copy()->addDays(10), null);
|
|
|
|
$this->assertSame(0, $this->media());
|
|
}
|
|
|
|
public function test_la_media_e_in_mesi_da_trenta_giorni_e_mezzo(): void
|
|
{
|
|
// 10 e 20 giorni: media 15, cioè 15/30,44 = 0,5 mesi.
|
|
$this->assegnazioneDiGiorni(10);
|
|
$this->assegnazioneDiGiorni(20);
|
|
|
|
$this->assertSame(0.5, $this->media());
|
|
}
|
|
|
|
public function test_un_rientro_nello_stesso_giorno_non_abbassa_la_media(): void
|
|
{
|
|
$this->assegnazioneDiGiorni(30);
|
|
$this->assegnazioneDiGiorni(0);
|
|
|
|
// Senza il filtro la media sarebbe 15 giorni invece di 30.
|
|
$this->assertSame(1.0, $this->media());
|
|
}
|
|
|
|
/**
|
|
* La conversione del vecchio database ha lasciato qualche data impossibile:
|
|
* il filtro esisteva per quelle, e deve continuare a esistere.
|
|
*/
|
|
public function test_una_data_antecedente_al_1900_viene_ignorata(): void
|
|
{
|
|
$this->assegnazioneDiGiorni(30);
|
|
|
|
$this->assegnazione('1899-01-01', '1899-03-01');
|
|
|
|
$this->assertSame(1.0, $this->media());
|
|
}
|
|
|
|
public function test_le_assegnazioni_di_un_altro_anno_non_entrano_nella_media(): void
|
|
{
|
|
$this->assegnazioneDiGiorni(30);
|
|
|
|
$altroAnno = AnnoTeocratico::create([
|
|
'label' => '2000-2001',
|
|
'start_date' => '2000-09-01',
|
|
'end_date' => '2001-08-31',
|
|
]);
|
|
$this->assegnazione('2000-09-01', '2001-03-01', $altroAnno);
|
|
|
|
$this->assertSame(1.0, $this->media());
|
|
}
|
|
|
|
// ─── helper ─────────────────────────────────────────────────
|
|
|
|
private function media(): float|int
|
|
{
|
|
$valore = Livewire::test(Home::class)->viewData('mediaDurataPercorrenzaMesi');
|
|
|
|
return $valore;
|
|
}
|
|
|
|
private function assegnazioneDiGiorni(int $giorni): void
|
|
{
|
|
$inizio = $this->anno->start_date->copy()->addDays(5);
|
|
|
|
$this->assegnazione($inizio, $inizio->copy()->addDays($giorni));
|
|
}
|
|
|
|
private function assegnazione(mixed $da, mixed $a, ?AnnoTeocratico $anno = null): void
|
|
{
|
|
$territorio = Territorio::create([
|
|
'numero' => (string) $this->prossimoTerritorio++,
|
|
'attivo' => true,
|
|
]);
|
|
|
|
Assegnazione::create([
|
|
'territorio_id' => $territorio->id,
|
|
'proclamatore_id' => $this->proclamatore->id,
|
|
'anno_teocratico_id' => ($anno ?? $this->anno)->id,
|
|
'assigned_at' => $da,
|
|
'returned_at' => $a,
|
|
'created_by' => $this->utente->id,
|
|
]);
|
|
}
|
|
}
|