Skip to content

Commit

Permalink
throw error if column is not available (#23)
Browse files Browse the repository at this point in the history
Signed-off-by: rahul <rcsofttech85@gmail.com>
  • Loading branch information
rcsofttech85 committed Sep 18, 2023
1 parent 8049fa9 commit f42b83f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/CsvFileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,13 @@ private function replaceKeywordInRow(array &$row, string $keyword, string $repla
* @param string $keyword
* @param string $replace
* @return int
* @throws FileHandlerException
*/
private function replaceKeywordInColumn(array &$row, string $column, string $keyword, string $replace): int
{
if (!array_key_exists($column, $row)) {
throw new FileHandlerException("invalid column name");
}
$count = 0;

if ($keyword === $row[$column]) {
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/CsvFileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ protected function tearDown(): void
$this->csvFileHandler = null;
}

#[Test]
#[DataProvider("wrongColumnNameProvider")]
public function throwExceptionIfWrongColumnNameProvided(string $columnName): void
{
$this->expectException(FileHandlerException::class);
$this->expectExceptionMessage("invalid column name");
$this->csvFileHandler->findAndReplaceInCsv("movie.csv", "Twilight", "hello", $columnName);
}


#[Test]
public function findAndReplaceInCsvMethodShouldReplaceTextWithoutColumnOption(): void
Expand Down Expand Up @@ -202,4 +211,13 @@ public static function fileProvider(): iterable
yield [$file2];
yield [$file3];
}

/**
* @return iterable<array<string>>
*/
public static function wrongColumnNameProvider(): iterable
{
yield ["wrong"];
yield ["honey bee"];
}
}

0 comments on commit f42b83f

Please sign in to comment.