8
8
9
9
use Magento \Customer \Api \CustomerRepositoryInterface ;
10
10
use Magento \Customer \Api \Data \CustomerInterface ;
11
+ use Magento \Customer \Model \Indexer \Processor ;
11
12
use Magento \Framework \App \Filesystem \DirectoryList ;
12
13
use Magento \Framework \Exception \NoSuchEntityException ;
13
- use Magento \ImportExport \Model \Import ;
14
+ use Magento \Framework \Filesystem \Directory \Write as DirectoryWrite ;
15
+ use Magento \Framework \Filesystem \File \WriteFactory ;
14
16
use Magento \Framework \Indexer \StateInterface ;
17
+ use Magento \Framework \ObjectManagerInterface ;
18
+ use Magento \ImportExport \Model \Import ;
19
+ use Magento \ImportExport \Model \Import \Source \CsvFactory ;
20
+ use Magento \TestFramework \Helper \Bootstrap ;
15
21
16
22
/**
17
- * Test for class \Magento\CustomerImportExport\Model\Import\ Customer which covers validation logic
23
+ * Test for class Customer which covers validation logic
18
24
*
19
25
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
20
26
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
@@ -24,44 +30,54 @@ class CustomerTest extends \PHPUnit\Framework\TestCase
24
30
/**
25
31
* Model object which used for tests
26
32
*
27
- * @var Customer|\PHPUnit\Framework\MockObject\MockObject
33
+ * @var ObjectManagerInterface
28
34
*/
29
- protected $ _model ;
35
+ private $ objectManager ;
30
36
31
37
/**
32
- * Customer data
38
+ * Model object which used for tests
33
39
*
40
+ * @var Customer&\PHPUnit\Framework\MockObject\MockObject
41
+ */
42
+ protected $ _model ;
43
+
44
+ /**
34
45
* @var array
35
46
*/
36
47
protected $ _customerData ;
37
48
38
49
/**
39
- * @var \Magento\Framework\Filesystem\Directory\Write
50
+ * @var DirectoryWrite
40
51
*/
41
52
protected $ directoryWrite ;
42
53
43
54
/**
44
- * @var \Magento\Customer\Model\Indexer\ Processor
55
+ * @var Processor
45
56
*/
46
57
private $ indexerProcessor ;
47
58
59
+ /**
60
+ * @var CsvFactory
61
+ */
62
+ private $ csvFactory ;
63
+
64
+ /**
65
+ * @var WriteFactory
66
+ */
67
+ private $ writeFactory ;
48
68
/**
49
69
* Create all necessary data for tests
50
70
*/
51
71
protected function setUp (): void
52
72
{
53
73
parent ::setUp ();
54
-
55
- $ this ->_model = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
56
- ->create (\Magento \CustomerImportExport \Model \Import \Customer::class);
74
+ $ this ->objectManager = Bootstrap::getObjectManager ();
75
+ $ this ->_model = $ this ->objectManager ->create (Customer::class);
57
76
$ this ->_model ->setParameters (['behavior ' => Import::BEHAVIOR_ADD_UPDATE ]);
58
- $ this ->indexerProcessor = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
59
- ->create (\Magento \Customer \Model \Indexer \Processor::class);
60
-
77
+ $ this ->indexerProcessor = $ this ->objectManager ->create (\Magento \Customer \Model \Indexer \Processor::class);
61
78
$ propertyAccessor = new \ReflectionProperty ($ this ->_model , 'errorMessageTemplates ' );
62
79
$ propertyAccessor ->setAccessible (true );
63
80
$ propertyAccessor ->setValue ($ this ->_model , []);
64
-
65
81
$ this ->_customerData = [
66
82
'firstname ' => 'Firstname ' ,
67
83
'lastname ' => 'Lastname ' ,
@@ -73,11 +89,10 @@ protected function setUp(): void
73
89
'website_id ' => 1 ,
74
90
'password ' => 'password ' ,
75
91
];
76
-
77
- $ filesystem = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
78
- ->create (\Magento \Framework \Filesystem::class);
79
- $ this ->directoryWrite = $ filesystem
80
- ->getDirectoryWrite (DirectoryList::ROOT );
92
+ $ filesystem = $ this ->objectManager ->create (\Magento \Framework \Filesystem::class);
93
+ $ this ->directoryWrite = $ filesystem ->getDirectoryWrite (DirectoryList::ROOT );
94
+ $ this ->writeFactory = $ this ->objectManager ->get (WriteFactory::class);
95
+ $ this ->csvFactory = $ this ->objectManager ->get (CsvFactory::class);
81
96
}
82
97
83
98
/**
@@ -149,6 +164,93 @@ public function testImportData()
149
164
);
150
165
}
151
166
167
+ /**
168
+ * Decompresses if gz compressed, stores in memory or temp file, and loads CSV adapter
169
+ *
170
+ * @param string $importData
171
+ * @return Import\AbstractSource
172
+ */
173
+ private function createImportAdapter (string $ importData )
174
+ {
175
+ if (0 === strncmp ("\x1f\x8b" , $ importData , 2 )) { // gz's magic string
176
+ // phpcs:ignore Magento2.Functions.DiscouragedFunction
177
+ $ importData = gzdecode ($ importData );
178
+ }
179
+ $ openedFile = $ this ->writeFactory ->create ('php://temp ' , '' , 'w ' );
180
+ $ openedFile ->write ($ importData );
181
+ unset($ importData );
182
+ $ directory = $ this ->directoryWrite ;
183
+ $ adapter = $ this ->csvFactory ->create (['directory ' => $ directory , 'file ' => $ openedFile ]);
184
+ return $ adapter ;
185
+ }
186
+
187
+ /**
188
+ * Test validateSource() and importData() and using same $ids between them
189
+ *
190
+ * @magentoDataFixture Magento/Customer/_files/import_export/customer.php
191
+ */
192
+ public function testValidateSourceAndImportSource ()
193
+ {
194
+ /** @var Import $import */
195
+ $ import = $ this ->objectManager ->create (Import::class);
196
+ $ importData = \file_get_contents (__DIR__ . '/_files/2k_customers.csv.gz ' );
197
+ $ source = $ this ->createImportAdapter ($ importData );
198
+ unset($ importData );
199
+ $ import ->setData ([
200
+ 'form_key ' => 'Ded3z8XBEaMWt3sH ' ,
201
+ 'entity ' => 'customer ' ,
202
+ 'behavior ' => 'add_update ' ,
203
+ 'validation_strategy ' => 'validation-stop-on-errors ' ,
204
+ 'allowed_error_count ' => '10 ' ,
205
+ '_import_field_separator ' => ', ' ,
206
+ '_import_multiple_value_separator ' => ', ' ,
207
+ '_import_empty_attribute_value_constant ' => '__EMPTY__VALUE__ ' ,
208
+ 'import_images_file_dir ' => '' ,
209
+ '_import_ids ' => '' ,
210
+ ]);
211
+ $ import ->validateSource ($ source );
212
+ $ ids = $ import ->getValidatedIds ();
213
+ $ errorAggregator = $ import ->getErrorAggregator ();
214
+ $ errorStrings = [];
215
+ foreach ($ errorAggregator ->getAllErrors () as $ error ) {
216
+ $ errorStrings [] = sprintf (
217
+ "Error: \nRowNumber: %s \nColumnName: %s \nCode: %s \nDescription: %s \nLevel: %s \nMessage: %s \n" ,
218
+ (string )$ error ->getRowNumber (),
219
+ $ error ->getColumnName (),
220
+ $ error ->getErrorCode (),
221
+ $ error ->getErrorDescription (),
222
+ $ error ->getErrorLevel (),
223
+ $ error ->getErrorMessage (),
224
+ );
225
+ }
226
+ if (!empty ($ errorStrings )) {
227
+ $ exceptionString = sprintf (
228
+ "Errors: \n%s \n" ,
229
+ implode ("\n" , $ errorStrings )
230
+ );
231
+ throw new \Exception ($ exceptionString );
232
+ }
233
+ $ this ->assertCount (20 , $ ids );
234
+ /** @var Import $import2 */
235
+ $ import2 = $ this ->objectManager ->create (Import::class);
236
+ $ import2 ->setData ([
237
+ 'form_key ' => 'DedGz8CNEaMWt3sH ' ,
238
+ 'entity ' => 'customer ' ,
239
+ 'behavior ' => 'add_update ' ,
240
+ 'validation_strategy ' => 'validation-stop-on-errors ' ,
241
+ 'allowed_error_count ' => '10 ' ,
242
+ '_import_field_separator ' => ', ' ,
243
+ '_import_multiple_value_separator ' => ', ' ,
244
+ '_import_empty_attribute_value_constant ' => '__EMPTY__VALUE__ ' ,
245
+ 'import_images_file_dir ' => '' ,
246
+ '_import_ids ' => implode (', ' , $ ids ),
247
+ ]);
248
+ $ this ->assertEmpty ($ import2 ->getValidatedIds ());
249
+ $ import2 ->importSource ();
250
+ $ createdItemsCount = $ import2 ->getCreatedItemsCount ();
251
+ $ this ->assertEquals (2000 , $ createdItemsCount );
252
+ }
253
+
152
254
/**
153
255
* Tests importData() method.
154
256
*
0 commit comments