Skip to content

Commit

Permalink
fix(test): fix test issue with address model relations
Browse files Browse the repository at this point in the history
Signed-off-by: Fery Wardiyanto <[email protected]>
  • Loading branch information
feryardiant committed May 9, 2024
1 parent d0fe4b6 commit a95b38a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 46 deletions.
4 changes: 3 additions & 1 deletion src/Models/Concerns/WithAddresses.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Creasi\Nusa\Models\Concerns;

use Illuminate\Database\Eloquent\Relations\MorphMany;

/**
* @mixin \Creasi\Nusa\Contracts\HasAddresses
*/
Expand All @@ -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');
}
Expand Down
28 changes: 17 additions & 11 deletions tests/Models/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,40 +20,47 @@ 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(),
]);

$address->associateWith($village);

$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);
}
Expand Down
34 changes: 0 additions & 34 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit a95b38a

Please sign in to comment.