/** * @dataProvider \g105b\phpcsv\TestHelper::data_randomFilePath */ public function testGetById($filePath) { $originalRows = TestHelper::createCsv($filePath); $headers = array_shift($originalRows); $csv = new Csv($filePath); $idField = "rowNum"; $rowToCheck = rand(1, count($originalRows) - 1); $csv->setIdField($idField); $result = $csv->getById($rowToCheck); $filteredRows = array_filter($originalRows, function ($row) use($headers, $rowToCheck, $idField) { $rowNumFieldIndex = array_search($idField, $headers); return $row[$rowNumFieldIndex] == $rowToCheck; }); // Reset the indices of the filtered array: $filteredRows = array_values($filteredRows); $expectedResult = $csv->toAssociative($filteredRows[0]); $this->assertCount(1, $filteredRows, 'There should only be one of the ID'); $this->assertEquals($expectedResult, $result); }
/** * @dataProvider \g105b\phpcsv\TestHelper::data_randomFilePath */ public function testIdFieldCase($filePath) { $idFieldArray = ["id", "ID", "Id"]; foreach ($idFieldArray as $idField) { $data = [[$idField => 1, "number" => "one"], [$idField => 2, "number" => "two"], [$idField => 3, "number" => "three"]]; if (!is_dir(dirname($filePath))) { mkdir(dirname($filePath), 0775, true); } $fh = fopen($filePath, "w"); fputcsv($fh, [$idField, "number"]); foreach ($data as $d) { fputcsv($fh, $d); } fclose($fh); $csv = new Csv($filePath); $row = $csv->getById(2); $this->assertEquals("two", $row["number"], print_r($row, true)); } }