insertOrIgnore(['id' => 1, 'nome' => 'Admin Test', 'cognome' => 'Test', 'created_at' => now(), 'updated_at' => now()]); $stabile = Stabile::firstOrCreate( ['codice_stabile' => '0021'], [ 'denominazione' => 'SUPERCONDOMINIO MILIZIE 3', 'indirizzo' => 'Viale delle Milizie 3', 'cap' => '00192', 'citta' => 'Roma', 'provincia' => 'RM', 'codice_fiscale' => '80000000021', 'amministratore_id' => 1, 'attivo' => true, ] ); // Step 1: Ensure condomin_mirror is populated $mirrorCount = DB::connection('gescon_import') ->table('condomin_mirror') ->where('cod_stabile', '0021') ->count(); if ($mirrorCount === 0) { Artisan::call('gescon:import-mirror-0021'); } // Step 2: Run reconstruction command $exitCode = Artisan::call('gescon:reconstruct-mirror-0021'); expect($exitCode)->toBe(0); $stabile = Stabile::where('codice_stabile', '0021')->first(); expect($stabile)->not->toBeNull(); // Step 3: Verify physical units count $unitsCount = UnitaImmobiliare::where('stabile_id', $stabile->id)->count(); expect($unitsCount)->toBe(230); // Step 4: Verify Scala A Int 11 subentro relations (ATER uscente + Benedetto Daniela subentrante) $unitA11 = UnitaImmobiliare::where('stabile_id', $stabile->id) ->where('scala', 'A') ->where('interno', '11') ->first(); expect($unitA11)->not->toBeNull(); $relA11 = PersonaUnitaRelazione::where('unita_id', $unitA11->id) ->orderBy('id') ->get(); expect($relA11->count())->toBe(2); $aterRel = $relA11->first(fn ($r) => $r->id_cond === '12'); $benedettoRel = $relA11->first(fn ($r) => $r->id_cond === '220'); expect($aterRel)->not->toBeNull() ->and($aterRel->attivo)->toBeFalse() ->and($aterRel->attivo_fino_al)->toBe('06/08/26 00:00:00') ->and($aterRel->subentro_adesso_ce)->toBe('220') ->and($aterRel->persona->nome_completo)->toContain('ATER'); expect($benedettoRel)->not->toBeNull() ->and($benedettoRel->attivo)->toBeTrue() ->and($benedettoRel->subentrato_dal)->toBe('06/09/26 00:00:00') ->and($benedettoRel->subentro_prima_cera)->toBe('12') ->and($benedettoRel->persona->nome_completo)->toContain('BENEDETTO DANIELA'); // Step 5: Test Idempotency (re-run command) $secondExit = Artisan::call('gescon:reconstruct-mirror-0021'); expect($secondExit)->toBe(0); $unitsCountSecond = UnitaImmobiliare::where('stabile_id', $stabile->id)->count(); expect($unitsCountSecond)->toBe(230); $relA11Second = PersonaUnitaRelazione::where('unita_id', $unitA11->id)->count(); expect($relA11Second)->toBe(2); });