Skip to content

Commit

Permalink
#12: fix builder build csv reader with mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
proggeler committed Nov 4, 2022
1 parent c144cee commit 59a1100
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/ReaderBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace DMT\Import\Reader;

use DMT\Import\Reader\Decorators\Csv\ColumnMappingDecorator;
use DMT\Import\Reader\Decorators\Handler\GenericHandlerDecorator;
use DMT\Import\Reader\Decorators\Handler\ToSimpleXmlElementDecorator;
use DMT\Import\Reader\Exceptions\UnreadableException;
use DMT\Import\Reader\Handlers\CsvReaderHandler;
Expand Down Expand Up @@ -85,6 +86,7 @@ public function build(string $file, array $options): ReaderInterface
}

if ($handler instanceof CsvReaderHandler && isset($mapping)) {
$decorators[] = new GenericHandlerDecorator();
$decorators[] = new ColumnMappingDecorator($mapping);
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Integration/CsvReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use DMT\Import\Reader\Handlers\CsvReaderHandler;
use DMT\Import\Reader\Handlers\Sanitizers\TrimSanitizer;
use DMT\Import\Reader\Reader;
use DMT\Import\Reader\ReaderBuilder;
use DMT\Test\Import\Reader\Fixtures\Plane;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -96,4 +97,22 @@ public function testReadCsvToDataTransferObjects()
$this->assertTrue($this->logger->hasWarningThatContains('Skipped row 3'));
$this->assertSame(4, $row);
}

public function testCsvReaderFromBuilder()
{
$reader = (new ReaderBuilder())
->build(__DIR__ . '/../files/planes.csv', [
'trim' => ['.', TrimSanitizer::TRIM_RIGHT],
'mapping' => [
'col1' => 'type',
'col8' => 'year',
]
]);

foreach ($reader->read(1) as $plane) {
$this->assertInstanceOf(ArrayObject::class, $plane);
$this->assertObjectHasAttribute('type', $plane);
$this->assertObjectHasAttribute('year', $plane);
}
}
}

0 comments on commit 59a1100

Please sign in to comment.