From 003e98e8ff26694aa4e54e489c99a21cd9e36713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Sun, 21 Jan 2018 16:54:00 +0100 Subject: [PATCH] Added SaveTest for coverage of writing files --- tests/methods/SaveTest.php | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 tests/methods/SaveTest.php diff --git a/tests/methods/SaveTest.php b/tests/methods/SaveTest.php new file mode 100644 index 0000000..d871975 --- /dev/null +++ b/tests/methods/SaveTest.php @@ -0,0 +1,46 @@ +csv = new parseCSV(); + $this->csv->auto(__DIR__ . '/../example_files/single_column.csv'); + + // Remove last 2 lines to simplify comparison + unset($this->csv->data[2], $this->csv->data[3]); + + $temp_dir = str_replace("\\", '/', sys_get_temp_dir()); + if (substr($temp_dir, -1) != '/') { + // From the PHP.net documentation: + // This function does not always add trailing slash. This behaviour + // is inconsistent across systems. + $temp_dir .= '/'; + } + $this->temp_filename = $temp_dir . 'parsecsv_test_file.csv'; + } + + public function testSaveWithDefaultSettings() { + $expected = "SMS\r0444\r5555\r"; + $this->saveAndCompare($expected); + } + + public function testAllQuotes() { + $this->csv->enclose_all = true; + $expected = "\"SMS\"\r\"0444\"\r\"5555\"\r"; + $this->saveAndCompare($expected); + } + + private function saveAndCompare($expected) { + $this->csv->save($this->temp_filename); + $content = file_get_contents($this->temp_filename); + $this->assertEquals($expected, $content); + } +}