Skip to content

Commit f255fae

Browse files
committed
MC-20624: Automate MC-11459
1 parent 6f1a6f7 commit f255fae

File tree

3 files changed

+129
-83
lines changed

3 files changed

+129
-83
lines changed

dev/tests/integration/testsuite/Magento/Customer/_files/import_export/customers.php

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
$customers = [];
76

8-
$customer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
9-
\Magento\Customer\Model\Customer::class
10-
);
7+
use Magento\TestFramework\Helper\Bootstrap;
8+
use Magento\Framework\ObjectManagerInterface;
9+
use Magento\Customer\Model\Customer;
10+
use Magento\Framework\Registry;
11+
12+
/** @var $objectManager ObjectManagerInterface */
13+
$objectManager = Bootstrap::getObjectManager();
14+
15+
$customers = [];
16+
$customer = $objectManager->create(Customer::class);
1117

1218
$customer->setWebsiteId(
1319
1
14-
)->setEntityId(
15-
1
1620
)->setEntityTypeId(
1721
1
1822
)->setAttributeSetId(
@@ -40,13 +44,9 @@
4044
$customer->save();
4145
$customers[] = $customer;
4246

43-
$customer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
44-
\Magento\Customer\Model\Customer::class
45-
);
47+
$customer = $objectManager->create(Customer::class);
4648
$customer->setWebsiteId(
4749
1
48-
)->setEntityId(
49-
2
5050
)->setEntityTypeId(
5151
1
5252
)->setAttributeSetId(
@@ -74,13 +74,9 @@
7474
$customer->save();
7575
$customers[] = $customer;
7676

77-
$customer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
78-
\Magento\Customer\Model\Customer::class
79-
);
77+
$customer = $objectManager->create(Customer::class);
8078
$customer->setWebsiteId(
8179
1
82-
)->setEntityId(
83-
3
8480
)->setEntityTypeId(
8581
1
8682
)->setAttributeSetId(
@@ -108,9 +104,7 @@
108104
$customer->save();
109105
$customers[] = $customer;
110106

111-
/** @var $objectManager \Magento\TestFramework\ObjectManager */
112-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
113-
$objectManager->get(\Magento\Framework\Registry::class)
107+
$objectManager->get(Registry::class)
114108
->unregister('_fixture/Magento_ImportExport_Customer_Collection');
115-
$objectManager->get(\Magento\Framework\Registry::class)
109+
$objectManager->get(Registry::class)
116110
->register('_fixture/Magento_ImportExport_Customer_Collection', $customers);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Framework\ObjectManagerInterface;
9+
use Magento\Framework\Registry;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
use Magento\Customer\Model\Customer;
12+
13+
/** @var $objectManager ObjectManagerInterface */
14+
$objectManager = Bootstrap::getObjectManager();
15+
16+
/** @var $registry Registry */
17+
$registry = $objectManager->get(Registry::class);
18+
$registry->unregister('isSecureArea');
19+
$registry->register('isSecureArea', true);
20+
21+
/** @var $customer Customer */
22+
$customer = $objectManager->create(Customer::class);
23+
24+
$emailsToDelete = [
25+
'customer@example.com',
26+
'julie.worrell@example.com',
27+
'david.lamar@example.com',
28+
];
29+
foreach ($emailsToDelete as $email) {
30+
try {
31+
$customer->loadByEmail($email)->delete();
32+
} catch (\Exception $e) {
33+
}
34+
}
35+
$registry->unregister('isSecureArea');
36+
$registry->register('isSecureArea', false);

dev/tests/integration/testsuite/Magento/CustomerImportExport/Model/Export/CustomerTest.php

Lines changed: 79 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,61 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
/**
8-
* Test for customer export model
9-
*/
107
namespace Magento\CustomerImportExport\Model\Export;
118

9+
use Magento\Framework\Registry;
10+
use Magento\Customer\Model\Attribute;
11+
use Magento\ImportExport\Model\Export;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\Framework\ObjectManagerInterface;
14+
use Magento\Store\Model\StoreManagerInterface;
15+
use Magento\ImportExport\Model\Export\Adapter\Csv;
16+
use Magento\Customer\Model\Customer as CustomerModel;
17+
use Magento\CustomerImportExport\Model\Export\Customer;
18+
use Magento\Customer\Model\ResourceModel\Attribute\Collection;
19+
use Magento\Customer\Model\ResourceModel\Customer\Collection as CustomerCollection;
20+
21+
/**
22+
* Tests for customer export model.
23+
*/
1224
class CustomerTest extends \PHPUnit\Framework\TestCase
1325
{
1426
/**
15-
* @var \Magento\CustomerImportExport\Model\Export\Customer
27+
* @var Customer
1628
*/
1729
protected $_model;
1830

31+
/**
32+
* @var ObjectManagerInterface
33+
*/
34+
private $objectManager;
35+
36+
/**
37+
* @inheritdoc
38+
*/
1939
protected function setUp()
2040
{
21-
$this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
22-
\Magento\CustomerImportExport\Model\Export\Customer::class
23-
);
41+
$this->objectManager = Bootstrap::getObjectManager();
42+
$this->_model = $this->objectManager->create(Customer::class);
2443
}
2544

2645
/**
27-
* Test export method
46+
* Export "Customer Main File".
2847
*
2948
* @magentoDataFixture Magento/Customer/_files/import_export/customers.php
3049
*/
3150
public function testExport()
3251
{
3352
$expectedAttributes = [];
34-
/** @var $collection \Magento\Customer\Model\ResourceModel\Attribute\Collection */
35-
$collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
36-
\Magento\Customer\Model\ResourceModel\Attribute\Collection::class
37-
);
38-
/** @var $attribute \Magento\Customer\Model\Attribute */
53+
/** @var $collection Collection */
54+
$collection = $this->objectManager->create(Collection::class);
55+
/** @var $attribute Attribute */
3956
foreach ($collection as $attribute) {
4057
$expectedAttributes[] = $attribute->getAttributeCode();
4158
}
4259
$expectedAttributes = array_diff($expectedAttributes, $this->_model->getDisabledAttributes());
4360

44-
$this->_model->setWriter(
45-
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
46-
\Magento\ImportExport\Model\Export\Adapter\Csv::class
47-
)
48-
);
61+
$this->_model->setWriter($this->objectManager->get(Csv::class));
4962
$data = $this->_model->export();
5063
$this->assertNotEmpty($data);
5164

@@ -54,32 +67,45 @@ public function testExport()
5467
$this->assertEquals(
5568
count($expectedAttributes),
5669
count(array_intersect($expectedAttributes, $lines['header'])),
57-
'Expected attribute codes were not exported'
70+
'Expected attribute codes were not exported.'
5871
);
5972

60-
$this->assertNotEmpty($lines['data'], 'No data was exported');
61-
62-
/** @var $objectManager \Magento\TestFramework\ObjectManager */
63-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
64-
/** @var $customers \Magento\Customer\Model\Customer[] */
65-
$customers = $objectManager->get(
66-
\Magento\Framework\Registry::class
67-
)->registry(
68-
'_fixture/Magento_ImportExport_Customer_Collection'
69-
);
70-
foreach ($customers as $key => $customer) {
71-
foreach ($expectedAttributes as $code) {
72-
if (!in_array($code, $this->_model->getDisabledAttributes()) && isset($lines[$key][$code])) {
73-
$this->assertEquals(
74-
$customer->getData($code),
75-
$lines[$key][$code],
76-
'Attribute "' . $code . '" is not equal'
77-
);
73+
$this->assertNotEmpty($lines['data'], 'No data was exported.');
74+
75+
/** @var $customers CustomerModel[] */
76+
$customers = $this->objectManager->create(CustomerCollection::class)->getItems();
77+
foreach ($customers as $customer) {
78+
$data = $customer->getData();
79+
$exportData = $lines['data'][$data['email']];
80+
$exportData = $this->unsetDuplicateData($exportData);
81+
array_walk(
82+
$exportData,
83+
function (&$value) {
84+
if (is_string($value) && $value === '') {
85+
$value = null;
86+
}
7887
}
79-
}
88+
);
89+
90+
$this->assertArraySubset($exportData, $data);
8091
}
8192
}
8293

94+
/**
95+
* Unset non-useful or duplicate data from exported file data.
96+
*
97+
* @param array $data
98+
* @return array
99+
*/
100+
private function unsetDuplicateData(array $data): array
101+
{
102+
unset($data['_website']);
103+
unset($data['_store']);
104+
unset($data['password']);
105+
106+
return $data;
107+
}
108+
83109
/**
84110
* Test entity type code value
85111
*/
@@ -93,25 +119,22 @@ public function testGetEntityTypeCode()
93119
*/
94120
public function testGetAttributeCollection()
95121
{
96-
$this->assertInstanceOf(
97-
\Magento\Customer\Model\ResourceModel\Attribute\Collection::class,
98-
$this->_model->getAttributeCollection()
99-
);
122+
$this->assertInstanceOf(Collection::class, $this->_model->getAttributeCollection());
100123
}
101124

102125
/**
103126
* Test for method filterAttributeCollection()
104127
*/
105128
public function testFilterAttributeCollection()
106129
{
107-
/** @var $collection \Magento\Customer\Model\ResourceModel\Attribute\Collection */
130+
/** @var $collection Collection */
108131
$collection = $this->_model->getAttributeCollection();
109132
$collection = $this->_model->filterAttributeCollection($collection);
110133
/**
111134
* Check that disabled attributes is not existed in attribute collection
112135
*/
113136
$existedAttributes = [];
114-
/** @var $attribute \Magento\Customer\Model\Attribute */
137+
/** @var $attribute Attribute */
115138
foreach ($collection as $attribute) {
116139
$existedAttributes[] = $attribute->getAttributeCode();
117140
}
@@ -127,7 +150,7 @@ public function testFilterAttributeCollection()
127150
* Check that all overridden attributes were affected during filtering process
128151
*/
129152
$overriddenAttributes = $this->_model->getOverriddenAttributes();
130-
/** @var $attribute \Magento\Customer\Model\Attribute */
153+
/** @var $attribute Attribute */
131154
foreach ($collection as $attribute) {
132155
if (isset($overriddenAttributes[$attribute->getAttributeCode()])) {
133156
foreach ($overriddenAttributes[$attribute->getAttributeCode()] as $propertyKey => $property) {
@@ -150,47 +173,40 @@ public function testFilterEntityCollection()
150173
{
151174
$createdAtDate = '2038-01-01';
152175

153-
/** @var $objectManager \Magento\TestFramework\ObjectManager */
154-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
176+
/** @var $objectManager ObjectManagerInterface */
177+
$objectManager = $this->objectManager;
155178

156179
/**
157180
* Change created_at date of first customer for future filter test.
158181
*/
159-
$customers = $objectManager->get(
160-
\Magento\Framework\Registry::class
161-
)->registry(
162-
'_fixture/Magento_ImportExport_Customer_Collection'
163-
);
182+
$customers = $objectManager->get(Registry::class)
183+
->registry('_fixture/Magento_ImportExport_Customer_Collection');
164184
$customers[0]->setCreatedAt($createdAtDate);
165185
$customers[0]->save();
166186
/**
167187
* Change type of created_at attribute. In this case we have possibility to test date rage filter
168188
*/
169-
$attributeCollection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
170-
\Magento\Customer\Model\ResourceModel\Attribute\Collection::class
171-
);
189+
$attributeCollection = $this->objectManager->create(Collection::class);
172190
$attributeCollection->addFieldToFilter('attribute_code', 'created_at');
173-
/** @var $createdAtAttribute \Magento\Customer\Model\Attribute */
191+
/** @var $createdAtAttribute Attribute */
174192
$createdAtAttribute = $attributeCollection->getFirstItem();
175193
$createdAtAttribute->setBackendType('datetime');
176194
$createdAtAttribute->save();
177195
/**
178196
* Prepare filter.asd
179197
*/
180198
$parameters = [
181-
\Magento\ImportExport\Model\Export::FILTER_ELEMENT_GROUP => [
199+
Export::FILTER_ELEMENT_GROUP => [
182200
'email' => 'example.com',
183201
'created_at' => [$createdAtDate, ''],
184-
'store_id' => \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
185-
\Magento\Store\Model\StoreManagerInterface::class
186-
)->getStore()->getId()
202+
'store_id' => $this->objectManager->get(StoreManagerInterface::class)->getStore()->getId()
187203
]
188204
];
189205
$this->_model->setParameters($parameters);
190-
/** @var $customers \Magento\Customer\Model\ResourceModel\Customer\Collection */
206+
/** @var $customers Collection */
191207
$collection = $this->_model->filterEntityCollection(
192-
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
193-
\Magento\Customer\Model\ResourceModel\Customer\Collection::class
208+
$this->objectManager->create(
209+
CustomerCollection::class
194210
)
195211
);
196212
$collection->load();

0 commit comments

Comments
 (0)