-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ddcd319
commit a464b48
Showing
4 changed files
with
183 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
|
||
namespace robertogallea\LaravelCodiceFiscale\CityCodeDecoders; | ||
|
||
|
||
class CompositeCitiesList implements CityDecoderInterface | ||
{ | ||
|
||
public static function getList() | ||
{ | ||
$result = []; | ||
|
||
foreach (config('codicefiscale.cities-decoder-list') as $citiesList) { | ||
$list = (new $citiesList())->getList(); | ||
$result = $list + $result; | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
|
||
namespace Tests\CityCodeDecoders; | ||
|
||
|
||
use Illuminate\Support\Facades\Config; | ||
use Orchestra\Testbench\TestCase; | ||
use robertogallea\LaravelCodiceFiscale\CityCodeDecoders\CityDecoderInterface; | ||
use robertogallea\LaravelCodiceFiscale\CityCodeDecoders\CompositeCitiesList; | ||
use robertogallea\LaravelCodiceFiscale\CodiceFiscaleServiceProvider; | ||
|
||
class CompositeCitiesListTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function if_empty_returns_empty_array() | ||
{ | ||
Config::set('codicefiscale.cities-lists', []); | ||
$citiesList = new CompositeCitiesList(); | ||
|
||
$list = $citiesList->getList(); | ||
|
||
$this->assertEquals([], $list); | ||
} | ||
|
||
/** @test */ | ||
public function it_merges_two_cities_list_results() | ||
{ | ||
$citiesList = $this->getMockedComposedList(['A001' => 'AAA'], ['B001' => 'BBB']); | ||
|
||
$list = $citiesList->getList(); | ||
|
||
$this->assertEquals([ | ||
'A001' => 'AAA', | ||
'B001' => 'BBB', | ||
], $list); | ||
} | ||
|
||
/** @test */ | ||
public function when_merging_last_decoder_has_precedence() | ||
{ | ||
$citiesList = $this->getMockedComposedList(['A001' => 'AAA'], ['A001' => 'BBB']); | ||
|
||
$list = $citiesList->getList(); | ||
|
||
$this->assertEquals([ | ||
'A001' => 'BBB', | ||
], $list); | ||
} | ||
|
||
/** | ||
* @return array|mixed | ||
*/ | ||
private function getMockedComposedList($firstArray, $secondArray) | ||
{ | ||
$list1 = \Mockery::namedMock( | ||
'FirstDecoder', | ||
CityDecoderInterface::class, | ||
function ($mock) use ($firstArray) { | ||
$mock->shouldReceive('getList')->once()->andReturn($firstArray); | ||
} | ||
); | ||
|
||
$list2 = \Mockery::namedMock( | ||
'SecondDecoder', | ||
CityDecoderInterface::class, | ||
function ($mock) use ($secondArray) { | ||
$mock->shouldReceive('getList')->once()->andReturn($secondArray); | ||
} | ||
); | ||
|
||
Config::set('codicefiscale.cities-decoder-list', $citiesListDecoders = [ | ||
get_class($list1), | ||
get_class($list2), | ||
]); | ||
|
||
$citiesList = new CompositeCitiesList(); | ||
|
||
return $citiesList; | ||
} | ||
|
||
public function getPackageProviders($application) | ||
{ | ||
return [ | ||
CodiceFiscaleServiceProvider::class, | ||
]; | ||
} | ||
} |