default services conflit ?
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace CommerceGuys\Addressing\Tests\AddressFormat;
|
||||
|
||||
use CommerceGuys\Addressing\AddressFormat\AddressField;
|
||||
use CommerceGuys\Addressing\AddressFormat\AddressFormat;
|
||||
use CommerceGuys\Addressing\AddressFormat\AddressFormatHelper;
|
||||
use CommerceGuys\Addressing\AddressFormat\FieldOverride;
|
||||
use CommerceGuys\Addressing\AddressFormat\FieldOverrides;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \CommerceGuys\Addressing\AddressFormat\AddressFormatHelper
|
||||
*/
|
||||
final class AddressFormatHelperTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers ::getGroupedFields
|
||||
*/
|
||||
public function testGetGroupedFields()
|
||||
{
|
||||
$format = "%givenName %familyName\n%organization\n%addressLine1\n%addressLine2\n%locality, %postalCode";
|
||||
$expectedGroupedFields = [
|
||||
[AddressField::GIVEN_NAME, AddressField::FAMILY_NAME],
|
||||
[AddressField::ORGANIZATION],
|
||||
[AddressField::ADDRESS_LINE1],
|
||||
[AddressField::ADDRESS_LINE2],
|
||||
[AddressField::LOCALITY, AddressField::POSTAL_CODE],
|
||||
];
|
||||
$this->assertEquals($expectedGroupedFields, AddressFormatHelper::getGroupedFields($format));
|
||||
|
||||
$fieldOverrides = new FieldOverrides([
|
||||
AddressField::ORGANIZATION => FieldOverride::HIDDEN,
|
||||
AddressField::LOCALITY => FieldOverride::HIDDEN,
|
||||
]);
|
||||
$expectedGroupedFields = [
|
||||
[AddressField::GIVEN_NAME, AddressField::FAMILY_NAME],
|
||||
[AddressField::ADDRESS_LINE1],
|
||||
[AddressField::ADDRESS_LINE2],
|
||||
[AddressField::POSTAL_CODE],
|
||||
];
|
||||
$this->assertEquals($expectedGroupedFields, AddressFormatHelper::getGroupedFields($format, $fieldOverrides));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getRequiredFields
|
||||
*/
|
||||
public function testGetRequiredFields()
|
||||
{
|
||||
$addressFormat = new AddressFormat([
|
||||
'country_code' => 'US',
|
||||
'format' => "%givenName %familyName\n%organization\n%addressLine1\n%addressLine2\n%locality, %administrativeArea %postalCode",
|
||||
'required_fields' => [
|
||||
AddressField::ADMINISTRATIVE_AREA,
|
||||
AddressField::LOCALITY,
|
||||
AddressField::POSTAL_CODE,
|
||||
],
|
||||
]);
|
||||
$fieldOverrides = new FieldOverrides([]);
|
||||
$expectedRequiredFields = [
|
||||
AddressField::ADMINISTRATIVE_AREA,
|
||||
AddressField::LOCALITY,
|
||||
AddressField::POSTAL_CODE,
|
||||
];
|
||||
$this->assertEquals($expectedRequiredFields, AddressFormatHelper::getRequiredFields($addressFormat, $fieldOverrides));
|
||||
|
||||
$fieldOverrides = new FieldOverrides([
|
||||
AddressField::ADMINISTRATIVE_AREA => FieldOverride::HIDDEN,
|
||||
AddressField::POSTAL_CODE => FieldOverride::OPTIONAL,
|
||||
AddressField::ADDRESS_LINE1 => FieldOverride::REQUIRED,
|
||||
]);
|
||||
$expectedRequiredFields = [
|
||||
AddressField::LOCALITY,
|
||||
AddressField::ADDRESS_LINE1,
|
||||
];
|
||||
$this->assertEquals($expectedRequiredFields, AddressFormatHelper::getRequiredFields($addressFormat, $fieldOverrides));
|
||||
}
|
||||
}
|
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace CommerceGuys\Addressing\Tests\AddressFormat;
|
||||
|
||||
use CommerceGuys\Addressing\AddressFormat\AddressFormat;
|
||||
use CommerceGuys\Addressing\AddressFormat\AddressFormatRepository;
|
||||
use CommerceGuys\Addressing\AddressFormat\AdministrativeAreaType;
|
||||
use CommerceGuys\Addressing\AddressFormat\LocalityType;
|
||||
use CommerceGuys\Addressing\AddressFormat\PostalCodeType;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \CommerceGuys\Addressing\AddressFormat\AddressFormatRepository
|
||||
*/
|
||||
final class AddressFormatRepositoryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers ::get
|
||||
* @covers ::processDefinition
|
||||
* @covers ::getGenericDefinition
|
||||
* @covers ::getDefinitions
|
||||
*/
|
||||
public function testGet()
|
||||
{
|
||||
$addressFormatRepository = new AddressFormatRepository();
|
||||
$addressFormat = $addressFormatRepository->get('ES');
|
||||
// Confirm that the right class has been returned, a known value has
|
||||
// been successfully populated, and defaults have been merged.
|
||||
$this->assertInstanceOf(AddressFormat::class, $addressFormat);
|
||||
$this->assertEquals('ES', $addressFormat->getCountryCode());
|
||||
$this->assertEquals(AdministrativeAreaType::PROVINCE, $addressFormat->getAdministrativeAreaType());
|
||||
$this->assertEquals(LocalityType::CITY, $addressFormat->getLocalityType());
|
||||
$this->assertEquals(PostalCodeType::POSTAL, $addressFormat->getPostalCodeType());
|
||||
$this->assertEquals('\\d{5}', $addressFormat->getPostalCodePattern());
|
||||
|
||||
// Confirm that passing a lowercase country code works.
|
||||
$anotherAddressFormat = $addressFormatRepository->get('es');
|
||||
$this->assertSame($anotherAddressFormat, $addressFormat);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::get
|
||||
* @covers ::processDefinition
|
||||
* @covers ::getGenericDefinition
|
||||
* @covers ::getDefinitions
|
||||
*/
|
||||
public function testGetNonExistingAddressFormat()
|
||||
{
|
||||
$addressFormatRepository = new AddressFormatRepository();
|
||||
$addressFormat = $addressFormatRepository->get('ZZ');
|
||||
$this->assertEquals('ZZ', $addressFormat->getCountryCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getAll
|
||||
* @covers ::processDefinition
|
||||
* @covers ::getGenericDefinition
|
||||
* @covers ::getDefinitions
|
||||
*/
|
||||
public function testGetAll()
|
||||
{
|
||||
$addressFormatRepository = new AddressFormatRepository();
|
||||
$addressFormats = $addressFormatRepository->getAll();
|
||||
$this->assertArrayHasKey('ES', $addressFormats);
|
||||
$this->assertArrayHasKey('RS', $addressFormats);
|
||||
$this->assertEquals('ES', $addressFormats['ES']->getCountryCode());
|
||||
$this->assertEquals(LocalityType::CITY, $addressFormats['ES']->getLocalityType());
|
||||
$this->assertEquals('RS', $addressFormats['RS']->getCountryCode());
|
||||
$this->assertEquals(LocalityType::CITY, $addressFormats['RS']->getLocalityType());
|
||||
}
|
||||
}
|
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace CommerceGuys\Addressing\Tests\AddressFormat;
|
||||
|
||||
use CommerceGuys\Addressing\AddressFormat\AddressField;
|
||||
use CommerceGuys\Addressing\AddressFormat\AddressFormat;
|
||||
use CommerceGuys\Addressing\AddressFormat\AdministrativeAreaType;
|
||||
use CommerceGuys\Addressing\AddressFormat\DependentLocalityType;
|
||||
use CommerceGuys\Addressing\AddressFormat\LocalityType;
|
||||
use CommerceGuys\Addressing\AddressFormat\PostalCodeType;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \CommerceGuys\Addressing\AddressFormat\AddressFormat
|
||||
*/
|
||||
final class AddressFormatTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers ::__construct
|
||||
*
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testMissingProperty()
|
||||
{
|
||||
$definition = [
|
||||
'country_code' => 'US',
|
||||
];
|
||||
$addressFormat = new AddressFormat($definition);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::__construct
|
||||
*
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testInvalidSubdivision()
|
||||
{
|
||||
$definition = [
|
||||
'country_code' => 'US',
|
||||
'format' => "%givenName %familyName\n%organization\n%addressLine1\n%addressLine2\n%dependentLocality",
|
||||
'required_fields' => [AddressField::ADDRESS_LINE1],
|
||||
'dependent_locality_type' => 'WRONG',
|
||||
];
|
||||
$addressFormat = new AddressFormat($definition);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::__construct
|
||||
* @covers ::getCountryCode
|
||||
* @covers ::getLocale
|
||||
* @covers ::getFormat
|
||||
* @covers ::getLocalFormat
|
||||
* @covers ::getUsedFields
|
||||
* @covers ::getUsedSubdivisionFields
|
||||
* @covers ::getRequiredFields
|
||||
* @covers ::getUppercaseFields
|
||||
* @covers ::getAdministrativeAreaType
|
||||
* @covers ::getLocalityType
|
||||
* @covers ::getDependentLocalityType
|
||||
* @covers ::getPostalCodeType
|
||||
* @covers ::getPostalCodePattern
|
||||
* @covers ::getPostalCodePrefix
|
||||
* @covers ::getSubdivisionDepth
|
||||
*/
|
||||
public function testValid()
|
||||
{
|
||||
$definition = [
|
||||
'country_code' => 'US',
|
||||
'locale' => 'en',
|
||||
'format' => "%givenName %familyName\n%organization\n%addressLine1\n%addressLine2\n%locality, %administrativeArea %postalCode",
|
||||
// The local format is made up, US doesn't have one usually.
|
||||
'local_format' => '%postalCode\n%addressLine1\n%organization\n%givenName %familyName',
|
||||
'required_fields' => [
|
||||
AddressField::ADMINISTRATIVE_AREA,
|
||||
AddressField::LOCALITY,
|
||||
AddressField::POSTAL_CODE,
|
||||
AddressField::ADDRESS_LINE1,
|
||||
],
|
||||
'uppercase_fields' => [
|
||||
AddressField::ADMINISTRATIVE_AREA,
|
||||
AddressField::LOCALITY,
|
||||
],
|
||||
'administrative_area_type' => AdministrativeAreaType::STATE,
|
||||
'locality_type' => LocalityType::CITY,
|
||||
'dependent_locality_type' => DependentLocalityType::DISTRICT,
|
||||
'postal_code_type' => PostalCodeType::ZIP,
|
||||
'postal_code_pattern' => '(\d{5})(?:[ \-](\d{4}))?',
|
||||
// US doesn't use postal code prefixes, fake one for test purposes.
|
||||
'postal_code_prefix' => 'US',
|
||||
'subdivision_depth' => 1,
|
||||
];
|
||||
$addressFormat = new AddressFormat($definition);
|
||||
|
||||
$this->assertEquals($definition['country_code'], $addressFormat->getCountryCode());
|
||||
$this->assertEquals($definition['locale'], $addressFormat->getLocale());
|
||||
$this->assertEquals($definition['format'], $addressFormat->getFormat());
|
||||
$this->assertEquals($definition['local_format'], $addressFormat->getLocalFormat());
|
||||
$this->assertEquals($definition['required_fields'], $addressFormat->getRequiredFields());
|
||||
$this->assertEquals($definition['uppercase_fields'], $addressFormat->getUppercaseFields());
|
||||
$this->assertEquals($definition['administrative_area_type'], $addressFormat->getAdministrativeAreaType());
|
||||
$this->assertEquals($definition['locality_type'], $addressFormat->getLocalityType());
|
||||
// The format has no %dependentLocality, the type must be NULL.
|
||||
$this->assertNull($addressFormat->getDependentLocalityType());
|
||||
$this->assertEquals($definition['postal_code_type'], $addressFormat->getPostalCodeType());
|
||||
$this->assertEquals($definition['postal_code_pattern'], $addressFormat->getPostalCodePattern());
|
||||
$this->assertEquals($definition['postal_code_prefix'], $addressFormat->getPostalCodePrefix());
|
||||
$this->assertEquals($definition['subdivision_depth'], $addressFormat->getSubdivisionDepth());
|
||||
|
||||
$expectedUsedFields = [
|
||||
AddressField::ADMINISTRATIVE_AREA,
|
||||
AddressField::LOCALITY,
|
||||
AddressField::POSTAL_CODE,
|
||||
AddressField::ADDRESS_LINE1,
|
||||
AddressField::ADDRESS_LINE2,
|
||||
AddressField::ORGANIZATION,
|
||||
AddressField::GIVEN_NAME,
|
||||
AddressField::FAMILY_NAME,
|
||||
];
|
||||
$this->assertEquals($expectedUsedFields, $addressFormat->getUsedFields());
|
||||
$expectedUsedSubdivisionFields = [
|
||||
AddressField::ADMINISTRATIVE_AREA,
|
||||
AddressField::LOCALITY,
|
||||
];
|
||||
$this->assertEquals($expectedUsedSubdivisionFields, $addressFormat->getUsedSubdivisionFields());
|
||||
}
|
||||
}
|
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace CommerceGuys\Addressing\Tests\AddressFormat;
|
||||
|
||||
use CommerceGuys\Addressing\AddressFormat\AddressField;
|
||||
use CommerceGuys\Addressing\AddressFormat\FieldOverride;
|
||||
use CommerceGuys\Addressing\AddressFormat\FieldOverrides;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \CommerceGuys\Addressing\AddressFormat\FieldOverrides
|
||||
*/
|
||||
final class FieldOverridesTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers ::__construct
|
||||
*
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testInvalidField()
|
||||
{
|
||||
$definition = [
|
||||
'INVALID_FIELD' => FieldOverride::HIDDEN,
|
||||
];
|
||||
$fieldOverrides = new FieldOverrides($definition);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::__construct
|
||||
*
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testInvalidOverride()
|
||||
{
|
||||
$definition = [
|
||||
AddressField::POSTAL_CODE => 'INVALID',
|
||||
];
|
||||
$fieldOverrides = new FieldOverrides($definition);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::__construct
|
||||
*/
|
||||
public function testEmptyDefinition()
|
||||
{
|
||||
$fieldOverrides = new FieldOverrides([]);
|
||||
$this->assertSame([], $fieldOverrides->getHiddenFields());
|
||||
$this->assertSame([], $fieldOverrides->getOptionalFields());
|
||||
$this->assertSame([], $fieldOverrides->getRequiredFields());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::__construct
|
||||
* @covers ::getHiddenFields
|
||||
* @covers ::getOptionalFields
|
||||
* @covers ::getRequiredFields
|
||||
*/
|
||||
public function testOverrides()
|
||||
{
|
||||
$fieldOverrides = new FieldOverrides([
|
||||
AddressField::GIVEN_NAME => FieldOverride::HIDDEN,
|
||||
AddressField::ADDITIONAL_NAME => FieldOverride::HIDDEN,
|
||||
AddressField::FAMILY_NAME => FieldOverride::HIDDEN,
|
||||
AddressField::ORGANIZATION => FieldOverride::REQUIRED,
|
||||
AddressField::POSTAL_CODE => FieldOverride::OPTIONAL,
|
||||
]);
|
||||
$this->assertSame([
|
||||
AddressField::GIVEN_NAME,
|
||||
AddressField::ADDITIONAL_NAME,
|
||||
AddressField::FAMILY_NAME
|
||||
], $fieldOverrides->getHiddenFields());
|
||||
$this->assertSame([AddressField::POSTAL_CODE], $fieldOverrides->getOptionalFields());
|
||||
$this->assertSame([AddressField::ORGANIZATION], $fieldOverrides->getRequiredFields());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user