forked from deblan/csv-validator
Documentation, tests
This commit is contained in:
parent
8210838485
commit
6c63cf0655
58
README.md
58
README.md
|
@ -1,11 +1,63 @@
|
||||||
csv-validator
|
csv-validator
|
||||||
=============
|
=============
|
||||||
|
|
||||||
CSV validator library.
|
CSV validator library
|
||||||
|
|
||||||
|
That uses the constraints of Symfony framework: [http://symfony.com/doc/current/reference/constraints.html](http://symfony.com/doc/current/reference/constraints.html).
|
||||||
|
|
||||||
|
Installation
|
||||||
|
------------
|
||||||
|
|
||||||
|
You need [composer]("https://getcomposer.org/"):
|
||||||
|
|
||||||
|
composer require deblan/csv-validator dev-master
|
||||||
|
|
||||||
Works in progress. You should not use this package.
|
|
||||||
|
|
||||||
Example
|
Example
|
||||||
-------
|
-------
|
||||||
|
|
||||||
Read the source of `example.php`.
|
```
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Deblan\Csv\CsvParser;
|
||||||
|
use Deblan\CsvValidator\Validator;
|
||||||
|
use Symfony\Component\Validator\Validation;
|
||||||
|
use Symfony\Component\Validator\Constraints\Email;
|
||||||
|
use Symfony\Component\Validator\Constraints\Date;
|
||||||
|
use Symfony\Component\Validator\Constraints\Callback;
|
||||||
|
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||||
|
|
||||||
|
require __DIR__.'/vendor/autoload.php';
|
||||||
|
|
||||||
|
// Initialisation of the parser
|
||||||
|
$parser = new CsvParser(__DIR__.'/tests/fixtures/example.csv');
|
||||||
|
$parser->setHasLegend(true);
|
||||||
|
|
||||||
|
// Initialisation of the validator
|
||||||
|
$validator = new Validator($parser, Validation::createValidator());
|
||||||
|
|
||||||
|
// The first field must contain an email
|
||||||
|
$validator->addFieldConstraint(0, new Email());
|
||||||
|
|
||||||
|
// The second field must contain a date
|
||||||
|
$validator->addFieldConstraint(1, new Date());
|
||||||
|
|
||||||
|
// An line must contain 3 columns
|
||||||
|
$validator->addDataConstraint(new Callback(function($data, ExecutionContextInterface $context) {
|
||||||
|
if (count($data) !== 6) { // 6 because of the legend (3 fields * 2)
|
||||||
|
$context->addViolation('The line must contain 3 columns');
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
$validator->validate();
|
||||||
|
|
||||||
|
if ($validator->isValid() === false) {
|
||||||
|
foreach ($validator->getErrors() as $violation) {
|
||||||
|
$line = $violation->getLine();
|
||||||
|
$column = $violation->getColumn();
|
||||||
|
$message = $violation->getViolation()->getMessage();
|
||||||
|
|
||||||
|
// Up to you!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
30
example.php
30
example.php
|
@ -5,18 +5,31 @@ use Deblan\CsvValidator\Validator;
|
||||||
use Symfony\Component\Validator\Validation;
|
use Symfony\Component\Validator\Validation;
|
||||||
use Symfony\Component\Validator\Constraints\Email;
|
use Symfony\Component\Validator\Constraints\Email;
|
||||||
use Symfony\Component\Validator\Constraints\Date;
|
use Symfony\Component\Validator\Constraints\Date;
|
||||||
|
use Symfony\Component\Validator\Constraints\Callback;
|
||||||
|
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||||
|
|
||||||
chdir(__DIR__);
|
require __DIR__.'/vendor/autoload.php';
|
||||||
|
|
||||||
require 'vendor/autoload.php';
|
// Initialisation of the parser
|
||||||
|
$parser = new CsvParser(__DIR__.'/tests/fixtures/example.csv');
|
||||||
$parser = new CsvParser('example.csv', ';', '');
|
$parser->setHasLegend(true);
|
||||||
|
|
||||||
|
// Initialisation of the validator
|
||||||
$validator = new Validator($parser, Validation::createValidator());
|
$validator = new Validator($parser, Validation::createValidator());
|
||||||
|
|
||||||
|
// The first field must contain an email
|
||||||
$validator->addFieldConstraint(0, new Email());
|
$validator->addFieldConstraint(0, new Email());
|
||||||
|
|
||||||
|
// The second field must contain a date
|
||||||
$validator->addFieldConstraint(1, new Date());
|
$validator->addFieldConstraint(1, new Date());
|
||||||
|
|
||||||
|
// An line must contain 3 columns
|
||||||
|
$validator->addDataConstraint(new Callback(function($data, ExecutionContextInterface $context) {
|
||||||
|
if (count($data) !== 6) { // 6 because of the legend (3 fields * 2)
|
||||||
|
$context->addViolation('The line must contain 3 columns');
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
$validator->validate();
|
$validator->validate();
|
||||||
|
|
||||||
if ($validator->isValid() === false) {
|
if ($validator->isValid() === false) {
|
||||||
|
@ -25,13 +38,6 @@ if ($validator->isValid() === false) {
|
||||||
$column = $violation->getColumn();
|
$column = $violation->getColumn();
|
||||||
$message = $violation->getViolation()->getMessage();
|
$message = $violation->getViolation()->getMessage();
|
||||||
|
|
||||||
echo <<<EOF
|
// Up to you!
|
||||||
Line : $line
|
|
||||||
Column : $column
|
|
||||||
Message: $message
|
|
||||||
|
|
||||||
|
|
||||||
EOF;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
86
tests/ValidatorTest.php
Normal file
86
tests/ValidatorTest.php
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Deblan\Csv\CsvParser;
|
||||||
|
use Deblan\CsvValidator\Validator;
|
||||||
|
use Symfony\Component\Validator\Validation;
|
||||||
|
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||||
|
use Symfony\Component\Validator\Constraints\Email;
|
||||||
|
|
||||||
|
class ValidatorTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
public function testViolation()
|
||||||
|
{
|
||||||
|
$parser = $this->generateParser('example.csv');
|
||||||
|
$validator = $this->generateValidator($parser);
|
||||||
|
$this->setExpectedException('\RuntimeException');
|
||||||
|
$validator->isValid();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNoConstraint()
|
||||||
|
{
|
||||||
|
$parser = $this->generateParser('example.csv');
|
||||||
|
$validator = $this->generateValidator($parser);
|
||||||
|
$validator->validate();
|
||||||
|
$this->assertEquals(true, $validator->isValid());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFieldContraintsOk()
|
||||||
|
{
|
||||||
|
$parser = $this->generateParser('example.csv');
|
||||||
|
$validator = $this->generateValidator($parser);
|
||||||
|
$validator->addFieldConstraint(0, new NotBlank());
|
||||||
|
$validator->validate();
|
||||||
|
$this->assertEquals(true, $validator->isValid());
|
||||||
|
$this->assertEquals(0, count($validator->getErrors()));
|
||||||
|
|
||||||
|
$parser = $this->generateParser('example.csv');
|
||||||
|
$validator = $this->generateValidator($parser);
|
||||||
|
$validator->addFieldConstraint(1, new NotBlank());
|
||||||
|
$validator->validate();
|
||||||
|
$this->assertEquals(true, $validator->isValid());
|
||||||
|
$this->assertEquals(0, count($validator->getErrors()));
|
||||||
|
|
||||||
|
$parser = $this->generateParser('example.csv');
|
||||||
|
$validator = $this->generateValidator($parser);
|
||||||
|
$validator->addFieldConstraint(0, new NotBlank());
|
||||||
|
$validator->addFieldConstraint(1, new NotBlank());
|
||||||
|
$validator->validate();
|
||||||
|
$this->assertEquals(true, $validator->isValid());
|
||||||
|
$this->assertEquals(0, count($validator->getErrors()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFieldContraintsKo()
|
||||||
|
{
|
||||||
|
$parser = $this->generateParser('example.csv');
|
||||||
|
$validator = $this->generateValidator($parser);
|
||||||
|
$validator->addFieldConstraint(0, new Email());
|
||||||
|
$validator->validate();
|
||||||
|
$this->assertEquals(false, $validator->isValid());
|
||||||
|
$this->assertEquals(4, count($validator->getErrors()));
|
||||||
|
|
||||||
|
$parser = $this->generateParser('example.csv');
|
||||||
|
$validator = $this->generateValidator($parser);
|
||||||
|
$validator->addFieldConstraint(1, new Email());
|
||||||
|
$validator->validate();
|
||||||
|
$this->assertEquals(false, $validator->isValid());
|
||||||
|
$this->assertEquals(4, count($validator->getErrors()));
|
||||||
|
|
||||||
|
$parser = $this->generateParser('example.csv');
|
||||||
|
$validator = $this->generateValidator($parser);
|
||||||
|
$validator->addFieldConstraint(0, new Email());
|
||||||
|
$validator->addFieldConstraint(1, new Email());
|
||||||
|
$validator->validate();
|
||||||
|
$this->assertEquals(false, $validator->isValid());
|
||||||
|
$this->assertEquals(8, count($validator->getErrors()));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function generateParser($file)
|
||||||
|
{
|
||||||
|
return new CsvParser(__DIR__.'/fixtures/'.$file);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function generateValidator(CsvParser $parser)
|
||||||
|
{
|
||||||
|
return new Validator($parser, Validation::createValidator());
|
||||||
|
}
|
||||||
|
}
|
4
tests/fixtures/example.csv
vendored
Normal file
4
tests/fixtures/example.csv
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
"foo";"bar"
|
||||||
|
"foo1";"bar1"
|
||||||
|
"foo2";"bar2"
|
||||||
|
"foo3";"bar3"
|
|
Loading…
Reference in a new issue