summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md1
-rw-r--r--src/Config/Output.php7
-rw-r--r--test/CsvTest.php115
-rw-r--r--test/FactoryTest.php4
-rw-r--r--test/StreamFilterTest.php108
5 files changed, 133 insertions, 102 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e3644b1..08ae398 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -19,6 +19,7 @@ All Notable changes to `League\Csv` will be documented in this file
- Default flags value is now `SplFileObject::READ_CSV|SplFileObject::DROP_NEW_LINE`
- `SplFileObject` constants are now copied when using `newReader` and `newWriter` methods
- BOM addition on export improved by removing existing BOM character if found
+- the `output` method now returns the number of bytes send to the output buffer
### Remove
- Setting `ini_set("auto_detect_line_endings", true);` is no longer set in the class constructor. Mac OS X users must explicitly set this ini options in their script.
diff --git a/src/Config/Output.php b/src/Config/Output.php
index 9a2dd16..89761b7 100644
--- a/src/Config/Output.php
+++ b/src/Config/Output.php
@@ -154,6 +154,8 @@ trait Output
*
* @param string $filename CSV downloaded name if present adds extra headers
*
+ * @return int Returns the number of characters read from the handle
+ * and passed through to the output.
*/
public function output($filename = null)
{
@@ -164,14 +166,15 @@ trait Output
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename=\"$filename\"");
}
- $this->fpassthru();
+
+ return $this->fpassthru();
}
/**
* Output all data from the CSV
*
* @return int Returns the number of characters read from the handle
- * and passed through to the output.
+ * and passed through to the output.
*/
protected function fpassthru()
{
diff --git a/test/CsvTest.php b/test/CsvTest.php
index ae55bf7..69c8418 100644
--- a/test/CsvTest.php
+++ b/test/CsvTest.php
@@ -92,17 +92,23 @@ EOF;
$this->assertSame($expected, $doc->saveXML());
}
+ public function testJsonSerialize()
+ {
+ $this->csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
+ $this->assertSame(json_encode($this->expected), json_encode($this->csv));
+ }
+
/**
* @param $rawCsv
*
* @dataProvider getIso8859Csv
*/
- public function testJsonInterface($rawCsv)
+ public function testJsonSerializeAffectedByReaderOptions($rawCsv)
{
- $this->csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
- $this->assertSame(json_encode($this->expected), json_encode($this->csv));
$csv = Reader::createFromString($rawCsv);
$csv->setEncodingFrom('iso-8859-15');
+ $csv->setOffset(799);
+ $csv->setLimit(50);
json_encode($csv);
$this->assertEquals(JSON_ERROR_NONE, json_last_error());
}
@@ -112,99 +118,6 @@ EOF;
return [[file_get_contents(__DIR__.'/data/prenoms.csv')]];
}
- 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());
- }
-
/**
* @runInSeparateProcess
*/
@@ -213,8 +126,8 @@ EOF;
if (defined('HHVM_VERSION')) {
$this->markTestSkipped();
}
- $this->csv->output("test.csv");
- $headers = xdebug_get_headers();
+ $this->assertSame(60, $this->csv->output("test.csv"));
+ $headers = \xdebug_get_headers();
$this->assertSame($headers[0], "Content-Type: application/octet-stream");
$this->assertSame($headers[1], "Content-Transfer-Encoding: binary");
$this->assertSame($headers[2], "Content-Disposition: attachment; filename=\"test.csv\"");
@@ -227,4 +140,10 @@ EOF;
{
$this->csv->output(new DateTime);
}
+
+ public function testToString()
+ {
+ $expected = "john,doe,john.doe@example.com\njane,doe,jane.doe@example.com\n";
+ $this->assertSame($expected, $this->csv->__toString());
+ }
}
diff --git a/test/FactoryTest.php b/test/FactoryTest.php
index 9aabb09..178149a 100644
--- a/test/FactoryTest.php
+++ b/test/FactoryTest.php
@@ -24,7 +24,7 @@ class FactoryTest extends PHPUnit_Framework_TestCase
$this->assertSame($path, $csv->getIterator()->getRealPath());
}
- public function testCreateFromPathWithFileObject()
+ public function testCreateFromPathWithSplFileInfo()
{
$path = __DIR__.'/foo.csv';
$csv = Reader::createFromPath(new SplFileInfo($path));
@@ -34,7 +34,7 @@ class FactoryTest extends PHPUnit_Framework_TestCase
public function testConstructorWithSplFileInfo()
{
$path = __DIR__.'/foo.csv';
- $csv = Reader::createFromPath(new SplFileInfo($path));
+ $csv = new Reader(new SplFileInfo($path));
$this->assertSame($path, $csv->getIterator()->getRealPath());
}
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());
+ }
+}