From b985ea09fa63e3cd1580b03bb78d46911dd80d81 Mon Sep 17 00:00:00 2001 From: Prasit Gebsaap Date: Sat, 28 Sep 2024 22:14:55 +0700 Subject: [PATCH 1/6] Fix PHPStan errors --- .github/workflows/run-tests.yml | 2 +- composer.json | 8 +- .../migrations/create_addresses_table.php | 7 +- .../create_thai_districts_table.php | 6 +- .../create_thai_geographies_table.php | 8 +- .../create_thai_provinces_table.php | 6 +- .../create_thai_subdistricts_table.php | 8 +- phpstan.neon.dist | 3 +- src/Facades/ThaiAddresses.php | 8 +- src/Models/Address.php | 11 +-- src/Models/District.php | 12 +-- src/Models/Geography.php | 9 +- src/Models/Province.php | 12 +-- src/Models/Subdistrict.php | 12 +-- src/ThaiAddresses.php | 85 +++++-------------- src/ThaiAddressesServiceProvider.php | 11 --- src/Traits/HasAddress.php | 7 +- .../migrations/create_users_table.php | 3 +- tests/src/Feature/UserTest.php | 2 +- 19 files changed, 81 insertions(+), 139 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 3388797..c48feb0 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -19,7 +19,7 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest] - php: [8.2] + php: [8.2, 8.3] laravel: [10.*, 11.*] stability: [prefer-stable] include: diff --git a/composer.json b/composer.json index 4260fed..cb0334e 100644 --- a/composer.json +++ b/composer.json @@ -20,23 +20,23 @@ } ], "require": { - "php": "^8.1|^8.2", + "php": "^8.2|^8.3", "illuminate/contracts": "^10.0|^11.0", "illuminate/support": "^10.0|^11.0", "laravel/pint": "^1.17", "spatie/laravel-package-tools": "^1.9.2" }, "require-dev": { - "nunomaduro/collision": "^6.0|^7.0", "larastan/larastan": "^2.0.1", + "nunomaduro/collision": "^6.0|^7.0", "orangehill/iseed": "^3.0", - "orchestra/testbench": "^8.0|^9.0", + "orchestra/testbench": "*", "pestphp/pest": "^1.21", "pestphp/pest-plugin-laravel": "^1.1", "phpstan/extension-installer": "^1.1", "phpstan/phpstan-deprecation-rules": "^1.0", "phpstan/phpstan-phpunit": "^1.0", - "phpunit/phpunit": "^9.5", + "phpunit/phpunit": "^9.5|^10", "spatie/laravel-ray": "^1.26" }, "autoload": { diff --git a/database/migrations/create_addresses_table.php b/database/migrations/create_addresses_table.php index fe3d336..018223c 100644 --- a/database/migrations/create_addresses_table.php +++ b/database/migrations/create_addresses_table.php @@ -9,8 +9,8 @@ { public function up() { - Schema::create(ThaiAddresses::getAddressTableName(), function (Blueprint $table) { - // Columns + $table = ThaiAddresses::getAddressTableName(); + Schema::create($table, function (Blueprint $table) { $table->id('id'); $table->morphs('addressable'); @@ -36,6 +36,7 @@ public function up() public function down() { - Schema::dropIfExists(ThaiAddresses::getAddressTableName()); + $table = ThaiAddresses::getAddressTableName(); + Schema::dropIfExists($table); } }; diff --git a/database/migrations/create_thai_districts_table.php b/database/migrations/create_thai_districts_table.php index 629a84c..ca415d3 100644 --- a/database/migrations/create_thai_districts_table.php +++ b/database/migrations/create_thai_districts_table.php @@ -9,7 +9,8 @@ { public function up() { - Schema::create(ThaiAddresses::getDistrictTableName(), function (Blueprint $table) { + $table = ThaiAddresses::getDistrictTableName(); + Schema::create($table, function (Blueprint $table) { $table->id(); // add fields @@ -24,6 +25,7 @@ public function up() public function down() { - Schema::dropIfExists(ThaiAddresses::getDistrictTableName()); + $table = ThaiAddresses::getDistrictTableName(); + Schema::dropIfExists($table); } }; diff --git a/database/migrations/create_thai_geographies_table.php b/database/migrations/create_thai_geographies_table.php index c8c9d8b..34e88db 100644 --- a/database/migrations/create_thai_geographies_table.php +++ b/database/migrations/create_thai_geographies_table.php @@ -9,11 +9,10 @@ { public function up() { - Schema::create(ThaiAddresses::getGeographyTableName(), function (Blueprint $table) { + $table = ThaiAddresses::getGeographyTableName(); + Schema::create($table, function (Blueprint $table) { $table->id(); - // add fields - $table->string('name_en'); $table->string('name_th'); $table->timestamps(); @@ -22,6 +21,7 @@ public function up() public function down() { - Schema::dropIfExists(ThaiAddresses::getGeographyTableName()); + $table = ThaiAddresses::getGeographyTableName(); + Schema::dropIfExists($table); } }; diff --git a/database/migrations/create_thai_provinces_table.php b/database/migrations/create_thai_provinces_table.php index 97fc49e..f95f3a2 100644 --- a/database/migrations/create_thai_provinces_table.php +++ b/database/migrations/create_thai_provinces_table.php @@ -9,7 +9,8 @@ { public function up() { - Schema::create(ThaiAddresses::getProvinceTableName(), function (Blueprint $table) { + $table = ThaiAddresses::getProvinceTableName(); + Schema::create($table, function (Blueprint $table) { $table->id(); // add fields @@ -23,6 +24,7 @@ public function up() public function down() { - Schema::dropIfExists(ThaiAddresses::getProvinceTableName()); + $table = ThaiAddresses::getProvinceTableName(); + Schema::dropIfExists($table); } }; diff --git a/database/migrations/create_thai_subdistricts_table.php b/database/migrations/create_thai_subdistricts_table.php index 59cd91f..4f96339 100644 --- a/database/migrations/create_thai_subdistricts_table.php +++ b/database/migrations/create_thai_subdistricts_table.php @@ -9,10 +9,9 @@ { public function up() { - Schema::create(ThaiAddresses::getSubdistrictTableName(), function (Blueprint $table) { + $table = ThaiAddresses::getSubdistrictTableName(); + Schema::create($table, function (Blueprint $table) { $table->id(); - - // add fields $table->string('zip_code'); $table->string('name_th'); $table->string('name_en'); @@ -24,6 +23,7 @@ public function up() public function down() { - Schema::dropIfExists(ThaiAddresses::getSubdistrictTableName()); + $table = ThaiAddresses::getSubdistrictTableName(); + Schema::dropIfExists($table); } }; diff --git a/phpstan.neon.dist b/phpstan.neon.dist index a91953b..de458d6 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -2,7 +2,7 @@ includes: - phpstan-baseline.neon parameters: - level: 4 + level: 5 paths: - src - config @@ -10,5 +10,4 @@ parameters: tmpDir: build/phpstan checkOctaneCompatibility: true checkModelProperties: true - checkMissingIterableValueType: false diff --git a/src/Facades/ThaiAddresses.php b/src/Facades/ThaiAddresses.php index 44cfaf5..7992e55 100644 --- a/src/Facades/ThaiAddresses.php +++ b/src/Facades/ThaiAddresses.php @@ -5,17 +5,15 @@ use Illuminate\Support\Facades\Facade; /** - * @see \Soap\ThaiProvinces\ThaiProvinces + * @see \Soap\ThaiProvinces\ThaiAddesses */ class ThaiAddresses extends Facade { /** * Alias of dynamic class, need to be registered in service provider - * - * @return string */ - protected static function getFacadeAccessor() + protected static function getFacadeAccessor(): string { - return 'thai-addresses'; + return \Soap\ThaiAddresses\ThaiAddresses::class; } } diff --git a/src/Models/Address.php b/src/Models/Address.php index 670d6a5..5b4e1d8 100644 --- a/src/Models/Address.php +++ b/src/Models/Address.php @@ -48,11 +48,9 @@ class Address extends Model 'deleted_at' => 'datetime', ]; - public function __construct(array $attributes = []) + public function getTable() { - parent::__construct($attributes); - - $this->table = ThaiAddresses::getAddressTableName(); + return ThaiAddresses::getAddressTableName(); } /** @@ -63,10 +61,7 @@ public function addressable(): MorphTo return $this->morphTo('addressable', 'addressable_type', 'addressable_id', 'id'); } - /** - * @return BelongsTo - */ - public function subdistrict() + public function subdistrict(): BelongsTo { return $this->belongsTo(Subdistrict::class); } diff --git a/src/Models/District.php b/src/Models/District.php index 0215e54..396899c 100644 --- a/src/Models/District.php +++ b/src/Models/District.php @@ -3,6 +3,8 @@ namespace Soap\ThaiAddresses\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\HasMany; use Soap\ThaiAddresses\Facades\ThaiAddresses; class District extends Model @@ -13,19 +15,17 @@ class District extends Model 'code', ]; - public function __construct(array $attributes = []) + public function getTable() { - parent::__construct($attributes); - - $this->table = ThaiAddresses::getDistrictTableName(); + return ThaiAddresses::getDistrictTableName(); } - public function province() + public function province(): BelongsTo { return $this->belongsTo(Province::class); } - public function subdistricts() + public function subdistricts(): HasMany { return $this->hasMany(Subdistrict::class); } diff --git a/src/Models/Geography.php b/src/Models/Geography.php index d8cfbe2..bfb1620 100644 --- a/src/Models/Geography.php +++ b/src/Models/Geography.php @@ -3,6 +3,7 @@ namespace Soap\ThaiAddresses\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasMany; use Soap\ThaiAddresses\Facades\ThaiAddresses; /** @@ -16,14 +17,12 @@ class Geography extends Model 'name_en', ]; - public function __construct(array $attributes = []) + public function getTable() { - parent::__construct($attributes); - - $this->table = ThaiAddresses::getGeographyTableName(); + return ThaiAddresses::getGeographyTableName(); } - public function provinces() + public function provinces(): HasMany { return $this->hasMany(Province::class); } diff --git a/src/Models/Province.php b/src/Models/Province.php index bcf4247..4774161 100644 --- a/src/Models/Province.php +++ b/src/Models/Province.php @@ -3,6 +3,8 @@ namespace Soap\ThaiAddresses\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\HasMany; use Soap\ThaiAddresses\Facades\ThaiAddresses; class Province extends Model @@ -13,19 +15,17 @@ class Province extends Model 'code', ]; - public function __construct(array $attributes = []) + public function getTable() { - parent::__construct($attributes); - - $this->table = ThaiAddresses::getProvinceTableName(); + return ThaiAddresses::getProvinceTableName(); } - public function geography() + public function geography(): BelongsTo { return $this->belongsTo(Geography::class); } - public function districts() + public function districts(): HasMany { return $this->hasMany(District::class); } diff --git a/src/Models/Subdistrict.php b/src/Models/Subdistrict.php index 1bdfb3e..cebaf90 100644 --- a/src/Models/Subdistrict.php +++ b/src/Models/Subdistrict.php @@ -4,6 +4,8 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\HasMany; use Soap\ThaiAddresses\Facades\ThaiAddresses; class Subdistrict extends Model @@ -16,19 +18,17 @@ class Subdistrict extends Model 'name_en', ]; - public function __construct(array $attributes = []) + public function getTable() { - parent::__construct($attributes); - - $this->table = ThaiAddresses::getSubdistrictTableName(); + return ThaiAddresses::getSubdistrictTableName(); } - public function district() + public function district(): BelongsTo { return $this->belongsTo(District::class); } - public function addresses() + public function addresses(): HasMany { return $this->hasMany(Address::class); } diff --git a/src/ThaiAddresses.php b/src/ThaiAddresses.php index 9ab3342..5967c83 100644 --- a/src/ThaiAddresses.php +++ b/src/ThaiAddresses.php @@ -2,112 +2,73 @@ namespace Soap\ThaiAddresses; -use Illuminate\Contracts\Container\BindingResolutionException; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; - class ThaiAddresses { + protected $config; + + public function __construct() + { + $this->config = config('thai-addresses'); + } + /** * Get geograpy table name (ตารางสำหรับข้อมูลภาค) - * - * @return mixed - * - * @throws BindingResolutionException - * @throws NotFoundExceptionInterface - * @throws ContainerExceptionInterface */ - public function getGeographyTableName() + public function getGeographyTableName(): string { - return config('thai-addresses.geography.table_name'); + return $this->config['geography']['table_name']; } /** * Get province table name (ตารางสำหรับข้อมูลจังหวัด) - * - * @return mixed - * - * @throws BindingResolutionException - * @throws NotFoundExceptionInterface - * @throws ContainerExceptionInterface */ - public function getProvinceTableName() + public function getProvinceTableName(): string { - return config('thai-addresses.province.table_name'); + return $this->config['province']['table_name']; } /** * Get district table name (ตารางสำหรับข้อมูลอำเภอ) - * - * @return mixed - * - * @throws BindingResolutionException - * @throws NotFoundExceptionInterface - * @throws ContainerExceptionInterface */ - public function getDistrictTableName() + public function getDistrictTableName(): string { - return config('thai-addresses.district.table_name'); + return $this->config['district']['table_name']; } /** * Get subdistrict table name (ตารางสำหรับข้อมูลตำบล/เทศบาล) - * - * @return mixed - * - * @throws BindingResolutionException - * @throws NotFoundExceptionInterface - * @throws ContainerExceptionInterface */ - public function getSubdistrictTableName() + public function getSubdistrictTableName(): string { - return config('thai-addresses.subdistrict.table_name'); + return $this->config['subdistrict']['table_name']; } - public function getAddressTableName() + public function getAddressTableName(): string { - return config('thai-addresses.address.table_name'); + return $this->config['address']['table_name']; } /** * Get geograpy foreign key name (อ้างอิงจากตารางจังหวัด) - * - * @return mixed - * - * @throws BindingResolutionException - * @throws NotFoundExceptionInterface - * @throws ContainerExceptionInterface */ - public function getGeographyForeignKeyName() + public function getGeographyForeignKeyName(): string { - return config('thai-addresses.geography.foreign_key'); + return $this->config['geography']['foreign_key']; } /** * Get province foreign key name (อ้างอิงโดยตารางอำเภอ) - * - * @return mixed - * - * @throws BindingResolutionException - * @throws NotFoundExceptionInterface - * @throws ContainerExceptionInterface */ - public function getProvinceForeignKeyName() + public function getProvinceForeignKeyName(): string { - return config('thai-addresses.province.foreign_key'); + return $this->config['province']['foreign_key']; } /** * Get district foreign key name (อ้างอิงโดยตารางภาค) - * - * @return mixed - * - * @throws BindingResolutionException - * @throws NotFoundExceptionInterface - * @throws ContainerExceptionInterface */ - public function getDistrictForeignKeyName() + public function getDistrictForeignKeyName(): string { - return config('thai-addresses.district.foreign_key'); + return $this->config['district']['foreign_key']; } } diff --git a/src/ThaiAddressesServiceProvider.php b/src/ThaiAddressesServiceProvider.php index b498598..a0c374d 100644 --- a/src/ThaiAddressesServiceProvider.php +++ b/src/ThaiAddressesServiceProvider.php @@ -40,15 +40,4 @@ public function configurePackage(Package $package): void ->askToStarRepoOnGitHub('soap/thai-addresses'); }); } - - public function registeringPackage() - { - $this->app->bind('thai-addresses', function ($app) { - return new ThaiAddresses(); - }); - } - - public function bootingPackage() - { - } } diff --git a/src/Traits/HasAddress.php b/src/Traits/HasAddress.php index b9d7503..d4b6bfb 100644 --- a/src/Traits/HasAddress.php +++ b/src/Traits/HasAddress.php @@ -51,13 +51,8 @@ public function addresses(): MorphMany /** * Find addressables by distance. - * - * @param string $distance - * @param string $unit - * @param string $latitude - * @param string $longitude */ - public function findByDistance($distance, $unit, $latitude, $longitude): Collection + public function findByDistance(string $distance, string $unit, string $latitude, string $longitude): Collection { // @TODO: this method needs to be refactored! return $this->addresses()->within($distance, $unit, $latitude, $longitude)->get(); diff --git a/tests/database/migrations/create_users_table.php b/tests/database/migrations/create_users_table.php index 2c9a716..8efafc6 100644 --- a/tests/database/migrations/create_users_table.php +++ b/tests/database/migrations/create_users_table.php @@ -4,7 +4,8 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class () extends Migration { +return new class extends Migration +{ public function up(): void { Schema::create('users', function (Blueprint $table): void { diff --git a/tests/src/Feature/UserTest.php b/tests/src/Feature/UserTest.php index f0a0125..1f72794 100644 --- a/tests/src/Feature/UserTest.php +++ b/tests/src/Feature/UserTest.php @@ -9,7 +9,7 @@ test('user can have address', function () { $user = User::factory()->create(); $subdistrict = Subdistrict::where('name_th', '=', 'กระบี่ใหญ่')->first(); - $address = new Address(); + $address = new Address; $address->fill([ 'label' => 'Default Address', 'given_name' => 'Prasit', From e086c84666c1db35e615bc5443e11509b383d19c Mon Sep 17 00:00:00 2001 From: soap Date: Sat, 28 Sep 2024 15:15:16 +0000 Subject: [PATCH 2/6] Fix styling --- tests/database/migrations/create_users_table.php | 3 +-- tests/src/Feature/UserTest.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/database/migrations/create_users_table.php b/tests/database/migrations/create_users_table.php index 8efafc6..2c9a716 100644 --- a/tests/database/migrations/create_users_table.php +++ b/tests/database/migrations/create_users_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class () extends Migration { public function up(): void { Schema::create('users', function (Blueprint $table): void { diff --git a/tests/src/Feature/UserTest.php b/tests/src/Feature/UserTest.php index 1f72794..f0a0125 100644 --- a/tests/src/Feature/UserTest.php +++ b/tests/src/Feature/UserTest.php @@ -9,7 +9,7 @@ test('user can have address', function () { $user = User::factory()->create(); $subdistrict = Subdistrict::where('name_th', '=', 'กระบี่ใหญ่')->first(); - $address = new Address; + $address = new Address(); $address->fill([ 'label' => 'Default Address', 'given_name' => 'Prasit', From e3f4a7538f9f56aa9dafaf909b8dbaacbdd24c55 Mon Sep 17 00:00:00 2001 From: Prasit Gebsaap Date: Sun, 29 Sep 2024 01:18:25 +0700 Subject: [PATCH 3/6] Update Pest to version 2 --- .phpunit.cache/test-results | 1 + composer.json | 24 +++++++------- phpunit.xml.dist | 66 ++++++++++++++----------------------- 3 files changed, 38 insertions(+), 53 deletions(-) create mode 100644 .phpunit.cache/test-results diff --git a/.phpunit.cache/test-results b/.phpunit.cache/test-results new file mode 100644 index 0000000..c0c30c1 --- /dev/null +++ b/.phpunit.cache/test-results @@ -0,0 +1 @@ +{"version":"pest_2.35.1","defects":[],"times":{"Soap\\ThaiAddresses\\Tests\\Unit\\ProvinceTest::a_province_has_name_en":0.012,"Soap\\ThaiAddresses\\Tests\\Unit\\ProvinceTest::a_province_has_name_th":0,"Soap\\ThaiAddresses\\Tests\\Unit\\ProvinceTest::a_province_belongs_to_region":0.002,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::district_record_exists":0.007,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::geography_records_exists":0,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::province_records_existsh":0.001,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::subdistrict_records_exists":0.056,"P\\Tests\\src\\Feature\\UserTest::__pest_evaluable_user_can_have_address":0.026}} \ No newline at end of file diff --git a/composer.json b/composer.json index cb0334e..fb9cb9c 100644 --- a/composer.json +++ b/composer.json @@ -21,22 +21,22 @@ ], "require": { "php": "^8.2|^8.3", - "illuminate/contracts": "^10.0|^11.0", - "illuminate/support": "^10.0|^11.0", - "laravel/pint": "^1.17", - "spatie/laravel-package-tools": "^1.9.2" + "illuminate/contracts": "^10.0||^11.0", + "illuminate/support": "^10.0||^11.0", + "spatie/laravel-package-tools": "^1.16" }, "require-dev": { - "larastan/larastan": "^2.0.1", - "nunomaduro/collision": "^6.0|^7.0", + "larastan/larastan": "^2.9", + "laravel/pint": "^1.17", + "nunomaduro/collision": "^8.1.1|^7.10", "orangehill/iseed": "^3.0", - "orchestra/testbench": "*", - "pestphp/pest": "^1.21", - "pestphp/pest-plugin-laravel": "^1.1", - "phpstan/extension-installer": "^1.1", - "phpstan/phpstan-deprecation-rules": "^1.0", + "orchestra/testbench": "^8.22||^9.0", + "pestphp/pest": "^2.34", + "pestphp/pest-plugin-arch": "^2.7", + "pestphp/pest-plugin-laravel": "^2.3", + "phpstan/extension-installer": "^1.3", + "phpstan/phpstan-deprecation-rules": "^1.1", "phpstan/phpstan-phpunit": "^1.0", - "phpunit/phpunit": "^9.5|^10", "spatie/laravel-ray": "^1.26" }, "autoload": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index bc2f8e7..8d68a54 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,43 +1,27 @@ - - - - tests - - - - - ./src - - - - - - - - - - - - - - + + + + tests + + + + + + + + + + + + + + + + + + + ./src + + From b6bf2165e4c04da1ac7d1a7f2e80b9ff66725f4e Mon Sep 17 00:00:00 2001 From: Prasit Gebsaap Date: Tue, 12 Nov 2024 10:21:19 +0700 Subject: [PATCH 4/6] Fix PHPStan error --- .phpunit.cache/test-results | 2 +- composer.json | 27 ++++++-- database/seeders/GeographySeeder.php | 2 +- database/seeders/ProvinceSeeder.php | 2 +- database/seeders/SubdistrictSeeder.php | 2 +- phpunit.xml.dist | 7 ++- tests/{src => }/Feature/UserTest.php | 8 +-- tests/TestCase.php | 61 ++++++++++++++++++ tests/Unit/DatabaseSeederTest.php | 24 +++++++ tests/database/factories/UserFactory.php | 23 ------- .../migrations/create_users_table.php | 25 -------- tests/database/seeders/DatabaseSeeder.php | 23 ------- tests/database/seeders/DistrictSeeder.php | 27 -------- tests/database/seeders/GeographySeeder.php | 25 -------- tests/database/seeders/ProvinceSeeder.php | 28 --------- tests/database/seeders/SubdistrictSeeder.php | 28 --------- tests/src/Models/User.php | 20 ------ tests/src/TestCase.php | 62 ------------------- tests/src/Unit/DatabaseSeederTest.php | 40 ------------ tests/src/Unit/ProvinceTest.php | 30 --------- workbench/app/Models/.gitkeep | 0 workbench/app/Models/User.php | 47 ++++++++++++++ .../Providers/WorkbenchServiceProvider.php | 24 +++++++ workbench/bootstrap/.gitkeep | 0 workbench/database/factories/.gitkeep | 0 workbench/database/factories/UserFactory.php | 54 ++++++++++++++++ workbench/database/migrations/.gitkeep | 0 workbench/database/seeders/DatabaseSeeder.php | 25 ++++++++ workbench/resources/views/.gitkeep | 0 workbench/routes/console.php | 8 +++ workbench/routes/web.php | 7 +++ 31 files changed, 285 insertions(+), 346 deletions(-) rename tests/{src => }/Feature/UserTest.php (80%) create mode 100644 tests/TestCase.php create mode 100644 tests/Unit/DatabaseSeederTest.php delete mode 100644 tests/database/factories/UserFactory.php delete mode 100644 tests/database/migrations/create_users_table.php delete mode 100644 tests/database/seeders/DatabaseSeeder.php delete mode 100644 tests/database/seeders/DistrictSeeder.php delete mode 100644 tests/database/seeders/GeographySeeder.php delete mode 100644 tests/database/seeders/ProvinceSeeder.php delete mode 100644 tests/database/seeders/SubdistrictSeeder.php delete mode 100644 tests/src/Models/User.php delete mode 100644 tests/src/TestCase.php delete mode 100644 tests/src/Unit/DatabaseSeederTest.php delete mode 100644 tests/src/Unit/ProvinceTest.php create mode 100644 workbench/app/Models/.gitkeep create mode 100644 workbench/app/Models/User.php create mode 100644 workbench/app/Providers/WorkbenchServiceProvider.php create mode 100644 workbench/bootstrap/.gitkeep create mode 100644 workbench/database/factories/.gitkeep create mode 100644 workbench/database/factories/UserFactory.php create mode 100644 workbench/database/migrations/.gitkeep create mode 100644 workbench/database/seeders/DatabaseSeeder.php create mode 100644 workbench/resources/views/.gitkeep create mode 100644 workbench/routes/console.php create mode 100644 workbench/routes/web.php diff --git a/.phpunit.cache/test-results b/.phpunit.cache/test-results index c0c30c1..89bad85 100644 --- a/.phpunit.cache/test-results +++ b/.phpunit.cache/test-results @@ -1 +1 @@ -{"version":"pest_2.35.1","defects":[],"times":{"Soap\\ThaiAddresses\\Tests\\Unit\\ProvinceTest::a_province_has_name_en":0.012,"Soap\\ThaiAddresses\\Tests\\Unit\\ProvinceTest::a_province_has_name_th":0,"Soap\\ThaiAddresses\\Tests\\Unit\\ProvinceTest::a_province_belongs_to_region":0.002,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::district_record_exists":0.007,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::geography_records_exists":0,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::province_records_existsh":0.001,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::subdistrict_records_exists":0.056,"P\\Tests\\src\\Feature\\UserTest::__pest_evaluable_user_can_have_address":0.026}} \ No newline at end of file +{"version":"pest_2.35.1","defects":{"P\\Tests\\Feature\\UserTest::__pest_evaluable_user_can_have_address":8,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::province_records_existsh":8,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::subdistrict_records_exists":8,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::district_record_exists":8,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::geography_records_exists":8,"Soap\\ThaiAddresses\\Tests\\Unit\\ProvinceTest::a_province_has_name_en":8,"Soap\\ThaiAddresses\\Tests\\Unit\\ProvinceTest::a_province_has_name_th":8,"Soap\\ThaiAddresses\\Tests\\Unit\\ProvinceTest::a_province_belongs_to_region":8,"P\\Tests\\Unit\\DatabaseSeederTest::__pest_evaluable_district_record_exists":7,"P\\Tests\\Unit\\DatabaseSeederTest::__pest_evaluable_province_records_existsh":7,"P\\Tests\\Unit\\DatabaseSeederTest::__pest_evaluable_geography_records_exists":7,"P\\Tests\\Unit\\DatabaseSeederTest::__pest_evaluable_subdistrict_records_exists":7,"P\\Tests\\Unit\\DatabaseSeederTest::__pest_evaluable_seeder_command_is_working":8},"times":{"Soap\\ThaiAddresses\\Tests\\Unit\\ProvinceTest::a_province_has_name_en":0.001,"Soap\\ThaiAddresses\\Tests\\Unit\\ProvinceTest::a_province_has_name_th":0.001,"Soap\\ThaiAddresses\\Tests\\Unit\\ProvinceTest::a_province_belongs_to_region":0.002,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::district_record_exists":0.007,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::geography_records_exists":0,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::province_records_existsh":0.001,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::subdistrict_records_exists":0.061,"P\\Tests\\src\\Feature\\UserTest::__pest_evaluable_user_can_have_address":0.037,"P\\Tests\\Unit\\DatabaseSeederTest::__pest_evaluable_geography_records_exists":0.008,"P\\Tests\\Unit\\DatabaseSeederTest::__pest_evaluable_province_records_existsh":0.007,"P\\Tests\\Unit\\DatabaseSeederTest::__pest_evaluable_subdistrict_records_exists":0.007,"P\\Tests\\Unit\\DatabaseSeederTest::__pest_evaluable_district_record_exists":0.011,"P\\Tests\\Feature\\UserTest::__pest_evaluable_user_can_have_address":0.007}} \ No newline at end of file diff --git a/composer.json b/composer.json index fb9cb9c..97b383e 100644 --- a/composer.json +++ b/composer.json @@ -33,6 +33,7 @@ "orchestra/testbench": "^8.22||^9.0", "pestphp/pest": "^2.34", "pestphp/pest-plugin-arch": "^2.7", + "pestphp/pest-plugin-drift": "^2.6", "pestphp/pest-plugin-laravel": "^2.3", "phpstan/extension-installer": "^1.3", "phpstan/phpstan-deprecation-rules": "^1.1", @@ -48,14 +49,32 @@ }, "autoload-dev": { "psr-4": { - "Soap\\ThaiAddresses\\Tests\\": "tests/src", - "Soap\\ThaiAddresses\\Tests\\Database\\": "tests/database" + "Soap\\ThaiAddresses\\Tests\\": "tests", + "Workbench\\App\\": "workbench/app/", + "Workbench\\Database\\Factories\\": "workbench/database/factories/", + "Workbench\\Database\\Seeders\\": "workbench/database/seeders/" } }, "scripts": { "analyse": "vendor/bin/phpstan analyse", "test": "vendor/bin/pest", - "test-coverage": "vendor/bin/pest --coverage" + "test-coverage": "vendor/bin/pest --coverage", + "post-autoload-dump": [ + "@clear", + "@prepare" + ], + "clear": "@php vendor/bin/testbench package:purge-skeleton --ansi", + "prepare": "@php vendor/bin/testbench package:discover --ansi", + "build": "@php vendor/bin/testbench workbench:build --ansi", + "serve": [ + "Composer\\Config::disableProcessTimeout", + "@build", + "@php vendor/bin/testbench serve --ansi" + ], + "lint": [ + "@php vendor/bin/pint --ansi", + "@php vendor/bin/phpstan analyse --verbose --ansi" + ] }, "config": { "sort-packages": true, @@ -70,7 +89,7 @@ "Soap\\ThaiAddresses\\ThaiAddressesServiceProvider" ], "aliases": { - "ThaiProvinces": "Soap\\ThaiAddresses\\Facades\\ThaiAddresses" + "ThaiAddresses": "Soap\\ThaiAddresses\\Facades\\ThaiAddresses" } } }, diff --git a/database/seeders/GeographySeeder.php b/database/seeders/GeographySeeder.php index 0be31db..2bf8437 100644 --- a/database/seeders/GeographySeeder.php +++ b/database/seeders/GeographySeeder.php @@ -11,7 +11,7 @@ class GeographySeeder extends Seeder public function run() { Geography::query()->delete(); - $json = File::get(base_path('vendor/soap/thai-addresses/database/seeders/data/geographies.json')); + $json = File::get(base_path('/vendor/soap/thai-addresses/database/seeders/data/geographies.json')); $geographies = json_decode($json); foreach ($geographies as $item) { diff --git a/database/seeders/ProvinceSeeder.php b/database/seeders/ProvinceSeeder.php index d68fc24..a9af188 100644 --- a/database/seeders/ProvinceSeeder.php +++ b/database/seeders/ProvinceSeeder.php @@ -12,7 +12,7 @@ class ProvinceSeeder extends Seeder public function run() { Province::query()->delete(); - $json = File::get(base_path('vendor/soap/thai-addresses/database/seeders/data/provinces.json')); + $json = File::get(base_path('/vendor/soap/thai-addresses/database/seeders/data/provinces.json')); $provinces = json_decode($json); foreach ($provinces as $item) { diff --git a/database/seeders/SubdistrictSeeder.php b/database/seeders/SubdistrictSeeder.php index eb9800d..60a4d01 100644 --- a/database/seeders/SubdistrictSeeder.php +++ b/database/seeders/SubdistrictSeeder.php @@ -11,7 +11,7 @@ class SubdistrictSeeder extends Seeder public function run() { Subdistrict::query()->delete(); - $json = File::get(base_path('vendor/soap/thai-addresses/database/seeders/data/subdistricts.json')); + $json = File::get(base_path('/vendor/soap/thai-addresses/database/seeders/data/subdistricts.json')); $subdistricts = json_decode($json); foreach ($subdistricts as $item) { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 8d68a54..b38d5a0 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,8 +1,11 @@ - - tests + + tests/Unit + + + tests/Feature diff --git a/tests/src/Feature/UserTest.php b/tests/Feature/UserTest.php similarity index 80% rename from tests/src/Feature/UserTest.php rename to tests/Feature/UserTest.php index f0a0125..428f7cc 100644 --- a/tests/src/Feature/UserTest.php +++ b/tests/Feature/UserTest.php @@ -1,15 +1,13 @@ create(); $subdistrict = Subdistrict::where('name_th', '=', 'กระบี่ใหญ่')->first(); - $address = new Address(); + $address = new Address; $address->fill([ 'label' => 'Default Address', 'given_name' => 'Prasit', @@ -27,5 +25,5 @@ $user->addresses()->save($address); - $this->assertCount(1, $user->addresses, 'User can be assigned address'); + expect($user->addresses)->toHaveCount(1, 'User can be assigned address'); }); diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..9335880 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,61 @@ + 'Soap\\ThaiAddresses\\Tests\\Database\\Factories\\'.class_basename($modelName).'Factory' + ); + + $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); //load package's migrations + } + + protected function getPackageProviders($app) + { + return [ + ThaiAddressesServiceProvider::class, + ]; + } + + public function getEnvironmentSetUp($app) + { + tap($app['config'], function (Repository $config) { + $config->set('auth.providers.users.model', User::class); + $config->set('database.default', 'testing'); + + $config->set('database.connections.mysql', [ + 'driver' => 'mysql', + 'database' => 'thai_addresses_package_test', + 'host' => '127.0.0.1', + 'username' => 'root', + 'prefix' => '', + 'charset' => 'utf8mb4', + 'collation' => 'utf8mb4_unicode_ci', + ]); + }); + } + + protected function defineDatabaseMigrations() + { + $this->loadMigrationsFrom(workbench_path('database/migrations')); + } +} diff --git a/tests/Unit/DatabaseSeederTest.php b/tests/Unit/DatabaseSeederTest.php new file mode 100644 index 0000000..2141ee1 --- /dev/null +++ b/tests/Unit/DatabaseSeederTest.php @@ -0,0 +1,24 @@ +artisan('thai-addresses:db-seed'); +}); + +test('seeder command is working', function () { + $regions = Geography::all(); + expect($regions)->toHaveCount(6, '6 regions exists'); + + $provinces = Province::all(); + expect($provinces)->toHaveCount(77, '77 provinces exists'); + + $districts = District::all(); + expect($districts)->toHaveCount(1006, '1006 districts exists'); + + $subdistricts = Subdistrict::all(); + expect($subdistricts)->toHaveCount(8913, '8913 subdistricts exists'); +}); diff --git a/tests/database/factories/UserFactory.php b/tests/database/factories/UserFactory.php deleted file mode 100644 index b3db6ab..0000000 --- a/tests/database/factories/UserFactory.php +++ /dev/null @@ -1,23 +0,0 @@ - $this->faker->name(), - 'email' => $this->faker->unique()->safeEmail(), - 'email_verified_at' => now(), - 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password - 'remember_token' => Str::random(10), - ]; - } -} diff --git a/tests/database/migrations/create_users_table.php b/tests/database/migrations/create_users_table.php deleted file mode 100644 index 2c9a716..0000000 --- a/tests/database/migrations/create_users_table.php +++ /dev/null @@ -1,25 +0,0 @@ -id(); - $table->string('name'); - $table->string('email')->unique(); - $table->timestamp('email_verified_at')->nullable(); - $table->string('password'); - $table->rememberToken(); - $table->timestamps(); - }); - } - - public function down(): void - { - Schema::dropIfExists('users'); - } -}; diff --git a/tests/database/seeders/DatabaseSeeder.php b/tests/database/seeders/DatabaseSeeder.php deleted file mode 100644 index bd1e479..0000000 --- a/tests/database/seeders/DatabaseSeeder.php +++ /dev/null @@ -1,23 +0,0 @@ -call([ - GeographySeeder::class, - ProvinceSeeder::class, - DistrictSeeder::class, - SubdistrictSeeder::class, - ]); - } -} diff --git a/tests/database/seeders/DistrictSeeder.php b/tests/database/seeders/DistrictSeeder.php deleted file mode 100644 index 5cb35b9..0000000 --- a/tests/database/seeders/DistrictSeeder.php +++ /dev/null @@ -1,27 +0,0 @@ -delete(); - $json = File::get(__DIR__.'/../../../database/seeders/data/districts.json'); - $districts = json_decode($json); - - foreach ($districts as $item) { - District::forceCreate([ - 'id' => $item->id, - 'code' => $item->code, - 'name_th' => $item->name_th, - 'name_en' => $item->name_en, - config('thai-addresses.province.foreign_key') => $item->province_id, - ]); - } - } -} diff --git a/tests/database/seeders/GeographySeeder.php b/tests/database/seeders/GeographySeeder.php deleted file mode 100644 index b8a276a..0000000 --- a/tests/database/seeders/GeographySeeder.php +++ /dev/null @@ -1,25 +0,0 @@ -delete(); - $json = File::get(__DIR__.'/../../../database/seeders/data/geographies.json'); - $geographies = json_decode($json); - - foreach ($geographies as $item) { - Geography::forceCreate([ - 'id' => $item->id, - 'name_th' => $item->name_th, - 'name_en' => $item->name_en, - ]); - } - } -} diff --git a/tests/database/seeders/ProvinceSeeder.php b/tests/database/seeders/ProvinceSeeder.php deleted file mode 100644 index 544f9c3..0000000 --- a/tests/database/seeders/ProvinceSeeder.php +++ /dev/null @@ -1,28 +0,0 @@ -delete(); - $json = File::get(__DIR__.'/../../../database/seeders/data/provinces.json'); - $provinces = json_decode($json); - - foreach ($provinces as $item) { - Province::forceCreate([ - 'id' => $item->id, - 'code' => $item->code, - 'name_th' => $item->name_th, - 'name_en' => $item->name_en, - ThaiAddresses::getGeographyForeignKeyName() => $item->geography_id, - ]); - } - } -} diff --git a/tests/database/seeders/SubdistrictSeeder.php b/tests/database/seeders/SubdistrictSeeder.php deleted file mode 100644 index 057e9b0..0000000 --- a/tests/database/seeders/SubdistrictSeeder.php +++ /dev/null @@ -1,28 +0,0 @@ -delete(); - $json = File::get(__DIR__.'/../../../database/seeders/data/subdistricts.json'); - $subdistricts = json_decode($json); - - foreach ($subdistricts as $item) { - Subdistrict::forceCreate([ - 'id' => $item->id, - 'zip_code' => $item->zip_code, - 'name_th' => $item->name_th, - 'name_en' => $item->name_en, - ThaiAddresses::getDistrictForeignKeyName() => $item->district_id, - ]); - } - } -} diff --git a/tests/src/Models/User.php b/tests/src/Models/User.php deleted file mode 100644 index f99d60d..0000000 --- a/tests/src/Models/User.php +++ /dev/null @@ -1,20 +0,0 @@ -artisan('migrate:fresh', [ - '--force' => true, - '--seed' => false, - '--path' => __DIR__.'/../../database/migrations', //package migration path - '--realpath' => true, - ])->run(); - - $this->artisan('migrate', [ - '--force' => true, - '--seed' => false, - '--path' => __DIR__.'/../database/migrations', //test migration path - '--realpath' => true, - ])->run(); - - $this->seed(DatabaseSeeder::class); - - Factory::guessFactoryNamesUsing( - fn (string $modelName) => 'Soap\\ThaiAddresses\\Tests\\Database\\Factories\\'.class_basename($modelName).'Factory' - ); - } - - protected function getPackageProviders($app) - { - return [ - ThaiAddressesServiceProvider::class, - ]; - } - - public function getEnvironmentSetUp($app) - { - config()->set('auth.providers.users.model', User::class); - config()->set('database.default', 'testing'); - - config()->set('database.connections.mysql', [ - 'driver' => 'mysql', - 'database' => 'thai_addresses_package_test', - 'host' => '127.0.0.1', - 'username' => 'root', - 'prefix' => '', - 'charset' => 'utf8mb4', - 'collation' => 'utf8mb4_unicode_ci', - ]); - } -} diff --git a/tests/src/Unit/DatabaseSeederTest.php b/tests/src/Unit/DatabaseSeederTest.php deleted file mode 100644 index 87105d8..0000000 --- a/tests/src/Unit/DatabaseSeederTest.php +++ /dev/null @@ -1,40 +0,0 @@ -assertCount(6, $regions, '6 regions exists'); - } - - /** @test */ - public function province_records_existsh() - { - $provinces = Province::all(); - $this->assertCount(77, $provinces, '77 provinces exists'); - } - - /** @test */ - public function district_record_exists() - { - $districts = District::all(); - $this->assertCount(1006, $districts, '1006 districts exists'); - } - - /** @test */ - public function subdistrict_records_exists() - { - $subdistricts = Subdistrict::all(); - $this->assertCount(8913, $subdistricts, '8913 subdistricts exists'); - } -} diff --git a/tests/src/Unit/ProvinceTest.php b/tests/src/Unit/ProvinceTest.php deleted file mode 100644 index aa92168..0000000 --- a/tests/src/Unit/ProvinceTest.php +++ /dev/null @@ -1,30 +0,0 @@ -first(); - $this->assertEquals('กระบี่', $province->name_th, 'province has Thai name'); - } - - /** @test */ - public function a_province_has_name_en() - { - $province = Province::where('name_th', '=', 'กระบี่')->first(); - $this->assertEquals('Krabi', $province->name_en, 'province has English name'); - } - - /** @test */ - public function a_province_belongs_to_region() - { - $province = Province::where('name_en', '=', 'Krabi')->first(); - $this->assertEquals('ภาคใต้', $province->geography->name_th, 'province belongs to region'); - } -} diff --git a/workbench/app/Models/.gitkeep b/workbench/app/Models/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/workbench/app/Models/User.php b/workbench/app/Models/User.php new file mode 100644 index 0000000..c4b3254 --- /dev/null +++ b/workbench/app/Models/User.php @@ -0,0 +1,47 @@ + + */ + protected $fillable = [ + 'name', + 'email', + 'password', + ]; + + /** + * The attributes that should be hidden for serialization. + * + * @var array + */ + protected $hidden = [ + 'password', + 'remember_token', + ]; + + /** + * The attributes that should be cast. + * + * @var array + */ + protected $casts = [ + 'email_verified_at' => 'datetime', + 'password' => 'hashed', + ]; +} diff --git a/workbench/app/Providers/WorkbenchServiceProvider.php b/workbench/app/Providers/WorkbenchServiceProvider.php new file mode 100644 index 0000000..e8cec9c --- /dev/null +++ b/workbench/app/Providers/WorkbenchServiceProvider.php @@ -0,0 +1,24 @@ + + */ +class UserFactory extends Factory +{ + /** + * The current password being used by the factory. + */ + protected static ?string $password; + + /** + * The name of the factory's corresponding model. + * + * @var class-string + */ + protected $model = User::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'name' => fake()->name(), + 'email' => fake()->unique()->safeEmail(), + 'email_verified_at' => now(), + 'password' => static::$password ??= Hash::make('password'), + 'remember_token' => Str::random(10), + ]; + } + + /** + * Indicate that the model's email address should be unverified. + */ + public function unverified(): static + { + return $this->state(fn (array $attributes) => [ + 'email_verified_at' => null, + ]); + } +} diff --git a/workbench/database/migrations/.gitkeep b/workbench/database/migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/workbench/database/seeders/DatabaseSeeder.php b/workbench/database/seeders/DatabaseSeeder.php new file mode 100644 index 0000000..195366c --- /dev/null +++ b/workbench/database/seeders/DatabaseSeeder.php @@ -0,0 +1,25 @@ +create([ + 'name' => 'Test User', + 'email' => 'test@example.com', + ]); + + $this->call([ + + ]); + } +} diff --git a/workbench/resources/views/.gitkeep b/workbench/resources/views/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/workbench/routes/console.php b/workbench/routes/console.php new file mode 100644 index 0000000..eff2ed2 --- /dev/null +++ b/workbench/routes/console.php @@ -0,0 +1,8 @@ +comment(Inspiring::quote()); +})->purpose('Display an inspiring quote')->hourly(); diff --git a/workbench/routes/web.php b/workbench/routes/web.php new file mode 100644 index 0000000..86a06c5 --- /dev/null +++ b/workbench/routes/web.php @@ -0,0 +1,7 @@ + Date: Tue, 12 Nov 2024 03:21:44 +0000 Subject: [PATCH 5/6] Fix styling --- tests/Feature/UserTest.php | 2 +- tests/TestCase.php | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/Feature/UserTest.php b/tests/Feature/UserTest.php index 428f7cc..27915d8 100644 --- a/tests/Feature/UserTest.php +++ b/tests/Feature/UserTest.php @@ -7,7 +7,7 @@ test('user can have address', function () { $user = User::factory()->create(); $subdistrict = Subdistrict::where('name_th', '=', 'กระบี่ใหญ่')->first(); - $address = new Address; + $address = new Address(); $address->fill([ 'label' => 'Default Address', 'given_name' => 'Prasit', diff --git a/tests/TestCase.php b/tests/TestCase.php index 9335880..8d934c2 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -7,11 +7,12 @@ use Illuminate\Foundation\Testing\RefreshDatabase; use Orchestra\Testbench\Concerns\WithWorkbench; use Orchestra\Testbench\TestCase as Orchestra; -use Soap\ThaiAddresses\Tests\Models\User; -use Soap\ThaiAddresses\ThaiAddressesServiceProvider; use function Orchestra\Testbench\workbench_path; +use Soap\ThaiAddresses\Tests\Models\User; +use Soap\ThaiAddresses\ThaiAddressesServiceProvider; + class TestCase extends Orchestra { // autoload using workbench.yaml From 709dec19f5b6ad5814e5a4071af930c1bf451760 Mon Sep 17 00:00:00 2001 From: Prasit Gebsaap Date: Tue, 12 Nov 2024 13:04:45 +0700 Subject: [PATCH 6/6] Update code for better testing --- .phpunit.cache/test-results | 2 +- database/seeders/DistrictSeeder.php | 6 +++++- database/seeders/GeographySeeder.php | 6 +++++- database/seeders/ProvinceSeeder.php | 7 ++++++- database/seeders/SubdistrictSeeder.php | 7 ++++++- src/ThaiAddresses.php | 5 +++++ tests/Feature/UserTest.php | 4 ++++ tests/TestCase.php | 12 +++++++++--- tests/Unit/DatabaseSeederTest.php | 2 +- 9 files changed, 42 insertions(+), 9 deletions(-) diff --git a/.phpunit.cache/test-results b/.phpunit.cache/test-results index 89bad85..f612a94 100644 --- a/.phpunit.cache/test-results +++ b/.phpunit.cache/test-results @@ -1 +1 @@ -{"version":"pest_2.35.1","defects":{"P\\Tests\\Feature\\UserTest::__pest_evaluable_user_can_have_address":8,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::province_records_existsh":8,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::subdistrict_records_exists":8,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::district_record_exists":8,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::geography_records_exists":8,"Soap\\ThaiAddresses\\Tests\\Unit\\ProvinceTest::a_province_has_name_en":8,"Soap\\ThaiAddresses\\Tests\\Unit\\ProvinceTest::a_province_has_name_th":8,"Soap\\ThaiAddresses\\Tests\\Unit\\ProvinceTest::a_province_belongs_to_region":8,"P\\Tests\\Unit\\DatabaseSeederTest::__pest_evaluable_district_record_exists":7,"P\\Tests\\Unit\\DatabaseSeederTest::__pest_evaluable_province_records_existsh":7,"P\\Tests\\Unit\\DatabaseSeederTest::__pest_evaluable_geography_records_exists":7,"P\\Tests\\Unit\\DatabaseSeederTest::__pest_evaluable_subdistrict_records_exists":7,"P\\Tests\\Unit\\DatabaseSeederTest::__pest_evaluable_seeder_command_is_working":8},"times":{"Soap\\ThaiAddresses\\Tests\\Unit\\ProvinceTest::a_province_has_name_en":0.001,"Soap\\ThaiAddresses\\Tests\\Unit\\ProvinceTest::a_province_has_name_th":0.001,"Soap\\ThaiAddresses\\Tests\\Unit\\ProvinceTest::a_province_belongs_to_region":0.002,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::district_record_exists":0.007,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::geography_records_exists":0,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::province_records_existsh":0.001,"Soap\\ThaiAddresses\\Tests\\Unit\\DatabaseSeederTest::subdistrict_records_exists":0.061,"P\\Tests\\src\\Feature\\UserTest::__pest_evaluable_user_can_have_address":0.037,"P\\Tests\\Unit\\DatabaseSeederTest::__pest_evaluable_geography_records_exists":0.008,"P\\Tests\\Unit\\DatabaseSeederTest::__pest_evaluable_province_records_existsh":0.007,"P\\Tests\\Unit\\DatabaseSeederTest::__pest_evaluable_subdistrict_records_exists":0.007,"P\\Tests\\Unit\\DatabaseSeederTest::__pest_evaluable_district_record_exists":0.011,"P\\Tests\\Feature\\UserTest::__pest_evaluable_user_can_have_address":0.007}} \ No newline at end of file +{"version":"pest_2.36.0","defects":[],"times":{"P\\Tests\\Feature\\UserTest::__pest_evaluable_user_can_have_address":0.097,"P\\Tests\\Unit\\DatabaseSeederTest::__pest_evaluable_seeder_command_is_working":0.092,"P\\Tests\\Unit\\DatabaseSeederTest::__pest_evaluable_seeder_commands_are_working":0.08}} \ No newline at end of file diff --git a/database/seeders/DistrictSeeder.php b/database/seeders/DistrictSeeder.php index ddd84dd..582a5cc 100644 --- a/database/seeders/DistrictSeeder.php +++ b/database/seeders/DistrictSeeder.php @@ -11,7 +11,11 @@ class DistrictSeeder extends Seeder public function run() { District::query()->delete(); - $json = File::get(base_path('vendor/soap/thai-addresses/database/seeders/data/districts.json')); + if (File::exists(base_path('vendor/soap/thai-addresses/database/seeders/data/districts.json'))) { + $json = File::get(base_path('vendor/soap/thai-addresses/database/seeders/data/districts.json')); + } else { + $json = File::get(realpath('database/seeders/data/districts.json')); + } $districts = json_decode($json); foreach ($districts as $item) { diff --git a/database/seeders/GeographySeeder.php b/database/seeders/GeographySeeder.php index 2bf8437..7fbafa2 100644 --- a/database/seeders/GeographySeeder.php +++ b/database/seeders/GeographySeeder.php @@ -11,7 +11,11 @@ class GeographySeeder extends Seeder public function run() { Geography::query()->delete(); - $json = File::get(base_path('/vendor/soap/thai-addresses/database/seeders/data/geographies.json')); + if (File::exists(base_path('/vendor/soap/thai-addresses/database/seeders/data/geographies.json'))) { + $json = File::get(base_path('/vendor/soap/thai-addresses/database/seeders/data/geographies.json')); + } else { + $json = File::get(__DIR__.'/data/geographies.json'); + } $geographies = json_decode($json); foreach ($geographies as $item) { diff --git a/database/seeders/ProvinceSeeder.php b/database/seeders/ProvinceSeeder.php index a9af188..8078122 100644 --- a/database/seeders/ProvinceSeeder.php +++ b/database/seeders/ProvinceSeeder.php @@ -12,7 +12,12 @@ class ProvinceSeeder extends Seeder public function run() { Province::query()->delete(); - $json = File::get(base_path('/vendor/soap/thai-addresses/database/seeders/data/provinces.json')); + if (File::exists(base_path('/vendor/soap/thai-addresses/database/seeders/data/provinces.json'))) { + $json = File::get(base_path('/vendor/soap/thai-addresses/database/seeders/data/provinces.json')); + } else { + $json = File::get(__DIR__.'/data/provinces.json'); + } + $provinces = json_decode($json); foreach ($provinces as $item) { diff --git a/database/seeders/SubdistrictSeeder.php b/database/seeders/SubdistrictSeeder.php index 60a4d01..9191cdc 100644 --- a/database/seeders/SubdistrictSeeder.php +++ b/database/seeders/SubdistrictSeeder.php @@ -11,7 +11,12 @@ class SubdistrictSeeder extends Seeder public function run() { Subdistrict::query()->delete(); - $json = File::get(base_path('/vendor/soap/thai-addresses/database/seeders/data/subdistricts.json')); + if (File::exists(base_path('/vendor/soap/thai-addresses/database/seeders/data/subdistricts.json'))) { + $json = File::get(base_path('/vendor/soap/thai-addresses/database/seeders/data/subdistricts.json')); + } else { + $json = File::get(__DIR__.'/data/subdistricts.json'); + } + $subdistricts = json_decode($json); foreach ($subdistricts as $item) { diff --git a/src/ThaiAddresses.php b/src/ThaiAddresses.php index 5967c83..53fa8b5 100644 --- a/src/ThaiAddresses.php +++ b/src/ThaiAddresses.php @@ -71,4 +71,9 @@ public function getDistrictForeignKeyName(): string { return $this->config['district']['foreign_key']; } + + public function lastUpdated() + { + return '2024-11-01'; + } } diff --git a/tests/Feature/UserTest.php b/tests/Feature/UserTest.php index 27915d8..98555d5 100644 --- a/tests/Feature/UserTest.php +++ b/tests/Feature/UserTest.php @@ -4,6 +4,10 @@ use Soap\ThaiAddresses\Models\Subdistrict; use Workbench\App\Models\User; +beforeEach(function () { + $this->artisan('thai-addresses:db-seed'); +}); + test('user can have address', function () { $user = User::factory()->create(); $subdistrict = Subdistrict::where('name_th', '=', 'กระบี่ใหญ่')->first(); diff --git a/tests/TestCase.php b/tests/TestCase.php index 8d934c2..794bef7 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,6 +5,7 @@ use Illuminate\Config\Repository; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Str; use Orchestra\Testbench\Concerns\WithWorkbench; use Orchestra\Testbench\TestCase as Orchestra; @@ -23,9 +24,14 @@ protected function setUp(): void { parent::setUp(); - Factory::guessFactoryNamesUsing( - fn (string $modelName) => 'Soap\\ThaiAddresses\\Tests\\Database\\Factories\\'.class_basename($modelName).'Factory' - ); + Factory::guessFactoryNamesUsing(function (string $modelName) { + if (Str::startsWith($modelName, 'Workbench\\App\\Models\\')) { + // Factories within the tests directory + return 'Workbench\\Database\\Factories\\'.class_basename($modelName).'Factory'; + } + + return 'Soap\\ThaiAddresses\\Tests\\Database\\Factories\\'.class_basename($modelName).'Factory'; + }); $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); //load package's migrations } diff --git a/tests/Unit/DatabaseSeederTest.php b/tests/Unit/DatabaseSeederTest.php index 2141ee1..e6c33c9 100644 --- a/tests/Unit/DatabaseSeederTest.php +++ b/tests/Unit/DatabaseSeederTest.php @@ -9,7 +9,7 @@ $this->artisan('thai-addresses:db-seed'); }); -test('seeder command is working', function () { +test('seeder commands are working', function () { $regions = Geography::all(); expect($regions)->toHaveCount(6, '6 regions exists');