diff --git a/Tests/MappedRow/MappedRowTest.php b/Tests/MappedRow/MappedRowTest.php index 21a572f..3844c54 100644 --- a/Tests/MappedRow/MappedRowTest.php +++ b/Tests/MappedRow/MappedRowTest.php @@ -23,7 +23,10 @@ public function testinvalidKey($key, $cols, $row) public function invalidKeyDataProvider() { - $cols = ['a', 'c']; + $cols = [ + 'a' => 0, + 'c' => 1, + ]; $mockRow = $this->createMock(Row::class); $mockRow ->method('getCellIterator') @@ -31,8 +34,16 @@ public function invalidKeyDataProvider() ; yield ['invalidCellKey', $cols, $mockRow]; - $cols = [455, 377]; - yield ['invalidCellKey', $cols, $mockRow]; + $numericCols = [ + 455 => 1, + 377 => 0, + ]; + $numericMockRow = $this->createMock(Row::class); + $numericMockRow + ->method('getCellIterator') + ->willReturn(new \ArrayIterator([$this->generateMockCell(4), $this->generateMockCell(5)])) + ; + yield ['invalidCellKey', $numericCols, $numericMockRow]; } /** diff --git a/Tests/SpreadSheetMappingTest.php b/Tests/SpreadSheetMappingTest.php index 70511d2..6430fe9 100644 --- a/Tests/SpreadSheetMappingTest.php +++ b/Tests/SpreadSheetMappingTest.php @@ -11,9 +11,13 @@ class SpreadSheetMappingTest extends TestCase { - public function testBasicIntegration() + /** + * @param string $filename + * @dataProvider basicIntegrationFilenameProvider + */ + public function testBasicIntegration($filename) { - $workbook = IOFactory::load('Tests/testData/simpleok.xlsx'); + $workbook = IOFactory::load($filename); $worksheet = $workbook->getSheet(0); $mapping = new Mapping(); @@ -31,6 +35,15 @@ public function testBasicIntegration() } } + /** + * @return \Generator|string[] + */ + public function basicIntegrationFilenameProvider() + { + yield ['Tests/testData/simpleok.xlsx']; + yield ['Tests/testData/gaps.xlsx']; + } + public function testOnlyColumns() { $workbook = IOFactory::load('Tests/testData/simpleok.xlsx'); diff --git a/Tests/testData/gaps.xlsx b/Tests/testData/gaps.xlsx new file mode 100644 index 0000000..bae2295 Binary files /dev/null and b/Tests/testData/gaps.xlsx differ diff --git a/src/MappedRow/MappedRow.php b/src/MappedRow/MappedRow.php index d17ef92..63c79df 100644 --- a/src/MappedRow/MappedRow.php +++ b/src/MappedRow/MappedRow.php @@ -2,8 +2,6 @@ namespace Dpiquet\SpreadsheetMapping\MappedRow; - -use Dpiquet\Mapping\Mapping; use Dpiquet\SpreadsheetMapping\MappedRow\Exception\InvalidKeyException; use Dpiquet\SpreadsheetMapping\MappedRow\Exception\MissingValueException; use PhpOffice\PhpSpreadsheet\Worksheet\Row; @@ -47,7 +45,7 @@ public function __construct(array $map, Row $row) * @throws MissingValueException */ public function get($key) { - if (!in_array($key, $this->map)) { + if (!array_key_exists($key, $this->map)) { throw new InvalidKeyException($key); }