Example #1
0
 /**
  * @return void
  */
 public function testGetColumns()
 {
     $columns = ['column_1', 'column_2'];
     $this->rowParserMock->expects($this->once())->method('getColumns')->willReturn($columns);
     $result = $this->import->getColumns();
     $this->assertEquals($columns, $result);
 }
Example #2
0
 /**
  * @param ReadInterface $file
  * @param int $websiteId
  * @param string $conditionShortName
  * @param string $conditionFullName
  * @param int $bunchSize
  * @return \Generator
  * @throws LocalizedException
  */
 public function getData(ReadInterface $file, $websiteId, $conditionShortName, $conditionFullName, $bunchSize = 5000)
 {
     $this->errors = [];
     $headers = $this->getHeaders($file);
     /** @var ColumnResolver $columnResolver */
     $columnResolver = $this->columnResolverFactory->create(['headers' => $headers]);
     $rowNumber = 1;
     $items = [];
     while (false !== ($csvLine = $file->readCsv())) {
         try {
             $rowNumber++;
             if (empty($csvLine)) {
                 continue;
             }
             $rowData = $this->rowParser->parse($csvLine, $rowNumber, $websiteId, $conditionShortName, $conditionFullName, $columnResolver);
             // protect from duplicate
             $hash = $this->dataHashGenerator->getHash($rowData);
             if (array_key_exists($hash, $this->uniqueHash)) {
                 throw new RowException(__('Duplicate Row #%1 (duplicates row #%2)', $rowNumber, $this->uniqueHash[$hash]));
             }
             $this->uniqueHash[$hash] = $rowNumber;
             $items[] = $rowData;
             if (count($items) === $bunchSize) {
                 (yield $items);
                 $items = [];
             }
         } catch (RowException $e) {
             $this->errors[] = $e->getMessage();
         }
     }
     if (count($items)) {
         (yield $items);
     }
 }
Example #3
0
 /**
  * @param $rowData
  * @param $conditionFullName
  * @param $rowNumber
  * @param $websiteId
  * @param $conditionShortName
  * @return array
  * @throws \Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\CSV\RowException
  */
 private function parse($rowData, $conditionFullName, $rowNumber, $websiteId, $conditionShortName, $columnValueMap)
 {
     $this->columnResolverMock->expects($this->any())->method('getColumnValue')->willReturnMap($columnValueMap);
     $result = $this->rowParser->parse($rowData, $rowNumber, $websiteId, $conditionShortName, $conditionFullName, $this->columnResolverMock);
     return $result;
 }