AddressTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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\ResourceModel;
  7. use Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite;
  8. use Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  10. /**
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class AddressTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /** @var \Magento\Customer\Test\Unit\Model\ResourceModel\SubResourceModelAddress */
  16. protected $addressResource;
  17. /** @var \Magento\Customer\Model\CustomerFactory|\PHPUnit_Framework_MockObject_MockObject */
  18. protected $customerFactory;
  19. /** @var \Magento\Eav\Model\Entity\Type */
  20. protected $eavConfigType;
  21. /** @var Snapshot|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $entitySnapshotMock;
  23. /** @var RelationComposite|\PHPUnit_Framework_MockObject_MockObject */
  24. protected $entityRelationCompositeMock;
  25. protected function setUp()
  26. {
  27. $this->entitySnapshotMock = $this->createMock(
  28. \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot::class
  29. );
  30. $this->entityRelationCompositeMock = $this->createMock(
  31. \Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite::class
  32. );
  33. $this->addressResource = (new ObjectManagerHelper($this))->getObject(
  34. \Magento\Customer\Test\Unit\Model\ResourceModel\SubResourceModelAddress::class,
  35. [
  36. 'resource' => $this->prepareResource(),
  37. 'entitySnapshot' => $this->entitySnapshotMock,
  38. 'entityRelationComposite' => $this->entityRelationCompositeMock,
  39. 'eavConfig' => $this->prepareEavConfig(),
  40. 'validatorFactory' => $this->prepareValidatorFactory(),
  41. 'customerFactory' => $this->prepareCustomerFactory()
  42. ]
  43. );
  44. }
  45. /**
  46. * @param $addressId
  47. * @param $isDefaultBilling
  48. * @param $isDefaultShipping
  49. *
  50. * @dataProvider getSaveDataProvider
  51. */
  52. public function testSave($addressId, $isDefaultBilling, $isDefaultShipping)
  53. {
  54. /** @var $address \Magento\Customer\Model\Address|\PHPUnit_Framework_MockObject_MockObject */
  55. $address = $this->createPartialMock(
  56. \Magento\Customer\Model\Address::class,
  57. [
  58. '__wakeup',
  59. 'getId',
  60. 'getEntityTypeId',
  61. 'getIsDefaultBilling',
  62. 'getIsDefaultShipping',
  63. 'hasDataChanges',
  64. 'validateBeforeSave',
  65. 'beforeSave',
  66. 'afterSave',
  67. 'isSaveAllowed'
  68. ]
  69. );
  70. $this->entitySnapshotMock->expects($this->once())->method('isModified')->willReturn(true);
  71. $this->entityRelationCompositeMock->expects($this->once())->method('processRelations');
  72. $address->expects($this->once())->method('isSaveAllowed')->willReturn(true);
  73. $address->expects($this->once())->method('validateBeforeSave');
  74. $address->expects($this->once())->method('beforeSave');
  75. $address->expects($this->once())->method('afterSave');
  76. $address->expects($this->any())->method('getEntityTypeId')->willReturn('3');
  77. $address->expects($this->any())->method('getId')->willReturn($addressId);
  78. $address->expects($this->any())->method('getIsDefaultShipping')->willReturn($isDefaultShipping);
  79. $address->expects($this->any())->method('getIsDefaultBilling')->willReturn($isDefaultBilling);
  80. $this->addressResource->setType('customer_address');
  81. $attributeLoaderMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\AttributeLoaderInterface::class)
  82. ->disableOriginalConstructor()
  83. ->getMock();
  84. $this->addressResource->setAttributeLoader($attributeLoaderMock);
  85. $this->addressResource->save($address);
  86. }
  87. /**
  88. * Data provider for testSave method
  89. *
  90. * @return array
  91. */
  92. public function getSaveDataProvider()
  93. {
  94. return [
  95. [null, true, true],
  96. [1, true, true],
  97. [1, true, false],
  98. [1, false, true],
  99. [1, false, false],
  100. ];
  101. }
  102. /**
  103. * Prepare resource mock object
  104. *
  105. * @return \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  106. */
  107. protected function prepareResource()
  108. {
  109. $dbSelect = $this->createMock(\Magento\Framework\DB\Select::class);
  110. $dbSelect->expects($this->any())->method('from')->willReturnSelf();
  111. $dbSelect->expects($this->any())->method('where')->willReturnSelf();
  112. $dbAdapter = $this->getMockBuilder(\Magento\Framework\DB\Adapter\Pdo\Mysql::class)
  113. ->disableOriginalConstructor()
  114. ->getMock();
  115. $dbAdapter->expects($this->any())
  116. ->method('describeTable')
  117. ->with('customer_address_entity')
  118. ->willReturn(
  119. [
  120. 'entity_type_id',
  121. 'attribute_set_id',
  122. 'created_at',
  123. 'updated_at',
  124. 'parent_id',
  125. 'increment_id',
  126. 'entity_id',
  127. ]
  128. );
  129. $dbAdapter->expects($this->any())->method('lastInsertId');
  130. $dbAdapter->expects($this->any())->method('select')->willReturn($dbSelect);
  131. $resource = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
  132. ->disableOriginalConstructor()
  133. ->getMock();
  134. $resource->expects($this->any())->method('getConnection')->will($this->returnValue($dbAdapter));
  135. $resource->expects($this->any())->method('getTableName')->will($this->returnValue('customer_address_entity'));
  136. return $resource;
  137. }
  138. /**
  139. * Prepare Eav config mock object
  140. *
  141. * @return \Magento\Eav\Model\Config|\PHPUnit_Framework_MockObject_MockObject
  142. */
  143. protected function prepareEavConfig()
  144. {
  145. $attributeMock = $this->createPartialMock(
  146. \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
  147. ['getAttributeCode', 'getBackend', '__wakeup']
  148. );
  149. $attributeMock->expects($this->any())
  150. ->method('getAttributeCode')
  151. ->willReturn('entity_id');
  152. $attributeMock->expects($this->any())
  153. ->method('getBackend')
  154. ->willReturn(
  155. $this->createMock(\Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend::class)
  156. );
  157. $this->eavConfigType = $this->createPartialMock(
  158. \Magento\Eav\Model\Entity\Type::class,
  159. ['getEntityIdField', 'getId', 'getEntityTable', '__wakeup']
  160. );
  161. $this->eavConfigType->expects($this->any())->method('getEntityIdField')->willReturn(false);
  162. $this->eavConfigType->expects($this->any())->method('getId')->willReturn(false);
  163. $this->eavConfigType->expects($this->any())->method('getEntityTable')->willReturn('customer_address_entity');
  164. $eavConfig = $this->createPartialMock(
  165. \Magento\Eav\Model\Config::class,
  166. ['getEntityType', 'getEntityAttributeCodes', 'getAttribute']
  167. );
  168. $eavConfig->expects($this->any())
  169. ->method('getEntityType')
  170. ->with('customer_address')
  171. ->willReturn($this->eavConfigType);
  172. $eavConfig->expects($this->any())
  173. ->method('getEntityAttributeCodes')
  174. ->with($this->eavConfigType)
  175. ->willReturn(
  176. [
  177. 'entity_type_id',
  178. 'attribute_set_id',
  179. 'created_at',
  180. 'updated_at',
  181. 'parent_id',
  182. 'increment_id',
  183. 'entity_id',
  184. ]
  185. );
  186. $eavConfig->expects($this->any())
  187. ->method('getAttribute')
  188. ->willReturnMap([
  189. [$this->eavConfigType, 'entity_type_id', $attributeMock],
  190. [$this->eavConfigType, 'attribute_set_id', $attributeMock],
  191. [$this->eavConfigType, 'created_at', $attributeMock],
  192. [$this->eavConfigType, 'updated_at', $attributeMock],
  193. [$this->eavConfigType, 'parent_id', $attributeMock],
  194. [$this->eavConfigType, 'increment_id', $attributeMock],
  195. [$this->eavConfigType, 'entity_id', $attributeMock],
  196. ]);
  197. return $eavConfig;
  198. }
  199. /**
  200. * Prepare validator mock object
  201. *
  202. * @return \Magento\Framework\Validator\Factory|\PHPUnit_Framework_MockObject_MockObject
  203. */
  204. protected function prepareValidatorFactory()
  205. {
  206. $validatorMock = $this->createPartialMock(\Magento\Framework\Validator::class, ['isValid']);
  207. $validatorMock->expects($this->any())
  208. ->method('isValid')
  209. ->willReturn(true);
  210. $validatorFactory = $this->createPartialMock(\Magento\Framework\Validator\Factory::class, ['createValidator']);
  211. $validatorFactory->expects($this->any())
  212. ->method('createValidator')
  213. ->with('customer_address', 'save')
  214. ->willReturn($validatorMock);
  215. return $validatorFactory;
  216. }
  217. /**
  218. * @return \Magento\Customer\Model\CustomerFactory|\PHPUnit_Framework_MockObject_MockObject
  219. */
  220. protected function prepareCustomerFactory()
  221. {
  222. $this->customerFactory = $this->createPartialMock(\Magento\Customer\Model\CustomerFactory::class, ['create']);
  223. return $this->customerFactory;
  224. }
  225. public function testGetType()
  226. {
  227. $this->assertSame($this->eavConfigType, $this->addressResource->getEntityType());
  228. }
  229. }
  230. /**
  231. * Class SubResourceModelAddress
  232. * Mock method getAttributeLoader
  233. * @package Magento\Customer\Test\Unit\Model\ResourceModel
  234. * @codingStandardsIgnoreStart
  235. */
  236. class SubResourceModelAddress extends \Magento\Customer\Model\ResourceModel\Address
  237. {
  238. protected $attributeLoader;
  239. /**
  240. * @param null $object
  241. * @return \Magento\Customer\Model\ResourceModel\Address|\Magento\Eav\Model\Entity\AbstractEntity
  242. */
  243. public function loadAllAttributes($object = null)
  244. {
  245. return $this->getAttributeLoader()->loadAllAttributes($this, $object);
  246. }
  247. /**
  248. * @param $attributeLoader
  249. */
  250. public function setAttributeLoader($attributeLoader)
  251. {
  252. $this->attributeLoader = $attributeLoader;
  253. }
  254. /**
  255. * @return \Magento\Eav\Model\Entity\AttributeLoaderInterface
  256. */
  257. protected function getAttributeLoader()
  258. {
  259. return $this->attributeLoader;
  260. }
  261. }
  262. // @codingStandardsIgnoreEnd