From a95b38ad76592f16389bee3696f9f85e111bf564 Mon Sep 17 00:00:00 2001 From: Fery Wardiyanto Date: Fri, 10 May 2024 04:14:07 +0700 Subject: [PATCH] fix(test): fix test issue with address model relations Signed-off-by: Fery Wardiyanto --- src/Models/Concerns/WithAddresses.php | 4 +++- tests/Models/AddressTest.php | 28 +++++++++++++--------- tests/TestCase.php | 34 --------------------------- 3 files changed, 20 insertions(+), 46 deletions(-) diff --git a/src/Models/Concerns/WithAddresses.php b/src/Models/Concerns/WithAddresses.php index 03e8559..a0cf2a9 100644 --- a/src/Models/Concerns/WithAddresses.php +++ b/src/Models/Concerns/WithAddresses.php @@ -4,6 +4,8 @@ namespace Creasi\Nusa\Models\Concerns; +use Illuminate\Database\Eloquent\Relations\MorphMany; + /** * @mixin \Creasi\Nusa\Contracts\HasAddresses */ @@ -12,7 +14,7 @@ trait WithAddresses /** * @return \Illuminate\Database\Eloquent\Relations\MorphMany|\Creasi\Nusa\Contracts\Address */ - public function addresses() + public function addresses(): MorphMany { return $this->morphMany(\config('creasi.nusa.addressable'), 'addressable'); } diff --git a/tests/Models/AddressTest.php b/tests/Models/AddressTest.php index 66b9e9f..247ac47 100644 --- a/tests/Models/AddressTest.php +++ b/tests/Models/AddressTest.php @@ -11,7 +11,6 @@ use Creasi\Tests\Fixtures\HasOneAddress; use Creasi\Tests\TestCase; use Illuminate\Foundation\Testing\WithFaker; -use PHPUnit\Framework\Attributes\Depends; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\Test; @@ -21,11 +20,16 @@ class AddressTest extends TestCase { use WithFaker; + private function createAddress(array $attrs): Address + { + return $this->app->make(AddressContract::class)->create($attrs); + } + #[Test] - public function it_may_accociate_with_address(): AddressContract + public function it_may_accociate_with_address(): void { $village = Village::query()->inRandomOrder()->first(); - $address = $this->app->make(AddressContract::class)->create([ + $address = $this->createAddress([ 'line' => $this->faker->streetAddress(), ]); @@ -33,28 +37,30 @@ public function it_may_accociate_with_address(): AddressContract $this->assertSame($village->province, $address->province); $this->assertNull($address->addressable); - - return $address->fresh(); } #[Test] - #[Depends('it_may_accociate_with_address')] - public function may_has_many_addresses(Address $address): void + public function may_has_many_addresses(): void { + /** @var HasManyAddresses */ $addressable = HasManyAddresses::create(); - $addressable->addresses()->save($address); + $addressable->addresses()->save( + $this->createAddress(['line' => 'Coba Alamat']) + ); $this->assertCount(1, $addressable->addresses); } #[Test] - #[Depends('it_may_accociate_with_address')] - public function may_has_one_address(Address $address): void + public function may_has_one_address(): void { + /** @var HasOneAddress */ $addressable = HasOneAddress::create(); - $addressable->address()->save($address); + $addressable->address()->save( + $this->createAddress(['line' => 'Coba Alamat']) + ); $this->assertInstanceOf(AddressContract::class, $addressable->address); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 04ae83d..ee5d283 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -39,40 +39,6 @@ protected function defineDatabaseSeeders() } } - /** - * @param \Illuminate\Foundation\Application $app - */ - protected function getEnvironmentSetUp($app): void - { - tap($app->make('config'), function (Repository $config) { - $config->set('app.locale', 'id'); - $config->set('app.faker_locale', 'id_ID'); - - // $conn = env('DB_CONNECTION', 'sqlite'); - - // $conn = $config->get('database.default'); - - // if ($conn === 'sqlite') { - // // $database = __DIR__.'/test.sqlite'; - - // // if (self::$shouldMigrate) { - // // $this->recreateDatabase($database); - // // } - - // $this->mergeConfig($config, 'database.connections.sqlite', [ - // 'database' => ':memory:', - // 'foreign_key_constraints' => true, - // ]); - // } else { - // $this->mergeConfig($config, 'database.connections.'.$conn, [ - // 'database' => env('DB_DATABASE', 'creasi_test'), - // 'username' => env('DB_USERNAME', 'creasico'), - // 'password' => env('DB_PASSWORD', 'secret'), - // ]); - // } - }); - } - private function recreateDatabase(string $path) { if (\file_exists($path)) {