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/.phpunit.cache/test-results b/.phpunit.cache/test-results new file mode 100644 index 0000000..f612a94 --- /dev/null +++ b/.phpunit.cache/test-results @@ -0,0 +1 @@ +{"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/composer.json b/composer.json index 4260fed..97b383e 100644 --- a/composer.json +++ b/composer.json @@ -20,23 +20,24 @@ } ], "require": { - "php": "^8.1|^8.2", - "illuminate/contracts": "^10.0|^11.0", - "illuminate/support": "^10.0|^11.0", - "laravel/pint": "^1.17", - "spatie/laravel-package-tools": "^1.9.2" + "php": "^8.2|^8.3", + "illuminate/contracts": "^10.0||^11.0", + "illuminate/support": "^10.0||^11.0", + "spatie/laravel-package-tools": "^1.16" }, "require-dev": { - "nunomaduro/collision": "^6.0|^7.0", - "larastan/larastan": "^2.0.1", + "larastan/larastan": "^2.9", + "laravel/pint": "^1.17", + "nunomaduro/collision": "^8.1.1|^7.10", "orangehill/iseed": "^3.0", - "orchestra/testbench": "^8.0|^9.0", - "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-drift": "^2.6", + "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", "spatie/laravel-ray": "^1.26" }, "autoload": { @@ -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/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/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 0be31db..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 d68fc24..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 eb9800d..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/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/phpunit.xml.dist b/phpunit.xml.dist index bc2f8e7..b38d5a0 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,43 +1,30 @@ - - - - tests - - - - - ./src - - - - - - - - - - - - - - + + + + tests/Unit + + + tests/Feature + + + + + + + + + + + + + + + + + + + ./src + + 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..53fa8b5 100644 --- a/src/ThaiAddresses.php +++ b/src/ThaiAddresses.php @@ -2,112 +2,78 @@ 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 $this->config['district']['foreign_key']; + } + + public function lastUpdated() { - return config('thai-addresses.district.foreign_key'); + return '2024-11-01'; } } 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/src/Feature/UserTest.php b/tests/Feature/UserTest.php similarity index 81% rename from tests/src/Feature/UserTest.php rename to tests/Feature/UserTest.php index f0a0125..98555d5 100644 --- a/tests/src/Feature/UserTest.php +++ b/tests/Feature/UserTest.php @@ -1,10 +1,12 @@ artisan('thai-addresses:db-seed'); +}); test('user can have address', function () { $user = User::factory()->create(); @@ -27,5 +29,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..794bef7 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,68 @@ +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..e6c33c9 --- /dev/null +++ b/tests/Unit/DatabaseSeederTest.php @@ -0,0 +1,24 @@ +artisan('thai-addresses:db-seed'); +}); + +test('seeder commands are 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 @@ +