default services conflit ?
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
namespace CommerceGuys\Addressing\Tests\Country;
|
||||
|
||||
use CommerceGuys\Addressing\Country\Country;
|
||||
use CommerceGuys\Addressing\Country\CountryRepository;
|
||||
use org\bovigo\vfs\vfsStream;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \CommerceGuys\Addressing\Country\CountryRepository
|
||||
*/
|
||||
final class CountryRepositoryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Country definitions.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $definitions = [
|
||||
'en' => [
|
||||
'FR' => 'France',
|
||||
'US' => 'United States',
|
||||
],
|
||||
'es' => [
|
||||
'FR' => 'Francia',
|
||||
'US' => 'Estados Unidos',
|
||||
],
|
||||
'de' => [
|
||||
'FR' => 'Frankreich',
|
||||
'US' => 'Vereinigte Staaten',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* @covers ::__construct
|
||||
*/
|
||||
public function testConstructor()
|
||||
{
|
||||
// Mock the existence of JSON definitions on the filesystem.
|
||||
$root = vfsStream::setup('resources');
|
||||
foreach ($this->definitions as $locale => $data) {
|
||||
vfsStream::newFile('country/' . $locale . '.json')->at($root)->setContent(json_encode($data));
|
||||
}
|
||||
|
||||
// Instantiate the country repository and confirm that the definition path
|
||||
// was properly set.
|
||||
$countryRepository = new CountryRepository('de', 'en', 'vfs://resources/country/');
|
||||
$definitionPath = $this->getObjectAttribute($countryRepository, 'definitionPath');
|
||||
$this->assertEquals('vfs://resources/country/', $definitionPath);
|
||||
|
||||
return $countryRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::get
|
||||
* @covers ::loadDefinitions
|
||||
*
|
||||
* @uses \CommerceGuys\Addressing\Country\Country
|
||||
* @uses \CommerceGuys\Addressing\Locale
|
||||
* @depends testConstructor
|
||||
*/
|
||||
public function testGet($countryRepository)
|
||||
{
|
||||
// Explicit locale.
|
||||
$country = $countryRepository->get('FR', 'es');
|
||||
$this->assertInstanceOf(Country::class, $country);
|
||||
$this->assertEquals('FR', $country->getCountryCode());
|
||||
$this->assertEquals('Francia', $country->getName());
|
||||
$this->assertEquals('FRA', $country->getThreeLetterCode());
|
||||
$this->assertEquals('250', $country->getNumericCode());
|
||||
$this->assertEquals('EUR', $country->getCurrencyCode());
|
||||
$this->assertEquals('es', $country->getLocale());
|
||||
|
||||
// Default locale, lowercase country code.
|
||||
$country = $countryRepository->get('fr');
|
||||
$this->assertInstanceOf(Country::class, $country);
|
||||
$this->assertEquals('FR', $country->getCountryCode());
|
||||
$this->assertEquals('Frankreich', $country->getName());
|
||||
$this->assertEquals('de', $country->getLocale());
|
||||
|
||||
// Fallback locale.
|
||||
$country = $countryRepository->get('FR', 'INVALID-LOCALE');
|
||||
$this->assertInstanceOf(Country::class, $country);
|
||||
$this->assertEquals('FR', $country->getCountryCode());
|
||||
$this->assertEquals('France', $country->getName());
|
||||
$this->assertEquals('en', $country->getLocale());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::get
|
||||
* @covers ::loadDefinitions
|
||||
*
|
||||
* @uses \CommerceGuys\Addressing\Locale
|
||||
* @expectedException \CommerceGuys\Addressing\Exception\UnknownCountryException
|
||||
* @depends testConstructor
|
||||
*/
|
||||
public function testGetInvalidCountry($countryRepository)
|
||||
{
|
||||
$countryRepository->get('INVALID');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getAll
|
||||
* @covers ::loadDefinitions
|
||||
*
|
||||
* @uses \CommerceGuys\Addressing\Country\Country
|
||||
* @uses \CommerceGuys\Addressing\Locale
|
||||
* @depends testConstructor
|
||||
*/
|
||||
public function testGetAll($countryRepository)
|
||||
{
|
||||
// Explicit locale.
|
||||
$countries = $countryRepository->getAll('es');
|
||||
$this->assertArrayHasKey('FR', $countries);
|
||||
$this->assertArrayHasKey('US', $countries);
|
||||
$this->assertEquals('Francia', $countries['FR']->getName());
|
||||
$this->assertEquals('Estados Unidos', $countries['US']->getName());
|
||||
|
||||
// Default locale.
|
||||
$countries = $countryRepository->getAll();
|
||||
$this->assertArrayHasKey('FR', $countries);
|
||||
$this->assertArrayHasKey('US', $countries);
|
||||
$this->assertEquals('Frankreich', $countries['FR']->getName());
|
||||
$this->assertEquals('Vereinigte Staaten', $countries['US']->getName());
|
||||
|
||||
// Fallback locale.
|
||||
$countries = $countryRepository->getAll('INVALID-LOCALE');
|
||||
$this->assertArrayHasKey('FR', $countries);
|
||||
$this->assertArrayHasKey('US', $countries);
|
||||
$this->assertEquals('France', $countries['FR']->getName());
|
||||
$this->assertEquals('United States', $countries['US']->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getList
|
||||
* @covers ::loadDefinitions
|
||||
*
|
||||
* @uses \CommerceGuys\Addressing\Locale
|
||||
* @depends testConstructor
|
||||
*/
|
||||
public function testGetList($countryRepository)
|
||||
{
|
||||
// Explicit locale.
|
||||
$list = $countryRepository->getList('es');
|
||||
$this->assertEquals(['FR' => 'Francia', 'US' => 'Estados Unidos'], $list);
|
||||
|
||||
// Default locale.
|
||||
$list = $countryRepository->getList();
|
||||
$this->assertEquals(['FR' => 'Frankreich', 'US' => 'Vereinigte Staaten'], $list);
|
||||
|
||||
// Fallback locale.
|
||||
$list = $countryRepository->getList('INVALID-LOCALE');
|
||||
$this->assertEquals(['FR' => 'France', 'US' => 'United States'], $list);
|
||||
}
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace CommerceGuys\Addressing\Tests\Country;
|
||||
|
||||
use CommerceGuys\Addressing\Country\Country;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \CommerceGuys\Addressing\Country\Country
|
||||
*/
|
||||
final class CountryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers ::__construct
|
||||
*/
|
||||
public function testMissingProperty()
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('Missing required property "country_code".');
|
||||
$country = new Country([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::__construct
|
||||
* @covers ::__toString
|
||||
* @covers ::getCountryCode
|
||||
* @covers ::getName
|
||||
* @covers ::getThreeLetterCode
|
||||
* @covers ::getNumericCode
|
||||
* @covers ::getCurrencyCode
|
||||
* @covers ::getTimezones
|
||||
* @covers ::getLocale
|
||||
*/
|
||||
public function testValid()
|
||||
{
|
||||
$definition = [
|
||||
'country_code' => 'DE',
|
||||
'name' => 'Allemagne',
|
||||
'three_letter_code' => 'DEU',
|
||||
'numeric_code' => '276',
|
||||
'currency_code' => 'EUR',
|
||||
'locale' => 'fr',
|
||||
];
|
||||
$country = new Country($definition);
|
||||
|
||||
$this->assertEquals($definition['country_code'], $country->__toString());
|
||||
$this->assertEquals($definition['country_code'], $country->getCountryCode());
|
||||
$this->assertEquals($definition['name'], $country->getName());
|
||||
$this->assertEquals($definition['three_letter_code'], $country->getThreeLetterCode());
|
||||
$this->assertEquals($definition['numeric_code'], $country->getNumericCode());
|
||||
$this->assertEquals($definition['currency_code'], $country->getCurrencyCode());
|
||||
$this->assertEquals(['Europe/Berlin', 'Europe/Busingen'], $country->getTimezones());
|
||||
$this->assertEquals($definition['locale'], $country->getLocale());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user