From 614060a54c7c9546b3627e0d4dc1fb2e11bce752 Mon Sep 17 00:00:00 2001 From: Luca Longo Date: Mon, 23 Sep 2024 12:03:51 +0200 Subject: [PATCH] Laravel 11 only --- .phpunit.cache/test-results | 1 + composer.json | 42 ++++- database/factories/AddressFactory.php | 66 ++++++++ database/factories/UserFactory.php | 23 +++ .../create_addressable_table.php.stub | 23 +-- phpstan-baseline.neon | 0 phpstan.neon.dist | 13 ++ phpunit.xml.dist | 43 ++--- src/AddressableServiceProvider.php | 10 +- src/Concerns/UsesAddressableConfig.php | 4 +- src/Geo/Casts/PointCast.php | 40 ----- src/Geo/Eloquent/Builder.php | 24 --- .../Concerns/ImplementsQueryScopes.php | 33 ---- src/Geo/Eloquent/GeoExpression.php | 35 ---- src/Geo/Eloquent/ImplementsGeoAttributes.php | 50 ------ src/Geo/Eloquent/QueryBuilder.php | 26 --- src/Geo/Types/GeoFactory.php | 48 ------ src/Geo/Types/Geometry.php | 45 ----- src/Geo/Types/Point.php | 69 -------- src/Models/Address.php | 12 +- src/Models/Concerns/ImplementsMarkPrimary.php | 2 +- tests/ArchTest.php | 5 + tests/Feature/Models/AddressTest.php | 159 +++++++----------- tests/Pest.php | 1 + tests/TestCase.php | 31 ++-- tests/TestClasses/User.php | 8 +- tests/database/factories/ShipmentFactory.php | 26 --- tests/database/factories/UserFactory.php | 15 -- .../2014_10_12_000000_create_users_table.php | 11 +- 29 files changed, 272 insertions(+), 593 deletions(-) create mode 100644 .phpunit.cache/test-results create mode 100644 database/factories/AddressFactory.php create mode 100644 database/factories/UserFactory.php create mode 100644 phpstan-baseline.neon create mode 100644 phpstan.neon.dist delete mode 100644 src/Geo/Casts/PointCast.php delete mode 100644 src/Geo/Eloquent/Builder.php delete mode 100644 src/Geo/Eloquent/Concerns/ImplementsQueryScopes.php delete mode 100644 src/Geo/Eloquent/GeoExpression.php delete mode 100644 src/Geo/Eloquent/ImplementsGeoAttributes.php delete mode 100644 src/Geo/Eloquent/QueryBuilder.php delete mode 100644 src/Geo/Types/GeoFactory.php delete mode 100644 src/Geo/Types/Geometry.php delete mode 100644 src/Geo/Types/Point.php create mode 100644 tests/ArchTest.php create mode 100644 tests/Pest.php delete mode 100644 tests/database/factories/ShipmentFactory.php delete mode 100644 tests/database/factories/UserFactory.php diff --git a/.phpunit.cache/test-results b/.phpunit.cache/test-results new file mode 100644 index 0000000..5681332 --- /dev/null +++ b/.phpunit.cache/test-results @@ -0,0 +1 @@ +{"version":"pest_3.1.0","defects":{"P\\Tests\\Feature\\Models\\AddressTest::__pest_evaluable_it_return_empty_addresses_collection_when_eloquent_has_no_addresses":8,"P\\Tests\\Feature\\Models\\AddressTest::__pest_evaluable_it_fires_billing_address_primary_unmarked_event_on_make_primary":8,"P\\Tests\\Feature\\Models\\AddressTest::__pest_evaluable_it_return_addresses_collection_when_eloquent_has_addresses":8,"P\\Tests\\Feature\\Models\\AddressTest::__pest_evaluable_it_fires_billing_address_primary_marked_event_on_make_primary":8,"P\\Tests\\Feature\\Models\\AddressTest::__pest_evaluable_it_fires_address_primary_unmarked_event_on_make_primary":8,"P\\Tests\\Feature\\Models\\AddressTest::__pest_evaluable_it_fires_shipping_address_primary_unmarked_event_on_make_primary":8,"P\\Tests\\Feature\\Models\\AddressTest::__pest_evaluable_it_fires_shipping_address_primary_marked_event_on_make_primary":8,"P\\Tests\\Feature\\Models\\AddressTest::__pest_evaluable_it_fires_address_primary_marked_event_on_make_primary":8},"times":{"P\\Tests\\Feature\\Models\\AddressTest::__pest_evaluable_it_return_addresses_collection_when_eloquent_has_addresses":0.006,"P\\Tests\\Feature\\Models\\AddressTest::__pest_evaluable_it_fires_address_primary_unmarked_event_on_make_primary":0.004,"P\\Tests\\Feature\\Models\\AddressTest::__pest_evaluable_it_fires_shipping_address_primary_unmarked_event_on_make_primary":0.004,"P\\Tests\\Feature\\Models\\AddressTest::__pest_evaluable_it_fires_address_primary_marked_event_on_make_primary":0.044,"P\\Tests\\Feature\\Models\\AddressTest::__pest_evaluable_it_fires_billing_address_primary_marked_event_on_make_primary":0.007,"P\\Tests\\Feature\\Models\\AddressTest::__pest_evaluable_it_fires_billing_address_primary_unmarked_event_on_make_primary":0.005,"P\\Tests\\Feature\\Models\\AddressTest::__pest_evaluable_it_return_empty_addresses_collection_when_eloquent_has_no_addresses":0.019,"P\\Tests\\Feature\\Models\\AddressTest::__pest_evaluable_it_fires_shipping_address_primary_marked_event_on_make_primary":0.004,"P\\Tests\\ArchTest::__pest_evaluable_it_will_not_use_debugging_functions":0.045}} \ No newline at end of file diff --git a/composer.json b/composer.json index 385b2fb..277ee3a 100644 --- a/composer.json +++ b/composer.json @@ -17,13 +17,19 @@ ], "require": { "php": "^8.1|^8.2|^8.3", - "illuminate/support": "^10.0|^11.0", - "geo-io/wkb-parser": "^1.0", - "jmikola/geojson": "^1.1" + "illuminate/contracts": "^11.23" }, "require-dev": { - "orchestra/testbench": "^6.23|^7.0|^8.0", - "phpunit/phpunit": "^10.0|^11.0" + "larastan/larastan": "^2.9", + "laravel/pint": "^1.17", + "orchestra/testbench": "^9.4", + "pestphp/pest": "^3.1", + "pestphp/pest-plugin-arch": "^3.0", + "pestphp/pest-plugin-drift": "3.x-dev", + "pestphp/pest-plugin-laravel": "^3.0", + "phpstan/extension-installer": "^1.4", + "phpstan/phpstan-deprecation-rules": "^1.2", + "phpstan/phpstan-phpunit": "^1.4" }, "autoload": { "psr-4": { @@ -32,15 +38,33 @@ }, "autoload-dev": { "psr-4": { - "Masterix21\\Addressable\\Tests\\": "tests" + "Masterix21\\Addressable\\Tests\\": "tests", + "Masterix21\\Addressable\\Database\\Factories\\": "database/factories" } }, "scripts": { - "test": "vendor/bin/phpunit", - "test-coverage": "vendor/bin/phpunit --coverage-html coverage" + "post-autoload-dump": "@composer run prepare", + "prepare": "@php vendor/bin/testbench package:discover --ansi", + "build": [ + "@composer run prepare", + "@php vendor/bin/testbench workbench:build --ansi" + ], + "start": [ + "Composer\\Config::disableProcessTimeout", + "@composer run build", + "@php vendor/bin/testbench serve" + ], + "analyse": "vendor/bin/phpstan analyse", + "test": "vendor/bin/pest", + "test-coverage": "vendor/bin/pest --coverage", + "format": "vendor/bin/pint" }, "config": { - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true, + "phpstan/extension-installer": true + } }, "extra": { "laravel": { diff --git a/database/factories/AddressFactory.php b/database/factories/AddressFactory.php new file mode 100644 index 0000000..d339e52 --- /dev/null +++ b/database/factories/AddressFactory.php @@ -0,0 +1,66 @@ + $this->faker->boolean, + 'is_billing' => $this->faker->boolean, + 'is_shipping' => $this->faker->boolean, + 'label' => $this->faker->boolean ? $this->faker->streetName : null, + 'street_address1' => $this->faker->streetAddress, + 'street_address2' => $this->faker->streetAddress, + 'zip' => $this->faker->postcode, + 'city' => $this->faker->city, + 'state' => $this->faker->state, + 'country' => $this->faker->countryCode, + 'coordinates' => [$this->faker->latitude, $this->faker->longitude], + ]; + } + + public function addressable(Model $model): Factory + { + return $this->state(function (array $attributes) use ($model) { + return [ + 'addressable_type' => $model::class, + 'addressable_id' => $model->getKey(), + ]; + }); + } + + public function primary(): Factory + { + return $this->state(function (array $attributes) { + return [ + 'is_primary' => true, + ]; + }); + } + + public function billing(): Factory + { + return $this->state(function (array $attributes) { + return [ + 'is_billing' => true, + ]; + }); + } + + public function shipping(): Factory + { + return $this->state(function (array $attributes) { + return [ + 'is_shipping' => true, + ]; + }); + } +} diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php new file mode 100644 index 0000000..b9c8d52 --- /dev/null +++ b/database/factories/UserFactory.php @@ -0,0 +1,23 @@ + $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/database/migrations/create_addressable_table.php.stub b/database/migrations/create_addressable_table.php.stub index fae51ef..1994f87 100644 --- a/database/migrations/create_addressable_table.php.stub +++ b/database/migrations/create_addressable_table.php.stub @@ -5,16 +5,11 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; use Masterix21\Addressable\Concerns\UsesAddressableConfig; -class CreateAddressableTable extends Migration +return new class extends Migration { use UsesAddressableConfig; - /** - * Run the migrations. - * - * @return void - */ - public function up() + public function up(): void { Schema::create($this->addressesDatabaseTable(), function (Blueprint $table) { $table->id(); @@ -29,21 +24,15 @@ class CreateAddressableTable extends Migration $table->string('zip')->nullable(); $table->string('city')->nullable(); $table->string('state')->nullable(); - $table->string('country')->nullable(); - $table->string('country_code', 2)->nullable(); - $table->point('position', config('addressable.srid'))->nullable(); + $table->string('country', 4)->nullable(); + $table->geography('coordinates', 'point')->nullable(); $table->timestamps(); }); } - /** - * Reverse the migrations. - * - * @return void - */ - public function down() + public function down(): void { Schema::dropIfExists($this->addressesDatabaseTable()); } -} +}; diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 0000000..e69de29 diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..de458d6 --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,13 @@ +includes: + - phpstan-baseline.neon + +parameters: + level: 5 + paths: + - src + - config + - database + tmpDir: build/phpstan + checkOctaneCompatibility: true + checkModelProperties: true + diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 5d17c25..bfe434d 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,28 +1,31 @@ - + - + tests - - - src/ - - - - - - + + + + ./src + + diff --git a/src/AddressableServiceProvider.php b/src/AddressableServiceProvider.php index 333490f..bf416c6 100644 --- a/src/AddressableServiceProvider.php +++ b/src/AddressableServiceProvider.php @@ -6,16 +6,16 @@ class AddressableServiceProvider extends ServiceProvider { - public function boot() + public function boot(): void { if ($this->app->runningInConsole()) { $this->publishes([ - __DIR__ . '/../config/addressable.php' => config_path('addressable.php'), + __DIR__.'/../config/addressable.php' => config_path('addressable.php'), ], 'config'); if (! class_exists('CreateAddressableTable')) { $this->publishes([ - __DIR__ . '/../database/migrations/create_addressable_table.php.stub' => database_path('migrations/' . date('Y_m_d_His', time()) . '_create_addressable_table.php'), + __DIR__.'/../database/migrations/create_addressable_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_addressable_table.php'), ], 'migrations'); } } @@ -23,8 +23,8 @@ public function boot() $this->loadViewsFrom(__DIR__.'/../resources/views', 'addressable'); } - public function register() + public function register(): void { - $this->mergeConfigFrom(__DIR__ . '/../config/addressable.php', 'addressable'); + $this->mergeConfigFrom(__DIR__.'/../config/addressable.php', 'addressable'); } } diff --git a/src/Concerns/UsesAddressableConfig.php b/src/Concerns/UsesAddressableConfig.php index 6102ee0..e75a06c 100644 --- a/src/Concerns/UsesAddressableConfig.php +++ b/src/Concerns/UsesAddressableConfig.php @@ -4,12 +4,12 @@ trait UsesAddressableConfig { - public function addressModel() : string + public function addressModel(): string { return config('addressable.models.address'); } - public function addressesDatabaseTable() : string + public function addressesDatabaseTable(): string { return config('addressable.tables.addresses'); } diff --git a/src/Geo/Casts/PointCast.php b/src/Geo/Casts/PointCast.php deleted file mode 100644 index c4854dd..0000000 --- a/src/Geo/Casts/PointCast.php +++ /dev/null @@ -1,40 +0,0 @@ -asWKT($value); - } - } - - return parent::update($values); - } - - protected function asWKT(Geometry $geometry) - { - return new GeoExpression($geometry); - } -} diff --git a/src/Geo/Eloquent/Concerns/ImplementsQueryScopes.php b/src/Geo/Eloquent/Concerns/ImplementsQueryScopes.php deleted file mode 100644 index f55548a..0000000 --- a/src/Geo/Eloquent/Concerns/ImplementsQueryScopes.php +++ /dev/null @@ -1,33 +0,0 @@ -select() - ->addSelect(DB::raw(sprintf('%s as distance', $this->toStDistanceSql($position)))) - ->whereRaw(sprintf('%s %s %d', $this->toStDistanceSql($position), $condition, $meters)); - } - - protected function toStDistanceSql(Point $position): string - { - return sprintf("ST_Distance_Sphere(position, ST_GeomFromText('%s', %d))", $position->toWKT(), $position->getSrid()); - } -} diff --git a/src/Geo/Eloquent/GeoExpression.php b/src/Geo/Eloquent/GeoExpression.php deleted file mode 100644 index 2c54082..0000000 --- a/src/Geo/Eloquent/GeoExpression.php +++ /dev/null @@ -1,35 +0,0 @@ -hasSrid()) { - return "ST_GeomFromText(?, ?)"; - } - - return "ST_GeomFromText(?)"; - } - - public function getGeoValue(): string - { - return $this->value->toWkt(); - } - - public function hasSrid(): bool - { - return filled($this->getSrid()); - } - - public function getSrid(): ?int - { - return $this->value->getSrid(); - } -} diff --git a/src/Geo/Eloquent/ImplementsGeoAttributes.php b/src/Geo/Eloquent/ImplementsGeoAttributes.php deleted file mode 100644 index 9f432fb..0000000 --- a/src/Geo/Eloquent/ImplementsGeoAttributes.php +++ /dev/null @@ -1,50 +0,0 @@ -getConnection(); - - return new QueryBuilder( - $connection, - $connection->getQueryGrammar(), - $connection->getPostProcessor() - ); - } - - public function newEloquentBuilder($query): Builder - { - return new Builder($query); - } - - protected function performInsert(EloquentBuilder $query, array $options = []) - { - $preserveGeometries = []; - - foreach ($this->attributes as $key => $value) { - if ($value instanceof Geometry) { - $preserveGeometries[$key] = $value; - $this->attributes[$key] = new GeoExpression($value); - } - } - - $insert = parent::performInsert($query, $options); - - foreach ($preserveGeometries as $key => $value) { - $this->attributes[$key] = $value; - } - - return $insert; - } -} diff --git a/src/Geo/Eloquent/QueryBuilder.php b/src/Geo/Eloquent/QueryBuilder.php deleted file mode 100644 index 32fee0d..0000000 --- a/src/Geo/Eloquent/QueryBuilder.php +++ /dev/null @@ -1,26 +0,0 @@ -getGeoValue(); - - if ($binding->hasSrid()) { - $geoBindings[] = $binding->getSrid(); - } - } - - return parent::cleanBindings($geoBindings); - } -} diff --git a/src/Geo/Types/GeoFactory.php b/src/Geo/Types/GeoFactory.php deleted file mode 100644 index 982a5e4..0000000 --- a/src/Geo/Types/GeoFactory.php +++ /dev/null @@ -1,48 +0,0 @@ -parse($wkb); - - if ($srid > 0) { - $parsed->setSrid($srid); - } - - return $parsed; - } - - public function getSrid(): int - { - return $this->srid ?? config('addressable.srid'); - } - - public function setSrid(int $value): self - { - $this->srid = $value; - - return $this; - } -} diff --git a/src/Geo/Types/Point.php b/src/Geo/Types/Point.php deleted file mode 100644 index d0a0864..0000000 --- a/src/Geo/Types/Point.php +++ /dev/null @@ -1,69 +0,0 @@ -longitude = $value; - - return $this; - } - - public function setLatitude(?float $value): self - { - $this->latitude = $value; - - return $this; - } - - public function getLongitude(): ?float - { - return $this->longitude; - } - - public function getLatitude(): ?float - { - return $this->latitude; - } - - public function toWKT(): string - { - return sprintf("POINT(%s)", $this); - } - - public function toArray(): array - { - return [ - 'longitude' => $this->longitude, - 'latitude' => $this->latitude, - 'srid' => $this->srid, - ]; - } - - public function __toString(): string - { - return $this->longitude .' '. $this->latitude; - } - - public static function castUsing(array $arguments) - { - return PointCast::class; - } - - public function jsonSerialize(): mixed - { - return $this->toArray(); - } -} diff --git a/src/Models/Address.php b/src/Models/Address.php index d905c75..0a012d4 100644 --- a/src/Models/Address.php +++ b/src/Models/Address.php @@ -2,16 +2,14 @@ namespace Masterix21\Addressable\Models; +use Illuminate\Database\Eloquent\Casts\AsArrayObject; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; -use Masterix21\Addressable\Geo\Eloquent\ImplementsGeoAttributes; -use Masterix21\Addressable\Geo\Types\Point; use Masterix21\Addressable\Models\Concerns\ImplementsMarkPrimary; class Address extends Model { use HasFactory; - use ImplementsGeoAttributes; use ImplementsMarkPrimary; protected $fillable = [ @@ -27,8 +25,7 @@ class Address extends Model 'city', 'state', 'country', - 'country_code', - 'position', + 'coordinates', ]; protected $appends = [ @@ -39,10 +36,10 @@ class Address extends Model 'is_primary' => 'bool', 'is_billing' => 'bool', 'is_shipping' => 'bool', - 'position' => Point::class, + 'coordinates' => AsArrayObject::class, ]; - public function getDisplayAddressAttribute() : string + public function getDisplayAddressAttribute(): string { $keys = [ 'street_address1', @@ -51,7 +48,6 @@ public function getDisplayAddressAttribute() : string 'city', 'state', 'country', - 'country_code', ]; return collect($this->getAttributes()) diff --git a/src/Models/Concerns/ImplementsMarkPrimary.php b/src/Models/Concerns/ImplementsMarkPrimary.php index 6713cf6..16f7449 100644 --- a/src/Models/Concerns/ImplementsMarkPrimary.php +++ b/src/Models/Concerns/ImplementsMarkPrimary.php @@ -24,7 +24,7 @@ public function markPrimary(): void ->where('is_billing', $this->is_billing) ->where('is_shipping', $this->is_shipment) ->where('id', '!=', $this->id) - ->update([ 'is_primary' => false ]); + ->update(['is_primary' => false]); event(new AddressPrimaryMarked($this)); diff --git a/tests/ArchTest.php b/tests/ArchTest.php new file mode 100644 index 0000000..87fb64c --- /dev/null +++ b/tests/ArchTest.php @@ -0,0 +1,5 @@ +expect(['dd', 'dump', 'ray']) + ->each->not->toBeUsed(); diff --git a/tests/Feature/Models/AddressTest.php b/tests/Feature/Models/AddressTest.php index 14c1a57..99f50db 100644 --- a/tests/Feature/Models/AddressTest.php +++ b/tests/Feature/Models/AddressTest.php @@ -1,7 +1,5 @@ create()->first(); +uses(TestCase::class); + +it('return empty addresses collection when eloquent has no addresses', function () { + $user = User::factory()->createOne(); + + expect($user->addresses)->toBeEmpty(); +}); - $this->assertEmpty($user->addresses); - } +it('return addresses collection when eloquent has addresses', function () { + $user = User::factory()->createOne(); - /** @test */ - public function it_return_addresses_collection_when_eloquent_has_addresses() - { - $user = factory(User::class, 1)->create()->first(); + Address::factory()->addressable($user)->createOne(); - factory(Address::class, 1)->create([ 'addressable_id' => $user->id ]); + expect($user->addresses)->not->toBeEmpty(); +}); - $this->assertNotEmpty($user->addresses); - } +it('fires address primary marked event on make primary', function () { + Event::fake(); - /** @test */ - public function it_fires_AddressPrimaryMarked_event_on_make_primary() - { - Event::fake(); + $user = User::factory()->createOne(); - $user = factory(User::class, 1)->create()->first(); + $address = Address::factory()->addressable($user)->createOne(); + $address->markPrimary(); - factory(Address::class, 1) - ->create([ 'addressable_id' => $user->id ]) - ->first() - ->markPrimary(); + Event::assertDispatched(AddressPrimaryMarked::class); +}); - Event::assertDispatched(AddressPrimaryMarked::class); - } +it('fires address primary unmarked event on make primary', function () { + Event::fake(); - /** @test */ - public function it_fires_AddressPrimaryUnmarked_event_on_make_primary() - { - Event::fake(); + $user = User::factory()->createOne(); - $user = factory(User::class, 1)->create()->first(); + $address = Address::factory()->addressable($user)->primary()->createOne(); - factory(Address::class, 1) - ->create([ - 'addressable_id' => $user->id, - 'is_primary' => true, - ]) - ->first() - ->unmarkPrimary(); + $address->unmarkPrimary(); - Event::assertDispatched(AddressPrimaryUnmarked::class); - } + Event::assertDispatched(AddressPrimaryUnmarked::class); +}); - /** @test */ - public function it_fires_BillingAddressPrimaryMarked_event_on_make_primary() - { - Event::fake(); +it('fires billing address primary marked event on make primary', function () { + Event::fake(); - $user = factory(User::class, 1)->create()->first(); + $user = User::factory()->createOne(); - factory(Address::class, 1) - ->create([ - 'addressable_id' => $user->id, - 'is_billing' => true, - ]); + Address::factory()->addressable($user)->billing()->createOne(); - $user->billingAddresses->first()->markPrimary(); + $user->billingAddresses->first()->markPrimary(); - Event::assertDispatched(AddressPrimaryMarked::class); - Event::assertDispatched(BillingAddressPrimaryMarked::class); - } + Event::assertDispatched(AddressPrimaryMarked::class); + Event::assertDispatched(BillingAddressPrimaryMarked::class); +}); - /** @test */ - public function it_fires_BillingAddressPrimaryUnmarked_event_on_make_primary() - { - Event::fake(); +it('fires billing address primary unmarked event on make primary', function () { + Event::fake(); - $user = factory(User::class, 1)->create()->first(); + $user = User::factory()->createOne(); - factory(Address::class, 1) - ->create([ - 'addressable_id' => $user->id, - 'is_primary' => true, - 'is_billing' => true, - ]); + Address::factory()->addressable($user)->primary()->billing()->createOne(); - $user->billingAddress->unmarkPrimary(); + $user->billingAddress->unmarkPrimary(); - Event::assertDispatched(AddressPrimaryUnmarked::class); - Event::assertDispatched(BillingAddressPrimaryUnmarked::class); - } + Event::assertDispatched(AddressPrimaryUnmarked::class); + Event::assertDispatched(BillingAddressPrimaryUnmarked::class); +}); - /** @test */ - public function it_fires_ShippingAddressPrimaryMarked_event_on_make_primary() - { - Event::fake(); +it('fires shipping address primary marked event on make primary', function () { + Event::fake(); - $user = factory(User::class, 1)->create()->first(); + $user = User::factory()->createOne(); - factory(Address::class, 1) - ->create([ - 'addressable_id' => $user->id, - 'is_shipping' => true, - ]); + Address::factory()->addressable($user)->shipping()->createOne(); - $user->shippingAddresses->first()->markPrimary(); + $user->shippingAddresses->first()->markPrimary(); - Event::assertDispatched(AddressPrimaryMarked::class); - Event::assertDispatched(ShippingAddressPrimaryMarked::class); - } + Event::assertDispatched(AddressPrimaryMarked::class); + Event::assertDispatched(ShippingAddressPrimaryMarked::class); +}); - /** @test */ - public function it_fires_ShippingAddressPrimaryUnmarked_event_on_make_primary() - { - Event::fake(); +it('fires shipping address primary unmarked event on make primary', function () { + Event::fake(); - $user = factory(User::class, 1)->create()->first(); + $user = User::factory()->createOne(); - factory(Address::class, 1) - ->create([ - 'addressable_id' => $user->id, - 'is_primary' => true, - 'is_shipping' => true, - ]); + Address::factory()->addressable($user)->primary()->shipping()->createOne(); - $user->shippingAddress->unmarkPrimary(); + $user->shippingAddress->unmarkPrimary(); - Event::assertDispatched(AddressPrimaryUnmarked::class); - Event::assertDispatched(ShippingAddressPrimaryUnmarked::class); - } -} + Event::assertDispatched(AddressPrimaryUnmarked::class); + Event::assertDispatched(ShippingAddressPrimaryUnmarked::class); +}); diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1 @@ +withFactories(__DIR__.'/database/factories'); + Factory::guessFactoryNamesUsing( + function (string $modelName) { + return 'Masterix21\\Addressable\\Database\\Factories\\'.class_basename($modelName).'Factory'; + } + ); } - protected function getPackageProviders($app) + protected function getPackageProviders($app): array { return [ AddressableServiceProvider::class, ]; } - public function migrateDb() - { - include_once __DIR__.'/database/migrations/2014_10_12_000000_create_users_table.php'; - (new \CreateUsersTable())->up(); - - include_once __DIR__.'/../database/migrations/create_addressable_table.php.stub'; - (new \CreateAddressableTable())->up(); - } - - public function getEnvironmentSetUp($app) + public function getEnvironmentSetUp($app): void { $app['config']->set('database.default', 'sqlite'); $app['config']->set('database.connections.sqlite', [ @@ -44,4 +40,13 @@ public function getEnvironmentSetUp($app) $this->migrateDb(); } + + public function migrateDb(): void + { + /*$migration = include __DIR__.'/database/migrations/2014_10_12_000000_create_users_table.php'; + $migration->up();*/ + + $migration = include __DIR__.'/../database/migrations/create_addressable_table.php.stub'; + $migration->up(); + } } diff --git a/tests/TestClasses/User.php b/tests/TestClasses/User.php index 6b62599..768d4bc 100644 --- a/tests/TestClasses/User.php +++ b/tests/TestClasses/User.php @@ -2,6 +2,7 @@ namespace Masterix21\Addressable\Tests\TestClasses; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as BaseUser; use Masterix21\Addressable\Models\Concerns\HasAddresses; use Masterix21\Addressable\Models\Concerns\HasBillingAddresses; @@ -9,7 +10,8 @@ class User extends BaseUser { - use HasAddresses, - HasShippingAddresses, - HasBillingAddresses; + use HasAddresses; + use HasBillingAddresses; + use HasFactory; + use HasShippingAddresses; } diff --git a/tests/database/factories/ShipmentFactory.php b/tests/database/factories/ShipmentFactory.php deleted file mode 100644 index a130122..0000000 --- a/tests/database/factories/ShipmentFactory.php +++ /dev/null @@ -1,26 +0,0 @@ -define(Address::class, function (Generator $faker) { - return [ - 'addressable_type' => User::class, - 'addressable_id' => User::inRandomOrder()->first()->id, - 'is_primary' => $faker->boolean, - 'is_billing' => $faker->boolean, - 'is_shipping' => $faker->boolean, - 'label' => $faker->boolean ? $faker->streetName : null, - 'street_address1' => $faker->streetAddress, - 'street_address2' => $faker->streetAddress, - 'zip' => $faker->postcode, - 'city' => $faker->city, - 'state' => $faker->state, - 'country' => $faker->country, - 'country_code' => $faker->countryCode, - 'latitude' => $faker->latitude, - 'longitude' => $faker->longitude, - ]; -}); diff --git a/tests/database/factories/UserFactory.php b/tests/database/factories/UserFactory.php deleted file mode 100644 index 95729cf..0000000 --- a/tests/database/factories/UserFactory.php +++ /dev/null @@ -1,15 +0,0 @@ -define(User::class, function (Generator $faker) { - return [ - 'name' => $faker->name, - 'email' => $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/2014_10_12_000000_create_users_table.php b/tests/database/migrations/2014_10_12_000000_create_users_table.php index 510ba61..750bc49 100644 --- a/tests/database/migrations/2014_10_12_000000_create_users_table.php +++ b/tests/database/migrations/2014_10_12_000000_create_users_table.php @@ -4,9 +4,9 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateUsersTable extends Migration +return new class extends Migration { - public function up() + public function up(): void { Schema::create('users', function (Blueprint $table) { $table->id(); @@ -18,4 +18,9 @@ public function up() $table->timestamps(); }); } -} + + public function down(): void + { + Schema::dropIfExists('users'); + } +};