Skip to content

Commit 939ee67

Browse files
author
Oleksandr Gorkun
committed
MAGETWO-83426: [Performance] Customer Import check data does not complete
1 parent 9abfe75 commit 939ee67

File tree

2 files changed

+78
-51
lines changed
  • app/code/Magento/CustomerImportExport

2 files changed

+78
-51
lines changed

app/code/Magento/CustomerImportExport/Model/ResourceModel/Import/Customer/Storage.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,28 +208,24 @@ public function prepareCustomers(array $customersToFind)
208208
|| !array_key_exists($websiteId, $this->_customerIds[$email])
209209
) {
210210
//Only looking for customers we don't already have ID for.
211+
//We need unique identifiers.
211212
$uniqueKey = $email .'_' .$websiteId;
212213
$identifiers[$uniqueKey] = [
213214
'email' => $email,
214215
'website_id' => $websiteId
215216
];
217+
//Recording that we've searched for a customer.
218+
if (!array_key_exists($email, $this->_customerIds)) {
219+
$this->_customerIds[$email] = [];
220+
}
221+
$this->_customerIds[$email][$websiteId] = null;
216222
}
217223
}
218224
if (!$identifiers) {
219225
return;
220226
}
227+
221228
//Loading customers data.
222229
$this->loadCustomersData($identifiers);
223-
224-
//Adding customers that don't exist.
225-
foreach ($identifiers as $customerIdentity) {
226-
$email = $customerIdentity['email'];
227-
$websiteId = $customerIdentity['website_id'];
228-
if (!array_key_exists($email, $this->_customerIds)
229-
|| !array_key_exists($websiteId, $this->_customerIds[$email])
230-
) {
231-
$this->_customerIds[$email][$websiteId] = null;
232-
}
233-
}
234230
}
235231
}

app/code/Magento/CustomerImportExport/Test/Unit/Model/ResourceModel/Import/Customer/StorageTest.php

Lines changed: 71 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
*/
66
namespace Magento\CustomerImportExport\Test\Unit\Model\ResourceModel\Import\Customer;
77

8-
use Magento\Customer\Api\CustomerRepositoryInterface;
98
use Magento\CustomerImportExport\Model\ResourceModel\Import\Customer\Storage;
109
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
10+
use Magento\Customer\Model\ResourceModel\Customer\Collection;
1111
use Magento\Framework\DataObject;
12-
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Framework\DB\Select;
1313
use Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory;
14+
use Magento\ImportExport\Model\ResourceModel\CollectionByPagesIterator;
1415

1516
class StorageTest extends \PHPUnit_Framework_TestCase
1617
{
@@ -20,23 +21,59 @@ class StorageTest extends \PHPUnit_Framework_TestCase
2021
private $_model;
2122

2223
/**
23-
* @var CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
24+
* @var CollectionByPagesIterator|\PHPUnit_Framework_MockObject_MockObject
2425
*/
25-
private $customerRepositoryMock;
26+
private $iteratorMock;
27+
28+
/**
29+
* @var Collection|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
private $collectionMock;
2632

2733
protected function setUp()
2834
{
35+
$this->iteratorMock = $this->getMockBuilder(
36+
CollectionByPagesIterator::class
37+
)
38+
->disableOriginalConstructor()
39+
->getMock();
40+
/** @var \PHPUnit_Framework_MockObject_MockObject|CollectionByPagesIteratorFactory $iteratorFactoryMock */
41+
$iteratorFactoryMock = $this->getMockBuilder(
42+
CollectionByPagesIteratorFactory::class
43+
)
44+
->disableOriginalConstructor()
45+
->getMock();
46+
$iteratorFactoryMock->expects($this->any())
47+
->method('create')
48+
->willReturn($this->iteratorMock);
49+
$this->collectionMock = $this->getMockBuilder(Collection::class)
50+
->disableOriginalConstructor()
51+
->getMock();
52+
/** @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject $collectionFactoryMock */
53+
$collectionFactoryMock = $this->getMockBuilder(
54+
CollectionFactory::class
55+
)
56+
->disableOriginalConstructor()
57+
->getMock();
58+
$collectionFactoryMock->expects($this->any())
59+
->method('create')
60+
->willReturn($this->collectionMock);
61+
/** @var \PHPUnit_Framework_MockObject_MockObject $selectMock */
62+
$selectMock = $this->getMockBuilder(Select::class)
63+
->disableOriginalConstructor()
64+
->getMock();
65+
$selectMock->expects($this->any())
66+
->method('getPart')
67+
->with(Select::FROM)
68+
->willReturn(['e' => []]);
69+
$this->collectionMock->expects($this->any())
70+
->method('getSelect')
71+
->willReturn($selectMock);
72+
2973
$this->_model = new Storage(
30-
$this->getMockBuilder(CollectionFactory::class)
31-
->disableOriginalConstructor()
32-
->getMock(),
33-
$this->getMockBuilder(CollectionByPagesIteratorFactory::class)
34-
->disableOriginalConstructor()
35-
->getMock(),
36-
[],
37-
$this->customerRepositoryMock = $this->getMockBuilder(CustomerRepositoryInterface::class)
38-
->disableOriginalConstructor()
39-
->getMock()
74+
$collectionFactoryMock,
75+
$iteratorFactoryMock,
76+
[]
4077
);
4178
}
4279

@@ -53,20 +90,26 @@ public function testGetCustomerId()
5390
$nonExistingEmail = 'test1@magento.com';
5491
$nonExistingWebsiteId = 2;
5592

56-
/** @var \PHPUnit_Framework_MockObject_MockObject $customerMock */
57-
$customerMock = new DataObject([
58-
'id' => $existingId,
59-
'email' => $existingEmail,
60-
'website_id' => $existingWebsiteId
61-
]);
62-
$this->customerRepositoryMock->expects($this->at(0))
63-
->method('get')
64-
->with($existingEmail, $existingWebsiteId)
65-
->willReturn($customerMock);
66-
$this->customerRepositoryMock->expects($this->at(1))
67-
->method('get')
68-
->with($nonExistingEmail, $nonExistingWebsiteId)
69-
->willThrowException(new NoSuchEntityException());
93+
$this->iteratorMock->expects($this->at(0))
94+
->method('iterate')
95+
->willReturnCallback(
96+
function (...$args) use (
97+
$existingId,
98+
$existingEmail,
99+
$existingWebsiteId
100+
) {
101+
/** @var callable $callable */
102+
foreach ($args[2] as $callable) {
103+
$callable(
104+
new DataObject([
105+
'id' => $existingId,
106+
'email' => $existingEmail,
107+
'website_id' => $existingWebsiteId,
108+
])
109+
);
110+
}
111+
}
112+
);
70113

71114
$this->assertEquals(
72115
$existingId,
@@ -78,17 +121,5 @@ public function testGetCustomerId()
78121
$nonExistingWebsiteId
79122
)
80123
);
81-
//Checking one more time to see whether the repo will be used once
82-
//again.
83-
$this->assertEquals(
84-
$existingId,
85-
$this->_model->getCustomerId($existingEmail, $existingWebsiteId)
86-
);
87-
$this->assertFalse(
88-
$this->_model->getCustomerId(
89-
$nonExistingEmail,
90-
$nonExistingWebsiteId
91-
)
92-
);
93124
}
94125
}

0 commit comments

Comments
 (0)