Beispiel #1
0
 /**
  * @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);
 }
Beispiel #2
0
 /**
  * @dataProvider \g105b\phpcsv\TestHelper::data_randomFilePath
  */
 public function testGetAllByField($filePath)
 {
     $originalRows = TestHelper::createCsv($filePath);
     $headers = array_shift($originalRows);
     $csv = new Csv($filePath);
     $result = $csv->getAllBy("gender", "M");
     $filteredRows = array_filter($originalRows, function ($row) use($headers) {
         $genderFieldNum = array_search("gender", $headers);
         return $row[$genderFieldNum] === "M";
     });
     foreach ($filteredRows as $i => $row) {
         $rowWithHeaders = $csv->toAssociative($row);
         $this->assertContains($rowWithHeaders, $result);
     }
 }