/** * @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 */ public function testUpdateRowWithMissingFields($filePath) { TestHelper::createCsv($filePath); $csv = new Csv($filePath); $csv->setIdField("rowNum"); $row = $csv->get(3); $newFirstName = "Updated-" . $row["firstName"]; $row["firstName"] = $newFirstName; $existingLastName = $row["lastName"]; unset($row["lastName"]); $updated = $csv->update($row); $this->assertTrue($updated); $row = $csv->get(3); $this->assertEquals($newFirstName, $row["firstName"]); $this->assertEquals($existingLastName, $row["lastName"]); }
/** * @dataProvider \g105b\phpcsv\TestHelper::data_randomFilePath */ public function testNewLine($filePath) { TestHelper::createCsv($filePath, 10); $csv = new Csv($filePath); $all = $csv->getAll(); $numberOfRows = count($all); $csv->setIdField("rowNum"); $headers = $csv->getHeaders(); $rowThatHasNewLine = rand(0, 9); $fieldThatHasQuotes = rand(0, count($headers) - 2); $headerName = $headers[$fieldThatHasQuotes]; $row = $csv->get($rowThatHasNewLine); $fieldValue = "New...\n...Line!"; $row[$headerName] = $fieldValue; $csv->updateRow($rowThatHasNewLine, $row); $all = $csv->getAll(true); $this->assertEquals($numberOfRows, count($all), 'Should have same number of rows after update'); $rowAfterUpdate = $csv->get($rowThatHasNewLine); $this->assertEquals($fieldValue, $rowAfterUpdate[$headerName]); }
/** * @dataProvider \g105b\phpcsv\TestHelper::data_randomFilePath */ public function testEmptyLine($filePath) { TestHelper::createCsv($filePath); // Force a few empty lines into the file by reading it as an array, // clearing 10 random lines, then writing the file again. $lines = file($filePath); $totalLinesIncludingEmptyAndHeaders = count($lines); // Generate 10 random keys: $emptyRowArray = array_rand($lines, 10); // Make sure none of them are the header row: foreach ($emptyRowArray as $i => $emptyRow) { if ($emptyRow == 0) { do { $emptyRow = array_rand($lines); } while (in_array($emptyRow, $emptyRowArray)); $emptyRowArray[$i] = $emptyRow; } } foreach ($emptyRowArray as $emptyRow) { $lines[$emptyRow] = "\n"; } // Write back the file. file_put_contents($filePath, implode("", $lines)); $csv = new Csv($filePath); $this->assertInstanceOf("\\g105b\\phpcsv\\Csv", $csv); $rowCount = 1; try { foreach ($csv as $rowNumber => $columns) { $rowCount++; $this->assertNotEmpty($columns); $this->assertNotEmpty($columns["firstName"]); } } catch (Exception $e) { die("WHAT>???????????????"); } $this->assertEquals($totalLinesIncludingEmptyAndHeaders - 10, $rowCount, "Should be 10 rows missing"); }
/** * @dataProvider \g105b\phpcsv\TestHelper::data_randomFilePath * @expectedException \g105b\phpcsv\InvalidPathException */ public function testConstructsWithDirectory($filePath) { TestHelper::createCsv($filePath, 1); $filePath = dirname($filePath); $csv = new Csv($filePath); }
/** * @dataProvider \g105b\phpcsv\TestHelper::data_randomFilePath */ public function testFileNotDeletedWhenExists($filePath) { TestHelper::createCsv($filePath, 10); $csv = new Csv($filePath); $csv->getAll(); $csv = null; $this->assertFileExists($filePath); }