From 3a5c6b179b53f320f818bd58431aa1a82dc51212 Mon Sep 17 00:00:00 2001 From: Attila Fulop <1162360+fulopattila122@users.noreply.github.com> Date: Thu, 21 Nov 2024 20:03:12 +0200 Subject: [PATCH] Added ProvinceSeeders registry and interface --- Changelog.md | 7 ++ composer.json | 3 +- src/Contracts/ProvinceSeeder.php | 16 ++++ src/Providers/ModuleServiceProvider.php | 33 +++++++ .../database/seeds/CountiesOfHungary.php | 9 +- .../database/seeds/CountiesOfRomania.php | 9 +- .../database/seeds/IsProvinceSeeder.php | 22 +++++ .../database/seeds/ProvinceSeeders.php | 47 +++++++++ .../seeds/ProvincesAndRegionsOfBelgium.php | 9 +- .../seeds/ProvincesAndTerritoriesOfCanada.php | 9 +- .../ProvincesAndTerritoriesOfCanadaFrench.php | 9 +- .../database/seeds/ProvincesOfIndonesia.php | 9 +- .../database/seeds/ProvincesOfNetherlands.php | 10 +- .../seeds/StatesAndTerritoriesOfIndia.php | 8 +- .../database/seeds/StatesOfGermany.php | 9 +- src/resources/database/seeds/StatesOfUsa.php | 8 +- tests/Seeders/ProvinceSeederRegistryTest.php | 96 +++++++++++++++++++ 17 files changed, 302 insertions(+), 11 deletions(-) create mode 100644 src/Contracts/ProvinceSeeder.php create mode 100644 src/resources/database/seeds/IsProvinceSeeder.php create mode 100644 src/resources/database/seeds/ProvinceSeeders.php create mode 100644 tests/Seeders/ProvinceSeederRegistryTest.php diff --git a/Changelog.md b/Changelog.md index 6d7f57e..6e1623c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,12 @@ # Address Module Changelog +## Unreleased +##### 2024-XX-YY + +- Added the konekt/xtend requirement +- Added the `ProvinceSeeders` registry +- Added the `ProvinceSeeder` interface and decorated all existing province seeders with it + ## 3.3.2 ##### 2024-10-22 diff --git a/composer.json b/composer.json index 8bc2a5d..fed2aba 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,8 @@ "doctrine/dbal": "^3.5.1|^4.0", "konekt/concord": "^1.13", "konekt/enum": "^4.1", - "konekt/enum-eloquent": "^1.9" + "konekt/enum-eloquent": "^1.9", + "konekt/xtend": "^1.2" }, "autoload": { "psr-4": { diff --git a/src/Contracts/ProvinceSeeder.php b/src/Contracts/ProvinceSeeder.php new file mode 100644 index 0000000..e35b39b --- /dev/null +++ b/src/Contracts/ProvinceSeeder.php @@ -0,0 +1,16 @@ +insert([ diff --git a/src/resources/database/seeds/CountiesOfRomania.php b/src/resources/database/seeds/CountiesOfRomania.php index ad7ad4f..98bd9a3 100644 --- a/src/resources/database/seeds/CountiesOfRomania.php +++ b/src/resources/database/seeds/CountiesOfRomania.php @@ -15,9 +15,16 @@ namespace Konekt\Address\Seeds; use Illuminate\Database\Seeder; +use Konekt\Address\Contracts\ProvinceSeeder; +use Konekt\Address\Models\ProvinceType; -class CountiesOfRomania extends Seeder +class CountiesOfRomania extends Seeder implements ProvinceSeeder { + use IsProvinceSeeder; + + protected static string $forCountry = 'RO'; + protected static array $provinceTypes = [ProvinceType::COUNTY]; + /** * Inserts the counties of Romania into the provinces table * diff --git a/src/resources/database/seeds/IsProvinceSeeder.php b/src/resources/database/seeds/IsProvinceSeeder.php new file mode 100644 index 0000000..b846b72 --- /dev/null +++ b/src/resources/database/seeds/IsProvinceSeeder.php @@ -0,0 +1,22 @@ + is_string($type) ? ProvinceTypeProxy::create($type): $type, self::$provinceTypes); + } + +} diff --git a/src/resources/database/seeds/ProvinceSeeders.php b/src/resources/database/seeds/ProvinceSeeders.php new file mode 100644 index 0000000..79c570c --- /dev/null +++ b/src/resources/database/seeds/ProvinceSeeders.php @@ -0,0 +1,47 @@ +make($class, $parameters); + } + + /** @return ProvinceSeeder[] */ + public static function availableSeedersOfCountry(string $country): array + { + return array_filter(self::$registry, fn($class) => $class::getCountryCode() === $country); + } + + public static function extend(string $seederClass): void + { + self::add(self::generateId($seederClass), $seederClass); + } + + private static function generateId(string $class): string + { + return Str::snake(class_basename($class)); + } +} diff --git a/src/resources/database/seeds/ProvincesAndRegionsOfBelgium.php b/src/resources/database/seeds/ProvincesAndRegionsOfBelgium.php index c65b2aa..9863bd6 100644 --- a/src/resources/database/seeds/ProvincesAndRegionsOfBelgium.php +++ b/src/resources/database/seeds/ProvincesAndRegionsOfBelgium.php @@ -15,9 +15,16 @@ namespace Konekt\Address\Seeds; use Illuminate\Database\Seeder; +use Konekt\Address\Contracts\ProvinceSeeder; +use Konekt\Address\Models\ProvinceType; -class ProvincesAndRegionsOfBelgium extends Seeder +class ProvincesAndRegionsOfBelgium extends Seeder implements ProvinceSeeder { + use IsProvinceSeeder; + + protected static string $forCountry = 'BE'; + protected static array $provinceTypes = [ProvinceType::REGION, ProvinceType::PROVINCE]; + public function run() { \DB::table('provinces')->insert([ diff --git a/src/resources/database/seeds/ProvincesAndTerritoriesOfCanada.php b/src/resources/database/seeds/ProvincesAndTerritoriesOfCanada.php index 631db21..d30af01 100644 --- a/src/resources/database/seeds/ProvincesAndTerritoriesOfCanada.php +++ b/src/resources/database/seeds/ProvincesAndTerritoriesOfCanada.php @@ -16,9 +16,16 @@ use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; +use Konekt\Address\Contracts\ProvinceSeeder; +use Konekt\Address\Models\ProvinceType; -class ProvincesAndTerritoriesOfCanada extends Seeder +class ProvincesAndTerritoriesOfCanada extends Seeder implements ProvinceSeeder { + use IsProvinceSeeder; + + protected static string $forCountry = 'CA'; + protected static array $provinceTypes = [ProvinceType::PROVINCE, ProvinceType::TERRITORY]; + public function run() { DB::table('provinces')->insert([ diff --git a/src/resources/database/seeds/ProvincesAndTerritoriesOfCanadaFrench.php b/src/resources/database/seeds/ProvincesAndTerritoriesOfCanadaFrench.php index 2f73f14..dd61952 100644 --- a/src/resources/database/seeds/ProvincesAndTerritoriesOfCanadaFrench.php +++ b/src/resources/database/seeds/ProvincesAndTerritoriesOfCanadaFrench.php @@ -16,9 +16,16 @@ use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; +use Konekt\Address\Contracts\ProvinceSeeder; +use Konekt\Address\Models\ProvinceType; -class ProvincesAndTerritoriesOfCanadaFrench extends Seeder +class ProvincesAndTerritoriesOfCanadaFrench extends Seeder implements ProvinceSeeder { + use IsProvinceSeeder; + + protected static string $forCountry = 'CA'; + protected static array $provinceTypes = [ProvinceType::PROVINCE, ProvinceType::TERRITORY]; + public function run() { DB::table('provinces')->insert([ diff --git a/src/resources/database/seeds/ProvincesOfIndonesia.php b/src/resources/database/seeds/ProvincesOfIndonesia.php index afaad6e..dabaffc 100644 --- a/src/resources/database/seeds/ProvincesOfIndonesia.php +++ b/src/resources/database/seeds/ProvincesOfIndonesia.php @@ -15,9 +15,16 @@ namespace Konekt\Address\Seeds; use Illuminate\Database\Seeder; +use Konekt\Address\Contracts\ProvinceSeeder; +use Konekt\Address\Models\ProvinceType; -class ProvincesOfIndonesia extends Seeder +class ProvincesOfIndonesia extends Seeder implements ProvinceSeeder { + use IsProvinceSeeder; + + protected static string $forCountry = 'ID'; + protected static array $provinceTypes = [ProvinceType::UNIT, ProvinceType::PROVINCE, ProvinceType::REGION]; + public function run() { $this->createJavaWithProvinces(); diff --git a/src/resources/database/seeds/ProvincesOfNetherlands.php b/src/resources/database/seeds/ProvincesOfNetherlands.php index 6f22bb3..afd9262 100644 --- a/src/resources/database/seeds/ProvincesOfNetherlands.php +++ b/src/resources/database/seeds/ProvincesOfNetherlands.php @@ -15,9 +15,17 @@ namespace Konekt\Address\Seeds; use Illuminate\Database\Seeder; +use Konekt\Address\Contracts\ProvinceSeeder; +use Konekt\Address\Models\ProvinceType; -class ProvincesOfNetherlands extends Seeder +class ProvincesOfNetherlands extends Seeder implements ProvinceSeeder { + use IsProvinceSeeder; + + protected static string $forCountry = 'NL'; + protected static array $provinceTypes = [ProvinceType::PROVINCE]; + + public function run() { \DB::table('provinces')->insert([ diff --git a/src/resources/database/seeds/StatesAndTerritoriesOfIndia.php b/src/resources/database/seeds/StatesAndTerritoriesOfIndia.php index ad77b6b..5dd9d29 100644 --- a/src/resources/database/seeds/StatesAndTerritoriesOfIndia.php +++ b/src/resources/database/seeds/StatesAndTerritoriesOfIndia.php @@ -15,10 +15,16 @@ namespace Konekt\Address\Seeds; use Illuminate\Database\Seeder; +use Konekt\Address\Contracts\ProvinceSeeder; use Konekt\Address\Models\ProvinceType; -class StatesAndTerritoriesOfIndia extends Seeder +class StatesAndTerritoriesOfIndia extends Seeder implements ProvinceSeeder { + use IsProvinceSeeder; + + protected static string $forCountry = 'IN'; + protected static array $provinceTypes = [ProvinceType::STATE, ProvinceType::TERRITORY]; + public function run() { \DB::table('provinces')->insert([ diff --git a/src/resources/database/seeds/StatesOfGermany.php b/src/resources/database/seeds/StatesOfGermany.php index 1be760e..9c038c7 100644 --- a/src/resources/database/seeds/StatesOfGermany.php +++ b/src/resources/database/seeds/StatesOfGermany.php @@ -15,9 +15,16 @@ namespace Konekt\Address\Seeds; use Illuminate\Database\Seeder; +use Konekt\Address\Contracts\ProvinceSeeder; +use Konekt\Address\Models\ProvinceType; -class StatesOfGermany extends Seeder +class StatesOfGermany extends Seeder implements ProvinceSeeder { + use IsProvinceSeeder; + + protected static string $forCountry = 'DE'; + protected static array $provinceTypes = [ProvinceType::STATE]; + public function run() { \DB::table('provinces')->insert([ diff --git a/src/resources/database/seeds/StatesOfUsa.php b/src/resources/database/seeds/StatesOfUsa.php index b13e301..c784c24 100644 --- a/src/resources/database/seeds/StatesOfUsa.php +++ b/src/resources/database/seeds/StatesOfUsa.php @@ -15,10 +15,16 @@ namespace Konekt\Address\Seeds; use Illuminate\Database\Seeder; +use Konekt\Address\Contracts\ProvinceSeeder; use Konekt\Address\Models\ProvinceType; -class StatesOfUsa extends Seeder +class StatesOfUsa extends Seeder implements ProvinceSeeder { + use IsProvinceSeeder; + + protected static string $forCountry = 'US'; + protected static array $provinceTypes = [ProvinceType::STATE, ProvinceType::FEDERAL_DISTRICT, ProvinceType::MILITARY, ProvinceType::TERRITORY]; + /** * Inserts the states of USA into the provinces table * diff --git a/tests/Seeders/ProvinceSeederRegistryTest.php b/tests/Seeders/ProvinceSeederRegistryTest.php new file mode 100644 index 0000000..3e62651 --- /dev/null +++ b/tests/Seeders/ProvinceSeederRegistryTest.php @@ -0,0 +1,96 @@ +assertCount(10, ProvinceSeeders::choices()); + } + + /** @test */ + public function it_has_two_seeders_for_canada() + { + $this->assertCount(2, ProvinceSeeders::availableSeedersOfCountry('CA')); + } + + /** @test */ + public function it_has_one_seeder_for_usa() + { + $seeders = ProvinceSeeders::availableSeedersOfCountry('US'); + $this->assertCount(1, $seeders); + $this->assertEquals(StatesOfUsa::class, Arr::first($seeders)); + } + + /** @test */ + public function it_has_one_seeder_for_indonesia() + { + $seeders = ProvinceSeeders::availableSeedersOfCountry('ID'); + $this->assertCount(1, $seeders); + $this->assertEquals(ProvincesOfIndonesia::class, Arr::first($seeders)); + } + + /** @test */ + public function it_has_one_seeder_for_belgium() + { + $seeders = ProvinceSeeders::availableSeedersOfCountry('BE'); + $this->assertCount(1, $seeders); + $this->assertEquals(ProvincesAndRegionsOfBelgium::class, Arr::first($seeders)); + } + + /** @test */ + public function it_has_one_seeder_for_netherlands() + { + $seeders = ProvinceSeeders::availableSeedersOfCountry('NL'); + $this->assertCount(1, $seeders); + $this->assertEquals(ProvincesOfNetherlands::class, Arr::first($seeders)); + } + + /** @test */ + public function it_has_one_seeder_for_germany() + { + $seeders = ProvinceSeeders::availableSeedersOfCountry('DE'); + $this->assertCount(1, $seeders); + $this->assertEquals(StatesOfGermany::class, Arr::first($seeders)); + } + + /** @test */ + public function it_has_one_seeder_for_hungary() + { + $seeders = ProvinceSeeders::availableSeedersOfCountry('HU'); + $this->assertCount(1, $seeders); + $this->assertEquals(CountiesOfHungary::class, Arr::first($seeders)); + } + + /** @test */ + public function it_has_one_seeder_for_romania() + { + $seeders = ProvinceSeeders::availableSeedersOfCountry('RO'); + $this->assertCount(1, $seeders); + $this->assertEquals(CountiesOfRomania::class, Arr::first($seeders)); + } + + /** @test */ + public function it_has_one_seeder_for_india() + { + $seeders = ProvinceSeeders::availableSeedersOfCountry('IN'); + $this->assertCount(1, $seeders); + $this->assertEquals(StatesAndTerritoriesOfIndia::class, Arr::first($seeders)); + } +}