From 16625b6aa1dc4d88356ba1c26712eb1b90f0929a Mon Sep 17 00:00:00 2001 From: Muhajirin Ilyas Date: Tue, 19 Mar 2024 11:04:06 +0700 Subject: [PATCH] Laravel 11 support and improvement (#4) * attempt to upgrade * move migration folder * fix test and documenting generic type * update github action * bot: cs fix --------- Co-authored-by: muhajirinlpu --- .github/workflows/dependabot-auto-merge.yml | 2 +- .github/workflows/lint.yml | 4 +-- .github/workflows/run-tests.yml | 4 +-- .github/workflows/update-changelog.yml | 4 +-- .gitignore | 4 ++- composer.json | 12 ++++----- phpunit.xml | 23 +++++++---------- src/Concerns/HasIndonesiaRegionData.php | 16 ++++++++++++ src/Contracts/IndoRegionResolver.php | 13 ++++++++++ src/IndoRegionServiceProvider.php | 2 -- src/Models/District.php | 18 +++++++++---- src/Models/Province.php | 16 +++++++++--- src/Models/Regency.php | 25 ++++++++++++++----- src/Models/Village.php | 13 +++++++--- src/RawDataGetter.php | 20 +++++++++++++++ tests/Feature/DistrictTest.php | 3 ++- tests/Feature/ProvinceTest.php | 1 + tests/Feature/RegencyTest.php | 1 + tests/Feature/VillageTest.php | 1 + tests/TestCase.php | 7 ++++-- ...022_10_09_142019_create_dummies_tables.php | 0 21 files changed, 137 insertions(+), 52 deletions(-) rename {tests/Stuff => workbench}/migrations/2022_10_09_142019_create_dummies_tables.php (100%) diff --git a/.github/workflows/dependabot-auto-merge.yml b/.github/workflows/dependabot-auto-merge.yml index d7685f3..f98ff2a 100644 --- a/.github/workflows/dependabot-auto-merge.yml +++ b/.github/workflows/dependabot-auto-merge.yml @@ -13,7 +13,7 @@ jobs: - name: Dependabot metadata id: metadata - uses: dependabot/fetch-metadata@v1.3.0 + uses: dependabot/fetch-metadata@v1.6.0 with: github-token: "${{ secrets.GITHUB_TOKEN }}" diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index c7f152c..4d9f841 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -10,7 +10,7 @@ jobs: php-lint: runs-on: ubuntu-22.04 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 with: @@ -21,7 +21,7 @@ jobs: run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist --ignore-platform-reqs - name: Run Laravel Pint for CS Fixer run: ./vendor/bin/pint --config pint.json - - uses: stefanzweifel/git-auto-commit-action@v4 + - uses: stefanzweifel/git-auto-commit-action@v5 with: commit_user_name: 'dicibibot' commit_user_email: 'dicibiofficial@gmail.com' diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 93ed8c1..27fef56 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -9,14 +9,14 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest] - php: [8.1] + php: [8.2, 8.3] stability: [prefer-lowest, prefer-stable] name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }} steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 diff --git a/.github/workflows/update-changelog.yml b/.github/workflows/update-changelog.yml index 215e50b..f3bee89 100644 --- a/.github/workflows/update-changelog.yml +++ b/.github/workflows/update-changelog.yml @@ -10,7 +10,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: ref: main @@ -21,7 +21,7 @@ jobs: release-notes: ${{ github.event.release.body }} - name: Commit updated CHANGELOG - uses: stefanzweifel/git-auto-commit-action@v4 + uses: stefanzweifel/git-auto-commit-action@v5 with: branch: main commit_message: Update CHANGELOG diff --git a/.gitignore b/.gitignore index c1d6dc4..91df935 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ /vendor composer.lock .idea -.phpunit.result.cache \ No newline at end of file +.phpunit.result.cache +.phpunit.cache/ +build/ \ No newline at end of file diff --git a/composer.json b/composer.json index c9675fe..d2ab5c0 100644 --- a/composer.json +++ b/composer.json @@ -25,16 +25,16 @@ ], "require": { "php": "^8.1", - "illuminate/support": "^8.0|^9.0|^10.0", - "illuminate/console": "^8.0|^9.0|^10.0", - "illuminate/database": "^8.0|^9.0|^10.0", + "illuminate/support": "^8.0|^9.0|^10.0|^11.0", + "illuminate/console": "^8.0|^9.0|^10.0|^11.0", + "illuminate/database": "^8.0|^9.0|^10.0|^11.0", "league/csv": "^9.8.0" }, "require-dev": { "roave/security-advisories": "dev-latest", - "orchestra/testbench": "^8.0", + "orchestra/testbench": "^9.0", "laravel/pint": "^1.1", - "pestphp/pest": "^1.22" + "pestphp/pest": "^2.34" }, "autoload": { "psr-4": { @@ -48,7 +48,7 @@ } }, "scripts": { - "test": "vendor/bin/phpunit", + "test": "vendor/bin/pest", "post-autoload-dump": [ "@php ./vendor/bin/testbench package:discover --ansi" ] diff --git a/phpunit.xml b/phpunit.xml index e0c44d0..20668ed 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,14 +1,8 @@ - + - - src/ - @@ -20,8 +14,9 @@ tests - - - - - \ No newline at end of file + + + src/ + + + diff --git a/src/Concerns/HasIndonesiaRegionData.php b/src/Concerns/HasIndonesiaRegionData.php index b1d73c8..9c34ee1 100644 --- a/src/Concerns/HasIndonesiaRegionData.php +++ b/src/Concerns/HasIndonesiaRegionData.php @@ -4,25 +4,41 @@ use Dicibi\IndoRegion\Enums\Feature; use Dicibi\IndoRegion\IndoRegion; +use Dicibi\IndoRegion\Models\District; +use Dicibi\IndoRegion\Models\Province; +use Dicibi\IndoRegion\Models\Regency; +use Dicibi\IndoRegion\Models\Village; use Illuminate\Database\Eloquent\Relations; trait HasIndonesiaRegionData { + /** + * @return Relations\BelongsTo + */ public function province(): Relations\BelongsTo { return $this->belongsTo(config('indoregion.models.province'), IndoRegion::getForeignKeyId(Feature::Province)); } + /** + * @return Relations\BelongsTo + */ public function regency(): Relations\BelongsTo { return $this->belongsTo(config('indoregion.models.regency'), IndoRegion::getForeignKeyId(Feature::Regency)); } + /** + * @return Relations\BelongsTo + */ public function district(): Relations\BelongsTo { return $this->belongsTo(config('indoregion.models.district'), IndoRegion::getForeignKeyId(Feature::District)); } + /** + * @return Relations\BelongsTo + */ public function village(): Relations\BelongsTo { return $this->belongsTo(config('indoregion.models.village'), IndoRegion::getForeignKeyId(Feature::Village)); diff --git a/src/Contracts/IndoRegionResolver.php b/src/Contracts/IndoRegionResolver.php index 10eabdc..aa603e9 100644 --- a/src/Contracts/IndoRegionResolver.php +++ b/src/Contracts/IndoRegionResolver.php @@ -5,15 +5,28 @@ use Dicibi\IndoRegion\Models\District; use Dicibi\IndoRegion\Models\Province; use Dicibi\IndoRegion\Models\Regency; +use Dicibi\IndoRegion\Models\Village; use Illuminate\Contracts\Pagination\CursorPaginator; interface IndoRegionResolver { + /** + * @return CursorPaginator + */ public function getProvinces(?string $searchQuery = null): CursorPaginator; + /** + * @return CursorPaginator + */ public function getRegencies(Province $province, ?string $searchQuery = null): CursorPaginator; + /** + * @return CursorPaginator + */ public function getDistricts(Regency $regency, ?string $searchQuery = null): CursorPaginator; + /** + * @return CursorPaginator + */ public function getVillages(District $district, ?string $searchQuery = null): CursorPaginator; } diff --git a/src/IndoRegionServiceProvider.php b/src/IndoRegionServiceProvider.php index 625f064..a8394e6 100644 --- a/src/IndoRegionServiceProvider.php +++ b/src/IndoRegionServiceProvider.php @@ -19,9 +19,7 @@ public function boot(): void $this->publishes([ __DIR__.'/../config' => $this->app->basePath('config'), ], 'indoregion-config'); - } - if ($this->app->runningInConsole()) { $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); } } diff --git a/src/Models/District.php b/src/Models/District.php index 600a29e..eb47314 100755 --- a/src/Models/District.php +++ b/src/Models/District.php @@ -17,11 +17,11 @@ /** * District Model. * - * @property int $id - * @property string $name - * @property Regency $regency - * @property \Illuminate\Database\Eloquent\Collection $villages - * @property int|string $idn_regency_id + * @property int $id + * @property string $name + * @property Regency $regency + * @property \Illuminate\Database\Eloquent\Collection $villages + * @property int|string $idn_regency_id */ class District extends Model { @@ -41,11 +41,19 @@ public function getTable(): string return IndoRegion::getTable(Feature::District); } + /** + * Get the regency that owns the district. + * + * @return Relations\BelongsTo + */ public function regency(): Relations\BelongsTo { return $this->belongsTo(config('indoregion.models.regency'), IndoRegion::getForeignKeyId(Feature::Regency)); } + /** + * @return Relations\HasMany + */ public function villages(): Relations\HasMany { return $this->hasMany(config('indoregion.models.village'), IndoRegion::getForeignKeyId(Feature::District)); diff --git a/src/Models/Province.php b/src/Models/Province.php index b67fc9c..8e4c341 100755 --- a/src/Models/Province.php +++ b/src/Models/Province.php @@ -17,10 +17,10 @@ /** * Province Model. * - * @property string $name - * @property \Illuminate\Database\Eloquent\Collection $regencies - * @property \Illuminate\Database\Eloquent\Collection $districts - * @property int $id + * @property string $name + * @property \Illuminate\Database\Eloquent\Collection $regencies + * @property \Illuminate\Database\Eloquent\Collection $districts + * @property int $id */ class Province extends Model { @@ -31,11 +31,19 @@ public function getTable(): string return IndoRegion::getTable(Feature::Province); } + /** + * Get all the regencies for the Province + * + * @return Relations\HasMany + */ public function regencies(): Relations\HasMany { return $this->hasMany(config('indoregion.models.regency'), IndoRegion::getForeignKeyId(Feature::Province)); } + /** + * @return Relations\HasManyThrough + */ public function districts(): Relations\HasManyThrough { return $this->hasManyThrough( diff --git a/src/Models/Regency.php b/src/Models/Regency.php index ce1c0a6..32e39b7 100755 --- a/src/Models/Regency.php +++ b/src/Models/Regency.php @@ -17,12 +17,12 @@ /** * Regency Model. * - * @property string $name - * @property \Dicibi\IndoRegion\Models\Province $province - * @property \Illuminate\Database\Eloquent\Collection $districts - * @property \Illuminate\Database\Eloquent\Collection $villages - * @property int|string $idn_province_id - * @property int $id + * @property string $name + * @property \Dicibi\IndoRegion\Models\Province $province + * @property \Illuminate\Database\Eloquent\Collection $districts + * @property \Illuminate\Database\Eloquent\Collection $villages + * @property int|string $idn_province_id + * @property int $id */ class Regency extends Model { @@ -42,16 +42,29 @@ public function getTable(): string return IndoRegion::getTable(Feature::Regency); } + /** + * Get the province that owns the regency. + * + * @return Relations\BelongsTo + */ public function province(): Relations\BelongsTo { return $this->belongsTo(config('indoregion.models.province'), IndoRegion::getForeignKeyId(Feature::Province)); } + /** + * Get all the districts for the Regency + * + * @return Relations\HasMany + */ public function districts(): Relations\HasMany { return $this->hasMany(config('indoregion.models.district'), IndoRegion::getForeignKeyId(Feature::Regency)); } + /** + * @return Relations\HasManyThrough + */ public function villages(): Relations\HasManyThrough { return $this->hasManyThrough( diff --git a/src/Models/Village.php b/src/Models/Village.php index 090df22..6e7636b 100755 --- a/src/Models/Village.php +++ b/src/Models/Village.php @@ -17,10 +17,10 @@ /** * Village Model. * - * @property string $name - * @property \Dicibi\IndoRegion\Models\District $district - * @property int|string $idn_district_id - * @property int $id + * @property string $name + * @property \Dicibi\IndoRegion\Models\District $district + * @property int|string $idn_district_id + * @property int $id */ class Village extends Model { @@ -40,6 +40,11 @@ public function getTable(): string return IndoRegion::getTable(Feature::Village); } + /** + * Get the district that owns the village. + * + * @return Relations\BelongsTo + */ public function district(): Relations\BelongsTo { return $this->belongsTo(config('indoregion.models.district'), IndoRegion::getForeignKeyId(Feature::District)); diff --git a/src/RawDataGetter.php b/src/RawDataGetter.php index f0b859a..89e432a 100644 --- a/src/RawDataGetter.php +++ b/src/RawDataGetter.php @@ -12,32 +12,52 @@ namespace Dicibi\IndoRegion; use Iterator; +use League\Csv\Exception; use League\Csv\Reader; +use League\Csv\UnavailableStream; class RawDataGetter { protected static string $path = __DIR__.'/../database/data/csv/'; + /** + * @return Iterator + */ public static function getProvinces(): Iterator { return self::getCsvData(self::$path.'provinces.csv'); } + /** + * @return Iterator + */ public static function getRegencies(): Iterator { return self::getCsvData(self::$path.'regencies.csv'); } + /** + * @return Iterator + */ public static function getDistricts(): Iterator { return self::getCsvData(self::$path.'districts.csv'); } + /** + * @return Iterator + */ public static function getVillages(): Iterator { return self::getCsvData(self::$path.'villages.csv'); } + /** + * @return Iterator> + * + * @throws Exception + * @throws UnavailableStream + */ public static function getCsvData(string $path): Iterator { $csv = Reader::createFromPath($path); diff --git a/tests/Feature/DistrictTest.php b/tests/Feature/DistrictTest.php index 1919200..d33b7aa 100644 --- a/tests/Feature/DistrictTest.php +++ b/tests/Feature/DistrictTest.php @@ -6,6 +6,7 @@ use Dicibi\IndoRegion\Models\District; use Dicibi\IndoRegion\Models\Regency; use Illuminate\Pagination\CursorPaginator; + use function PHPUnit\Framework\assertCount; use function PHPUnit\Framework\assertInstanceOf; use function PHPUnit\Framework\assertNotEmpty; @@ -15,7 +16,7 @@ /** @var District $district */ $district = District::query()->first(); - assertNotEmpty($district->regency); + expect($district->regency)->not->toBeEmpty(); }); it('can retrieve villages', function () { diff --git a/tests/Feature/ProvinceTest.php b/tests/Feature/ProvinceTest.php index 9cd3600..81a2ea4 100644 --- a/tests/Feature/ProvinceTest.php +++ b/tests/Feature/ProvinceTest.php @@ -5,6 +5,7 @@ use Dicibi\IndoRegion\Contracts\IndoRegionResolver; use Dicibi\IndoRegion\Models\Province; use Illuminate\Pagination\CursorPaginator; + use function PHPUnit\Framework\assertCount; use function PHPUnit\Framework\assertInstanceOf; use function PHPUnit\Framework\assertNotEmpty; diff --git a/tests/Feature/RegencyTest.php b/tests/Feature/RegencyTest.php index e2b3875..1c0b717 100644 --- a/tests/Feature/RegencyTest.php +++ b/tests/Feature/RegencyTest.php @@ -6,6 +6,7 @@ use Dicibi\IndoRegion\Models\Province; use Dicibi\IndoRegion\Models\Regency; use Illuminate\Pagination\CursorPaginator; + use function PHPUnit\Framework\assertCount; use function PHPUnit\Framework\assertInstanceOf; use function PHPUnit\Framework\assertNotEmpty; diff --git a/tests/Feature/VillageTest.php b/tests/Feature/VillageTest.php index 061beba..78f5450 100644 --- a/tests/Feature/VillageTest.php +++ b/tests/Feature/VillageTest.php @@ -6,6 +6,7 @@ use Dicibi\IndoRegion\Models\District; use Dicibi\IndoRegion\Models\Village; use Illuminate\Pagination\CursorPaginator; + use function PHPUnit\Framework\assertCount; use function PHPUnit\Framework\assertInstanceOf; use function PHPUnit\Framework\assertNotEmpty; diff --git a/tests/TestCase.php b/tests/TestCase.php index 2a0e32b..0a389a8 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,8 +5,12 @@ use Dicibi\IndoRegion\Database\Seeders\IndoRegionSeeder; use Dicibi\IndoRegion\IndoRegionServiceProvider; use Illuminate\Foundation\Testing\RefreshDatabase; +use Orchestra\Testbench\Attributes\WithConfig; use Orchestra\Testbench\TestCase as TestbenchTestCase; +use function Orchestra\Testbench\workbench_path; + +#[WithConfig('app.key', 'base64:tXaZmfbKgk04ki71jcfxlGZAeEZxMMMPZYrdNcokYeM='), WithConfig('database.default', 'testing')] class TestCase extends TestbenchTestCase { use RefreshDatabase; @@ -24,7 +28,6 @@ protected function getPackageProviders($app): array protected function defineDatabaseMigrations(): void { - $this->loadMigrationsFrom(__DIR__.'/Stuff/migrations'); - $this->loadLaravelMigrations(); + $this->loadMigrationsFrom(workbench_path('migrations')); } } diff --git a/tests/Stuff/migrations/2022_10_09_142019_create_dummies_tables.php b/workbench/migrations/2022_10_09_142019_create_dummies_tables.php similarity index 100% rename from tests/Stuff/migrations/2022_10_09_142019_create_dummies_tables.php rename to workbench/migrations/2022_10_09_142019_create_dummies_tables.php