click($selector); // Wait for file to be downloaded for ($i = 1; $i < 15; ++$i) { sleep(2); if (file_exists($file)) { return $file; } } // Timeout $this->fail("CSV {$file} was not downloaded."); } /** * Read CSV file and fire provided assertions. * * @param string $file * File path of CSV file. * @param array $checkColumns * Check first row of csv. * independent of index. * @param array $checkRows * Array of header and rows according to row index. * eg: array( * 1 => array( * // Row index 1 * // column name 'First Name', value 'Jones' * 'First Name' => 'Jones', * 'Last Name' => 'Franklin' * ), * 2 => array( * // Row index 2 * 'First Name' => 'Rajan', * 'Last Name' => 'mayekar' * ), * ); * @param int $rowCount * Count rows (excluding header row). * @param array $settings * Used for override settings. */ public function reviewCSV($file, $checkColumns = array(), $checkRows = array(), $rowCount = 0, $settings = array()) { // Check file exists before proceed. $this->assertTrue(($file && file_exists($file)), "Not able to locate {$file}."); // We are going to read downloaded file. $fd = fopen($file, 'r'); if (!$fd) { $this->fail("Could not read {$file}."); } // Default seperator ','. $fieldSeparator = !empty($settings['fieldSeparator']) ? $settings['fieldSeparator'] : ','; $allRows = array(); // Read header row. $headerRow = fgetcsv($fd, 0, $fieldSeparator); $allRows[] = $headerRow; // Read all other rows. while ($row = fgetcsv($fd, 0, $fieldSeparator)) { $allRows[] = $row; } // We have done with the CSV reading. fclose($fd); // Check header columns. if (!empty($checkColumns)) { foreach ($checkColumns as $column) { if (!in_array($column, $headerRow)) { $this->fail("Missing column {$column}."); } } } // Check row count, excluding header row. if ($rowCount && !($rowCount == (count($allRows) - 1))) { $this->fail("Mismatching row count"); } // Check all other rows. if (!empty($checkRows)) { foreach ($checkRows as $rowIndex => $row) { if ($rowIndex == 0) { // Skip checking header row, since we are already doing it above. continue; } foreach ($row as $column => $value) { $headerIndex = array_search($column, $headerRow); if ($headerIndex === FALSE) { $this->fail("Not able to locate column {$column} for row index {$rowIndex}."); } if (!isset($allRows[$rowIndex][$headerIndex]) || !($value == $allRows[$rowIndex][$headerIndex])) { $this->fail("Expected: {$value}, Got: {$allRows[$rowIndex][$headerIndex]}, for column {$column} for row index {$rowIndex}."); } } } } // Delete file, since we no longer need it. if (empty($settings['skipDeleteFile'])) { @unlink($file); } } }