Skip to content

Commit

Permalink
refactor(tests): rearrange the way of test sequence runs
Browse files Browse the repository at this point in the history
Signed-off-by: Fery Wardiyanto <[email protected]>
  • Loading branch information
feryardiant committed Oct 2, 2023
1 parent a6de208 commit c5762c5
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 217 deletions.
49 changes: 17 additions & 32 deletions tests/Features/DistrictTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Creasi\Tests\Features;

use Creasi\Tests\Models\DistrictTest as ModelTest;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\Attributes\DependsOnClass;
Expand Down Expand Up @@ -40,86 +41,70 @@ public static function invalidCodes(): array
}

#[Test]
#[DependsOnClass(ModelTest::class)]
#[DependsOnClass(RegencyTest::class)]
#[DataProvider('availableQueries')]
public function it_shows_available_districts(?string ...$with)
public function it_shows_available_districts(?string ...$with): void
{
$response = $this->getJson($this->path(query: [
'with' => $with,
]));

$response->assertOk()->assertJsonStructure([
'data' => [self::FIELDS],
'links' => ['first', 'last', 'prev', 'next'],
'meta' => ['current_page', 'from', 'last_page', 'links', 'path', 'per_page', 'to', 'total'],
]);
$this->assertCollectionResponse($response, self::FIELDS, $with);
}

#[Test]
#[Depends('it_shows_available_districts')]
public function it_shows_districts_by_selected_codes()
#[DataProvider('invalidCodes')]
public function it_shows_errors_for_invalid_codes(mixed $codes): void
{
$response = $this->getJson($this->path(query: [
'codes' => [337503, 337504],
'codes' => $codes,
]));

$response->assertOk()->assertJsonStructure([
'data' => [self::FIELDS],
'meta' => [],
])->assertJsonCount(2, 'data');
$response->assertUnprocessable();
}

#[Test]
#[Depends('it_shows_available_districts')]
#[DataProvider('invalidCodes')]
public function it_shows_errors_for_invalid_codes(mixed $codes)
public function it_shows_districts_by_selected_codes(): void
{
$response = $this->getJson($this->path(query: [
'codes' => $codes,
'codes' => [337503, 337504],
]));

$response->assertUnprocessable();
$this->assertCollectionResponse($response, self::FIELDS)->assertJsonCount(2, 'data');
}

#[Test]
#[Depends('it_shows_available_districts')]
public function it_shows_districts_by_search_query()
public function it_shows_districts_by_search_query(): void
{
$response = $this->getJson($this->path(query: [
'search' => 'Pekalongan',
]));

$response->assertOk()->assertJsonStructure([
'data' => [self::FIELDS],
'meta' => [],
])->assertJsonCount(5, 'data');
$this->assertCollectionResponse($response, self::FIELDS)->assertJsonCount(5, 'data');
}

#[Test]
#[Depends('it_shows_available_districts')]
#[DataProvider('availableQueries')]
public function it_shows_single_district(?string ...$with)
public function it_shows_single_district(?string ...$with): void
{
$response = $this->getJson($this->path('337503', [
'with' => $with,
]));

$response->assertOk()->assertJsonStructure([
'data' => self::FIELDS,
'meta' => [],
]);
$this->assertSingleResponse($response, self::FIELDS, $with);
}

#[Test]
#[Depends('it_shows_available_districts')]
public function it_shows_available_villages_in_a_district()
public function it_shows_available_villages_in_a_district(): void
{
$response = $this->getJson($this->path('337503/villages'));

$response->assertOk()->assertJsonStructure([
'data' => [VillageTest::FIELDS],
'links' => ['first', 'last', 'prev', 'next'],
'meta' => ['current_page', 'from', 'last_page', 'links', 'path', 'per_page', 'to', 'total'],
]);
$this->assertCollectionResponse($response, VillageTest::FIELDS);
}
}
66 changes: 22 additions & 44 deletions tests/Features/ProvinceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Creasi\Tests\Features;

use Creasi\Tests\Models\ProvinceTest as ModelTest;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\Attributes\DependsOnClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;

Expand Down Expand Up @@ -42,111 +44,87 @@ public static function invalidCodes(): array
}

#[Test]
#[DependsOnClass(ModelTest::class)]
#[DataProvider('availableQueries')]
public function it_shows_all_available_provinces(?string ...$with)
public function it_shows_all_available_provinces(?string ...$with): void
{
$response = $this->getJson($this->path(query: [
'with' => $with,
]));

$response->assertOk()->assertJsonStructure([
'data' => [self::FIELDS],
'links' => ['first', 'last', 'prev', 'next'],
'meta' => ['current_page', 'from', 'last_page', 'links', 'path', 'per_page', 'to', 'total'],
]);
$this->assertCollectionResponse($response, self::FIELDS, $with);
}

#[Test]
#[Depends('it_shows_all_available_provinces')]
public function it_shows_provinces_by_selected_codes()
#[DataProvider('invalidCodes')]
public function it_shows_errors_for_invalid_codes(mixed $codes): void
{
$response = $this->getJson($this->path(query: [
'codes' => [33, 32],
'codes' => $codes,
]));

$response->assertOk()->assertJsonStructure([
'data' => [self::FIELDS],
'meta' => [],
])->assertJsonCount(2, 'data');
$response->assertUnprocessable();
}

#[Test]
#[Depends('it_shows_all_available_provinces')]
#[DataProvider('invalidCodes')]
public function it_shows_errors_for_invalid_codes(mixed $codes)
public function it_shows_provinces_by_selected_codes(): void
{
$response = $this->getJson($this->path(query: [
'codes' => $codes,
'codes' => [33, 32],
]));

$response->assertUnprocessable();
$this->assertCollectionResponse($response, self::FIELDS)->assertJsonCount(2, 'data');
}

#[Test]
#[Depends('it_shows_all_available_provinces')]
public function it_shows_provinces_by_search_query()
public function it_shows_provinces_by_search_query(): void
{
$response = $this->getJson($this->path(query: [
'search' => 'Jawa Tengah',
]));

$response->assertOk()->assertJsonStructure([
'data' => [self::FIELDS],
'meta' => [],
])->assertJsonCount(1, 'data');
$this->assertCollectionResponse($response, self::FIELDS)->assertJsonCount(1, 'data');
}

#[Test]
#[Depends('it_shows_all_available_provinces')]
#[DataProvider('availableQueries')]
public function it_shows_single_province(?string ...$with)
public function it_shows_single_province(?string ...$with): void
{
$response = $this->getJson($this->path('33', [
'with' => $with,
]));

$response->assertOk()->assertJsonStructure([
'data' => self::FIELDS,
'meta' => [],
]);
$this->assertSingleResponse($response, self::FIELDS, $with);
}

#[Test]
#[Depends('it_shows_all_available_provinces')]
public function it_shows_available_regencies_in_a_province()
public function it_shows_available_regencies_in_a_province(): void
{
$response = $this->getJson($this->path('33/regencies'));

$response->assertOk()->assertJsonStructure([
'data' => [RegencyTest::FIELDS],
'links' => ['first', 'last', 'prev', 'next'],
'meta' => ['current_page', 'from', 'last_page', 'links', 'path', 'per_page', 'to', 'total'],
]);
$this->assertCollectionResponse($response, RegencyTest::FIELDS);
}

#[Test]
#[Depends('it_shows_all_available_provinces')]
public function it_shows_available_districts_in_a_province()
public function it_shows_available_districts_in_a_province(): void
{
$response = $this->getJson($this->path('33/districts'));

$response->assertOk()->assertJsonStructure([
'data' => [DistrictTest::FIELDS],
'links' => ['first', 'last', 'prev', 'next'],
'meta' => ['current_page', 'from', 'last_page', 'links', 'path', 'per_page', 'to', 'total'],
]);
$this->assertCollectionResponse($response, DistrictTest::FIELDS);
}

#[Test]
#[Depends('it_shows_all_available_provinces')]
public function it_shows_available_villages_in_a_province()
public function it_shows_available_villages_in_a_province(): void
{
$response = $this->getJson($this->path('33/villages'));

$response->assertOk()->assertJsonStructure([
'data' => [VillageTest::FIELDS],
'links' => ['first', 'last', 'prev', 'next'],
'meta' => ['current_page', 'from', 'last_page', 'links', 'path', 'per_page', 'to', 'total'],
]);
$this->assertCollectionResponse($response, VillageTest::FIELDS);
}
}
54 changes: 19 additions & 35 deletions tests/Features/RegencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Creasi\Tests\Features;

use Creasi\Tests\Models\RegencyTest as ModelTest;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\Attributes\DependsOnClass;
Expand Down Expand Up @@ -44,96 +45,79 @@ public static function invalidCodes(): array
}

#[Test]
#[DependsOnClass(ModelTest::class)]
#[DependsOnClass(ProvinceTest::class)]
#[DataProvider('availableQueries')]
public function it_shows_all_available_regencies(?string ...$with)
public function it_shows_all_available_regencies(?string ...$with): void
{
$response = $this->getJson($this->path(query: [
'with' => $with,
]));

$response->assertOk()->assertJsonStructure([
'data' => [self::FIELDS],
'links' => ['first', 'last', 'prev', 'next'],
'meta' => ['current_page', 'from', 'last_page', 'links', 'path', 'per_page', 'to', 'total'],
]);
$this->assertCollectionResponse($response, self::FIELDS, $with);
}

#[Test]
#[Depends('it_shows_all_available_regencies')]
public function it_shows_regencies_by_selected_codes()
#[DataProvider('invalidCodes')]
public function it_shows_errors_for_invalid_codes(mixed $codes): void
{
$response = $this->getJson($this->path(query: [
'codes' => [3375, 3325],
'codes' => $codes,
]));

$response->assertOk()->assertJsonCount(2, 'data');
$response->assertUnprocessable();
}

#[Test]
#[Depends('it_shows_all_available_regencies')]
#[DataProvider('invalidCodes')]
public function it_shows_errors_for_invalid_codes(mixed $codes)
public function it_shows_regencies_by_selected_codes(): void
{
$response = $this->getJson($this->path(query: [
'codes' => $codes,
'codes' => [3375, 3325],
]));

$response->assertUnprocessable();
$this->assertCollectionResponse($response, self::FIELDS)->assertJsonCount(2, 'data');
}

#[Test]
#[Depends('it_shows_all_available_regencies')]
public function it_shows_regencies_by_search_query()
public function it_shows_regencies_by_search_query(): void
{
$response = $this->getJson($this->path(query: [
'search' => 'Pekalongan',
]));

$response->assertOk()->assertJsonStructure([
'data' => [self::FIELDS],
'meta' => [],
])->assertJsonCount(2, 'data');
$this->assertCollectionResponse($response, self::FIELDS)->assertJsonCount(2, 'data');
}

#[Test]
#[Depends('it_shows_all_available_regencies')]
#[DataProvider('availableQueries')]
public function it_shows_single_regency(?string ...$with)
public function it_shows_single_regency(?string ...$with): void
{
$response = $this->getJson($this->path('3375', [
'with' => $with,
]));

$response->assertOk()->assertJsonStructure([
'data' => self::FIELDS,
'meta' => [],
]);
$this->assertSingleResponse($response, self::FIELDS, $with);
}

#[Test]
#[Depends('it_shows_all_available_regencies')]
public function it_shows_available_districts_in_a_regency()
public function it_shows_available_districts_in_a_regency(): void
{
$response = $this->getJson($this->path('3375/districts'));

$response->assertOk()->assertJsonStructure([
'data' => [DistrictTest::FIELDS],
'links' => ['first', 'last', 'prev', 'next'],
'meta' => ['current_page', 'from', 'last_page', 'links', 'path', 'per_page', 'to', 'total'],
]);
$this->assertCollectionResponse($response, DistrictTest::FIELDS);
}

#[Test]
#[Depends('it_shows_all_available_regencies')]
public function it_shows_available_villages_in_a_regency()
public function it_shows_available_villages_in_a_regency(): void
{
$response = $this->getJson($this->path('3375/villages'));

$response->assertOk()->assertJsonStructure([
'data' => [VillageTest::FIELDS],
'links' => ['first', 'last', 'prev', 'next'],
'meta' => ['current_page', 'from', 'last_page', 'links', 'path', 'per_page', 'to', 'total'],
]);
$this->assertCollectionResponse($response, VillageTest::FIELDS);
}
}
Loading

0 comments on commit c5762c5

Please sign in to comment.