Skip to content

Commit

Permalink
chore(tests): update tests regarding recent changes
Browse files Browse the repository at this point in the history
Signed-off-by: Fery Wardiyanto <ferywardiyanto@gmail.com>
  • Loading branch information
feryardiant committed Jun 26, 2023
1 parent f0e4a4a commit e242faa
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 79 deletions.
58 changes: 35 additions & 23 deletions tests/Models/DistrictTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

namespace Creasi\Tests\Models;

use Creasi\Nusa\Contracts\District as DistrictContract;
use Creasi\Nusa\Models\District;
use Creasi\Nusa\Models\Province;
use Creasi\Nusa\Models\Regency;
use Creasi\Nusa\Models\Village;
use Creasi\Nusa\Contracts\Province;
use Creasi\Nusa\Contracts\Regency;
use Creasi\Nusa\Contracts\Village;
use Creasi\Tests\TestCase;
use Illuminate\Support\Collection;
use PHPUnit\Framework\Attributes\Depends;
Expand All @@ -19,45 +20,56 @@
#[Group('districts')]
class DistrictTest extends TestCase
{
/**
* @param Collection<int, District> $districts
* @return Collection<int, District>
*/
#[Test]
#[DependsExternal(RegencyTest::class, 'it_should_be_true')]
public function it_should_be_true()
#[DependsExternal(ProvinceTest::class, 'it_should_has_many_districts')]
public function it_should_has_many_districts(Collection $districts)
{
$this->assertTrue(\class_exists(District::class));

return District::with([
'province',
'regency',
'villages' => function ($query) {
$query->take(10);
},
])->take(10)->get();
$districts->each(function (District $district) {
$this->assertIsInt($district->code, 'Code should be int');

$this->assertInstanceOf(DistrictContract::class, $district);
});

return $districts;
}

/**
* @param Collection<int, District> $districts
*/
#[Test]
#[Depends('it_should_be_true')]
#[Depends('it_should_has_many_districts')]
public function it_should_belongs_to_province(Collection $districts)
{
$districts->each(function (District $dis) {
$this->assertInstanceOf(Province::class, $dis->province);
$districts->each(function (District $district) {
$this->assertInstanceOf(Province::class, $district->province);
});
}

/**
* @param Collection<int, District> $districts
*/
#[Test]
#[Depends('it_should_be_true')]
#[Depends('it_should_has_many_districts')]
public function it_should_belongs_to_regency(Collection $regencies)
{
$regencies->each(function (District $dis) {
$this->assertInstanceOf(Regency::class, $dis->regency);
$regencies->each(function (District $district) {
$this->assertInstanceOf(Regency::class, $district->regency);
});
}

/**
* @param Collection<int, District> $districts
*/
#[Test]
#[Depends('it_should_be_true')]
#[Depends('it_should_has_many_districts')]
public function it_should_has_many_villages(Collection $districts)
{
$districts->each(function (District $dis) {
$this->assertTrue($dis->villages->every(fn ($vil) => $vil instanceof Village));
$districts->each(function (District $district) {
$this->assertTrue($district->villages->every(fn ($vil) => $vil instanceof Village));
});
}
}
72 changes: 56 additions & 16 deletions tests/Models/ProvinceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

namespace Creasi\Tests\Models;

use Creasi\Nusa\Models\District;
use Creasi\Nusa\Contracts\District;
use Creasi\Nusa\Contracts\Province as ProvinceContract;
use Creasi\Nusa\Models\Province;
use Creasi\Nusa\Models\Regency;
use Creasi\Nusa\Models\Village;
use Creasi\Nusa\Contracts\Regency;
use Creasi\Nusa\Contracts\Village;
use Creasi\Tests\NusaTest;
use Creasi\Tests\TestCase;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Collection;
use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\Attributes\DependsExternal;
use PHPUnit\Framework\Attributes\Group;
Expand All @@ -21,12 +22,13 @@
class ProvinceTest extends TestCase
{
#[Test]
#[DependsExternal(NusaTest::class, 'it_should_be_true')]
public function it_should_be_true()
{
$this->assertTrue(\class_exists(Province::class));
if (! env('GIT_BRANCH')) {
$this->artisan('nusa:sync');
}

return Province::with([
$provinces = Province::with([
'regencies' => function ($query) {
$query->take(10);
},
Expand All @@ -37,32 +39,70 @@ public function it_should_be_true()
$query->take(10);
},
])->get();

$provinces->each(function (Province $province) {
$this->assertIsInt($province->code, 'Code should be int');
$this->assertIsFloat($province->latitude, 'Latitude should be float');
$this->assertIsFloat($province->longitude, 'Longitude should be float');
$this->assertIsArray($province->coordinates, 'Coordinates should be array');

$this->assertInstanceOf(ProvinceContract::class, $province);
});

return $provinces;
}

/**
* @param Collection<int, Province> $provinces
*/
#[Test]
#[Depends('it_should_be_true')]
public function it_should_has_many_regencies(Collection $provinces)
{
$provinces->each(function (Province $prov) {
$this->assertTrue($prov->regencies->every(fn ($reg) => $reg instanceof Regency));
});
$regencies = collect();

foreach ($provinces as $province) {
$this->assertTrue($province->regencies->every(fn ($reg) => $reg instanceof Regency));

$regencies->push(...$province->regencies);
}

return $regencies;
}

/**
* @param Collection<int, Province> $provinces
*/
#[Test]
#[Depends('it_should_be_true')]
public function it_should_has_many_districts(Collection $provinces)
{
$provinces->each(function (Province $prov) {
$this->assertTrue($prov->districts->every(fn ($dis) => $dis instanceof District));
});
$districts = \collect();

foreach ($provinces as $province) {
$this->assertTrue($province->districts->every(fn ($dis) => $dis instanceof District));

$districts->push(...$province->districts);
}

return $districts;
}

/**
* @param Collection<int, Province> $provinces
*/
#[Test]
#[Depends('it_should_be_true')]
public function it_should_has_many_villages(Collection $provinces)
{
$provinces->each(function (Province $prov) {
$this->assertTrue($prov->villages->every(fn ($vil) => $vil instanceof Village));
});
$villages = \collect();

foreach ($provinces as $province) {
$this->assertTrue($province->villages->every(fn ($vil) => $vil instanceof Village));

$villages->push(...$province->villages);
}

return $villages;
}
}
61 changes: 37 additions & 24 deletions tests/Models/RegencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

namespace Creasi\Tests\Models;

use Creasi\Nusa\Models\District;
use Creasi\Nusa\Models\Province;
use Creasi\Nusa\Contracts\District;
use Creasi\Nusa\Contracts\Province;
use Creasi\Nusa\Contracts\Regency as RegencyContract;
use Creasi\Nusa\Models\Regency;
use Creasi\Nusa\Models\Village;
use Creasi\Nusa\Contracts\Village;
use Creasi\Tests\TestCase;
use Illuminate\Support\Collection;
use PHPUnit\Framework\Attributes\Depends;
Expand All @@ -19,47 +20,59 @@
#[Group('regencies')]
class RegencyTest extends TestCase
{
/**
* @param Collection<int, Regency> $regencies
* @return Collection<int, Regency>
*/
#[Test]
#[DependsExternal(ProvinceTest::class, 'it_should_be_true')]
public function it_should_be_true()
#[DependsExternal(ProvinceTest::class, 'it_should_has_many_regencies')]
public function it_should_has_many_regencies(Collection $regencies)
{
$this->assertTrue(\class_exists(Regency::class));
$regencies->each(function (Regency $regency) {
$this->assertIsInt($regency->code, 'Code should be int');
$this->assertIsFloat($regency->latitude, 'Latitude should be float');
$this->assertIsFloat($regency->longitude, 'Longitude should be float');
$this->assertIsArray($regency->coordinates, 'Coordinates should be array');

return Regency::with([
'province',
'districts' => function ($query) {
$query->take(10);
},
'villages' => function ($query) {
$query->take(10);
},
])->get();
$this->assertInstanceOf(RegencyContract::class, $regency);
});

return $regencies;
}

/**
* @param Collection<int, Regency> $regencies
*/
#[Test]
#[Depends('it_should_be_true')]
#[Depends('it_should_has_many_regencies')]
public function it_should_belongs_to_province(Collection $regencies)
{
$regencies->each(function (Regency $reg) {
$this->assertInstanceOf(Province::class, $reg->province);
$regencies->each(function (Regency $regency) {
$this->assertInstanceOf(Province::class, $regency->province);
});
}

/**
* @param Collection<int, Regency> $regencies
*/
#[Test]
#[Depends('it_should_be_true')]
#[Depends('it_should_has_many_regencies')]
public function it_should_has_many_districts(Collection $regencies)
{
$regencies->each(function (Regency $reg) {
$this->assertTrue($reg->districts->every(fn ($dis) => $dis instanceof District));
$regencies->each(function (Regency $regency) {
$this->assertTrue($regency->districts->every(fn ($dis) => $dis instanceof District));
});
}

/**
* @param Collection<int, Regency> $regencies
*/
#[Test]
#[Depends('it_should_be_true')]
#[Depends('it_should_has_many_regencies')]
public function it_should_has_many_villages(Collection $regencies)
{
$regencies->each(function (Regency $reg) {
$this->assertTrue($reg->villages->every(fn ($vil) => $vil instanceof Village));
$regencies->each(function (Regency $regency) {
$this->assertTrue($regency->villages->every(fn ($vil) => $vil instanceof Village));
});
}
}
51 changes: 35 additions & 16 deletions tests/Models/VillageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace Creasi\Tests\Models;

use Creasi\Nusa\Models\District;
use Creasi\Nusa\Models\Province;
use Creasi\Nusa\Models\Regency;
use Creasi\Nusa\Contracts\District;
use Creasi\Nusa\Contracts\Province;
use Creasi\Nusa\Contracts\Regency;
use Creasi\Nusa\Contracts\Village as VillageContract;
use Creasi\Nusa\Models\Village;
use Creasi\Tests\TestCase;
use Illuminate\Support\Collection;
Expand All @@ -19,39 +20,57 @@
#[Group('villages')]
class VillageTest extends TestCase
{
/**
* @param Collection<int, Village> $villages
* @return Collection<int, Village>
*/
#[Test]
#[DependsExternal(DistrictTest::class, 'it_should_be_true')]
public function it_should_be_true()
#[DependsExternal(ProvinceTest::class, 'it_should_has_many_villages')]
public function it_should_has_many_villages(Collection $villages)
{
$this->assertTrue(\class_exists(Village::class));
$villages->each(function (Village $village) {
$this->assertIsInt($village->code, 'Code should be int');
$this->assertIsInt($village->postal_code, 'Postal Code should be int');

return Village::with('province', 'regency', 'district')->take(10)->get();
$this->assertInstanceOf(VillageContract::class, $village);
});

return $villages;
}

/**
* @param Collection<int, Village> $villages
*/
#[Test]
#[Depends('it_should_be_true')]
#[Depends('it_should_has_many_villages')]
public function it_should_belongs_to_province(Collection $villages)
{
$villages->each(function (Village $vil) {
$this->assertInstanceOf(Province::class, $vil->province);
$villages->each(function (Village $village) {
$this->assertInstanceOf(Province::class, $village->province);
});
}

/**
* @param Collection<int, Village> $villages
*/
#[Test]
#[Depends('it_should_be_true')]
#[Depends('it_should_has_many_villages')]
public function it_should_belongs_to_regency(Collection $villages)
{
$villages->each(function (Village $vil) {
$this->assertInstanceOf(Regency::class, $vil->regency);
$villages->each(function (Village $village) {
$this->assertInstanceOf(Regency::class, $village->regency);
});
}

/**
* @param Collection<int, Village> $villages
*/
#[Test]
#[Depends('it_should_be_true')]
#[Depends('it_should_has_many_villages')]
public function it_should_belongs_to_district(Collection $villages)
{
$villages->each(function (Village $vil) {
$this->assertInstanceOf(District::class, $vil->district);
$villages->each(function (Village $village) {
$this->assertInstanceOf(District::class, $village->district);
});
}
}

0 comments on commit e242faa

Please sign in to comment.