summaryrefslogtreecommitdiffstats
path: root/test/StreamFilterTest.php
diff options
context:
space:
mode:
authorIgnace Nyamagana Butera <nyamsprod@gmail.com>2015-02-11 14:25:03 +0100
committerIgnace Nyamagana Butera <nyamsprod@gmail.com>2015-02-11 14:25:03 +0100
commit450ff0ad3bfd79ff93a235bba990d508b04cd05b (patch)
treeb02d025be9eb1f9e45ae589d63410dbe01a3528f /test/StreamFilterTest.php
parentc8a4abc0d9b04ef5db83068a8ba834a61af7e01e (diff)
downloadcsv-450ff0ad3bfd79ff93a235bba990d508b04cd05b.zip
csv-450ff0ad3bfd79ff93a235bba990d508b04cd05b.tar.gz
csv-450ff0ad3bfd79ff93a235bba990d508b04cd05b.tar.bz2
output methods improved
Diffstat (limited to 'test/StreamFilterTest.php')
-rw-r--r--test/StreamFilterTest.php108
1 files changed, 108 insertions, 0 deletions
diff --git a/test/StreamFilterTest.php b/test/StreamFilterTest.php
new file mode 100644
index 0000000..d8c2162
--- /dev/null
+++ b/test/StreamFilterTest.php
@@ -0,0 +1,108 @@
+<?php
+
+namespace League\Csv\test;
+
+use League\Csv\Reader;
+use League\Csv\Writer;
+use PHPUnit_Framework_TestCase;
+use SplFileObject;
+use SplTempFileObject;
+
+/**
+ * @group csv
+ */
+class StreamFilterTest extends PHPUnit_Framework_TestCase
+{
+ public function testInitStreamFilter()
+ {
+ $filter = 'php://filter/write=string.rot13/resource='.__DIR__.'/foo.csv';
+ $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($filter);
+ $this->assertTrue($csv->hasStreamFilter('string.toupper'));
+ $this->assertSame(STREAM_FILTER_READ, $csv->getStreamFilterMode());
+ }
+
+ /**
+ * @expectedException LogicException
+ * @expectedExceptionMessage The stream filter API can not be used
+ */
+ public function testInitStreamFilterWithSplFileObject()
+ {
+ Reader::createFromFileObject(new SplFileObject(__DIR__.'/foo.csv'))->getStreamFilterMode();
+ }
+
+ public function testappendStreamFilter()
+ {
+ $csv = Reader::createFromPath(__DIR__.'/foo.csv');
+ $csv->appendStreamFilter('string.toupper');
+ $csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
+ foreach ($csv->getIterator() as $row) {
+ $this->assertSame($row, ['JOHN', 'DOE', 'JOHN.DOE@EXAMPLE.COM']);
+ }
+ }
+
+ /**
+ * @expectedException LogicException
+ * @expectedExceptionMessage The stream filter API can not be used
+ */
+ public function testFailedprependStreamFilter()
+ {
+ $csv = Reader::createFromFileObject(new SplTempFileObject());
+ $this->assertFalse($csv->isActiveStreamFilter());
+ $csv->prependStreamFilter('string.toupper');
+ }
+
+ /**
+ * @expectedException LogicException
+ * @expectedExceptionMessage The stream filter API can not be used
+ */
+ public function testFailedapppendStreamFilter()
+ {
+ $csv = Writer::createFromFileObject(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()
+ {
+ $csv = Reader::createFromPath(__DIR__.'/foo.csv');
+ $csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
+ $csv->appendStreamFilter('string.tolower');
+ $csv->prependStreamFilter('string.rot13');
+ $csv->appendStreamFilter('string.toupper');
+ $this->assertTrue($csv->hasStreamFilter('string.tolower'));
+ $csv->removeStreamFilter('string.tolower');
+ $this->assertFalse($csv->hasStreamFilter('string.tolower'));
+
+ foreach ($csv->getIterator() as $row) {
+ $this->assertSame($row, ['WBUA', 'QBR', 'WBUA.QBR@RKNZCYR.PBZ']);
+ }
+ $csv->clearStreamFilter();
+ $this->assertFalse($csv->hasStreamFilter('string.rot13'));
+
+ $csv->appendStreamFilter('string.toupper');
+ $this->assertSame(STREAM_FILTER_READ, $csv->getStreamFilterMode());
+ $csv->setStreamFilterMode(STREAM_FILTER_WRITE);
+ $this->assertSame(STREAM_FILTER_WRITE, $csv->getStreamFilterMode());
+ foreach ($csv->getIterator() as $row) {
+ $this->assertSame($row, ['john', 'doe', 'john.doe@example.com']);
+ }
+ $csv->setStreamFilterMode(34);
+ }
+
+ public function testGetFilterPath()
+ {
+ $csv = Writer::createFromPath(__DIR__.'/foo.csv');
+ $csv->appendStreamFilter('string.rot13');
+ $csv->prependStreamFilter('string.toupper');
+ $this->assertFalse($csv->getIterator()->getRealPath());
+ }
+}