66
77namespace Magento \AdvancedPricingImportExport \Model \Export ;
88
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 ;
159use Magento \AdvancedPricingImportExport \Model \Export \AdvancedPricing as ExportAdvancedPricing ;
10+ use Magento \AdvancedPricingImportExport \Model \Import \AdvancedPricing as ImportAdvancedPricing ;
1611use Magento \Catalog \Api \ProductRepositoryInterface ;
1712use 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 ;
1817use 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 ;
2118use 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 ;
2222
2323/**
2424 * Test for \Magento\AdvancedPricingImportExport\Model\Export\AdvancedPricing
@@ -41,6 +41,11 @@ class AdvancedPricingTest extends TestCase
4141 */
4242 protected $ fileSystem ;
4343
44+ /**
45+ * @var Write
46+ */
47+ private $ directory ;
48+
4449 // @codingStandardsIgnoreStart
4550 public static function setUpBeforeClass (): void
4651 {
@@ -64,6 +69,7 @@ protected function setUp(): void
6469
6570 $ this ->objectManager = Bootstrap::getObjectManager ();
6671 $ this ->fileSystem = $ this ->objectManager ->get (Filesystem::class);
72+ $ this ->directory = $ this ->fileSystem ->getDirectoryWrite (DirectoryList::VAR_IMPORT_EXPORT );
6773 $ this ->model = $ this ->objectManager ->create (ExportAdvancedPricing::class);
6874 }
6975
@@ -179,7 +185,7 @@ public function testExportImportOfAdvancedPricing(): void
179185 {
180186 $ simpleSku = 'simple ' ;
181187 $ secondSimpleSku = 'second_simple ' ;
182- $ csvfile = uniqid ('importexport_ ' ) . '.csv ' ;
188+ $ csvfile = $ this -> directory -> getAbsolutePath ( uniqid ('importexport_ ' ) . '.csv ' ) ;
183189 $ exportContent = $ this ->exportData ($ csvfile );
184190 $ this ->assertStringContainsString (
185191 \sprintf ('%s,"All Websites [USD]","ALL GROUPS",10.0000,3.00,Discount ' , $ secondSimpleSku ),
@@ -269,10 +275,7 @@ private function updateTierPriceDataInCsv(string $csvfile): void
269275 ],
270276 ];
271277
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 );
276279 }
277280
278281 /**
@@ -281,16 +284,15 @@ private function updateTierPriceDataInCsv(string $csvfile): void
281284 */
282285 private function exportData ($ csvFile )
283286 {
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 );
291290 $ exportContent = $ this ->model ->export ();
292291 $ this ->assertNotEmpty ($ exportContent );
293292
293+ $ driver = $ this ->directory ->getDriver ();
294+ $ driver ->filePutContents ($ this ->directory ->getAbsolutePath ($ csvFile ), $ exportContent );
295+
294296 return $ exportContent ;
295297 }
296298
@@ -301,12 +303,11 @@ private function importData($csvFile)
301303 {
302304 /** @var ImportAdvancedPricing $importModel */
303305 $ importModel = $ this ->objectManager ->create (ImportAdvancedPricing::class);
304- $ directory = $ this ->fileSystem ->getDirectoryWrite (DirectoryList::VAR_DIR );
305306 $ source = $ this ->objectManager ->create (
306307 ImportSourceCsv::class,
307308 [
308309 'file ' => $ csvFile ,
309- 'directory ' => $ directory
310+ 'directory ' => $ this -> directory
310311 ]
311312 );
312313 $ errors = $ importModel ->setParameters (
@@ -366,4 +367,24 @@ private function removeImportedProducts(array $skus): void
366367 $ registry ->unregister ('isSecureArea ' );
367368 $ registry ->register ('isSecureArea ' , false );
368369 }
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+ }
369390}
0 commit comments