169 lines
5.6 KiB
PHP
169 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Etl;
|
|
|
|
use App\Etl\Importers\InstitutionImporter;
|
|
use App\Models\Institution;
|
|
use Database\Seeders\InstitutionCategorySeeder;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\Concerns\CreatesLegacyDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class InstitutionImporterTest extends TestCase
|
|
{
|
|
use CreatesLegacyDatabase;
|
|
use RefreshDatabase;
|
|
|
|
/** Categorie v2 con gli id legacy fissi (2,3,4,6). */
|
|
private function seedV2Categories(): void
|
|
{
|
|
$this->seed(InstitutionCategorySeeder::class);
|
|
}
|
|
|
|
public function test_it_imports_an_institution_and_creates_the_official_link(): void
|
|
{
|
|
$this->seedV2Categories();
|
|
$this->fakeLegacyConnection();
|
|
$this->insertLegacyInstitution([
|
|
'id' => 1,
|
|
'category' => 3,
|
|
'name' => 'Blekinge Museum',
|
|
'abbreviation' => 'BLM',
|
|
'city' => 'Karlskrona',
|
|
'url' => 'https://blekingemuseum.se/',
|
|
]);
|
|
|
|
$summary = (new InstitutionImporter)->import(false);
|
|
|
|
$this->assertSame(1, $summary->created);
|
|
$this->assertDatabaseHas('institutions', [
|
|
'legacy_id' => 1,
|
|
'category_id' => 3,
|
|
'name' => 'Blekinge Museum',
|
|
'city' => 'Karlskrona',
|
|
]);
|
|
|
|
$institution = Institution::where('legacy_id', 1)->firstOrFail();
|
|
$this->assertNotEmpty($institution->uuid);
|
|
$this->assertDatabaseHas('institution_links', [
|
|
'institution_id' => $institution->id,
|
|
'type' => 'official',
|
|
'url' => 'https://blekingemuseum.se/',
|
|
]);
|
|
}
|
|
|
|
public function test_it_creates_no_link_when_the_url_is_blank(): void
|
|
{
|
|
$this->seedV2Categories();
|
|
$this->fakeLegacyConnection();
|
|
$this->insertLegacyInstitution(['id' => 21, 'url' => ' ']);
|
|
|
|
(new InstitutionImporter)->import(false);
|
|
|
|
$this->assertDatabaseCount('institution_links', 0);
|
|
}
|
|
|
|
public function test_it_trims_textual_fields(): void
|
|
{
|
|
$this->seedV2Categories();
|
|
$this->fakeLegacyConnection();
|
|
$this->insertLegacyInstitution(['id' => 20, 'name' => 'Lund University Library ']);
|
|
|
|
(new InstitutionImporter)->import(false);
|
|
|
|
$this->assertDatabaseHas('institutions', ['legacy_id' => 20, 'name' => 'Lund University Library']);
|
|
}
|
|
|
|
public function test_it_falls_back_to_the_default_color_when_legacy_color_is_blank(): void
|
|
{
|
|
$this->seedV2Categories();
|
|
$this->fakeLegacyConnection();
|
|
$this->insertLegacyInstitution(['id' => 1, 'color' => null]);
|
|
|
|
(new InstitutionImporter)->import(false);
|
|
|
|
$this->assertDatabaseHas('institutions', ['legacy_id' => 1, 'color' => '#c5cae9']);
|
|
}
|
|
|
|
public function test_it_skips_institutions_with_an_unknown_category(): void
|
|
{
|
|
$this->seedV2Categories();
|
|
$this->fakeLegacyConnection();
|
|
$this->insertLegacyInstitution(['id' => 99, 'category' => 999]);
|
|
|
|
$summary = (new InstitutionImporter)->import(false);
|
|
|
|
$this->assertSame(1, $summary->skipped);
|
|
$this->assertNotEmpty($summary->warnings);
|
|
$this->assertDatabaseMissing('institutions', ['legacy_id' => 99]);
|
|
}
|
|
|
|
public function test_it_regenerates_the_uuid_instead_of_copying_the_legacy_one(): void
|
|
{
|
|
$this->seedV2Categories();
|
|
$this->fakeLegacyConnection();
|
|
$legacyUuid = '00000000-0000-0000-0000-000000000000';
|
|
$this->insertLegacyInstitution(['id' => 1, 'uuid' => $legacyUuid]);
|
|
|
|
(new InstitutionImporter)->import(false);
|
|
|
|
$this->assertNotSame($legacyUuid, Institution::where('legacy_id', 1)->value('uuid'));
|
|
}
|
|
|
|
public function test_it_is_idempotent_and_keeps_the_uuid_stable(): void
|
|
{
|
|
$this->seedV2Categories();
|
|
$this->fakeLegacyConnection();
|
|
$this->insertLegacyInstitution(['id' => 1]);
|
|
|
|
$first = (new InstitutionImporter)->import(false);
|
|
$uuid = Institution::where('legacy_id', 1)->value('uuid');
|
|
$second = (new InstitutionImporter)->import(false);
|
|
|
|
$this->assertSame(1, $first->created);
|
|
$this->assertSame(0, $second->created);
|
|
$this->assertSame(1, $second->updated);
|
|
$this->assertDatabaseCount('institutions', 1);
|
|
$this->assertSame($uuid, Institution::where('legacy_id', 1)->value('uuid'));
|
|
}
|
|
|
|
public function test_dry_run_writes_nothing(): void
|
|
{
|
|
$this->seedV2Categories();
|
|
$this->fakeLegacyConnection();
|
|
$this->insertLegacyInstitution(['id' => 1]);
|
|
|
|
$summary = (new InstitutionImporter)->import(true);
|
|
|
|
$this->assertSame(1, $summary->created);
|
|
$this->assertDatabaseCount('institutions', 0);
|
|
}
|
|
|
|
public function test_dry_run_counts_an_existing_institution_as_updated(): void
|
|
{
|
|
$this->seedV2Categories();
|
|
$this->fakeLegacyConnection();
|
|
$this->insertLegacyInstitution(['id' => 1]);
|
|
|
|
(new InstitutionImporter)->import(false);
|
|
$summary = (new InstitutionImporter)->import(true);
|
|
|
|
$this->assertSame(0, $summary->created);
|
|
$this->assertSame(1, $summary->updated);
|
|
$this->assertDatabaseCount('institutions', 1);
|
|
}
|
|
|
|
public function test_it_warns_and_skips_when_the_v2_lookup_is_empty(): void
|
|
{
|
|
// Nessun seed delle categorie v2.
|
|
$this->fakeLegacyConnection();
|
|
$this->insertLegacyInstitution(['id' => 1]);
|
|
|
|
$summary = (new InstitutionImporter)->import(false);
|
|
|
|
$this->assertNotEmpty($summary->warnings);
|
|
$this->assertSame(0, $summary->total());
|
|
$this->assertDatabaseCount('institutions', 0);
|
|
}
|
|
}
|