-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from courtney-miles/enhance/13/EnsureConsisten…
…tRecordValues #13 Ensure CSV rows have consistent number of fields.
- Loading branch information
Showing
8 changed files
with
272 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/** | ||
* Author: Courtney Miles | ||
* Date: 27/08/18 | ||
* Time: 6:32 PM | ||
*/ | ||
|
||
namespace MilesAsylum\Slurp\Extract\CsvFileExtractor; | ||
|
||
use Traversable; | ||
|
||
class MapIterator extends \IteratorIterator | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $headers; | ||
|
||
public function __construct(Traversable $iterator, array $headers) | ||
{ | ||
parent::__construct($iterator); | ||
$this->headers = $headers; | ||
} | ||
|
||
public function current() | ||
{ | ||
$record = parent::current(); | ||
|
||
return array_combine($this->headers, $record); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
/** | ||
* Author: Courtney Miles | ||
* Date: 27/08/18 | ||
* Time: 6:05 PM | ||
*/ | ||
|
||
namespace MilesAsylum\Slurp\Extract\CsvFileExtractor; | ||
|
||
use MilesAsylum\Slurp\Extract\Exception\ValueCountMismatchException; | ||
use Traversable; | ||
|
||
class VerifyValueCountIterator extends \IteratorIterator | ||
{ | ||
private $expectedValueCount; | ||
|
||
public function __construct(Traversable $iterator, int $expectedValueCount) | ||
{ | ||
parent::__construct($iterator); | ||
$this->expectedValueCount = $expectedValueCount; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* @throws ValueCountMismatchException | ||
*/ | ||
public function current() | ||
{ | ||
$record = parent::current(); | ||
|
||
if (count($record) !== $this->expectedValueCount) { | ||
$recordId = $this->key(); | ||
throw new ValueCountMismatchException( | ||
sprintf( | ||
'Record %s contained %s values where we expected %s.', | ||
$recordId, | ||
count($record), | ||
$this->expectedValueCount | ||
) | ||
); | ||
} | ||
|
||
return $record; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
/** | ||
* Author: Courtney Miles | ||
* Date: 27/08/18 | ||
* Time: 6:10 PM | ||
*/ | ||
|
||
namespace MilesAsylum\Slurp\Extract\Exception; | ||
|
||
interface ExceptionInterface | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
/** | ||
* Author: Courtney Miles | ||
* Date: 27/08/18 | ||
* Time: 6:10 PM | ||
*/ | ||
|
||
namespace MilesAsylum\Slurp\Extract\Exception; | ||
|
||
class ValueCountMismatchException extends \Exception implements ExceptionInterface | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
/** | ||
* Author: Courtney Miles | ||
* Date: 27/08/18 | ||
* Time: 6:47 PM | ||
*/ | ||
|
||
namespace MilesAsylum\Slurp\Tests\Slurp\Extract\CsvFileExtractor; | ||
|
||
use MilesAsylum\Slurp\Extract\CsvFileExtractor\MapIterator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class MapIteratorTest extends TestCase | ||
{ | ||
public function testMapHeaders() | ||
{ | ||
$mi = new MapIterator( | ||
new \ArrayIterator([[123, 234]]), | ||
['val_1', 'val_2'] | ||
); | ||
|
||
$mi->rewind(); | ||
|
||
$this->assertSame( | ||
['val_1' => 123, 'val_2' => 234], | ||
$mi->current() | ||
); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
tests/Slurp/Extract/CsvFileExtractor/VerifyValueCountIteratorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* Author: Courtney Miles | ||
* Date: 27/08/18 | ||
* Time: 6:53 PM | ||
*/ | ||
|
||
namespace MilesAsylum\Slurp\Tests\Slurp\Extract\CsvFileExtractor; | ||
|
||
use MilesAsylum\Slurp\Extract\CsvFileExtractor\VerifyValueCountIterator; | ||
use MilesAsylum\Slurp\Extract\Exception\ValueCountMismatchException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class VerifyValueCountIteratorTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider getValueCountMismatchTestData | ||
*/ | ||
public function testValueCountMismatch($values, $expectedCount) | ||
{ | ||
$this->expectException(ValueCountMismatchException::class); | ||
$this->expectExceptionMessage( | ||
sprintf( | ||
'Record 0 contained %s values where we expected %.', | ||
count($values), | ||
$expectedCount | ||
) | ||
); | ||
|
||
$iterator = new VerifyValueCountIterator( | ||
new \ArrayIterator( | ||
[$values] | ||
), | ||
$expectedCount | ||
); | ||
|
||
$iterator->rewind(); | ||
$iterator->current(); | ||
} | ||
|
||
public function getValueCountMismatchTestData() | ||
{ | ||
return [ | ||
[[123,234], 1], | ||
[[123,234], 3], | ||
]; | ||
} | ||
} |