PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-customer-import-export/Test/Unit/Model/ResourceModel/Import/CustomerComposite/DataTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 222 lines | 168 code | 15 blank | 39 comment | 2 complexity | 1f489324efd225cf8087a75bf1857aab MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Test class for \Magento\CustomerImportExport\Model\ResourceModel\Import\CustomerComposite\Data
  8. */
  9. namespace Magento\CustomerImportExport\Test\Unit\Model\ResourceModel\Import\CustomerComposite;
  10. use Magento\CustomerImportExport\Model\Import\Address;
  11. use Magento\CustomerImportExport\Model\Import\CustomerComposite;
  12. class DataTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * Array of customer attributes
  16. *
  17. * @var array
  18. */
  19. protected $_customerAttributes = ['customer_attribute1', 'customer_attribute2'];
  20. /**
  21. * Generate dependencies for model
  22. *
  23. * @param string $entityType
  24. * @param array $bunchData
  25. * @return array
  26. */
  27. protected function _getDependencies($entityType, $bunchData)
  28. {
  29. /** @var $statementMock \Magento\Framework\DB\Statement\Pdo\Mysql */
  30. $statementMock = $this->getMock(
  31. 'Magento\Framework\DB\Statement\Pdo\Mysql',
  32. ['setFetchMode', 'getIterator'],
  33. [],
  34. '',
  35. false
  36. );
  37. $statementMock->expects(
  38. $this->any()
  39. )->method(
  40. 'getIterator'
  41. )->will(
  42. $this->returnValue(new \ArrayIterator($bunchData))
  43. );
  44. /** @var $selectMock \Magento\Framework\DB\Select */
  45. $selectMock = $this->getMock('Magento\Framework\DB\Select', ['from', 'order'], [], '', false);
  46. $selectMock->expects($this->any())->method('from')->will($this->returnSelf());
  47. $selectMock->expects($this->any())->method('order')->will($this->returnSelf());
  48. /** @var $connectionMock \Magento\Framework\DB\Adapter\AdapterInterface */
  49. $connectionMock = $this->getMock(
  50. 'Magento\Framework\DB\Adapter\Pdo\Mysql',
  51. ['select', 'from', 'order', 'query'],
  52. [],
  53. '',
  54. false
  55. );
  56. $connectionMock->expects($this->any())->method('select')->will($this->returnValue($selectMock));
  57. $connectionMock->expects($this->any())->method('query')->will($this->returnValue($statementMock));
  58. /** @var $resourceModelMock \Magento\Framework\App\ResourceConnection */
  59. $resourceModelMock = $this->getMock(
  60. 'Magento\Framework\App\ResourceConnection',
  61. [],
  62. [],
  63. '',
  64. false
  65. );
  66. $resourceModelMock->expects($this->any())->method('getConnection')->will($this->returnValue($connectionMock));
  67. $data = ['resource' => $resourceModelMock, 'entity_type' => $entityType];
  68. if ($entityType == CustomerComposite::COMPONENT_ENTITY_ADDRESS) {
  69. $data['customer_attributes'] = $this->_customerAttributes;
  70. }
  71. return $data;
  72. }
  73. /**
  74. * @covers \Magento\CustomerImportExport\Model\ResourceModel\Import\CustomerComposite\Data::getNextBunch
  75. * @covers \Magento\CustomerImportExport\Model\ResourceModel\Import\CustomerComposite\Data::_prepareRow
  76. * @covers \Magento\CustomerImportExport\Model\ResourceModel\Import\CustomerComposite\Data::_prepareAddressRowData
  77. *
  78. * @dataProvider getNextBunchDataProvider
  79. * @param string $entityType
  80. * @param string $bunchData
  81. * @param array $expectedData
  82. */
  83. public function testGetNextBunch($entityType, $bunchData, $expectedData)
  84. {
  85. $dependencies = $this->_getDependencies($entityType, [[$bunchData]]);
  86. $resource = $dependencies['resource'];
  87. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  88. $jsonDecoderMock = $this->getMockBuilder('Magento\Framework\Json\DecoderInterface')
  89. ->disableOriginalConstructor()
  90. ->getMock();
  91. $jsonDecoderMock->expects($this->once())
  92. ->method('decode')
  93. ->willReturn(\Zend_Json::decode($bunchData));
  94. $jsonHelper = $helper->getObject(
  95. 'Magento\Framework\Json\Helper\Data',
  96. [
  97. 'jsonDecoder' => $jsonDecoderMock,
  98. ]
  99. );
  100. unset($dependencies['resource'], $dependencies['json_helper']);
  101. $contextMock = $this->getMock('\Magento\Framework\Model\ResourceModel\Db\Context', [], [], '', false);
  102. $contextMock->expects($this->once())->method('getResources')->willReturn($resource);
  103. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  104. $object = $objectManager->getObject(
  105. '\Magento\CustomerImportExport\Model\ResourceModel\Import\CustomerComposite\Data',
  106. [
  107. 'context' => $contextMock,
  108. 'jsonHelper' => $jsonHelper,
  109. 'arguments' => $dependencies,
  110. ]
  111. );
  112. $this->assertEquals($expectedData, $object->getNextBunch());
  113. }
  114. /**
  115. * Data provider of row data and expected result of getNextBunch() method
  116. *
  117. * @return array
  118. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  119. */
  120. public function getNextBunchDataProvider()
  121. {
  122. return [
  123. 'address entity' => [
  124. '$entityType' => CustomerComposite::COMPONENT_ENTITY_ADDRESS,
  125. '$bunchData' => \Zend_Json::encode(
  126. [
  127. [
  128. '_scope' => CustomerComposite::SCOPE_DEFAULT,
  129. Address::COLUMN_WEBSITE => 'website1',
  130. Address::COLUMN_EMAIL => 'email1',
  131. Address::COLUMN_ADDRESS_ID => null,
  132. CustomerComposite::COLUMN_DEFAULT_BILLING => 'value',
  133. CustomerComposite::COLUMN_DEFAULT_SHIPPING => 'value',
  134. 'customer_attribute1' => 'value',
  135. 'customer_attribute2' => 'value',
  136. CustomerComposite::COLUMN_ADDRESS_PREFIX . 'attribute1' => 'value',
  137. CustomerComposite::COLUMN_ADDRESS_PREFIX . 'attribute2' => 'value',
  138. ],
  139. ]
  140. ),
  141. '$expectedData' => [
  142. 0 => [
  143. Address::COLUMN_WEBSITE => 'website1',
  144. Address::COLUMN_EMAIL => 'email1',
  145. Address::COLUMN_ADDRESS_ID => null,
  146. CustomerComposite::COLUMN_DEFAULT_BILLING => 'value',
  147. CustomerComposite::COLUMN_DEFAULT_SHIPPING => 'value',
  148. 'attribute1' => 'value',
  149. 'attribute2' => 'value',
  150. ],
  151. ],
  152. ],
  153. 'customer entity default scope' => [
  154. '$entityType' => CustomerComposite::COMPONENT_ENTITY_CUSTOMER,
  155. '$bunchData' => \Zend_Json::encode(
  156. [
  157. [
  158. '_scope' => CustomerComposite::SCOPE_DEFAULT,
  159. Address::COLUMN_WEBSITE => 'website1',
  160. Address::COLUMN_EMAIL => 'email1',
  161. Address::COLUMN_ADDRESS_ID => null,
  162. CustomerComposite::COLUMN_DEFAULT_BILLING => 'value',
  163. CustomerComposite::COLUMN_DEFAULT_SHIPPING => 'value',
  164. 'customer_attribute1' => 'value',
  165. 'customer_attribute2' => 'value',
  166. CustomerComposite::COLUMN_ADDRESS_PREFIX . 'attribute1' => 'value',
  167. CustomerComposite::COLUMN_ADDRESS_PREFIX . 'attribute2' => 'value',
  168. ],
  169. ]
  170. ),
  171. '$expectedData' => [
  172. 0 => [
  173. Address::COLUMN_WEBSITE => 'website1',
  174. Address::COLUMN_EMAIL => 'email1',
  175. Address::COLUMN_ADDRESS_ID => null,
  176. CustomerComposite::COLUMN_DEFAULT_BILLING => 'value',
  177. CustomerComposite::COLUMN_DEFAULT_SHIPPING => 'value',
  178. 'customer_attribute1' => 'value',
  179. 'customer_attribute2' => 'value',
  180. CustomerComposite::COLUMN_ADDRESS_PREFIX . 'attribute1' => 'value',
  181. CustomerComposite::COLUMN_ADDRESS_PREFIX . 'attribute2' => 'value',
  182. ],
  183. ],
  184. ],
  185. 'customer entity address scope' => [
  186. '$entityType' => CustomerComposite::COMPONENT_ENTITY_CUSTOMER,
  187. '$bunchData' => \Zend_Json::encode(
  188. [
  189. [
  190. '_scope' => CustomerComposite::SCOPE_ADDRESS,
  191. Address::COLUMN_WEBSITE => 'website1',
  192. Address::COLUMN_EMAIL => 'email1',
  193. Address::COLUMN_ADDRESS_ID => null,
  194. CustomerComposite::COLUMN_DEFAULT_BILLING => 'value',
  195. CustomerComposite::COLUMN_DEFAULT_SHIPPING => 'value',
  196. 'customer_attribute1' => 'value',
  197. 'customer_attribute2' => 'value',
  198. CustomerComposite::COLUMN_ADDRESS_PREFIX . 'attribute1' => 'value',
  199. CustomerComposite::COLUMN_ADDRESS_PREFIX . 'attribute2' => 'value',
  200. ],
  201. ]
  202. ),
  203. '$expectedData' => [],
  204. ]
  205. ];
  206. }
  207. }