6
6
7
7
namespace Magento \AdvancedPricingImportExport \Model \Export ;
8
8
9
- use Magento \Framework \App \Filesystem \DirectoryList ;
10
- use Magento \Framework \Exception \NoSuchEntityException ;
11
- use Magento \Framework \File \Csv ;
12
- use Magento \TestFramework \Indexer \TestCase ;
13
- use Magento \TestFramework \Helper \Bootstrap ;
14
- use Magento \Framework \Filesystem ;
15
9
use Magento \AdvancedPricingImportExport \Model \Export \AdvancedPricing as ExportAdvancedPricing ;
10
+ use Magento \AdvancedPricingImportExport \Model \Import \AdvancedPricing as ImportAdvancedPricing ;
16
11
use Magento \Catalog \Api \ProductRepositoryInterface ;
17
12
use Magento \Catalog \Model \Product ;
13
+ use Magento \Framework \App \Filesystem \DirectoryList ;
14
+ use Magento \Framework \Exception \NoSuchEntityException ;
15
+ use Magento \Framework \Filesystem ;
16
+ use Magento \Framework \Filesystem \Directory \Write ;
18
17
use Magento \ImportExport \Model \Export \Adapter \Csv as ExportAdapterCsv ;
19
- use Magento \AdvancedPricingImportExport \Model \Import \AdvancedPricing as ImportAdvancedPricing ;
20
- use Magento \ImportExport \Model \Import \Source \Csv as ImportSourceCsv ;
21
18
use Magento \ImportExport \Model \Import ;
19
+ use Magento \ImportExport \Model \Import \Source \Csv as ImportSourceCsv ;
20
+ use Magento \TestFramework \Helper \Bootstrap ;
21
+ use Magento \TestFramework \Indexer \TestCase ;
22
22
23
23
/**
24
24
* Test for \Magento\AdvancedPricingImportExport\Model\Export\AdvancedPricing
@@ -41,6 +41,11 @@ class AdvancedPricingTest extends TestCase
41
41
*/
42
42
protected $ fileSystem ;
43
43
44
+ /**
45
+ * @var Write
46
+ */
47
+ private $ directory ;
48
+
44
49
// @codingStandardsIgnoreStart
45
50
public static function setUpBeforeClass (): void
46
51
{
@@ -64,6 +69,7 @@ protected function setUp(): void
64
69
65
70
$ this ->objectManager = Bootstrap::getObjectManager ();
66
71
$ this ->fileSystem = $ this ->objectManager ->get (Filesystem::class);
72
+ $ this ->directory = $ this ->fileSystem ->getDirectoryWrite (DirectoryList::VAR_IMPORT_EXPORT );
67
73
$ this ->model = $ this ->objectManager ->create (ExportAdvancedPricing::class);
68
74
}
69
75
@@ -179,7 +185,7 @@ public function testExportImportOfAdvancedPricing(): void
179
185
{
180
186
$ simpleSku = 'simple ' ;
181
187
$ secondSimpleSku = 'second_simple ' ;
182
- $ csvfile = uniqid ('importexport_ ' ) . '.csv ' ;
188
+ $ csvfile = $ this -> directory -> getAbsolutePath ( uniqid ('importexport_ ' ) . '.csv ' ) ;
183
189
$ exportContent = $ this ->exportData ($ csvfile );
184
190
$ this ->assertStringContainsString (
185
191
\sprintf ('%s,"All Websites [USD]","ALL GROUPS",10.0000,3.00,Discount ' , $ secondSimpleSku ),
@@ -269,10 +275,7 @@ private function updateTierPriceDataInCsv(string $csvfile): void
269
275
],
270
276
];
271
277
272
- /** @var Csv $csv */
273
- $ csv = $ this ->objectManager ->get (Csv::class);
274
- $ varDirectory = $ this ->fileSystem ->getDirectoryWrite (DirectoryList::VAR_DIR );
275
- $ csv ->appendData ($ varDirectory ->getAbsolutePath ($ csvfile ), $ csvNewData );
278
+ $ this ->updateCsvFile ($ csvfile , $ csvNewData );
276
279
}
277
280
278
281
/**
@@ -281,16 +284,15 @@ private function updateTierPriceDataInCsv(string $csvfile): void
281
284
*/
282
285
private function exportData ($ csvFile )
283
286
{
284
- $ this ->model ->setWriter (
285
- Bootstrap::getObjectManager ()
286
- ->create (
287
- ExportAdapterCsv::class,
288
- ['fileSystem ' => $ this ->fileSystem , 'destination ' => $ csvFile ]
289
- )
290
- );
287
+ $ writer = Bootstrap::getObjectManager ()->create (ExportAdapterCsv::class, ['fileSystem ' => $ this ->fileSystem ]);
288
+
289
+ $ this ->model ->setWriter ($ writer );
291
290
$ exportContent = $ this ->model ->export ();
292
291
$ this ->assertNotEmpty ($ exportContent );
293
292
293
+ $ driver = $ this ->directory ->getDriver ();
294
+ $ driver ->filePutContents ($ this ->directory ->getAbsolutePath ($ csvFile ), $ exportContent );
295
+
294
296
return $ exportContent ;
295
297
}
296
298
@@ -301,12 +303,11 @@ private function importData($csvFile)
301
303
{
302
304
/** @var ImportAdvancedPricing $importModel */
303
305
$ importModel = $ this ->objectManager ->create (ImportAdvancedPricing::class);
304
- $ directory = $ this ->fileSystem ->getDirectoryWrite (DirectoryList::VAR_DIR );
305
306
$ source = $ this ->objectManager ->create (
306
307
ImportSourceCsv::class,
307
308
[
308
309
'file ' => $ csvFile ,
309
- 'directory ' => $ directory
310
+ 'directory ' => $ this -> directory
310
311
]
311
312
);
312
313
$ errors = $ importModel ->setParameters (
@@ -366,4 +367,24 @@ private function removeImportedProducts(array $skus): void
366
367
$ registry ->unregister ('isSecureArea ' );
367
368
$ registry ->register ('isSecureArea ' , false );
368
369
}
370
+
371
+ /**
372
+ * Appends csv data to the file
373
+ *
374
+ * @param string $filePath
375
+ * @param array $csv
376
+ * @return void
377
+ */
378
+ private function updateCsvFile (string $ filePath , array $ csv ): void
379
+ {
380
+ $ driver = $ this ->directory ->getDriver ();
381
+ $ driver ->deleteFile ($ filePath );
382
+ $ fileResource = $ driver ->fileOpen ($ filePath , 'w ' );
383
+
384
+ foreach ($ csv as $ dataRow ) {
385
+ $ driver ->filePutCsv ($ fileResource , $ dataRow );
386
+ }
387
+
388
+ $ driver ->fileClose ($ fileResource );
389
+ }
369
390
}
0 commit comments