AddressMetadataTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model\Metadata;
  7. use Magento\Customer\Api\AddressMetadataInterface;
  8. use Magento\Customer\Api\Data\AttributeMetadataInterface;
  9. use Magento\Customer\Model\Attribute;
  10. use Magento\Customer\Model\AttributeMetadataConverter;
  11. use Magento\Customer\Model\AttributeMetadataDataProvider;
  12. use Magento\Customer\Model\Metadata\AddressMetadata;
  13. use Magento\Customer\Model\ResourceModel\Form\Attribute\Collection;
  14. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  15. class AddressMetadataTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /** @var AddressMetadata */
  18. protected $model;
  19. /** @var AttributeMetadataConverter|\PHPUnit_Framework_MockObject_MockObject */
  20. protected $attributeConverterMock;
  21. /** @var AttributeMetadataDataProvider|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $attributeProviderMock;
  23. protected function setUp()
  24. {
  25. $this->attributeConverterMock = $this->getMockBuilder(\Magento\Customer\Model\AttributeMetadataConverter::class)
  26. ->disableOriginalConstructor()
  27. ->getMock();
  28. $this->attributeProviderMock = $this->getMockBuilder(
  29. \Magento\Customer\Model\AttributeMetadataDataProvider::class
  30. )
  31. ->disableOriginalConstructor()
  32. ->getMock();
  33. $this->model = new AddressMetadata(
  34. $this->attributeConverterMock,
  35. $this->attributeProviderMock
  36. );
  37. }
  38. public function testGetAttributes()
  39. {
  40. $formCode = 'formcode';
  41. $attributeCode = 'attr';
  42. /** @var Attribute|\PHPUnit_Framework_MockObject_MockObject $attributeMock */
  43. $attributeMock = $this->getMockBuilder(\Magento\Customer\Model\Attribute::class)
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $attributes = [$attributeMock];
  47. /** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
  48. $collectionMock = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Form\Attribute\Collection::class)
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $this->attributeProviderMock->expects($this->once())
  52. ->method('loadAttributesCollection')
  53. ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $formCode)
  54. ->willReturn($collectionMock);
  55. $collectionMock->expects($this->once())
  56. ->method('getIterator')
  57. ->willReturn(new \ArrayIterator($attributes));
  58. $attributeMock->expects($this->once())
  59. ->method('getAttributeCode')
  60. ->willReturn($attributeCode);
  61. /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $metadataMock */
  62. $metadataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $result = [$attributeCode => $metadataMock];
  66. $this->attributeConverterMock->expects($this->once())
  67. ->method('createMetadataAttribute')
  68. ->with($attributeMock)
  69. ->willReturn($metadataMock);
  70. $this->assertEquals($result, $this->model->getAttributes($formCode));
  71. }
  72. /**
  73. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  74. * @expectedExceptionMessage No such entity with formCode = formcode
  75. */
  76. public function testGetAttributesWithException()
  77. {
  78. $formCode = 'formcode';
  79. $attributes = [];
  80. /** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
  81. $collectionMock = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Form\Attribute\Collection::class)
  82. ->disableOriginalConstructor()
  83. ->getMock();
  84. $this->attributeProviderMock->expects($this->once())
  85. ->method('loadAttributesCollection')
  86. ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $formCode)
  87. ->willReturn($collectionMock);
  88. $collectionMock->expects($this->once())
  89. ->method('getIterator')
  90. ->willReturn(new \ArrayIterator($attributes));
  91. $this->model->getAttributes($formCode);
  92. }
  93. public function testGetAttributeMetadata()
  94. {
  95. $attributeCode = 'attr';
  96. $attributeId = 12;
  97. /** @var AbstractAttribute|\PHPUnit_Framework_MockObject_MockObject $attributeMock */
  98. $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
  99. ->disableOriginalConstructor()
  100. ->setMethods(['getId'])
  101. ->getMockForAbstractClass();
  102. $this->attributeProviderMock->expects($this->once())
  103. ->method('getAttribute')
  104. ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $attributeCode)
  105. ->willReturn($attributeMock);
  106. $attributeMock->expects($this->once())
  107. ->method('getId')
  108. ->willReturn($attributeId);
  109. /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $metadataMock */
  110. $metadataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
  111. ->disableOriginalConstructor()
  112. ->getMock();
  113. $this->attributeConverterMock->expects($this->once())
  114. ->method('createMetadataAttribute')
  115. ->with($attributeMock)
  116. ->willReturn($metadataMock);
  117. $this->assertEquals($metadataMock, $this->model->getAttributeMetadata($attributeCode));
  118. }
  119. public function testGetAttributeMetadataWithCodeId()
  120. {
  121. $attributeCode = 'id';
  122. /** @var AbstractAttribute|\PHPUnit_Framework_MockObject_MockObject $attributeMock */
  123. $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
  124. ->disableOriginalConstructor()
  125. ->getMockForAbstractClass();
  126. $this->attributeProviderMock->expects($this->once())
  127. ->method('getAttribute')
  128. ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $attributeCode)
  129. ->willReturn($attributeMock);
  130. /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $metadataMock */
  131. $metadataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
  132. ->disableOriginalConstructor()
  133. ->getMock();
  134. $this->attributeConverterMock->expects($this->once())
  135. ->method('createMetadataAttribute')
  136. ->with($attributeMock)
  137. ->willReturn($metadataMock);
  138. $this->assertEquals($metadataMock, $this->model->getAttributeMetadata($attributeCode));
  139. }
  140. /**
  141. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  142. * @expectedExceptionMessage No such entity with entityType = customer_address, attributeCode = id
  143. */
  144. public function testGetAttributeMetadataWithoutAttribute()
  145. {
  146. $attributeCode = 'id';
  147. $this->attributeProviderMock->expects($this->once())
  148. ->method('getAttribute')
  149. ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $attributeCode)
  150. ->willReturn(null);
  151. $this->model->getAttributeMetadata($attributeCode);
  152. }
  153. public function testGetAllAttributesMetadata()
  154. {
  155. $attributeCode = 'id';
  156. $attributeCodes = [$attributeCode];
  157. $this->attributeProviderMock->expects($this->once())
  158. ->method('getAllAttributeCodes')
  159. ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS)
  160. ->willReturn($attributeCodes);
  161. /** @var AbstractAttribute|\PHPUnit_Framework_MockObject_MockObject $attributeMock */
  162. $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
  163. ->disableOriginalConstructor()
  164. ->getMockForAbstractClass();
  165. $this->attributeProviderMock->expects($this->once())
  166. ->method('getAttribute')
  167. ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $attributeCode)
  168. ->willReturn($attributeMock);
  169. /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $metadataMock */
  170. $metadataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
  171. ->disableOriginalConstructor()
  172. ->getMock();
  173. $result = [$metadataMock];
  174. $this->attributeConverterMock->expects($this->once())
  175. ->method('createMetadataAttribute')
  176. ->with($attributeMock)
  177. ->willReturn($metadataMock);
  178. $this->assertEquals($result, $this->model->getAllAttributesMetadata());
  179. }
  180. public function testGetAllAttributesMetadataWithoutEntity()
  181. {
  182. $attributeCode = 'id';
  183. $attributeCodes = [$attributeCode];
  184. $this->attributeProviderMock->expects($this->once())
  185. ->method('getAllAttributeCodes')
  186. ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS)
  187. ->willReturn($attributeCodes);
  188. $this->attributeProviderMock->expects($this->once())
  189. ->method('getAttribute')
  190. ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $attributeCode)
  191. ->willReturn(null);
  192. $result = [];
  193. $this->assertEquals($result, $this->model->getAllAttributesMetadata());
  194. }
  195. public function testGetCustomAttributesMetadata()
  196. {
  197. $attributeCode = 'attr';
  198. $attributeId = 12;
  199. $attributeCodes = [$attributeCode];
  200. $this->attributeProviderMock->expects($this->once())
  201. ->method('getAllAttributeCodes')
  202. ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS)
  203. ->willReturn($attributeCodes);
  204. /** @var AbstractAttribute|\PHPUnit_Framework_MockObject_MockObject $attributeMock */
  205. $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
  206. ->disableOriginalConstructor()
  207. ->setMethods(['getId'])
  208. ->getMockForAbstractClass();
  209. $this->attributeProviderMock->expects($this->once())
  210. ->method('getAttribute')
  211. ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $attributeCode)
  212. ->willReturn($attributeMock);
  213. $attributeMock->expects($this->once())
  214. ->method('getId')
  215. ->willReturn($attributeId);
  216. /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $metadataMock */
  217. $metadataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
  218. ->disableOriginalConstructor()
  219. ->getMock();
  220. $result = [$metadataMock];
  221. $this->attributeConverterMock->expects($this->once())
  222. ->method('createMetadataAttribute')
  223. ->with($attributeMock)
  224. ->willReturn($metadataMock);
  225. $metadataMock->expects($this->once())
  226. ->method('getAttributeCode')
  227. ->willReturn($attributeCode);
  228. $metadataMock->expects($this->once())
  229. ->method('isSystem')
  230. ->willReturn(false);
  231. $this->assertEquals($result, $this->model->getCustomAttributesMetadata());
  232. }
  233. public function testGetCustomAttributesMetadataWithSystemAttribute()
  234. {
  235. $attributeCode = 'attr';
  236. $attributeId = 12;
  237. $attributeCodes = [$attributeCode];
  238. $this->attributeProviderMock->expects($this->once())
  239. ->method('getAllAttributeCodes')
  240. ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS)
  241. ->willReturn($attributeCodes);
  242. /** @var AbstractAttribute|\PHPUnit_Framework_MockObject_MockObject $attributeMock */
  243. $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
  244. ->disableOriginalConstructor()
  245. ->setMethods(['getId'])
  246. ->getMockForAbstractClass();
  247. $this->attributeProviderMock->expects($this->once())
  248. ->method('getAttribute')
  249. ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $attributeCode)
  250. ->willReturn($attributeMock);
  251. $attributeMock->expects($this->once())
  252. ->method('getId')
  253. ->willReturn($attributeId);
  254. /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $metadataMock */
  255. $metadataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
  256. ->disableOriginalConstructor()
  257. ->getMock();
  258. $result = [];
  259. $this->attributeConverterMock->expects($this->once())
  260. ->method('createMetadataAttribute')
  261. ->with($attributeMock)
  262. ->willReturn($metadataMock);
  263. $metadataMock->expects($this->once())
  264. ->method('getAttributeCode')
  265. ->willReturn($attributeCode);
  266. $metadataMock->expects($this->once())
  267. ->method('isSystem')
  268. ->willReturn(true);
  269. $this->assertEquals($result, $this->model->getCustomAttributesMetadata());
  270. }
  271. public function testGetCustomAttributesMetadataWithoutAttributes()
  272. {
  273. $attributeCode = 'id';
  274. $attributeCodes = [$attributeCode];
  275. $this->attributeProviderMock->expects($this->once())
  276. ->method('getAllAttributeCodes')
  277. ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS)
  278. ->willReturn($attributeCodes);
  279. /** @var AbstractAttribute|\PHPUnit_Framework_MockObject_MockObject $attributeMock */
  280. $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
  281. ->disableOriginalConstructor()
  282. ->getMockForAbstractClass();
  283. $this->attributeProviderMock->expects($this->once())
  284. ->method('getAttribute')
  285. ->with(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $attributeCode)
  286. ->willReturn($attributeMock);
  287. /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $metadataMock */
  288. $metadataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
  289. ->disableOriginalConstructor()
  290. ->getMock();
  291. $result = [];
  292. $this->attributeConverterMock->expects($this->once())
  293. ->method('createMetadataAttribute')
  294. ->with($attributeMock)
  295. ->willReturn($metadataMock);
  296. $metadataMock->expects($this->once())
  297. ->method('getAttributeCode')
  298. ->willReturn($attributeCode);
  299. $this->assertEquals($result, $this->model->getCustomAttributesMetadata());
  300. }
  301. }