Skip to content

Instantly share code, notes, and snippets.

@samuelkordik
Last active December 20, 2015 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuelkordik/6073896 to your computer and use it in GitHub Desktop.
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.
<?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