default services conflit ?

This commit is contained in:
armansansd
2022-04-27 11:30:43 +02:00
parent 28190a5749
commit 8bb1064a3b
8132 changed files with 900138 additions and 426 deletions

View File

@@ -0,0 +1,73 @@
<?php
namespace CommerceGuys\Addressing\Tests\Subdivision;
use CommerceGuys\Addressing\Subdivision\LazySubdivisionCollection;
use PHPUnit\Framework\TestCase;
/**
* @coversDefaultClass \CommerceGuys\Addressing\Subdivision\LazySubdivisionCollection
*/
final class LazySubdivisionCollectionTest extends TestCase
{
/**
* @var LazySubdivisionCollection
*/
protected $collection;
/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->collection = new LazySubdivisionCollection(['BR', 'Porto Acre']);
}
/**
* @covers ::__construct
*/
public function testConstructor()
{
$collection = new LazySubdivisionCollection(['BR', 'Porto Acre']);
$this->assertEquals(['BR', 'Porto Acre'], $this->getObjectAttribute($collection, 'parents'));
}
/**
* @covers ::doInitialize
*/
public function testInitialize()
{
$subdivision = $this
->getMockBuilder('CommerceGuys\Addressing\Subdivision\Subdivision')
->disableOriginalConstructor()
->getMock();
$subdivisionRepository = $this
->getMockBuilder('CommerceGuys\Addressing\Subdivision\SubdivisionRepository')
->disableOriginalConstructor()
->getMock();
$subdivisionRepository
->expects($this->any())
->method('getAll')
->with(['BR', 'Porto Acre'])
->will($this->returnValue([$subdivision]));
$this->collection->setRepository($subdivisionRepository);
$this->assertFalse($this->collection->isInitialized());
$this->assertCount(1, $this->collection);
$this->assertTrue($this->collection->isInitialized());
}
/**
* @covers ::getRepository
* @covers ::setRepository
*/
public function testRepository()
{
$subdivisionRepository = $this
->getMockBuilder('CommerceGuys\Addressing\Subdivision\SubdivisionRepository')
->disableOriginalConstructor()
->getMock();
$this->collection->setRepository($subdivisionRepository);
$this->assertSame($subdivisionRepository, $this->collection->getRepository());
}
}

View File

@@ -0,0 +1,183 @@
<?php
namespace CommerceGuys\Addressing\Tests\Subdivision;
use CommerceGuys\Addressing\Subdivision\Subdivision;
use CommerceGuys\Addressing\Subdivision\SubdivisionRepository;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
/**
* @coversDefaultClass \CommerceGuys\Addressing\Subdivision\SubdivisionRepository
*/
final class SubdivisionRepositoryTest extends TestCase
{
/**
* Subdivisions.
*
* @var array
*/
protected $subdivisions = [
'BR' => [
'country_code' => 'BR',
'locale' => 'pt',
'subdivisions' => [
'SC' => [
'name' => 'Santa Catarina',
'iso_code' => 'BR-SC',
'postal_code_pattern' => '8[89]',
'postal_code_pattern_type' => 'full',
'has_children' => true,
],
'SP' => [
'name' => 'São Paulo',
'iso_code' => 'BR-SP',
'postal_code_pattern' => '[01][1-9]',
'has_children' => true,
],
],
],
'BR-249a39f10ac434b1fcd4d51516266b8e' => [
'country_code' => 'BR',
'parents' => ['BR', 'SC'],
'locale' => 'pt',
'subdivisions' => [
'Abelardo Luz' => [],
],
],
'BR-8ef7a36db3f5d47d46566f851be5f610' => [
'country_code' => 'BR',
'parents' => ['BR', 'SP'],
'locale' => 'pt',
'subdivisions' => [
'Anhumas' => [],
]
],
];
/**
* @covers ::__construct
*/
public function testConstructor()
{
// Mock the existence of JSON definitions on the filesystem.
$root = vfsStream::setup('resources');
$directory = vfsStream::newDirectory('subdivision')->at($root);
foreach ($this->subdivisions as $parent => $data) {
$filename = $parent . '.json';
vfsStream::newFile($filename)->at($directory)->setContent(json_encode($data));
}
// Instantiate the subdivision repository and confirm that the
// definition path was properly set.
$subdivisionRepository = new SubdivisionRepository(null, 'vfs://resources/subdivision/');
$definitionPath = $this->getObjectAttribute($subdivisionRepository, 'definitionPath');
$this->assertEquals('vfs://resources/subdivision/', $definitionPath);
return $subdivisionRepository;
}
/**
* @covers ::get
* @covers ::hasData
* @covers ::loadDefinitions
* @covers ::processDefinitions
* @covers ::buildGroup
* @covers ::createSubdivisionFromDefinitions
*
* @depends testConstructor
*/
public function testGet($subdivisionRepository)
{
$subdivision = $subdivisionRepository->get('SC', ['BR']);
$subdivisionChild = $subdivisionRepository->get('Abelardo Luz', ['BR', 'SC']);
$this->assertInstanceOf(Subdivision::class, $subdivision);
$this->assertEquals(null, $subdivision->getParent());
$this->assertEquals('BR', $subdivision->getCountryCode());
$this->assertEquals('pt', $subdivision->getLocale());
$this->assertEquals('SC', $subdivision->getCode());
$this->assertEquals('Santa Catarina', $subdivision->getName());
$this->assertEquals('BR-SC', $subdivision->getIsoCode());
$this->assertEquals('8[89]', $subdivision->getPostalCodePattern());
$this->assertEquals('full', $subdivision->getPostalCodePatternType());
$children = $subdivision->getChildren();
$this->assertEquals($subdivisionChild, $children['Abelardo Luz']);
$this->assertInstanceOf(Subdivision::class, $subdivisionChild);
$this->assertEquals('Abelardo Luz', $subdivisionChild->getCode());
// $subdivision contains the loaded children while $parent doesn't,
// so they can't be compared directly.
$parent = $subdivisionChild->getParent();
$this->assertInstanceOf(Subdivision::class, $parent);
$this->assertEquals($subdivision->getCode(), $parent->getCode());
}
/**
* @covers ::get
* @covers ::hasData
* @covers ::loadDefinitions
* @covers ::processDefinitions
* @covers ::buildGroup
* @covers ::createSubdivisionFromDefinitions
*
* @depends testConstructor
*/
public function testGetInvalidSubdivision($subdivisionRepository)
{
$subdivision = $subdivisionRepository->get('FAKE', ['BR']);
$this->assertNull($subdivision);
}
/**
* @covers ::getAll
* @covers ::hasData
* @covers ::loadDefinitions
* @covers ::processDefinitions
* @covers ::buildGroup
* @covers ::createSubdivisionFromDefinitions
*
* @depends testConstructor
*/
public function testGetAll($subdivisionRepository)
{
$subdivisions = $subdivisionRepository->getAll(['RS']);
$this->assertEquals([], $subdivisions);
$subdivisions = $subdivisionRepository->getAll(['BR']);
$this->assertCount(2, $subdivisions);
$this->assertArrayHasKey('SC', $subdivisions);
$this->assertArrayHasKey('SP', $subdivisions);
$this->assertEquals($subdivisions['SC']->getCode(), 'SC');
$this->assertEquals($subdivisions['SP']->getCode(), 'SP');
$subdivisions = $subdivisionRepository->getAll(['BR', 'SC']);
$this->assertCount(1, $subdivisions);
$this->assertArrayHasKey('Abelardo Luz', $subdivisions);
$this->assertEquals($subdivisions['Abelardo Luz']->getCode(), 'Abelardo Luz');
}
/**
* @covers ::getList
* @covers ::hasData
* @covers ::loadDefinitions
* @covers ::processDefinitions
* @covers ::buildGroup
*
* @depends testConstructor
*/
public function testGetList($subdivisionRepository)
{
$list = $subdivisionRepository->getList(['RS']);
$this->assertEquals([], $list);
$list = $subdivisionRepository->getList(['BR']);
$expectedList = ['SC' => 'Santa Catarina', 'SP' => 'São Paulo'];
$this->assertEquals($expectedList, $list);
$list = $subdivisionRepository->getList(['BR', 'SC']);
$expectedList = ['Abelardo Luz' => 'Abelardo Luz'];
$this->assertEquals($expectedList, $list);
}
}

View File

@@ -0,0 +1,78 @@
<?php
namespace CommerceGuys\Addressing\Tests\Subdivision;
use CommerceGuys\Addressing\Subdivision\PatternType;
use CommerceGuys\Addressing\Subdivision\Subdivision;
use Doctrine\Common\Collections\ArrayCollection;
use PHPUnit\Framework\TestCase;
/**
* @coversDefaultClass \CommerceGuys\Addressing\Subdivision\Subdivision
*/
final class SubdivisionTest extends TestCase
{
/**
* @covers ::__construct
*
* @expectedException \InvalidArgumentException
*/
public function testMissingProperty()
{
$definition = [
'country_code' => 'US',
];
$subdivision = new Subdivision($definition);
}
/**
* @covers ::__construct
* @covers ::getParent
* @covers ::getCountryCode
* @covers ::getLocale
* @covers ::getCode
* @covers ::getLocalCode
* @covers ::getName
* @covers ::getLocalName
* @covers ::getIsoCode
* @covers ::getPostalCodePattern
* @covers ::getPostalCodePatternType
* @covers ::getChildren
* @covers ::hasChildren
*/
public function testValid()
{
$mockBuilder = $this->getMockBuilder('CommerceGuys\Addressing\Subdivision\Subdivision');
$mockBuilder = $mockBuilder->disableOriginalConstructor();
$parent = $mockBuilder->getMock();
$children = new ArrayCollection([$mockBuilder->getMock(), $mockBuilder->getMock()]);
$definition = [
'parent' => $parent,
'country_code' => 'US',
'locale' => 'en',
'code' => 'CA',
'local_code' => 'CA!',
'name' => 'California',
'local_name' => 'California!',
'iso_code' => 'US-CA',
'postal_code_pattern' => '9[0-5]|96[01]',
'postal_code_pattern_type' => PatternType::START,
'children' => $children,
];
$subdivision = new Subdivision($definition);
$this->assertEquals($definition['parent'], $subdivision->getParent());
$this->assertEquals($definition['country_code'], $subdivision->getCountryCode());
$this->assertEquals($definition['locale'], $subdivision->getLocale());
$this->assertEquals($definition['code'], $subdivision->getCode());
$this->assertEquals($definition['local_code'], $subdivision->getLocalCode());
$this->assertEquals($definition['name'], $subdivision->getName());
$this->assertEquals($definition['local_name'], $subdivision->getLocalName());
$this->assertEquals($definition['iso_code'], $subdivision->getIsoCode());
$this->assertEquals($definition['postal_code_pattern'], $subdivision->getPostalCodePattern());
$this->assertEquals($definition['postal_code_pattern_type'], $subdivision->getPostalCodePatternType());
$this->assertEquals($definition['children'], $subdivision->getChildren());
$this->assertTrue($subdivision->hasChildren());
}
}