diff options
author | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2015-02-09 09:52:29 +0100 |
---|---|---|
committer | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2015-02-09 09:52:29 +0100 |
commit | 801e8f1bc66556a2ca1112ae0ba37efffe72190f (patch) | |
tree | bcbce9475956378363daf561f9ba4beb749367a1 | |
parent | 03ff1538d062013db1e8f221357ffc23f75814f0 (diff) | |
download | csv-801e8f1bc66556a2ca1112ae0ba37efffe72190f.zip csv-801e8f1bc66556a2ca1112ae0ba37efffe72190f.tar.gz csv-801e8f1bc66556a2ca1112ae0ba37efffe72190f.tar.bz2 |
improve newline handling
-rw-r--r-- | src/AbstractCsv.php | 3 | ||||
-rw-r--r-- | src/Config/Factory.php | 10 | ||||
-rw-r--r-- | test/CsvTest.php | 206 | ||||
-rw-r--r-- | test/ReaderTest.php | 1 |
4 files changed, 4 insertions, 216 deletions
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index c09773c..db24a44 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -174,11 +174,12 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate protected function newInstance($class_name, $open_mode) { $csv = new $class_name($this->path, $open_mode); + $csv->bom = $this->bom; $csv->delimiter = $this->delimiter; $csv->enclosure = $this->enclosure; $csv->escape = $this->escape; $csv->encodingFrom = $this->encodingFrom; - $csv->bom = $this->bom; + $csv->newline = $this->newline; return $csv; } diff --git a/src/Config/Factory.php b/src/Config/Factory.php index c77ebc6..ecc39ac 100644 --- a/src/Config/Factory.php +++ b/src/Config/Factory.php @@ -97,18 +97,10 @@ trait Factory * @param string|object $str the string * @param string $newline the newline character * - * @throws \InvalidArgumentException If the data provided is invalid - * * @return static */ - public static function createFromString($str, $newline = PHP_EOL) + public static function createFromString($str, $newline = "\n") { - if (! self::isValidString($str)) { - throw new InvalidArgumentException( - 'the submitted data must be a string or an object implementing the `__toString` method' - ); - } - $file = new SplTempFileObject(); $file->fwrite(rtrim($str).$newline); diff --git a/test/CsvTest.php b/test/CsvTest.php index 2172cf2..ad64826 100644 --- a/test/CsvTest.php +++ b/test/CsvTest.php @@ -39,199 +39,6 @@ class CsvTest extends PHPUnit_Framework_TestCase $this->csv = null; } - public function testCreateFromPathWithFilePath() - { - $path = __DIR__.'/foo.csv'; - - $csv = Reader::createFromPath($path); - $this->assertSame($path, $csv->getIterator()->getRealPath()); - } - - public function testCreateFromPathWithFileObject() - { - $path = __DIR__.'/foo.csv'; - - $csv = Reader::createFromPath(new SplFileInfo($path)); - $this->assertSame($path, $csv->getIterator()->getRealPath()); - } - - public function testConstructorWithSplFileInfo() - { - $path = __DIR__.'/foo.csv'; - $csv = new Reader(new SplFileInfo($path)); - - $this->assertSame($path, $csv->getIterator()->getRealPath()); - } - - public function testCreateFromPathWithPHPWrapper() - { - $path = __DIR__.'/foo.csv'; - - $csv = Reader::createFromPath('php://filter/read=string.toupper/resource='.$path); - $this->assertFalse($csv->getIterator()->getRealPath()); - } - - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage an `SplTempFileObject` object does not contain a valid path - */ - public function testCreateFromPathWithSplTempFileObject() - { - Reader::createFromPath(new SplTempFileObject()); - } - - public function testCreateFromString() - { - $expected = "john,doe,john.doe@example.com".PHP_EOL - ."jane,doe,jane.doe@example.com".PHP_EOL; - $reader = Reader::createFromString($expected); - $this->assertInstanceof('League\Csv\Reader', $reader); - } - - /** - * @expectedException PHPUnit_Framework_Error - */ - public function testCreateFromStringThrowExceptionWithBadNewline() - { - $expected = "john,doe,john.doe@example.com".PHP_EOL - ."jane,doe,jane.doe@example.com".PHP_EOL; - Reader::createFromString($expected, new \StdClass); - } - - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage The delimiter must be a single character - */ - public function testDelimeter() - { - $this->csv->setDelimiter('o'); - $this->assertSame('o', $this->csv->getDelimiter()); - - $this->csv->setDelimiter('foo'); - } - - public function testDetectDelimiterList() - { - $this->assertSame([4 => ','], $this->csv->detectDelimiterList()); - } - - public function testBOMSettings() - { - $this->assertNull($this->csv->getOutputBOM()); - $this->csv->setOutputBOM(Reader::BOM_UTF8); - $this->assertSame(Reader::BOM_UTF8, $this->csv->getOutputBOM()); - $this->csv->setOutputBOM(); - $this->assertNull($this->csv->getOutputBOM()); - } - - public function testAddBOMSequences() - { - $this->csv->setOutputBOM(Reader::BOM_UTF8); - $expected = chr(239).chr(187).chr(191)."john,doe,john.doe@example.com".PHP_EOL - ."jane,doe,jane.doe@example.com".PHP_EOL; - $this->assertSame($expected, $this->csv->__toString()); - } - - public function testGetBomOnInputWithNoBOM() - { - $expected = "john,doe,john.doe@example.com".PHP_EOL - ."jane,doe,jane.doe@example.com".PHP_EOL; - $reader = Reader::createFromString($expected); - $this->assertEmpty($reader->getInputBOM()); - } - - public function testGetBomOnInputWithBOM() - { - $expected = "\x00\x00\xFE\xFFjohn,doe,john.doe@example.com".PHP_EOL - ."jane,doe,jane.doe@example.com".PHP_EOL; - $reader = Reader::createFromString($expected); - $this->assertSame(Reader::BOM_UTF32_BE, $reader->getInputBOM()); - } - - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage `$nb_rows` must be a valid positive integer - */ - public function testDetectDelimiterListWithInvalidRowLimit() - { - $this->csv->detectDelimiterList(-4); - } - - public function testDetectDelimiterListWithNoCSV() - { - $file = new SplTempFileObject(); - $file->fwrite("How are you today ?\nI'm doing fine thanks!"); - $csv = Writer::createFromFileObject($file); - $this->assertSame([], $csv->detectDelimiterList(5, ['toto', '|'])); - } - - public function testDetectDelimiterListWithInconsistentCSV() - { - $data = new SplTempFileObject(); - $data->setCsvControl(';'); - $data->fputcsv(['toto', 'tata', 'tutu']); - $data->setCsvControl('|'); - $data->fputcsv(['toto', 'tata', 'tutu']); - $data->fputcsv(['toto', 'tata', 'tutu']); - $data->fputcsv(['toto', 'tata', 'tutu']); - - $csv = Writer::createFromFileObject($data); - $this->assertSame([12 => '|', 4 => ';'], $csv->detectDelimiterList(5, ['|'])); - } - - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage The escape character must be a single character - */ - public function testEscape() - { - $this->csv->setEscape('o'); - $this->assertSame('o', $this->csv->getEscape()); - - $this->csv->setEscape('foo'); - } - - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage The enclosure must be a single character - */ - public function testEnclosure() - { - $this->csv->setEnclosure('o'); - $this->assertSame('o', $this->csv->getEnclosure()); - - $this->csv->setEnclosure('foo'); - } - - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage the submitted data must be a string or an object implementing the `__toString` method - */ - public function testCreateFromStringFromNotStringableObject() - { - Reader::createFromString(new DateTime()); - } - - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage you should use a valid charset - */ - public function testEncoding() - { - $expected = 'iso-8859-15'; - $this->csv->setEncodingFrom($expected); - $this->assertSame(strtoupper($expected), $this->csv->getEncodingFrom()); - - $this->csv->setEncodingFrom(''); - } - - public function testToString() - { - $expected = "john,doe,john.doe@example.com\n" - ."jane,doe,jane.doe@example.com".PHP_EOL; - $this->assertSame($expected, $this->csv->__toString()); - } - public function testIterator() { $this->csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY); @@ -240,19 +47,6 @@ class CsvTest extends PHPUnit_Framework_TestCase } } - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage you should use a `SplFileObject` Constant - */ - public function testSetFlags() - { - $this->csv->setFlags(SplFileObject::SKIP_EMPTY); - $this->assertSame(SplFileObject::SKIP_EMPTY, $this->csv->getFlags() & SplFileObject::SKIP_EMPTY); - $this->assertSame(SplFileObject::READ_CSV, $this->csv->getFlags() & SplFileObject::READ_CSV); - - $this->csv->setFlags(-3); - } - public function testToHTML() { $expected = <<<EOF diff --git a/test/ReaderTest.php b/test/ReaderTest.php index 5257a6b..9520ecb 100644 --- a/test/ReaderTest.php +++ b/test/ReaderTest.php @@ -27,6 +27,7 @@ class ReaderTest extends PHPUnit_Framework_TestCase } $this->csv = Reader::createFromFileObject($csv); + $this->csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY); } /** |