Skip to content

Commit 47a0598

Browse files
committed
Merge remote-tracking branch 'origin/MC-30148' into 2.4-develop-pr11
2 parents f5f7b0b + 84b2825 commit 47a0598

File tree

3 files changed

+92
-14
lines changed

3 files changed

+92
-14
lines changed

app/code/Magento/ImportExport/Model/Export/Adapter/Csv.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
*/
66
namespace Magento\ImportExport\Model\Export\Adapter;
77

8+
use Magento\Framework\Exception\LocalizedException;
9+
use Magento\Framework\Filesystem\File\Write;
10+
811
/**
912
* Export adapter csv.
1013
*
1114
* @api
1215
* @since 100.0.2
1316
*/
14-
class Csv extends \Magento\ImportExport\Model\Export\Adapter\AbstractAdapter
17+
class Csv extends AbstractAdapter
1518
{
1619
/**
1720
* Field delimiter.
@@ -30,28 +33,28 @@ class Csv extends \Magento\ImportExport\Model\Export\Adapter\AbstractAdapter
3033
/**
3134
* Source file handler.
3235
*
33-
* @var \Magento\Framework\Filesystem\File\Write
36+
* @var Write
3437
*/
3538
protected $_fileHandler;
3639

3740
/**
38-
* {@inheritdoc }
41+
* Object destructor
3942
*/
40-
public function __construct(\Magento\Framework\Filesystem $filesystem, $destination = null)
43+
public function __destruct()
4144
{
42-
register_shutdown_function([$this, 'destruct']);
43-
parent::__construct($filesystem, $destination);
45+
$this->destruct();
4446
}
4547

4648
/**
47-
* Object destructor.
49+
* Clean cached values
4850
*
4951
* @return void
5052
*/
5153
public function destruct()
5254
{
5355
if (is_object($this->_fileHandler)) {
5456
$this->_fileHandler->close();
57+
$this->_directoryHandle->delete($this->_destination);
5558
}
5659
}
5760

@@ -96,7 +99,7 @@ public function getFileExtension()
9699
public function setHeaderCols(array $headerColumns)
97100
{
98101
if (null !== $this->_headerCols) {
99-
throw new \Magento\Framework\Exception\LocalizedException(__('The header column names are already set.'));
102+
throw new LocalizedException(__('The header column names are already set.'));
100103
}
101104
if ($headerColumns) {
102105
foreach ($headerColumns as $columnName) {

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/AbstractProductExportImportTestCase.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Framework\App\Bootstrap;
99
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\ImportExport\Model\Export\Adapter\AbstractAdapter;
1011
use Magento\Store\Model\Store;
1112

1213
/**
@@ -62,6 +63,11 @@ abstract class AbstractProductExportImportTestCase extends \PHPUnit\Framework\Te
6263
'tax_class_id',
6364
];
6465

66+
/**
67+
* @var AbstractAdapter
68+
*/
69+
private $writer;
70+
6571
/**
6672
* @inheritdoc
6773
*/
@@ -367,7 +373,7 @@ protected function executeImportReplaceTest(
367373
* Export products in the system.
368374
*
369375
* @param \Magento\CatalogImportExport\Model\Export\Product|null $exportProduct
370-
* @return string Return exported file name
376+
* @return string Return exported file
371377
*/
372378
private function exportProducts(\Magento\CatalogImportExport\Model\Export\Product $exportProduct = null)
373379
{
@@ -376,12 +382,11 @@ private function exportProducts(\Magento\CatalogImportExport\Model\Export\Produc
376382
$exportProduct = $exportProduct ?: $this->objectManager->create(
377383
\Magento\CatalogImportExport\Model\Export\Product::class
378384
);
379-
$exportProduct->setWriter(
380-
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
381-
\Magento\ImportExport\Model\Export\Adapter\Csv::class,
382-
['fileSystem' => $this->fileSystem, 'destination' => $csvfile]
383-
)
385+
$this->writer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
386+
\Magento\ImportExport\Model\Export\Adapter\Csv::class,
387+
['fileSystem' => $this->fileSystem, 'destination' => $csvfile]
384388
);
389+
$exportProduct->setWriter($this->writer);
385390
$this->assertNotEmpty($exportProduct->export());
386391

387392
return $csvfile;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
namespace Magento\ImportExport\Model\Export\Adapter;
9+
10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\Filesystem;
12+
use Magento\Framework\ObjectManagerInterface;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* Test for Export adapter csv
18+
*/
19+
class CsvTest extends TestCase
20+
{
21+
/**
22+
* @var string Destination file name
23+
*/
24+
private $destination = 'destinationFile';
25+
26+
/**
27+
* @var ObjectManagerInterface
28+
*/
29+
private $objectManager;
30+
31+
/**
32+
* @var Csv
33+
*/
34+
private $csv;
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
protected function setUp()
40+
{
41+
parent::setUp();
42+
43+
$this->objectManager = Bootstrap::getObjectManager();
44+
$this->csv = $this->objectManager->create(
45+
Csv::class,
46+
['destination' => $this->destination]
47+
);
48+
}
49+
50+
/**
51+
* Test to destruct export adapter
52+
*/
53+
public function testDestruct(): void
54+
{
55+
/** @var Filesystem $fileSystem */
56+
$fileSystem = $this->objectManager->get(Filesystem::class);
57+
$directoryHandle = $fileSystem->getDirectoryRead(DirectoryList::VAR_DIR);
58+
/** Assert that the destination file is present after construct */
59+
$this->assertFileExists(
60+
$directoryHandle->getAbsolutePath($this->destination),
61+
'The destination file was\'t created after construct'
62+
);
63+
/** Assert that the destination file was removed after destruct */
64+
$this->csv = null;
65+
$this->assertFileNotExists(
66+
$directoryHandle->getAbsolutePath($this->destination),
67+
'The destination file was\'t removed after destruct'
68+
);
69+
}
70+
}

0 commit comments

Comments
 (0)