123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Test\Unit\Model\Metadata;
- use Magento\Customer\Api\AddressMetadataInterface;
- use Magento\Customer\Api\Data\AttributeMetadataInterface;
- use Magento\Customer\Model\Attribute;
- use Magento\Customer\Model\AttributeMetadataConverter;
- use Magento\Customer\Model\AttributeMetadataDataProvider;
- use Magento\Customer\Model\Metadata\AddressMetadata;
- use Magento\Customer\Model\ResourceModel\Form\Attribute\Collection;
- use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
- class AddressMetadataTest extends \PHPUnit\Framework\TestCase
- {
- /** @var AddressMetadata */
- protected $model;
- /** @var AttributeMetadataConverter|\PHPUnit_Framework_MockObject_MockObject */
- protected $attributeConverterMock;
- /** @var AttributeMetadataDataProvider|\PHPUnit_Framework_MockObject_MockObject */
- protected $attributeProviderMock;
- protected function setUp()
- {
- $this->attributeConverterMock = $this->getMockBuilder(\Magento\Customer\Model\AttributeMetadataConverter::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->attributeProviderMock = $this->getMockBuilder(
- \Magento\Customer\Model\AttributeMetadataDataProvider::class
- )
- ->disableOriginalConstructor()
- ->getMock();
- $this->model = new AddressMetadata(
- $this->attributeConverterMock,
- $this->attributeProviderMock
- );
- }
- public function testGetAttributes()
- {
- $formCode = 'formcode';
- $attributeCode = 'attr';
- /** @var Attribute|\PHPUnit_Framework_MockObject_MockObject $attributeMock */
- $attributeMock = $this->getMockBuilder(\Magento\Customer\Model\Attribute::class)
- ->disableOriginalConstructor()
- ->getMock();
- $attributes = [$attributeMock];
- /** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
- $collectionMock = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Form\Attribute\Collection::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->attributeProviderMock->expects($this->once())
- ->method('loadAttributesCollection')
- ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $formCode)
- ->willReturn($collectionMock);
- $collectionMock->expects($this->once())
- ->method('getIterator')
- ->willReturn(new \ArrayIterator($attributes));
- $attributeMock->expects($this->once())
- ->method('getAttributeCode')
- ->willReturn($attributeCode);
- /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $metadataMock */
- $metadataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $result = [$attributeCode => $metadataMock];
- $this->attributeConverterMock->expects($this->once())
- ->method('createMetadataAttribute')
- ->with($attributeMock)
- ->willReturn($metadataMock);
- $this->assertEquals($result, $this->model->getAttributes($formCode));
- }
- /**
- * @expectedException \Magento\Framework\Exception\NoSuchEntityException
- * @expectedExceptionMessage No such entity with formCode = formcode
- */
- public function testGetAttributesWithException()
- {
- $formCode = 'formcode';
- $attributes = [];
- /** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
- $collectionMock = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Form\Attribute\Collection::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->attributeProviderMock->expects($this->once())
- ->method('loadAttributesCollection')
- ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $formCode)
- ->willReturn($collectionMock);
- $collectionMock->expects($this->once())
- ->method('getIterator')
- ->willReturn(new \ArrayIterator($attributes));
- $this->model->getAttributes($formCode);
- }
- public function testGetAttributeMetadata()
- {
- $attributeCode = 'attr';
- $attributeId = 12;
- /** @var AbstractAttribute|\PHPUnit_Framework_MockObject_MockObject $attributeMock */
- $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
- ->disableOriginalConstructor()
- ->setMethods(['getId'])
- ->getMockForAbstractClass();
- $this->attributeProviderMock->expects($this->once())
- ->method('getAttribute')
- ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $attributeCode)
- ->willReturn($attributeMock);
- $attributeMock->expects($this->once())
- ->method('getId')
- ->willReturn($attributeId);
- /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $metadataMock */
- $metadataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->attributeConverterMock->expects($this->once())
- ->method('createMetadataAttribute')
- ->with($attributeMock)
- ->willReturn($metadataMock);
- $this->assertEquals($metadataMock, $this->model->getAttributeMetadata($attributeCode));
- }
- public function testGetAttributeMetadataWithCodeId()
- {
- $attributeCode = 'id';
- /** @var AbstractAttribute|\PHPUnit_Framework_MockObject_MockObject $attributeMock */
- $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->attributeProviderMock->expects($this->once())
- ->method('getAttribute')
- ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $attributeCode)
- ->willReturn($attributeMock);
- /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $metadataMock */
- $metadataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->attributeConverterMock->expects($this->once())
- ->method('createMetadataAttribute')
- ->with($attributeMock)
- ->willReturn($metadataMock);
- $this->assertEquals($metadataMock, $this->model->getAttributeMetadata($attributeCode));
- }
- /**
- * @expectedException \Magento\Framework\Exception\NoSuchEntityException
- * @expectedExceptionMessage No such entity with entityType = customer_address, attributeCode = id
- */
- public function testGetAttributeMetadataWithoutAttribute()
- {
- $attributeCode = 'id';
- $this->attributeProviderMock->expects($this->once())
- ->method('getAttribute')
- ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $attributeCode)
- ->willReturn(null);
- $this->model->getAttributeMetadata($attributeCode);
- }
- public function testGetAllAttributesMetadata()
- {
- $attributeCode = 'id';
- $attributeCodes = [$attributeCode];
- $this->attributeProviderMock->expects($this->once())
- ->method('getAllAttributeCodes')
- ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS)
- ->willReturn($attributeCodes);
- /** @var AbstractAttribute|\PHPUnit_Framework_MockObject_MockObject $attributeMock */
- $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->attributeProviderMock->expects($this->once())
- ->method('getAttribute')
- ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $attributeCode)
- ->willReturn($attributeMock);
- /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $metadataMock */
- $metadataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $result = [$metadataMock];
- $this->attributeConverterMock->expects($this->once())
- ->method('createMetadataAttribute')
- ->with($attributeMock)
- ->willReturn($metadataMock);
- $this->assertEquals($result, $this->model->getAllAttributesMetadata());
- }
- public function testGetAllAttributesMetadataWithoutEntity()
- {
- $attributeCode = 'id';
- $attributeCodes = [$attributeCode];
- $this->attributeProviderMock->expects($this->once())
- ->method('getAllAttributeCodes')
- ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS)
- ->willReturn($attributeCodes);
- $this->attributeProviderMock->expects($this->once())
- ->method('getAttribute')
- ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $attributeCode)
- ->willReturn(null);
- $result = [];
- $this->assertEquals($result, $this->model->getAllAttributesMetadata());
- }
- public function testGetCustomAttributesMetadata()
- {
- $attributeCode = 'attr';
- $attributeId = 12;
- $attributeCodes = [$attributeCode];
- $this->attributeProviderMock->expects($this->once())
- ->method('getAllAttributeCodes')
- ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS)
- ->willReturn($attributeCodes);
- /** @var AbstractAttribute|\PHPUnit_Framework_MockObject_MockObject $attributeMock */
- $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
- ->disableOriginalConstructor()
- ->setMethods(['getId'])
- ->getMockForAbstractClass();
- $this->attributeProviderMock->expects($this->once())
- ->method('getAttribute')
- ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $attributeCode)
- ->willReturn($attributeMock);
- $attributeMock->expects($this->once())
- ->method('getId')
- ->willReturn($attributeId);
- /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $metadataMock */
- $metadataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $result = [$metadataMock];
- $this->attributeConverterMock->expects($this->once())
- ->method('createMetadataAttribute')
- ->with($attributeMock)
- ->willReturn($metadataMock);
- $metadataMock->expects($this->once())
- ->method('getAttributeCode')
- ->willReturn($attributeCode);
- $metadataMock->expects($this->once())
- ->method('isSystem')
- ->willReturn(false);
- $this->assertEquals($result, $this->model->getCustomAttributesMetadata());
- }
- public function testGetCustomAttributesMetadataWithSystemAttribute()
- {
- $attributeCode = 'attr';
- $attributeId = 12;
- $attributeCodes = [$attributeCode];
- $this->attributeProviderMock->expects($this->once())
- ->method('getAllAttributeCodes')
- ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS)
- ->willReturn($attributeCodes);
- /** @var AbstractAttribute|\PHPUnit_Framework_MockObject_MockObject $attributeMock */
- $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
- ->disableOriginalConstructor()
- ->setMethods(['getId'])
- ->getMockForAbstractClass();
- $this->attributeProviderMock->expects($this->once())
- ->method('getAttribute')
- ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $attributeCode)
- ->willReturn($attributeMock);
- $attributeMock->expects($this->once())
- ->method('getId')
- ->willReturn($attributeId);
- /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $metadataMock */
- $metadataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $result = [];
- $this->attributeConverterMock->expects($this->once())
- ->method('createMetadataAttribute')
- ->with($attributeMock)
- ->willReturn($metadataMock);
- $metadataMock->expects($this->once())
- ->method('getAttributeCode')
- ->willReturn($attributeCode);
- $metadataMock->expects($this->once())
- ->method('isSystem')
- ->willReturn(true);
- $this->assertEquals($result, $this->model->getCustomAttributesMetadata());
- }
- public function testGetCustomAttributesMetadataWithoutAttributes()
- {
- $attributeCode = 'id';
- $attributeCodes = [$attributeCode];
- $this->attributeProviderMock->expects($this->once())
- ->method('getAllAttributeCodes')
- ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS)
- ->willReturn($attributeCodes);
- /** @var AbstractAttribute|\PHPUnit_Framework_MockObject_MockObject $attributeMock */
- $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->attributeProviderMock->expects($this->once())
- ->method('getAttribute')
- ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $attributeCode)
- ->willReturn($attributeMock);
- /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $metadataMock */
- $metadataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $result = [];
- $this->attributeConverterMock->expects($this->once())
- ->method('createMetadataAttribute')
- ->with($attributeMock)
- ->willReturn($metadataMock);
- $metadataMock->expects($this->once())
- ->method('getAttributeCode')
- ->willReturn($attributeCode);
- $this->assertEquals($result, $this->model->getCustomAttributesMetadata());
- }
- }
|