diff options
author | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2015-02-16 11:58:56 +0100 |
---|---|---|
committer | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2015-02-16 11:58:56 +0100 |
commit | d5712a9a310112e4eff47675061b081b903f4c3f (patch) | |
tree | bb17a3cbf0ced18f5b11bf8b8269a023592aac52 /test/WriterTest.php | |
parent | 2d7246457b4ac81169bf4a50ec9c7ba0165785d2 (diff) | |
download | csv-d5712a9a310112e4eff47675061b081b903f4c3f.zip csv-d5712a9a310112e4eff47675061b081b903f4c3f.tar.gz csv-d5712a9a310112e4eff47675061b081b903f4c3f.tar.bz2 |
improve Writer validation capability
Diffstat (limited to 'test/WriterTest.php')
-rw-r--r-- | test/WriterTest.php | 128 |
1 files changed, 16 insertions, 112 deletions
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)); + } } |