++ Prima versione del designer piantine + Skill grafica
This commit is contained in:
147
tests/Unit/PiantinaPdfServiceTest.php
Normal file
147
tests/Unit/PiantinaPdfServiceTest.php
Normal file
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\Territorio;
|
||||
use App\Models\TerritorioMappa;
|
||||
use App\Models\TerritorioPdfVersione;
|
||||
use App\Services\Piantine\PiantinaPdfService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use RuntimeException;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Composizione del PDF sul modulo S-12 e giro completo dello storico:
|
||||
* sostituzione, ripristino, e i casi in cui manca qualcosa.
|
||||
*/
|
||||
class PiantinaPdfServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Storage::fake('public');
|
||||
Storage::fake('local');
|
||||
}
|
||||
|
||||
/** Un territorio con immagine della piantina gia' renderizzata. */
|
||||
private function territorioConImmagine(): array
|
||||
{
|
||||
$territorio = Territorio::create([
|
||||
'numero' => '7',
|
||||
'attivo' => true,
|
||||
'confini' => "Nord: via Roma\nSud: via Milano",
|
||||
]);
|
||||
|
||||
$mappa = TerritorioMappa::create([
|
||||
'territorio_id' => $territorio->id,
|
||||
'geojson' => '{"type":"Polygon","coordinates":[[[1,1],[2,1],[2,2],[1,1]]]}',
|
||||
'image_path' => 'territori-mappe/territorio-' . $territorio->id . '.png',
|
||||
'rendered_at' => now(),
|
||||
]);
|
||||
|
||||
// Un PNG vero, piccolo: FPDF lo legge davvero, un finto no.
|
||||
$immagine = imagecreatetruecolor(80, 50);
|
||||
imagefilledrectangle($immagine, 0, 0, 79, 49, imagecolorallocate($immagine, 230, 230, 230));
|
||||
ob_start();
|
||||
imagepng($immagine);
|
||||
Storage::disk('public')->put($mappa->image_path, ob_get_clean());
|
||||
imagedestroy($immagine);
|
||||
|
||||
return [$territorio, $mappa];
|
||||
}
|
||||
|
||||
private function servizio(): PiantinaPdfService
|
||||
{
|
||||
return app(PiantinaPdfService::class);
|
||||
}
|
||||
|
||||
public function test_genera_produce_un_pdf_di_due_pagine(): void
|
||||
{
|
||||
[$territorio, $mappa] = $this->territorioConImmagine();
|
||||
|
||||
$pdf = $this->servizio()->genera($territorio, $mappa);
|
||||
|
||||
$this->assertStringStartsWith('%PDF', $pdf);
|
||||
// FPDF conta le pagine nel catalogo: /Count 2.
|
||||
$this->assertStringContainsString('/Count 2', $pdf);
|
||||
}
|
||||
|
||||
public function test_genera_senza_immagine_si_ferma_con_un_messaggio_chiaro(): void
|
||||
{
|
||||
[$territorio, $mappa] = $this->territorioConImmagine();
|
||||
Storage::disk('public')->delete($mappa->image_path);
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
|
||||
$this->servizio()->genera($territorio, $mappa);
|
||||
}
|
||||
|
||||
public function test_sostituisci_archivia_il_pdf_precedente_nello_storico(): void
|
||||
{
|
||||
[$territorio, $mappa] = $this->territorioConImmagine();
|
||||
Storage::disk('public')->put('territori-pdf/vecchio.pdf', '%PDF-vecchio');
|
||||
$territorio->update(['pdf_path' => 'territori-pdf/vecchio.pdf']);
|
||||
|
||||
$this->servizio()->sostituisci($territorio, $mappa);
|
||||
|
||||
$territorio->refresh();
|
||||
$this->assertNotSame('territori-pdf/vecchio.pdf', $territorio->pdf_path);
|
||||
Storage::disk('public')->assertExists($territorio->pdf_path);
|
||||
Storage::disk('public')->assertMissing('territori-pdf/vecchio.pdf');
|
||||
|
||||
$versione = TerritorioPdfVersione::sole();
|
||||
$this->assertSame($territorio->id, $versione->territorio_id);
|
||||
Storage::disk('public')->assertExists($versione->pdf_path);
|
||||
$this->assertSame('%PDF-vecchio', Storage::disk('public')->get($versione->pdf_path));
|
||||
}
|
||||
|
||||
public function test_sostituisci_senza_pdf_precedente_non_crea_versioni(): void
|
||||
{
|
||||
[$territorio, $mappa] = $this->territorioConImmagine();
|
||||
|
||||
$this->servizio()->sostituisci($territorio, $mappa);
|
||||
|
||||
$this->assertNotNull($territorio->refresh()->pdf_path);
|
||||
$this->assertSame(0, TerritorioPdfVersione::count());
|
||||
}
|
||||
|
||||
public function test_ripristina_rimette_in_vigore_la_versione_e_archivia_l_attuale(): void
|
||||
{
|
||||
[$territorio, $mappa] = $this->territorioConImmagine();
|
||||
Storage::disk('public')->put('territori-pdf/vecchio.pdf', '%PDF-vecchio');
|
||||
$territorio->update(['pdf_path' => 'territori-pdf/vecchio.pdf']);
|
||||
|
||||
$this->servizio()->sostituisci($territorio, $mappa);
|
||||
$versione = TerritorioPdfVersione::sole();
|
||||
|
||||
$this->servizio()->ripristina($versione);
|
||||
|
||||
$territorio->refresh();
|
||||
$this->assertSame('%PDF-vecchio', Storage::disk('public')->get($territorio->pdf_path));
|
||||
|
||||
// La versione usata e' sparita; al suo posto c'e' quella del PDF che
|
||||
// era in vigore, quindi il ripristino resta a sua volta reversibile.
|
||||
$this->assertSame(0, TerritorioPdfVersione::whereKey($versione->id)->count());
|
||||
$this->assertSame(1, TerritorioPdfVersione::count());
|
||||
$this->assertSame('ripristino', TerritorioPdfVersione::sole()->origine);
|
||||
}
|
||||
|
||||
public function test_ripristina_con_file_sparito_si_ferma_con_un_messaggio_chiaro(): void
|
||||
{
|
||||
[$territorio] = $this->territorioConImmagine();
|
||||
|
||||
$versione = TerritorioPdfVersione::create([
|
||||
'territorio_id' => $territorio->id,
|
||||
'pdf_path' => 'territori-pdf-versioni/inesistente.pdf',
|
||||
'origine' => 'sostituzione',
|
||||
'sostituito_il' => now(),
|
||||
]);
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
|
||||
$this->servizio()->ripristina($versione);
|
||||
}
|
||||
}
|
||||
86
tests/Unit/PiantinaRendererTest.php
Normal file
86
tests/Unit/PiantinaRendererTest.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\Territorio;
|
||||
use App\Models\TerritorioMappa;
|
||||
use App\Services\Piantine\PiantinaRenderer;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use RuntimeException;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Il client del container map-renderer: cosa succede quando risponde bene,
|
||||
* quando risponde con un errore e quando risponde con qualcosa che non e' un
|
||||
* PNG (un proxy che restituisce una pagina HTML, per esempio).
|
||||
*/
|
||||
class PiantinaRendererTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private const GEOMETRIA = '{"type":"Polygon","coordinates":[[[13.35,38.11],[13.37,38.11],[13.37,38.13],[13.35,38.11]]]}';
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Storage::fake('public');
|
||||
}
|
||||
|
||||
private function mappa(): TerritorioMappa
|
||||
{
|
||||
$territorio = Territorio::create(['numero' => '42', 'attivo' => true]);
|
||||
|
||||
return TerritorioMappa::create([
|
||||
'territorio_id' => $territorio->id,
|
||||
'geojson' => self::GEOMETRIA,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_un_rendering_riuscito_salva_il_png_e_aggiorna_la_mappa(): void
|
||||
{
|
||||
Http::fake(['*/render' => Http::response("\x89PNG\r\n\x1a\ncontenuto-finto")]);
|
||||
|
||||
$mappa = $this->mappa();
|
||||
$path = (new PiantinaRenderer())->render($mappa);
|
||||
|
||||
Storage::disk('public')->assertExists($path);
|
||||
$mappa->refresh();
|
||||
$this->assertSame($path, $mappa->image_path);
|
||||
$this->assertNotNull($mappa->rendered_at);
|
||||
}
|
||||
|
||||
public function test_un_errore_del_renderer_diventa_un_messaggio_leggibile(): void
|
||||
{
|
||||
Http::fake(['*/render' => Http::response(['error' => 'area troppo estesa'], 413)]);
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
$this->expectExceptionMessage('area troppo estesa');
|
||||
|
||||
(new PiantinaRenderer())->render($this->mappa());
|
||||
}
|
||||
|
||||
public function test_una_risposta_che_non_e_un_png_viene_rifiutata(): void
|
||||
{
|
||||
Http::fake(['*/render' => Http::response('<html>proxy error</html>')]);
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
|
||||
(new PiantinaRenderer())->render($this->mappa());
|
||||
}
|
||||
|
||||
public function test_una_geometria_corrotta_blocca_prima_di_chiamare_il_renderer(): void
|
||||
{
|
||||
Http::fake();
|
||||
|
||||
$mappa = $this->mappa();
|
||||
$mappa->update(['geojson' => 'non-json']);
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
|
||||
(new PiantinaRenderer())->render($mappa->refresh());
|
||||
|
||||
Http::assertNothingSent();
|
||||
}
|
||||
}
|
||||
117
tests/Unit/PiantineAreaTest.php
Normal file
117
tests/Unit/PiantineAreaTest.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Services\Piantine\PiantineArea;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use RuntimeException;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Il KML dell'area citta': dal file caricato alla FeatureCollection su disco.
|
||||
*
|
||||
* I KML reali arrivano da sorgenti diverse (Google My Maps, Google Earth,
|
||||
* export storici) e dichiarano il namespace in modi diversi: il parsing deve
|
||||
* digerirli tutti, perche' l'amministratore non puo' "sistemare" un KML.
|
||||
*/
|
||||
class PiantineAreaTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private const KML_POLIGONO = <<<'XML'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<kml xmlns="http://www.opengis.net/kml/2.2">
|
||||
<Document>
|
||||
<Placemark>
|
||||
<name>Area centro</name>
|
||||
<Polygon><outerBoundaryIs><LinearRing>
|
||||
<coordinates>
|
||||
13.35,38.11,0 13.37,38.11,0 13.37,38.13,0 13.35,38.11,0
|
||||
</coordinates>
|
||||
</LinearRing></outerBoundaryIs></Polygon>
|
||||
</Placemark>
|
||||
</Document>
|
||||
</kml>
|
||||
XML;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Storage::fake('local');
|
||||
}
|
||||
|
||||
public function test_un_poligono_con_namespace_ogc_viene_riconosciuto(): void
|
||||
{
|
||||
$geojson = (new PiantineArea())->kmlInGeoJson(self::KML_POLIGONO);
|
||||
|
||||
$this->assertSame('FeatureCollection', $geojson['type']);
|
||||
$this->assertCount(1, $geojson['features']);
|
||||
|
||||
$feature = $geojson['features'][0];
|
||||
$this->assertSame('Area centro', $feature['properties']['name']);
|
||||
$this->assertSame('Polygon', $feature['geometry']['type']);
|
||||
$this->assertSame([13.35, 38.11], $feature['geometry']['coordinates'][0][0]);
|
||||
$this->assertCount(4, $feature['geometry']['coordinates'][0]);
|
||||
}
|
||||
|
||||
public function test_una_linestring_senza_namespace_viene_riconosciuta(): void
|
||||
{
|
||||
$kml = <<<'XML'
|
||||
<?xml version="1.0"?>
|
||||
<kml><Document><Folder><Placemark>
|
||||
<name>Perimetro</name>
|
||||
<LineString><coordinates>13.1,38.1 13.2,38.2 13.3,38.1</coordinates></LineString>
|
||||
</Placemark></Folder></Document></kml>
|
||||
XML;
|
||||
|
||||
$geojson = (new PiantineArea())->kmlInGeoJson($kml);
|
||||
|
||||
$this->assertCount(1, $geojson['features']);
|
||||
$this->assertSame('LineString', $geojson['features'][0]['geometry']['type']);
|
||||
$this->assertCount(3, $geojson['features'][0]['geometry']['coordinates']);
|
||||
}
|
||||
|
||||
public function test_un_kml_senza_geometrie_viene_rifiutato(): void
|
||||
{
|
||||
$this->expectException(RuntimeException::class);
|
||||
|
||||
(new PiantineArea())->salvaKml('<kml><Document></Document></kml>', 'vuoto.kml');
|
||||
}
|
||||
|
||||
public function test_un_file_non_xml_viene_rifiutato(): void
|
||||
{
|
||||
$this->expectException(RuntimeException::class);
|
||||
|
||||
(new PiantineArea())->salvaKml('questo non è xml', 'rotto.kml');
|
||||
}
|
||||
|
||||
public function test_salva_kml_scrive_area_e_configurazione(): void
|
||||
{
|
||||
$area = new PiantineArea();
|
||||
|
||||
$quante = $area->salvaKml(self::KML_POLIGONO, 'citta.kml');
|
||||
|
||||
$this->assertSame(1, $quante);
|
||||
Storage::disk('local')->assertExists('piantine/area.kml');
|
||||
Storage::disk('local')->assertExists('piantine/area.geojson');
|
||||
|
||||
$this->assertSame('citta.kml', $area->config()['kml_nome']);
|
||||
$this->assertSame('Polygon', $area->area()['features'][0]['geometry']['type']);
|
||||
}
|
||||
|
||||
public function test_la_localita_esplicita_vince_sul_nome_congregazione(): void
|
||||
{
|
||||
\App\Models\Setting::firstOrCreate([], ['congregazione_nome' => 'Congregazione di prova']);
|
||||
|
||||
$area = new PiantineArea();
|
||||
$this->assertSame('Congregazione di prova', $area->localita());
|
||||
|
||||
$area->impostaLocalita('Palermo Nord');
|
||||
$this->assertSame('Palermo Nord', $area->localita());
|
||||
|
||||
// Svuotarla riporta al nome della congregazione.
|
||||
$area->impostaLocalita(' ');
|
||||
$this->assertSame('Congregazione di prova', $area->localita());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user