Last active
December 20, 2015 04:49
-
-
Save samuelkordik/6073896 to your computer and use it in GitHub Desktop.
Function to load CSV file from fixtures directory and return it as an array; useful in Unit Testing to get a mock return value for a database operation. Code is totally cribbed from various places online and mashed together for my use.
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
<?php | |
public function getCSVData($file) { | |
$filename = __DIR__."/fixtures/$file.csv"; | |
if (!file_exists($filename)) throw new Exception("CSV file $filename doesn't exist."); | |
$csv = array_map("str_getcsv", file($filename, FILE_SKIP_EMPTY_LINES)); | |
$keys = array_shift($csv); | |
foreach ($csv as $i=>$row) { | |
$csv[$i] = array_combine($keys, $row); | |
} | |
return $csv; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment