/** * @dataProvider \g105b\phpcsv\TestHelper::data_randomFilePath */ public function testDeleteByReferenceRemovesExpectedRow($filePath) { TestHelper::createCsv($filePath, 10); $csv = new Csv($filePath); $rowThree = $csv->get(3); $allRows = $csv->getAll(); $this->assertContains($rowThree, $allRows); $csv->delete($rowThree); $allRowsAfterDelete = $csv->getAll(); $this->assertNotContains($rowThree, $allRowsAfterDelete); $searchResult = $csv->getAllBy("firstName", $rowThree["firstName"]); $this->assertNotContains($rowThree, $searchResult); }
/** * @dataProvider \g105b\phpcsv\TestHelper::data_randomFilePath * @expectedException \g105b\phpcsv\InvalidFieldException */ public function testGetAllByFieldThatDoesNotExist($filePath) { TestHelper::createCsv($filePath); $csv = new Csv($filePath); $csv->getAllBy("this-field-does-not-exist", "it's true!"); }
/** * @dataProvider \g105b\phpcsv\TestHelper::data_randomFilePath */ public function testCsvGetsAfterAdding($filePath) { $csv = new Csv($filePath); foreach ($this->details as $rowDetail) { $csv->add($rowDetail); } $microsoftRows = $csv->getAllBy("Company", "Microsoft"); $count = 0; foreach ($this->details as $rowDetail) { if ($rowDetail["Company"] === "Microsoft") { $count++; $this->assertContains($rowDetail, $microsoftRows); } } $this->assertCount($count, $microsoftRows); }
/** * @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); } }