From 277a33a89b9567779e6fd41e5450ea7a32551095 Mon Sep 17 00:00:00 2001 From: Sagar Gautam Date: Wed, 10 Feb 2021 14:49:30 +0545 Subject: [PATCH] get all municipalities of a province --- src/Entities/Municipality.php | 11 ++++++++++ tests/Unit/MunicipalityTest.php | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/Entities/Municipality.php b/src/Entities/Municipality.php index 53db89e..f49c878 100644 --- a/src/Entities/Municipality.php +++ b/src/Entities/Municipality.php @@ -91,6 +91,17 @@ public function getMunicipalityByCategory($categoryId) })); } + public function getMunicipalityByProvince($provinceId) + { + $district = new District(); + $districts = $district->getDistrictsByProvince($provinceId); + $municipalities = array_map(function ($item) { + return $this->getMunicipalitiesByDistrict($item->id); + }, $districts); + + return array_merge(...$municipalities); + } + /** * Find municipality by id * diff --git a/tests/Unit/MunicipalityTest.php b/tests/Unit/MunicipalityTest.php index d8debd1..9fb5e2f 100644 --- a/tests/Unit/MunicipalityTest.php +++ b/tests/Unit/MunicipalityTest.php @@ -254,4 +254,43 @@ public function testSortBy() } } } + + /** + * Individual Province Municipality Count Test + */ + public function test_GetMunicipalityByProvince_For_Individual_Correct_Count() + { + $expectedSet = array( + '1' => 137, + '2' => 136, + '3' => 119, + '4' => 85, + '5' => 109, + '6' => 79, + '7' => 88 + ); + + $actualSet = array(); + + for ($index = 1; $index<=7; $index++){ + $actualSet[(string)$index] = count($this->municipality->getMunicipalityByProvince($index)); + } + + $this->assertSame($expectedSet, $actualSet); + } + + /** + * Total Municipality Count Test + */ + public function test_GetMunicipalityByProvince_For_Total_Count() + { + $expectedTotal = 753; + + $actualTotal = 0; + for ($index = 1; $index<=7; $index++){ + $actualTotal += count($this->municipality->getMunicipalityByProvince($index)); + } + + $this->assertSame($actualTotal, $expectedTotal); + } }