diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/Validators/ColumnConsistencyTest.php | 78 | ||||
-rw-r--r-- | test/Validators/NullHandlingTest.php | 117 | ||||
-rw-r--r-- | test/WriterTest.php | 128 |
3 files changed, 211 insertions, 112 deletions
diff --git a/test/Validators/ColumnConsistencyTest.php b/test/Validators/ColumnConsistencyTest.php new file mode 100644 index 0000000..2233532 --- /dev/null +++ b/test/Validators/ColumnConsistencyTest.php @@ -0,0 +1,78 @@ +<?php + +namespace League\Csv\Test\Validators; + +use ArrayIterator; +use DateTime; +use League\Csv\Writer; +use League\Csv\Validators\ColumnConsistency; +use LimitIterator; +use PHPUnit_Framework_TestCase; +use SplFileObject; +use SplTempFileObject; + +date_default_timezone_set('UTC'); + +/** + * @group validators + */ +class ColumnConsistencyTest extends PHPUnit_Framework_TestCase +{ + private $csv; + + public function setUp() + { + $this->csv = Writer::createFromFileObject(new SplTempFileObject()); + } + + public function tearDown() + { + $csv = new SplFileObject(__DIR__.'/foo.csv', 'w'); + $csv->setCsvControl(); + $csv->fputcsv(["john", "doe", "john.doe@example.com"], ",", '"'); + $this->csv = null; + } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage the column count must an integer greater or equals to -1 + */ + public function testColumsCountSetterGetter() + { + $consistency = new ColumnConsistency(); + $this->assertSame(-1, $consistency->getColumnsCount()); + $consistency->setColumnsCount(3); + $this->assertSame(3, $consistency->getColumnsCount()); + $consistency->setColumnsCount('toto'); + } + + /** + * @expectedException RuntimeException + * @expectedExceptionMessageRegexp Adding \d+ cells on a \d+ cells per row CSV + */ + public function testColumsCountConsistency() + { + $consistency = new ColumnConsistency(); + $this->csv->addValidationRule($consistency); + $this->csv->insertOne(['john', 'doe', 'john.doe@example.com']); + $consistency->setColumnsCount(2); + $this->csv->insertOne(['jane', 'jane.doe@example.com']); + $consistency->setColumnsCount(3); + $this->csv->insertOne(['jane', 'jane.doe@example.com']); + } + + /** + * @expectedException RuntimeException + * @expectedExceptionMessageRegexp Adding \d+ cells on a \d+ cells per row CSV + */ + public function testAutoDetectColumnsCount() + { + $consistency = new ColumnConsistency(); + $this->csv->addValidationRule($consistency); + $consistency->autodetectColumnsCount(); + $this->assertSame(-1, $consistency->getColumnsCount()); + $this->csv->insertOne(['john', 'doe', 'john.doe@example.com']); + $this->assertSame(3, $consistency->getColumnsCount()); + $this->csv->insertOne(['jane', 'jane.doe@example.com']); + } +} diff --git a/test/Validators/NullHandlingTest.php b/test/Validators/NullHandlingTest.php new file mode 100644 index 0000000..8132a98 --- /dev/null +++ b/test/Validators/NullHandlingTest.php @@ -0,0 +1,117 @@ +<?php + +namespace League\Csv\Test\Validators; + +use ArrayIterator; +use DateTime; +use League\Csv\Writer; +use League\Csv\Validators\NullHandling; +use LimitIterator; +use PHPUnit_Framework_TestCase; +use SplFileObject; +use SplTempFileObject; + +date_default_timezone_set('UTC'); + +/** + * @group validators + */ +class NullHandlingTest extends PHPUnit_Framework_TestCase +{ + private $csv; + + public function setUp() + { + $this->csv = Writer::createFromFileObject(new SplTempFileObject()); + } + + public function tearDown() + { + $csv = new SplFileObject(__DIR__.'/foo.csv', 'w'); + $csv->setCsvControl(); + $csv->fputcsv(["john", "doe", "john.doe@example.com"], ",", '"'); + $this->csv = null; + } + + /** + * @expectedException OutOfBoundsException + * @expectedExceptionMessage invalid value for null handling + */ + public function testSetterGetterNullBehavior() + { + $plugin = new NullHandling(); + $plugin->setNullHandlingMode(NullHandling::NULL_AS_SKIP_CELL); + $this->assertSame(NullHandling::NULL_AS_SKIP_CELL, $plugin->getNullHandlingMode()); + + $plugin->setNullHandlingMode(23); + } + + + public function testInsertNullToSkipCell() + { + $expected = [ + ['john', 'doe', 'john.doe@example.com'], + 'john,doe,john.doe@example.com', + ['john', null, 'john.doe@example.com'], + ]; + $plugin = new NullHandling(); + $plugin->setNullHandlingMode(NullHandling::NULL_AS_SKIP_CELL); + $this->csv->addValidationRule($plugin); + foreach ($expected as $row) { + $this->csv->insertOne($row); + } + $iterator = new LimitIterator($this->csv->getIterator(), 2, 1); + $iterator->rewind(); + $res = $iterator->getInnerIterator()->current(); + $this->assertSame(['john', 'john.doe@example.com'], $res); + } + + public function testInsertNullToEmpty() + { + $expected = [ + ['john', 'doe', 'john.doe@example.com'], + 'john,doe,john.doe@example.com', + ['john', null, 'john.doe@example.com'], + ]; + $plugin = new NullHandling(); + $plugin->setNullHandlingMode(NullHandling::NULL_AS_EMPTY); + $this->csv->addValidationRule($plugin); + foreach ($expected as $row) { + $this->csv->insertOne($row); + } + $iterator = new LimitIterator($this->csv->getIterator(), 2, 1); + $iterator->rewind(); + $res = $iterator->getInnerIterator()->current(); + $this->assertSame(['john', '', 'john.doe@example.com'], $res); + } + + public function testInsertWithoutNullHandlingMode() + { + $expected = [ + ['john', 'doe', 'john.doe@example.com'], + 'john,doe,john.doe@example.com', + ['john', null, 'john.doe@example.com'], + ]; + $plugin = new NullHandling(); + $plugin->setNullHandlingMode(NullHandling::NULL_HANDLING_DISABLED); + $this->csv->addValidationRule($plugin); + $this->csv->insertAll($expected); + + $iterator = new LimitIterator($this->csv->getIterator(), 2, 1); + $iterator->rewind(); + $res = $iterator->getInnerIterator()->current(); + $this->assertSame(['john', '', 'john.doe@example.com'], $res); + } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage + */ + public function testInsertNullThrowsException() + { + $plugin = new NullHandling(); + $plugin->setNullHandlingMode(NullHandling::NULL_AS_EXCEPTION); + $this->csv->addValidationRule($plugin); + $this->csv->insertOne(['john', null, 'john.doe@example.com']); + } +} diff --git a/test/WriterTest.php b/test/WriterTest.php index 96874de..0d59dc1 100644 --- a/test/WriterTest.php +++ b/test/WriterTest.php @@ -56,18 +56,6 @@ class WriterTest extends PHPUnit_Framework_TestCase } } - /** - * @expectedException OutOfBoundsException - * @expectedExceptionMessage invalid value for null handling - */ - public function testSetterGetterNullBehavior() - { - $this->csv->setNullHandlingMode(Writer::NULL_AS_SKIP_CELL); - $this->assertSame(Writer::NULL_AS_SKIP_CELL, $this->csv->getNullHandlingMode()); - - $this->csv->setNullHandlingMode(23); - } - public function testInsertNormalFile() { $csv = Writer::createFromPath(__DIR__.'/foo.csv', 'a+'); @@ -77,56 +65,6 @@ class WriterTest extends PHPUnit_Framework_TestCase $this->assertSame(['jane', 'doe', 'jane.doe@example.com'], $iterator->getInnerIterator()->current()); } - public function testInsertNullToSkipCell() - { - $expected = [ - ['john', 'doe', 'john.doe@example.com'], - 'john,doe,john.doe@example.com', - ['john', null, 'john.doe@example.com'], - ]; - $this->csv->setNullHandlingMode(Writer::NULL_AS_SKIP_CELL); - foreach ($expected as $row) { - $this->csv->insertOne($row); - } - $iterator = new LimitIterator($this->csv->getIterator(), 2, 1); - $iterator->rewind(); - $res = $iterator->getInnerIterator()->current(); - $this->assertSame(['john', 'john.doe@example.com'], $res); - } - - public function testInsertNullToEmpty() - { - $expected = [ - ['john', 'doe', 'john.doe@example.com'], - 'john,doe,john.doe@example.com', - ['john', null, 'john.doe@example.com'], - ]; - $this->csv->setNullHandlingMode(Writer::NULL_AS_EMPTY); - foreach ($expected as $row) { - $this->csv->insertOne($row); - } - $iterator = new LimitIterator($this->csv->getIterator(), 2, 1); - $iterator->rewind(); - $res = $iterator->getInnerIterator()->current(); - $this->assertSame(['john', '', 'john.doe@example.com'], $res); - } - - public function testInsertWithoutNullHandlingMode() - { - $expected = [ - ['john', 'doe', 'john.doe@example.com'], - 'john,doe,john.doe@example.com', - ['john', null, 'john.doe@example.com'], - ]; - $this->csv->setNullHandlingMode(Writer::NULL_HANDLING_DISABLED); - $this->csv->insertAll($expected); - - $iterator = new LimitIterator($this->csv->getIterator(), 2, 1); - $iterator->rewind(); - $res = $iterator->getInnerIterator()->current(); - $this->assertSame(['john', '', 'john.doe@example.com'], $res); - } - /** * @expectedException PHPUnit_Framework_Error */ @@ -143,54 +81,6 @@ class WriterTest extends PHPUnit_Framework_TestCase } /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage - */ - public function testInsertNullThrowsException() - { - $this->csv->setNullHandlingMode(Writer::NULL_AS_EXCEPTION); - $this->csv->insertOne(['john', null, 'john.doe@example.com']); - } - - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage the column count must an integer greater or equals to -1 - */ - public function testColumsCountSetterGetter() - { - $this->assertSame(-1, $this->csv->getColumnsCount()); - $this->csv->setColumnsCount(3); - $this->assertSame(3, $this->csv->getColumnsCount()); - $this->csv->setColumnsCount('toto'); - } - - /** - * @expectedException RuntimeException - * @expectedExceptionMessageRegexp Adding \d+ cells on a \d+ cells per row CSV - */ - public function testColumsCountConsistency() - { - $this->csv->insertOne(['john', 'doe', 'john.doe@example.com']); - $this->csv->setColumnsCount(2); - $this->csv->insertOne(['jane', 'jane.doe@example.com']); - $this->csv->setColumnsCount(3); - $this->csv->insertOne(['jane', 'jane.doe@example.com']); - } - - /** - * @expectedException RuntimeException - * @expectedExceptionMessageRegexp Adding \d+ cells on a \d+ cells per row CSV - */ - public function testAutoDetectColumnsCount() - { - $this->csv->autodetectColumnsCount(); - $this->assertSame(-1, $this->csv->getColumnsCount()); - $this->csv->insertOne(['john', 'doe', 'john.doe@example.com']); - $this->assertSame(3, $this->csv->getColumnsCount()); - $this->csv->insertOne(['jane', 'jane.doe@example.com']); - } - - /** * @expectedException PHPUnit_Framework_Error */ public function testFailedInsertWithWrongData() @@ -199,8 +89,7 @@ class WriterTest extends PHPUnit_Framework_TestCase } /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessageRegexp Adding \d+ cells on a \d+ cells per row CSV + * @expectedException PHPUnit_Framework_Error */ public function testFailedInsertWithMultiDimensionArray() { @@ -266,4 +155,19 @@ class WriterTest extends PHPUnit_Framework_TestCase $csv = Writer::createFromString($raw, $expected); $this->assertSame($expected, $csv->getNewline()); } + + public function testAddRules() + { + $func = function (array $row) { + return $row; + }; + + $this->csv->addValidationRule($func); + $this->csv->addValidationRule($func); + $this->assertTrue($this->csv->hasValidationRule($func)); + $this->csv->removeValidationRule($func); + $this->assertTrue($this->csv->hasValidationRule($func)); + $this->csv->clearValidationRules(); + $this->assertFalse($this->csv->hasValidationRule($func)); + } } |