4
4
* See COPYING.txt for license details.
5
5
*/
6
6
7
- /**
8
- * Test for customer export model
9
- */
10
7
namespace Magento \CustomerImportExport \Model \Export ;
11
8
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
+ */
12
24
class CustomerTest extends \PHPUnit \Framework \TestCase
13
25
{
14
26
/**
15
- * @var \Magento\CustomerImportExport\Model\Export\ Customer
27
+ * @var Customer
16
28
*/
17
29
protected $ _model ;
18
30
31
+ /**
32
+ * @var ObjectManagerInterface
33
+ */
34
+ private $ objectManager ;
35
+
36
+ /**
37
+ * @inheritdoc
38
+ */
19
39
protected function setUp ()
20
40
{
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);
24
43
}
25
44
26
45
/**
27
- * Test export method
46
+ * Export "Customer Main File".
28
47
*
29
48
* @magentoDataFixture Magento/Customer/_files/import_export/customers.php
30
49
*/
31
50
public function testExport ()
32
51
{
33
52
$ 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 */
39
56
foreach ($ collection as $ attribute ) {
40
57
$ expectedAttributes [] = $ attribute ->getAttributeCode ();
41
58
}
42
59
$ expectedAttributes = array_diff ($ expectedAttributes , $ this ->_model ->getDisabledAttributes ());
43
60
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));
49
62
$ data = $ this ->_model ->export ();
50
63
$ this ->assertNotEmpty ($ data );
51
64
@@ -54,32 +67,45 @@ public function testExport()
54
67
$ this ->assertEquals (
55
68
count ($ expectedAttributes ),
56
69
count (array_intersect ($ expectedAttributes , $ lines ['header ' ])),
57
- 'Expected attribute codes were not exported '
70
+ 'Expected attribute codes were not exported. '
58
71
);
59
72
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
+ }
78
87
}
79
- }
88
+ );
89
+
90
+ $ this ->assertArraySubset ($ exportData , $ data );
80
91
}
81
92
}
82
93
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
+
83
109
/**
84
110
* Test entity type code value
85
111
*/
@@ -93,25 +119,22 @@ public function testGetEntityTypeCode()
93
119
*/
94
120
public function testGetAttributeCollection ()
95
121
{
96
- $ this ->assertInstanceOf (
97
- \Magento \Customer \Model \ResourceModel \Attribute \Collection::class,
98
- $ this ->_model ->getAttributeCollection ()
99
- );
122
+ $ this ->assertInstanceOf (Collection::class, $ this ->_model ->getAttributeCollection ());
100
123
}
101
124
102
125
/**
103
126
* Test for method filterAttributeCollection()
104
127
*/
105
128
public function testFilterAttributeCollection ()
106
129
{
107
- /** @var $collection \Magento\Customer\Model\ResourceModel\Attribute\ Collection */
130
+ /** @var $collection Collection */
108
131
$ collection = $ this ->_model ->getAttributeCollection ();
109
132
$ collection = $ this ->_model ->filterAttributeCollection ($ collection );
110
133
/**
111
134
* Check that disabled attributes is not existed in attribute collection
112
135
*/
113
136
$ existedAttributes = [];
114
- /** @var $attribute \Magento\Customer\Model\ Attribute */
137
+ /** @var $attribute Attribute */
115
138
foreach ($ collection as $ attribute ) {
116
139
$ existedAttributes [] = $ attribute ->getAttributeCode ();
117
140
}
@@ -127,7 +150,7 @@ public function testFilterAttributeCollection()
127
150
* Check that all overridden attributes were affected during filtering process
128
151
*/
129
152
$ overriddenAttributes = $ this ->_model ->getOverriddenAttributes ();
130
- /** @var $attribute \Magento\Customer\Model\ Attribute */
153
+ /** @var $attribute Attribute */
131
154
foreach ($ collection as $ attribute ) {
132
155
if (isset ($ overriddenAttributes [$ attribute ->getAttributeCode ()])) {
133
156
foreach ($ overriddenAttributes [$ attribute ->getAttributeCode ()] as $ propertyKey => $ property ) {
@@ -150,47 +173,40 @@ public function testFilterEntityCollection()
150
173
{
151
174
$ createdAtDate = '2038-01-01 ' ;
152
175
153
- /** @var $objectManager \Magento\TestFramework\ObjectManager */
154
- $ objectManager = \ Magento \ TestFramework \ Helper \Bootstrap:: getObjectManager () ;
176
+ /** @var $objectManager ObjectManagerInterface */
177
+ $ objectManager = $ this -> objectManager ;
155
178
156
179
/**
157
180
* Change created_at date of first customer for future filter test.
158
181
*/
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 ' );
164
184
$ customers [0 ]->setCreatedAt ($ createdAtDate );
165
185
$ customers [0 ]->save ();
166
186
/**
167
187
* Change type of created_at attribute. In this case we have possibility to test date rage filter
168
188
*/
169
- $ attributeCollection = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
170
- \Magento \Customer \Model \ResourceModel \Attribute \Collection::class
171
- );
189
+ $ attributeCollection = $ this ->objectManager ->create (Collection::class);
172
190
$ attributeCollection ->addFieldToFilter ('attribute_code ' , 'created_at ' );
173
- /** @var $createdAtAttribute \Magento\Customer\Model\ Attribute */
191
+ /** @var $createdAtAttribute Attribute */
174
192
$ createdAtAttribute = $ attributeCollection ->getFirstItem ();
175
193
$ createdAtAttribute ->setBackendType ('datetime ' );
176
194
$ createdAtAttribute ->save ();
177
195
/**
178
196
* Prepare filter.asd
179
197
*/
180
198
$ parameters = [
181
- \ Magento \ ImportExport \ Model \ Export::FILTER_ELEMENT_GROUP => [
199
+ Export::FILTER_ELEMENT_GROUP => [
182
200
'email ' => 'example.com ' ,
183
201
'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 ()
187
203
]
188
204
];
189
205
$ this ->_model ->setParameters ($ parameters );
190
- /** @var $customers \Magento\Customer\Model\ResourceModel\Customer\ Collection */
206
+ /** @var $customers Collection */
191
207
$ 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
194
210
)
195
211
);
196
212
$ collection ->load ();
0 commit comments