diff options
-rwxr-xr-x | examples/extract.php | 2 | ||||
-rwxr-xr-x | examples/filtering.php | 2 | ||||
-rwxr-xr-x | examples/stream.php | 154 | ||||
-rwxr-xr-x | examples/table.php | 2 | ||||
-rwxr-xr-x | examples/writing.php | 2 | ||||
-rw-r--r-- | scrutinizer.yml | 2 | ||||
-rw-r--r-- | src/AbstractCsv.php | 60 | ||||
-rw-r--r-- | src/Config/StreamFilter.php | 63 | ||||
-rw-r--r-- | src/Writer.php | 32 | ||||
-rw-r--r-- | test/CsvTest.php | 48 | ||||
-rw-r--r-- | test/ReaderTest.php | 5 | ||||
-rw-r--r-- | test/WriterTest.php | 19 |
12 files changed, 265 insertions, 126 deletions
diff --git a/examples/extract.php b/examples/extract.php index a33c985..1d78b03 100755 --- a/examples/extract.php +++ b/examples/extract.php @@ -20,7 +20,7 @@ $res = $inputCsv->setOffset(800)->setLimit(25)->fetchAll(); <!doctype html> <html lang="fr"> <head> - <meta charset="<?=$inputCsv->getEncodingFrom()?>"> + <meta charset="utf-8"> <title>\League\Csv\Reader simple usage</title> <link rel="stylesheet" href="example.css"> </head> diff --git a/examples/filtering.php b/examples/filtering.php index ee4140c..8948bd5 100755 --- a/examples/filtering.php +++ b/examples/filtering.php @@ -40,7 +40,7 @@ $headers = $inputCsv->fetchOne(0); <!doctype html> <html lang="fr"> <head> - <meta charset="<?=$inputCsv->getEncodingFrom()?>"> + <meta charset="utf-8"> <title>\League\Csv\Reader filtering method</title> <link rel="stylesheet" href="example.css"> </head> diff --git a/examples/stream.php b/examples/stream.php index cc0249a..9dd9d47 100755 --- a/examples/stream.php +++ b/examples/stream.php @@ -9,70 +9,134 @@ use League\Csv\Reader; use League\Csv\Writer; use lib\FilterTranscode; -require '../vendor/autoload.php'; //load all the classes in dev composer install dev mode +require '../vendor/autoload.php'; //load all the necessary classes when using composer install in dev mode //you must register your class for it to be usable by the CSV Lib stream_filter_register(FilterTranscode::FILTER_NAME."*", "\lib\FilterTranscode"); +?> +<!doctype html> +<html lang="fr"> +<head> + <meta charset="utf-8"> + <title>League\Csv Stream Filter API example</title> + <link rel="stylesheet" href="example.css"> +</head> +<body> + +<p>Stream Filters can only be used if the <code><strong>isActiveStreamFilter</strong></code> method returns <code><strong>true</strong></code></p> +<h3>Using the Reader class</h3> + +<pre><code>$reader = Reader::createFromPath(__DIR__.'/data/prenoms.csv'); +if ($reader->isActiveStreamFilter()) { + $reader->appendStreamFilter(FilterTranscode::FILTER_NAME."iso-8859-1:utf-8"); + $reader->appendStreamFilter('string.toupper'); + $reader->appendStreamFilter('string.rot13'); +} +$reader->setDelimiter(';'); +$reader->setOffset(6); +$reader->setLimit(3); +$res = $reader->fetchAssoc(['Prenom', 'Occurences', 'Sexe', 'Annee']); +</code></pre> + +<p>the data is :</p> +<ol> +<li>transcoded by the Stream Filter from ISO-8859-1 to UTF-8</li> +<li>uppercased</li> +<li>rot13 transformed</li> +</ol> +<?php //BETWEEN fetch* call you CAN update/remove/add stream filter $reader = Reader::createFromPath(__DIR__.'/data/prenoms.csv'); -$reader->appendStreamFilter(FilterTranscode::FILTER_NAME."iso-8859-1:utf-8"); -$reader->appendStreamFilter('string.toupper'); -$reader->appendStreamFilter('string.rot13'); +if ($reader->isActiveStreamFilter()) { + $reader->appendStreamFilter(FilterTranscode::FILTER_NAME."iso-8859-1:utf-8"); + $reader->appendStreamFilter('string.toupper'); + $reader->appendStreamFilter('string.rot13'); +} $reader->setDelimiter(';'); $reader->setOffset(6); $reader->setLimit(3); $res = $reader->fetchAssoc(['Prenom', 'Occurences', 'Sexe', 'Annee']); -echo '<pre> -the data is : - - transcoded by the Stream Filter from ISO-8859-1 to UTF-8 - - uppercased - - rot13 transform -'; - var_dump($res); - -$reader->removeStreamFilter('string.toupper'); +?> +<p>Let's remove the <code><strong>string.toupper</strong></code> stream filter</p> +<pre><code>if ($reader->isActiveStreamFilter()) { + $reader->removeStreamFilter('string.toupper'); +} $reader->setOffset(6); $reader->setLimit(3); $res = $reader->fetchAssoc(['Prenom', 'Occurences', 'Sexe', 'Annee']); -echo 'the data is : - - transcoded by the Stream Filter from ISO-8859-1 to UTF-8 - - rot13 transform -'; - -var_dump($res); - -// because of the limited support for stream filters with the SplFileObject -// BETWEEN insert call **YOU CAN NOT UPDATE** stream filters +var_dump($res);</code></pre> -echo 'Using the Writer:' . PHP_EOL; -echo 'Filters can only be used with <code><strong>createFromPath</strong></code> method'.PHP_EOL; +<?php +if ($reader->isActiveStreamFilter()) { + $reader->removeStreamFilter('string.toupper'); +} +$reader->setOffset(6); +$reader->setLimit(3); +$res = $reader->fetchAssoc(['Prenom', 'Occurences', 'Sexe', 'Annee']); -$writer = Writer::createFromPath('/tmp/test.csv', 'w'); -$writer->appendStreamFilter('string.toupper'); +var_dump($res); +?> +<h3>Using the Writer class</h3> + +<p><strong>You can not add/remove/update stream filters between inserts calls</strong> +<pre><code>$writer = Writer::createFromPath('/tmp/test.csv', 'w'); +if ($writer->isActiveStreamFilter()) { + $writer->appendStreamFilter('string.toupper'); +} $writer->insertOne('je,suis,toto,le,héros'); -$writer->appendStreamFilter('string.rot13'); //this stream won't be apploed +</code></pre> +<?php +$writer = Writer::createFromPath('/tmp/test.csv', 'w'); +if ($writer->isActiveStreamFilter()) { + $writer->appendStreamFilter('string.toupper'); +} $writer->insertOne('je,suis,toto,le,héros'); - -echo '- the 2 first inserted rows are only uppercased'.PHP_EOL.PHP_EOL - .'To update the filters you need to - create a new Writer object with a different <code><strong>$open_mode</strong></code>'.PHP_EOL; - -$writer = Writer::createFromPath('/tmp/test.csv', 'a+'); -$writer->appendStreamFilter('string.toupper'); -$writer->appendStreamFilter(FilterTranscode::FILTER_NAME."iso-8859-1:utf-8"); -$writer->appendStreamFilter('string.rot13'); -$writer->removeStreamFilter(FilterTranscode::FILTER_NAME."iso-8859-1:utf-8"); +?> +<p>When the first insert call is done... the stream filter status is +frozen and can no longer be updated !! Any added row will be uppercased only no matter what.</p> +<?php +if ($writer->isActiveStreamFilter()) { + $writer->appendStreamFilter('string.rot13'); + $writer->removeStreamFilter('string.toupper'); +} $writer->insertOne('je,suis,toto,le,héros'); +?> + +<p>To update the filters you need to:</p> +<ol> +<li> create a new Writer object <em>don't forget to update the <code>$open_mode</code></em></li> +<li>apply the new stream filters</li> +</ol> + +<pre><code>$writer = $writer->newWriter('a+'); +if ($writer->isActiveStreamFilter()) { + $writer->appendStreamFilter('string.rot13'); + $writer->prependStreamFilter('string.strip_tags'); +} +$writer->insertAll([ + 'je,suis,toto,le,héros', + 'je,<strong>suis</strong>,toto,le,héros' +]); +echo $writer->newReader()->toHTML(); +</code></pre> -echo 'the following rows are : - - uppercased - - rot13 transform -'; - -$reader = $writer->newReader(); -$reader->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY); -var_dump($reader->fetchAll()); +<?php +$writer = $writer->newWriter('a+'); +if ($writer->isActiveStreamFilter()) { + $writer->appendStreamFilter('string.rot13'); + $writer->prependStreamFilter('string.strip_tags'); +} +$writer->insertAll([ + 'je,suis,toto,le,héros', + 'je,<strong>suis</strong>,toto,le,héros' +]); + +echo $writer->newReader()->toHTML(), PHP_EOL; +?> + +</body> +</html> diff --git a/examples/table.php b/examples/table.php index 5dbda3e..55e024e 100755 --- a/examples/table.php +++ b/examples/table.php @@ -14,7 +14,7 @@ $inputCsv->setEncodingFrom("iso-8859-15"); <!doctype html> <html lang="fr"> <head> - <meta charset="<?=$inputCsv->getEncodingFrom()?>"> + <meta charset="utf-8"> <title>Using the toHTML() method</title> <link rel="stylesheet" href="example.css"> </head> diff --git a/examples/writing.php b/examples/writing.php index 5be7716..185cfeb 100755 --- a/examples/writing.php +++ b/examples/writing.php @@ -29,7 +29,7 @@ $writer->insertAll($teams); <!doctype html> <html lang="fr"> <head> - <meta charset="<?=$writer->getEncodingFrom()?>"> + <meta charset="utf-8"> <title>Using the \League\Writer object</title> <link rel="stylesheet" href="example.css"> </head> diff --git a/scrutinizer.yml b/scrutinizer.yml index 67d0e4f..1c6931e 100644 --- a/scrutinizer.yml +++ b/scrutinizer.yml @@ -1,5 +1,5 @@ filter: - excluded_paths: [examples/*] + excluded_paths: [examples/*, test/*] checks: php: code_rating: true diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index bb1a7bb..32087ce 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -72,15 +72,11 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate */ public function __construct($path, $open_mode = 'r+') { - if (! $path instanceof SplFileInfo) { - $path = (string) $path; - $path = trim($path); - } ini_set("auto_detect_line_endings", '1'); - $this->path = $path; + $this->path = $this->normalizePath($path); $this->open_mode = strtolower($open_mode); - $this->initStreamFilter($path); + $this->initStreamFilter($this->path); } /** @@ -95,11 +91,23 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate /** * Create a {@link AbstractCsv} from a string * - * The path must be an SplFileInfo, or a SplFileObject, - * or an object that implements the `__toString` method, - * or a string + * The path can be: + * - an SplFileInfo, + * - a SplFileObject, + * - an object that implements the `__toString` method, + * - a string + * * BUT NOT a SplTempFileObject * + * ```php + *<?php + * $csv = new Reader::createFromPath('/path/to/file.csv', 'a+'); + * $csv = new Reader::createFromPath(new SplFileInfo('/path/to/file.csv')); + * $csv = new Reader::createFromPath(new SplFileObject('/path/to/file.csv'), 'rb'); + * + * ?> + * ``` + * * @param \SplFileInfo|\SplFileObject|object|string $path file path * @param string $open_mode the file open mode flag * @@ -124,6 +132,18 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate /** * Create a {@link AbstractCsv} from a SplFileObject * + * The path can be: + * - a SplFileObject, + * - a SplTempFileObject + * + * ```php + *<?php + * $csv = new Writer::createFromFileObject(new SplFileInfo('/path/to/file.csv')); + * $csv = new Writer::createFromFileObject(new SplTempFileObject); + * + * ?> + * ``` + * * @param SplFileObject $obj * * @return static @@ -159,6 +179,28 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate } /** + * Return a normalize path which could be a SplFileObject + * or a string path + * + * @param \SplFileInfo|object|string $path the filepath + * + * @return \SplFileObject|string + */ + protected function normalizePath($path) + { + if ($path instanceof SplFileObject) { + return $path; + } elseif ($path instanceof SplFileInfo) { + return $path->getPath().'/'.$path->getBasename(); + } + + $path = (string) $path; + $path = trim($path); + + return $path; + } + + /** * Create a {@link AbstractCsv} instance from another {@link AbstractCsv} object * * @param string $class_name the class to be instantiated diff --git a/src/Config/StreamFilter.php b/src/Config/StreamFilter.php index c46c970..90c2cff 100644 --- a/src/Config/StreamFilter.php +++ b/src/Config/StreamFilter.php @@ -15,7 +15,6 @@ namespace League\Csv\Config; use LogicException; use OutOfBoundsException; use SplFileInfo; -use SplTempFileObject; /** * A Trait to ease PHP Stream Filters manipulation @@ -56,24 +55,20 @@ trait StreamFilter * an object that implements the `__toString` method * a path to a file * - * @param \SplFileInfo|object|string $path The file path + * @param \SplFileObject|string $path The file path * * @return void */ protected function initStreamFilter($path) { - if ($path instanceof SplTempFileObject) { - $this->stream_real_path = null; - - return $this; - - } elseif ($path instanceof SplFileInfo) { - //$path->getRealPath() returns false for php stream wrapper - $path = $path->getPath().'/'.$path->getBasename(); + $this->stream_filters = []; + $this->stream_real_path = null; + if (! is_string($path)) { + return; } - $path = (string) $path; - $path = trim($path); + $this->stream_real_path = $path; + //if we are submitting a filter meta wrapper //we extract and inject the mode, the filter and the path if (preg_match( @@ -91,12 +86,8 @@ trait StreamFilter $this->stream_filter_mode = $mode; $this->stream_real_path = $matches['resource']; $this->stream_filters = explode('|', $matches['filters']); - - return $this; } - $this->stream_real_path = $path; - $this->stream_filters = []; } /** @@ -108,12 +99,22 @@ trait StreamFilter */ protected function checkStreamApiAvailability() { - if (is_null($this->stream_real_path)) { + if (! is_string($this->stream_real_path)) { throw new LogicException('The stream filter API can not be used'); } } /** + * Tells whether the stream filter capabilities can be used + * + * @return boolean + */ + public function isActiveStreamFilter() + { + return is_string($this->stream_real_path); + } + + /** * stream filter mode Setter * * Set the new Stream Filter mode and remove all @@ -121,9 +122,9 @@ trait StreamFilter * * @param integer $mode * - * @return self + * @return static * - * @throws \LogicException If the API can not be use + * @throws \OutOfBoundsException If the mode is invalid */ public function setStreamFilterMode($mode) { @@ -142,8 +143,6 @@ trait StreamFilter * stream filter mode getter * * @return integer - * - * @throws \LogicException If the API can not be use */ public function getStreamFilterMode() { @@ -158,8 +157,6 @@ trait StreamFilter * @param string $filter_name the stream filter name * * @return string - * - * @throws \LogicException If the API can not be use */ protected function sanitizeStreamFilter($filter_name) { @@ -174,9 +171,7 @@ trait StreamFilter * * @param string $filter_name a string or an object that implements the '__toString' method * - * @return self - * - * @throws \LogicException If the API can not be use + * @return static */ public function appendStreamFilter($filter_name) { @@ -191,9 +186,7 @@ trait StreamFilter * * @param string $filter_name a string or an object that implements the '__toString' method * - * @return self - * - * @throws \LogicException If the API can not be use + * @return static */ public function prependStreamFilter($filter_name) { @@ -209,8 +202,6 @@ trait StreamFilter * @param string $filter_name * * @return boolean - * - * @throws \LogicException If the API can not be use */ public function hasStreamFilter($filter_name) { @@ -224,9 +215,7 @@ trait StreamFilter * * @param string $filter_name * - * @return self - * - * @throws \LogicException If the API can not be use + * @return static */ public function removeStreamFilter($filter_name) { @@ -242,9 +231,7 @@ trait StreamFilter /** * Remove all registered stream filter * - * @return self - * - * @throws \LogicException If the API can not be use + * @return static */ public function clearStreamFilter() { @@ -258,8 +245,6 @@ trait StreamFilter * Return the filter path * * @return string - * - * @throws \LogicException If the API can not be use */ protected function getStreamFilterPath() { diff --git a/src/Writer.php b/src/Writer.php index 532f2f1..0ae9154 100644 --- a/src/Writer.php +++ b/src/Writer.php @@ -114,6 +114,16 @@ class Writer extends AbstractCsv } /** + * Tells whether the stream filter capabilities can be used + * + * @return boolean + */ + public function isActiveStreamFilter() + { + return parent::isActiveStreamFilter() && is_null($this->csv); + } + + /** * Format the row according to the null handling behavior * * @param array $row @@ -201,7 +211,7 @@ class Writer extends AbstractCsv /** * Is the submitted row valid * - * @param \Traversable|array|string $row + * @param string[]|string $row * * @return array * @@ -209,17 +219,10 @@ class Writer extends AbstractCsv */ protected function validateRow($row) { - //convert input string row into a proper array - if (self::isValidString($row)) { + if (! is_array($row)) { $row = str_getcsv((string) $row, $this->delimiter, $this->enclosure, $this->escape); } - if (!is_array($row)) { - throw new InvalidArgumentException( - 'the data provided must be convertible into arrays' - ); - } - foreach ($row as $value) { if (!$this->isConvertibleContent($value)) { throw new InvalidArgumentException( @@ -252,20 +255,13 @@ class Writer extends AbstractCsv * avoid loosing the cursor position * * @return SplFileObject - * - * @throws \RuntimeException If the file could not be created and/or opened */ protected function getCsv() { if (! is_null($this->csv)) { return $this->csv; - } elseif ($this->path instanceof SplFileObject) { - $this->csv = $this->path; - - return $this->csv; } - - $this->csv = new SplFileObject($this->getStreamFilterPath(), $this->open_mode); + $this->csv = $this->getIterator(); return $this->csv; } @@ -273,7 +269,7 @@ class Writer extends AbstractCsv /** * Add a new CSV row to the generated CSV * - * @param array|string $data a string, an array or an object implementing to '__toString' method + * @param string[]|string $data a string, an array or an object implementing to '__toString' method * * @return self * diff --git a/test/CsvTest.php b/test/CsvTest.php index 8957e2d..c912a18 100644 --- a/test/CsvTest.php +++ b/test/CsvTest.php @@ -55,6 +55,14 @@ class CsvTest extends PHPUnit_Framework_TestCase $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'; @@ -65,6 +73,7 @@ class CsvTest extends PHPUnit_Framework_TestCase /** * @expectedException InvalidArgumentException + * @expectedExceptionMessage an `SplTempFileObject` object does not contain a valid path */ public function testCreateFromPathWithSplTempFileObject() { @@ -81,6 +90,7 @@ class CsvTest extends PHPUnit_Framework_TestCase /** * @expectedException InvalidArgumentException + * @expectedExceptionMessage The delimiter must be a single character */ public function testDelimeter() { @@ -97,6 +107,7 @@ class CsvTest extends PHPUnit_Framework_TestCase /** * @expectedException InvalidArgumentException + * @expectedExceptionMessage `$nb_rows` must be a valid positive integer */ public function testDetectDelimiterWithInvalidRowLimit() { @@ -113,6 +124,7 @@ class CsvTest extends PHPUnit_Framework_TestCase /** * @expectedException RuntimeException + * @expectedExceptionMessage 'too many delimiters were found: `;`,`|`' */ public function testDetectDelimiterWithInconsistentCSV() { @@ -125,11 +137,12 @@ class CsvTest extends PHPUnit_Framework_TestCase $data->fputcsv(['toto', 'tata', 'tutu']); $csv = Writer::createFromFileObject($data); - $csv->detectDelimiter(5, ['toto', '|']); + $csv->detectDelimiter(5, ['|']); } /** * @expectedException InvalidArgumentException + * @expectedExceptionMessage The escape character must be a single character */ public function testEscape() { @@ -141,6 +154,7 @@ class CsvTest extends PHPUnit_Framework_TestCase /** * @expectedException InvalidArgumentException + * @expectedExceptionMessage The enclosure must be a single character */ public function testEnclosure() { @@ -152,6 +166,7 @@ class CsvTest extends PHPUnit_Framework_TestCase /** * @expectedException InvalidArgumentException + * @expectedExceptionMessage the submitted data must be a string or an object implementing the `__toString` method */ public function testCreateFromStringFromNotStringableObject() { @@ -160,6 +175,7 @@ class CsvTest extends PHPUnit_Framework_TestCase /** * @expectedException InvalidArgumentException + * @expectedExceptionMessage you should use a valid charset */ public function testEncoding() { @@ -187,6 +203,7 @@ class CsvTest extends PHPUnit_Framework_TestCase /** * @expectedException InvalidArgumentException + * @expectedExceptionMessage you should use a `SplFileObject` Constant */ public function testSetFlags() { @@ -265,22 +282,28 @@ EOF; public function testInitStreamFilter() { $filter = 'php://filter/write=string.rot13/resource='.__DIR__.'/foo.csv'; - $csv = Reader::createFromPath(new SplFileInfo($filter)); + $csv = Reader::createFromPath($filter); $this->assertTrue($csv->hasStreamFilter('string.rot13')); $this->assertSame(STREAM_FILTER_WRITE, $csv->getStreamFilterMode()); $filter = 'php://filter/read=string.toupper/resource='.__DIR__.'/foo.csv'; - $csv = Reader::createFromPath(new SplFileInfo($filter)); + $csv = Reader::createFromPath($filter); $this->assertTrue($csv->hasStreamFilter('string.toupper')); $this->assertSame(STREAM_FILTER_READ, $csv->getStreamFilterMode()); + } - $csv = new Reader(new SplFileInfo($filter)); - $this->assertSame(STREAM_FILTER_READ, $csv->getStreamFilterMode()); + /** + * @expectedException LogicException + * @expectedExceptionMessage The stream filter API can not be used + */ + public function testInitStreamFilterWithSplFileObject() + { + (new Reader(new SplFileObject(__DIR__.'/foo.csv')))->getStreamFilterMode(); } public function testappendStreamFilter() { - $csv = Reader::createFromPath(__DIR__.'/foo.csv'); + $csv = new Reader(__DIR__.'/foo.csv'); $csv->appendStreamFilter('string.toupper'); $csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY); foreach ($csv->getIterator() as $row) { @@ -290,22 +313,29 @@ EOF; /** * @expectedException LogicException + * @expectedExceptionMessage The stream filter API can not be used */ public function testFailedprependStreamFilter() { - Reader::createFromFileObject(new SplTempFileObject)->prependStreamFilter('string.toupper'); + $csv = new Reader(new SplTempFileObject); + $this->assertFalse($csv->isActiveStreamFilter()); + $csv->prependStreamFilter('string.toupper'); } /** * @expectedException LogicException + * @expectedExceptionMessage The stream filter API can not be used */ public function testFailedapppendStreamFilter() { - Reader::createFromFileObject(new SplTempFileObject)->appendStreamFilter('string.toupper'); + $csv = new Writer(new SplTempFileObject); + $this->assertFalse($csv->isActiveStreamFilter()); + $csv->appendStreamFilter('string.toupper'); } /** * @expectedException OutOfBoundsException + * @expectedExceptionMessage the $mode should be a valid `STREAM_FILTER_*` constant */ public function testaddMultipleStreamFilter() { @@ -336,7 +366,7 @@ EOF; public function testGetFilterPath() { - $csv = Writer::createFromPath(__DIR__.'/foo.csv'); + $csv = new Writer(__DIR__.'/foo.csv'); $csv->appendStreamFilter('string.rot13'); $csv->prependStreamFilter('string.toupper'); $this->assertFalse($csv->getIterator()->getRealPath()); diff --git a/test/ReaderTest.php b/test/ReaderTest.php index 3118427..7773d6e 100644 --- a/test/ReaderTest.php +++ b/test/ReaderTest.php @@ -31,6 +31,7 @@ class ReaderTest extends PHPUnit_Framework_TestCase /** * @expectedException \InvalidArgumentException + * @expectedExceptionMessage the limit must an integer greater or equals to -1 */ public function testSetLimit() { @@ -41,6 +42,7 @@ class ReaderTest extends PHPUnit_Framework_TestCase /** * @expectedException \InvalidArgumentException + * @expectedExceptionMessage the offset must be a positive integer or 0 */ public function testSetOffset() { @@ -171,6 +173,7 @@ class ReaderTest extends PHPUnit_Framework_TestCase /** * @expectedException InvalidArgumentException + * @expectedExceptionMessage The named keys should be unique strings Or integer */ public function testFetchAssocKeyFailure() { @@ -212,6 +215,7 @@ class ReaderTest extends PHPUnit_Framework_TestCase /** * @expectedException \InvalidArgumentException + * @expectedExceptionMessage the column index must be a positive integer or 0 */ public function testFetchColFailure() { @@ -220,6 +224,7 @@ class ReaderTest extends PHPUnit_Framework_TestCase /** * @expectedException \InvalidArgumentException + * @expectedExceptionMessage the offset must be a positive integer or 0 */ public function testfetchOne() { diff --git a/test/WriterTest.php b/test/WriterTest.php index 1be9075..7b514c6 100644 --- a/test/WriterTest.php +++ b/test/WriterTest.php @@ -30,6 +30,16 @@ class WriterTest extends PHPUnit_Framework_TestCase $csv = new SplFileObject(__DIR__.'/foo.csv', 'w'); $csv->setCsvControl(); $csv->fputcsv(["john", "doe", "john.doe@example.com"], ",", '"'); + $this->csv = null; + } + + public function testSupportsStreamFilter() + { + $csv = Writer::createFromPath(__DIR__.'/foo.csv'); + $this->assertTrue($csv->isActiveStreamFilter()); + $csv->appendStreamFilter('string.toupper'); + $csv->insertOne(['jane', 'doe', 'jane@example.com']); + $this->assertFalse($csv->isActiveStreamFilter()); } public function testInsert() @@ -49,6 +59,7 @@ class WriterTest extends PHPUnit_Framework_TestCase /** * @expectedException OutOfBoundsException + * @expectedExceptionMessage invalid value for null handling */ public function testSetterGetterNullBehavior() { @@ -103,6 +114,7 @@ class WriterTest extends PHPUnit_Framework_TestCase /** * @expectedException InvalidArgumentException + * @expectedExceptionMessage */ public function testInsertNullThrowsException() { @@ -112,6 +124,7 @@ class WriterTest extends PHPUnit_Framework_TestCase /** * @expectedException InvalidArgumentException + * @expectedExceptionMessage the column count must an integer greater or equals to -1 */ public function testColumsCountSetterGetter() { @@ -123,6 +136,7 @@ class WriterTest extends PHPUnit_Framework_TestCase /** * @expectedException RuntimeException + * @expectedExceptionMessage You are trying to add */ public function testColumsCountConsistency() { @@ -135,6 +149,7 @@ class WriterTest extends PHPUnit_Framework_TestCase /** * @expectedException RuntimeException + * @expectedExceptionMessage You are trying to add */ public function testAutoDetectColumnsCount() { @@ -146,7 +161,7 @@ class WriterTest extends PHPUnit_Framework_TestCase } /** - * @expectedException InvalidArgumentException + * @expectedException PHPUnit_Framework_Error */ public function testFailedInsertWithWrongData() { @@ -155,6 +170,7 @@ class WriterTest extends PHPUnit_Framework_TestCase /** * @expectedException InvalidArgumentException + * @expectedExceptionMessage the values are not convertible into strings */ public function testFailedInsertWithMultiDimensionArray() { @@ -181,6 +197,7 @@ class WriterTest extends PHPUnit_Framework_TestCase /** * @expectedException InvalidArgumentException + * @expectedExceptionMessage the provided data must be an array OR a \Traversable object */ public function testFailedSaveWithWrongType() { |