Skip to content

Commit c65b572

Browse files
author
Serhii Balko
committed
Merge remote-tracking branch 'origin/MC-40012' into 2.4-develop-pr48
2 parents 24cb7ac + 6404f8d commit c65b572

File tree

2 files changed

+109
-89
lines changed

2 files changed

+109
-89
lines changed

app/code/Magento/CustomerImportExport/Model/Export/Address.php

Lines changed: 77 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55
*/
66
namespace Magento\CustomerImportExport\Model\Export;
77

8+
use Magento\Customer\Model\ResourceModel\Address\Collection;
9+
use Magento\Customer\Model\ResourceModel\Address\CollectionFactory;
10+
use Magento\Eav\Model\Config;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\DB\Select;
13+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
14+
use Magento\ImportExport\Model\Export\Entity\AbstractEav;
15+
use Magento\ImportExport\Model\Export\Factory;
16+
use Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory;
17+
use Magento\Store\Model\StoreManagerInterface;
18+
819
/**
920
* Customer address export
1021
*
@@ -13,7 +24,7 @@
1324
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1425
* @since 100.0.2
1526
*/
16-
class Address extends \Magento\ImportExport\Model\Export\Entity\AbstractEav
27+
class Address extends AbstractEav
1728
{
1829
/**#@+
1930
* Permanent column names
@@ -93,7 +104,7 @@ class Address extends \Magento\ImportExport\Model\Export\Entity\AbstractEav
93104
/**
94105
* Customer addresses collection
95106
*
96-
* @var \Magento\Customer\Model\ResourceModel\Address\Collection
107+
* @var Collection
97108
*/
98109
protected $_addressCollection;
99110

@@ -118,31 +129,31 @@ class Address extends \Magento\ImportExport\Model\Export\Entity\AbstractEav
118129
*
119130
* @var array
120131
*/
121-
protected $_customers = [];
132+
protected $_customers;
122133

123134
/**
124-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
125-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
126-
* @param \Magento\ImportExport\Model\Export\Factory $collectionFactory
127-
* @param \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory
128-
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
129-
* @param \Magento\Eav\Model\Config $eavConfig
135+
* @param ScopeConfigInterface $scopeConfig
136+
* @param StoreManagerInterface $storeManager
137+
* @param Factory $collectionFactory
138+
* @param CollectionByPagesIteratorFactory $resourceColFactory
139+
* @param TimezoneInterface $localeDate
140+
* @param Config $eavConfig
130141
* @param \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerColFactory
131-
* @param \Magento\CustomerImportExport\Model\Export\CustomerFactory $eavCustomerFactory
132-
* @param \Magento\Customer\Model\ResourceModel\Address\CollectionFactory $addressColFactory
142+
* @param CustomerFactory $eavCustomerFactory
143+
* @param CollectionFactory $addressColFactory
133144
* @param array $data
134145
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
135146
*/
136147
public function __construct(
137-
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
138-
\Magento\Store\Model\StoreManagerInterface $storeManager,
139-
\Magento\ImportExport\Model\Export\Factory $collectionFactory,
140-
\Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory,
141-
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
142-
\Magento\Eav\Model\Config $eavConfig,
148+
ScopeConfigInterface $scopeConfig,
149+
StoreManagerInterface $storeManager,
150+
Factory $collectionFactory,
151+
CollectionByPagesIteratorFactory $resourceColFactory,
152+
TimezoneInterface $localeDate,
153+
Config $eavConfig,
143154
\Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerColFactory,
144-
\Magento\CustomerImportExport\Model\Export\CustomerFactory $eavCustomerFactory,
145-
\Magento\Customer\Model\ResourceModel\Address\CollectionFactory $addressColFactory,
155+
CustomerFactory $eavCustomerFactory,
156+
CollectionFactory $addressColFactory,
146157
array $data = []
147158
) {
148159
parent::__construct(
@@ -178,19 +189,20 @@ public function __construct(
178189
*/
179190
protected function _initCustomers()
180191
{
181-
if (empty($this->_customers)) {
192+
if ($this->_customers === null) {
193+
$this->_customers = [];
182194
// add customer default addresses column name to customer attribute mapping array
183195
$this->_customerCollection->addAttributeToSelect(self::$_defaultAddressAttributeMapping);
184196
// filter customer collection
185197
$this->_customerCollection = $this->_customerEntity->filterEntityCollection($this->_customerCollection);
186198

187-
$customers = [];
188-
$addCustomer = function (\Magento\Customer\Model\Customer $customer) use (&$customers) {
189-
$customers[$customer->getId()] = $customer->getData();
190-
};
199+
$selectIds = $this->_customerCollection->getAllIdsSql();
200+
$this->_customerCollection->setPageSize($this->_pageSize);
201+
$pageCount = $this->_customerCollection->getLastPageNumber();
191202

192-
$this->_byPagesIterator->iterate($this->_customerCollection, $this->_pageSize, [$addCustomer]);
193-
$this->_customers = $customers;
203+
for ($pageNum = 1; $pageNum <= $pageCount; $pageNum++) {
204+
$this->_customers += $this->loadCustomerData($selectIds, $pageNum);
205+
}
194206
}
195207

196208
return $this;
@@ -211,7 +223,7 @@ protected function _getHeaderColumns()
211223
/**
212224
* Get customers collection
213225
*
214-
* @return \Magento\Customer\Model\ResourceModel\Address\Collection
226+
* @return Collection
215227
*/
216228
protected function _getEntityCollection()
217229
{
@@ -227,7 +239,7 @@ public function export()
227239
{
228240
// skip and filter by customer address attributes
229241
$this->_prepareEntityCollection($this->_getEntityCollection());
230-
$this->_getEntityCollection()->setCustomerFilter(array_keys($this->_customers));
242+
$this->_getEntityCollection()->setCustomerFilter(array_keys($this->getCustomers()));
231243

232244
// prepare headers
233245
$this->getWriter()->setHeaderCols($this->_getHeaderColumns());
@@ -248,7 +260,7 @@ public function exportItem($item)
248260
$row = $this->_addAttributeValuesToRow($item);
249261

250262
/** @var $customer \Magento\Customer\Model\Customer */
251-
$customer = $this->_customers[$item->getParentId()];
263+
$customer = $this->getCustomers()[$item->getParentId()];
252264

253265
// Fill row with default address attributes values
254266
foreach (self::$_defaultAddressAttributeMapping as $columnName => $attributeCode) {
@@ -274,10 +286,8 @@ public function exportItem($item)
274286
*/
275287
public function setParameters(array $parameters)
276288
{
277-
// push filters from post into export customer model
289+
// push filters from post into export customer model
278290
$this->_customerEntity->setParameters($parameters);
279-
$this->_initCustomers();
280-
281291
return parent::setParameters($parameters);
282292
}
283293

@@ -290,4 +300,39 @@ public function getEntityTypeCode()
290300
{
291301
return $this->getAttributeCollection()->getEntityTypeCode();
292302
}
303+
304+
/**
305+
* Get Customers Data
306+
*
307+
* @return array
308+
*/
309+
private function getCustomers(): array
310+
{
311+
$this->_initCustomers();
312+
return $this->_customers;
313+
}
314+
315+
/**
316+
* Load Customers Data
317+
*
318+
* @param Select $selectIds
319+
* @param int $pageNum
320+
* @return array
321+
*/
322+
private function loadCustomerData(Select $selectIds, int $pageNum = 0): array
323+
{
324+
$select = $this->_customerCollection->getConnection()->select();
325+
$select->from(
326+
['customer' => $this->_customerCollection->getTable('customer_entity')],
327+
['entity_id', 'email', 'store_id', 'website_id', 'default_billing', 'default_shipping']
328+
)->where(
329+
'customer.entity_id IN (?)', $selectIds
330+
);
331+
332+
if ($pageNum > 0) {
333+
$select->limitPage($pageNum, $this->_pageSize);
334+
}
335+
336+
return $this->_customerCollection->getConnection()->fetchAssoc($select);
337+
}
293338
}

app/code/Magento/CustomerImportExport/Test/Unit/Model/Export/AddressTest.php

Lines changed: 32 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77

88
namespace Magento\CustomerImportExport\Test\Unit\Model\Export;
99

10-
use Magento\Customer\Model\AddressFactory;
11-
use Magento\Customer\Model\Config\Share;
12-
use Magento\Customer\Model\GroupFactory;
13-
use Magento\Customer\Model\ResourceModel\Customer;
10+
use Magento\Customer\Model\ResourceModel\Customer\Collection as CustomerCollection;
1411
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
1512
use Magento\CustomerImportExport\Model\Export\Address;
1613
use Magento\CustomerImportExport\Model\Export\CustomerFactory;
@@ -19,9 +16,10 @@
1916
use Magento\Eav\Model\Entity\TypeFactory;
2017
use Magento\Framework\App\Config\ScopeConfigInterface;
2118
use Magento\Framework\Data\Collection;
22-
use Magento\Framework\Data\Collection\AbstractDb;
2319
use Magento\Framework\Data\Collection\EntityFactory;
2420
use Magento\Framework\DataObject;
21+
use Magento\Framework\DB\Adapter\AdapterInterface;
22+
use Magento\Framework\DB\Select;
2523
use Magento\Framework\Model\AbstractModel;
2624
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
2725
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
@@ -30,7 +28,6 @@
3028
use Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory;
3129
use Magento\Store\Model\Store;
3230
use Magento\Store\Model\StoreManager;
33-
use PHPUnit\Framework\MockObject\MockObject;
3431
use PHPUnit\Framework\TestCase;
3532

3633
/**
@@ -82,7 +79,7 @@ class AddressTest extends TestCase
8279
/**
8380
* ObjectManager helper
8481
*
85-
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
82+
* @var ObjectManager
8683
*/
8784
protected $_objectManager;
8885

@@ -93,6 +90,9 @@ class AddressTest extends TestCase
9390
*/
9491
protected $_model;
9592

93+
/**
94+
* @inheritdoc
95+
*/
9696
protected function setUp(): void
9797
{
9898
$storeManager = $this->createMock(StoreManager::class);
@@ -119,6 +119,9 @@ protected function setUp(): void
119119
);
120120
}
121121

122+
/**
123+
* @inheritdoc
124+
*/
122125
protected function tearDown(): void
123126
{
124127
unset($this->_model);
@@ -132,8 +135,9 @@ protected function tearDown(): void
132135
*/
133136
protected function _getModelDependencies()
134137
{
135-
$translator = $this->createMock(\stdClass::class);
138+
$pageSize = 1;
136139

140+
$translator = $this->createMock(\stdClass::class);
137141
$entityFactory = $this->createMock(EntityFactory::class);
138142

139143
/** @var Collection|TestCase $attributeCollection */
@@ -167,34 +171,35 @@ protected function _getModelDependencies()
167171
$attributeCollection->addItem($attribute);
168172
}
169173

170-
$byPagesIterator = $this->getMockBuilder(\stdClass::class)->addMethods(['iterate'])
171-
->disableOriginalConstructor()
172-
->getMock();
173-
$byPagesIterator->expects(
174-
$this->once()
175-
)->method(
176-
'iterate'
177-
)->willReturnCallback(
178-
[$this, 'iterate']
179-
);
180-
181-
$customerCollection = $this->getMockBuilder(AbstractDb::class)
182-
->setMethods(['addAttributeToSelect'])
183-
->disableOriginalConstructor()
184-
->getMockForAbstractClass();
174+
$connection = $this->createMock(AdapterInterface::class);
175+
$customerCollection = $this->createMock(CustomerCollection::class);
176+
$customerCollection->method('getConnection')->willReturn($connection);
177+
$customerCollection->expects($this->once())->method('setPageSize')->with($pageSize);
178+
$customerCollection->method('getLastPageNumber')->willReturn(1);
179+
$allIdsSelect = $this->createMock(Select::class);
180+
$customerCollection->method('getAllIdsSql')->willReturn($allIdsSelect);
181+
182+
$customerSelect = $this->createMock(Select::class);
183+
$customerSelect->method('from')->willReturnSelf();
184+
$customerSelect->expects($this->once())
185+
->method('where')
186+
->with('customer.entity_id IN (?)', $allIdsSelect)
187+
->willReturnSelf();
188+
$customerSelect->expects($this->once())->method('limitPage')->with(1, $pageSize);
189+
$connection->method('select')->willReturn($customerSelect);
190+
$connection->method('fetchAssoc')->with($customerSelect)->willReturn([1 => $this->_customerData]);
185191

186192
$customerEntity = $this->getMockBuilder(\stdClass::class)
187193
->addMethods(['filterEntityCollection', 'setParameters'])
188194
->disableOriginalConstructor()
189195
->getMock();
190-
$customerEntity->expects($this->any())->method('filterEntityCollection')->willReturnArgument(0);
191-
$customerEntity->expects($this->any())->method('setParameters')->willReturnSelf();
196+
$customerEntity->method('filterEntityCollection')->willReturnArgument(0);
197+
$customerEntity->method('setParameters')->willReturnSelf();
192198

193199
$data = [
194200
'translator' => $translator,
195201
'attribute_collection' => $attributeCollection,
196-
'page_size' => 1,
197-
'collection_by_pages_iterator' => $byPagesIterator,
202+
'page_size' => $pageSize,
198203
'entity_type_id' => 1,
199204
'customer_collection' => $customerCollection,
200205
'customer_entity' => $customerEntity,
@@ -228,36 +233,6 @@ public function getWebsites($withDefault = false)
228233
return $websites;
229234
}
230235

231-
/**
232-
* Iterate stub
233-
*
234-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
235-
*
236-
* @param AbstractDb $collection
237-
* @param int $pageSize
238-
* @param array $callbacks
239-
*/
240-
public function iterate(AbstractDb $collection, $pageSize, array $callbacks)
241-
{
242-
$resource = $this->createPartialMock(Customer::class, ['getIdFieldName']);
243-
$resource->expects($this->any())->method('getIdFieldName')->willReturn('id');
244-
$arguments = [
245-
'data' => $this->_customerData,
246-
'resource' => $resource,
247-
$this->createMock(Share::class),
248-
$this->createMock(AddressFactory::class),
249-
$this->createMock(\Magento\Customer\Model\ResourceModel\Address\CollectionFactory::class),
250-
$this->createMock(GroupFactory::class),
251-
$this->createMock(\Magento\Customer\Model\AttributeFactory::class),
252-
];
253-
/** @var $customer \Magento\Customer\Model\Customer|MockObject */
254-
$customer = $this->_objectManager->getObject(\Magento\Customer\Model\Customer::class, $arguments);
255-
256-
foreach ($callbacks as $callback) {
257-
call_user_func($callback, $customer);
258-
}
259-
}
260-
261236
/**
262237
* Test for method exportItem()
263238
*

0 commit comments

Comments
 (0)