Skip to content

Commit 76c9677

Browse files
committed
Merge remote-tracking branch 'origin/MC-35479' into 2.4-develop-pr34
2 parents e97624a + 2f496e2 commit 76c9677

File tree

3 files changed

+105
-147
lines changed
  • app/code/Magento/ImportExport
  • dev/tests/integration/testsuite/Magento/ImportExport/Model/Report

3 files changed

+105
-147
lines changed

app/code/Magento/ImportExport/Model/Report/Csv.php

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\ImportExport\Model\Report;
89

@@ -60,22 +61,16 @@ public function __construct(
6061
}
6162

6263
/**
63-
* @param string $originalFileName
64-
* @param ProcessingErrorAggregatorInterface $errorAggregator
65-
* @param bool $writeOnlyErrorItems
66-
* @return string
67-
* @throws \Magento\Framework\Exception\LocalizedException
64+
* @inheritDoc
6865
*/
6966
public function createReport(
7067
$originalFileName,
7168
ProcessingErrorAggregatorInterface $errorAggregator,
7269
$writeOnlyErrorItems = false
7370
) {
74-
$sourceCsv = $this->createSourceCsvModel($originalFileName);
75-
76-
$outputFileName = $this->generateOutputFileName($originalFileName);
77-
$outputCsv = $this->createOutputCsvModel($outputFileName);
71+
$outputCsv = $this->outputCsvFactory->create();
7872

73+
$sourceCsv = $this->createSourceCsvModel($originalFileName);
7974
$columnsName = $sourceCsv->getColNames();
8075
$columnsName[] = self::REPORT_ERROR_COLUMN_NAME;
8176
$outputCsv->setHeaderCols($columnsName);
@@ -88,10 +83,16 @@ public function createReport(
8883
}
8984
}
9085

86+
$directory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
87+
$outputFileName = $this->generateOutputFileName($originalFileName);
88+
$directory->writeFile(Import::IMPORT_HISTORY_DIR . $outputFileName, $outputCsv->getContents());
89+
9190
return $outputFileName;
9291
}
9392

9493
/**
94+
* Retrieve error messages
95+
*
9596
* @param int $rowNumber
9697
* @param ProcessingErrorAggregatorInterface $errorAggregator
9798
* @return string
@@ -112,16 +113,21 @@ public function retrieveErrorMessagesByRowNumber($rowNumber, ProcessingErrorAggr
112113
}
113114

114115
/**
116+
* Generate output filename based on source filename
117+
*
115118
* @param string $sourceFile
116119
* @return string
117120
*/
118121
protected function generateOutputFileName($sourceFile)
119122
{
123+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
120124
$fileName = basename($sourceFile, self::ERROR_REPORT_FILE_EXTENSION);
121125
return $fileName . self::ERROR_REPORT_FILE_SUFFIX . self::ERROR_REPORT_FILE_EXTENSION;
122126
}
123127

124128
/**
129+
* Create source CSV model
130+
*
125131
* @param string $sourceFile
126132
* @return \Magento\ImportExport\Model\Import\Source\Csv
127133
*/
@@ -135,18 +141,4 @@ protected function createSourceCsvModel($sourceFile)
135141
]
136142
);
137143
}
138-
139-
/**
140-
* @param string $outputFileName
141-
* @return \Magento\ImportExport\Model\Export\Adapter\Csv
142-
*/
143-
protected function createOutputCsvModel($outputFileName)
144-
{
145-
return $this->outputCsvFactory->create(
146-
[
147-
'destination' => Import::IMPORT_HISTORY_DIR . $outputFileName,
148-
'destinationDirectoryCode' => DirectoryList::VAR_DIR,
149-
]
150-
);
151-
}
152144
}

app/code/Magento/ImportExport/Test/Unit/Model/Report/CsvTest.php

Lines changed: 0 additions & 124 deletions
This file was deleted.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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\Report;
9+
10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\Filesystem;
12+
use Magento\ImportExport\Model\Import;
13+
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingError;
14+
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
17+
/**
18+
* @magentoAppIsolation enabled
19+
* @magentoDbIsolation enabled
20+
* @magentoAppArea adminhtml
21+
*/
22+
class CsvTest extends \PHPUnit\Framework\TestCase
23+
{
24+
/**
25+
* @var Filesystem\Directory\WriteInterface
26+
*/
27+
private $directory;
28+
29+
/**
30+
* @var Csv
31+
*/
32+
private $csvReport;
33+
34+
/**
35+
* @var string|null
36+
*/
37+
private $importFilePath;
38+
39+
/**
40+
* @var string|null
41+
*/
42+
private $reportPath;
43+
44+
/**
45+
* @inheritDoc
46+
*/
47+
protected function setUp(): void
48+
{
49+
$filesystem = Bootstrap::getObjectManager()->create(Filesystem::class);
50+
$this->directory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
51+
52+
$this->csvReport = Bootstrap::getObjectManager()->create(Csv::class);
53+
}
54+
/**
55+
* @inheritDoc
56+
*/
57+
protected function tearDown(): void
58+
{
59+
foreach ([$this->importFilePath, $this->reportPath] as $path) {
60+
if ($path && $this->directory->isExist($path)) {
61+
$this->directory->delete($path);
62+
}
63+
}
64+
}
65+
66+
/**
67+
* @return void
68+
*/
69+
public function testCreateReport()
70+
{
71+
$importData = <<<fileContent
72+
sku,store_view_code,name,price,product_type,attribute_set_code,weight
73+
simple1,,"simple 1",10,simple,Default,-5
74+
fileContent;
75+
$this->importFilePath = 'test_import.csv';
76+
$this->directory->writeFile($this->importFilePath, $importData);
77+
78+
$errorAggregator = Bootstrap::getObjectManager()->create(ProcessingErrorAggregatorInterface::class);
79+
$error = 'Value for \'weight\' attribute contains incorrect value';
80+
$errorAggregator->addError($error, ProcessingError::ERROR_LEVEL_CRITICAL, 1, 'weight', $error);
81+
82+
$outputFileName = $this->csvReport->createReport(
83+
$this->directory->getAbsolutePath($this->importFilePath),
84+
$errorAggregator
85+
);
86+
87+
$this->reportPath = Import::IMPORT_HISTORY_DIR . $outputFileName;
88+
$this->assertTrue($this->directory->isExist($this->reportPath), 'Report was not generated');
89+
}
90+
}

0 commit comments

Comments
 (0)