Compare commits
12 commits
Author | SHA1 | Date | |
---|---|---|---|
Simon Vieille | a2e1ac418c | ||
Simon Vieille | 2d06d4adc5 | ||
Simon Vieille | fac4371d69 | ||
Simon Vieille | 0b9c77ae7f | ||
Simon Vieille | cd43e2fed9 | ||
Simon Vieille | af0c45c6bc | ||
Simon Vieille | 6169083b5e | ||
Simon Vieille | 95c470e404 | ||
Simon Vieille | 78610d50ea | ||
Simon Vieille | 8ee01fc209 | ||
Simon Vieille | 1e6ed3d204 | ||
Simon Vieille | 31adc51188 |
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,3 +1 @@
|
|||
*.swp
|
||||
tags
|
||||
vendor
|
||||
|
|
25
Jenkinsfile
vendored
Normal file
25
Jenkinsfile
vendored
Normal file
|
@ -0,0 +1,25 @@
|
|||
pipeline {
|
||||
agent any
|
||||
|
||||
stages {
|
||||
stage('PHP 7.1') {
|
||||
steps {
|
||||
sh '/usr/local/bin/composer-php7.1 install'
|
||||
sh 'php7.1 /usr/local/bin/phpunit-7'
|
||||
}
|
||||
}
|
||||
stage('PHP 7.3') {
|
||||
steps {
|
||||
sh '/usr/local/bin/composer-php7.3 update'
|
||||
sh 'php7.3 /usr/local/bin/phpunit-9'
|
||||
}
|
||||
}
|
||||
stage('PHP 7.4') {
|
||||
steps {
|
||||
sh '/usr/local/bin/composer-php7.4 update'
|
||||
sh 'php7.4 /usr/local/bin/phpunit-9'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
1
LICENSE
1
LICENSE
|
@ -21,3 +21,4 @@ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
|
|
43
README.md
43
README.md
|
@ -1,7 +1,7 @@
|
|||
csv-validator
|
||||
=============
|
||||
|
||||
![](https://phpci.gitnet.fr//build-status/image/1)
|
||||
[![Build Status](https://ci.gitnet.fr/buildStatus/icon?job=Gitnet%2Fcsv-validator%2F3)](https://ci.gitnet.fr/job/Gitnet/job/csv-validator/job/3/)
|
||||
|
||||
CSV validator library
|
||||
|
||||
|
@ -45,7 +45,7 @@ $validator->addFieldConstraint(0, new Email());
|
|||
$validator->addFieldConstraint(1, new Date());
|
||||
|
||||
// Validate the legend
|
||||
$validator->setExpectedLegend(array('foo', 'bar', 'bim'));
|
||||
$validator->setExpectedHeaders(['foo', 'bar', 'bim']);
|
||||
|
||||
// An line must contain 3 columns
|
||||
$validator->addDataConstraint(new Callback(function($data, ExecutionContextInterface $context) {
|
||||
|
@ -55,10 +55,10 @@ $validator->addDataConstraint(new Callback(function($data, ExecutionContextInter
|
|||
}));
|
||||
|
||||
// Initialisation of the parser
|
||||
$parser = new CsvParser(__DIR__.'/tests/fixtures/example.csv');
|
||||
$parser->setHasLegend(true);
|
||||
$parser = new CsvParser();
|
||||
$parser->setHasHeaders(true);
|
||||
|
||||
$validator->validate($parser);
|
||||
$validator->validate($parser->parseFile(__DIR__.'/tests/fixtures/example.csv'));
|
||||
|
||||
if ($validator->isValid() === false) {
|
||||
foreach ($validator->getErrors() as $error) {
|
||||
|
@ -80,6 +80,39 @@ EOF;
|
|||
}
|
||||
```
|
||||
|
||||
Run `example.php` and see results:
|
||||
|
||||
```
|
||||
<ul>
|
||||
<li>Line: 1</li>
|
||||
<li>Column: </li>
|
||||
<li>
|
||||
<p>Invalid legend.</p>
|
||||
</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Line: 4</li>
|
||||
<li>Column: </li>
|
||||
<li>
|
||||
<p>The line must contain 3 columns</p>
|
||||
</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Line: 2</li>
|
||||
<li>Column: 1</li>
|
||||
<li>
|
||||
<p>This value is not a valid email address.</p>
|
||||
</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Line: 3</li>
|
||||
<li>Column: 2</li>
|
||||
<li>
|
||||
<p>This value is not a valid date.</p>
|
||||
</li>
|
||||
</ul>
|
||||
```
|
||||
|
||||
Contributors
|
||||
------------
|
||||
|
||||
|
|
|
@ -15,8 +15,8 @@
|
|||
},
|
||||
"minimum-stability": "dev",
|
||||
"require": {
|
||||
"php": ">=5.6.0",
|
||||
"symfony/validator": "2.*",
|
||||
"deblan/csv": "v1.1"
|
||||
"php": "^7.1.3",
|
||||
"symfony/validator": "^4",
|
||||
"deblan/csv": "v2.0.*|v3.*"
|
||||
}
|
||||
}
|
||||
|
|
11
example.php
11
example.php
|
@ -2,7 +2,6 @@
|
|||
|
||||
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;
|
||||
|
@ -20,20 +19,20 @@ $validator->addFieldConstraint(0, new Email());
|
|||
$validator->addFieldConstraint(1, new Date());
|
||||
|
||||
// Validate the legend
|
||||
$validator->setExpectedLegend(array('foo', 'bar', 'bim'));
|
||||
$validator->setExpectedHeaders(['foo', 'bar', 'bim']);
|
||||
|
||||
// An line must contain 3 columns
|
||||
$validator->addDataConstraint(new Callback(function($data, ExecutionContextInterface $context) {
|
||||
$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');
|
||||
}
|
||||
}));
|
||||
|
||||
// Initialisation of the parser
|
||||
$parser = new CsvParser(__DIR__.'/tests/fixtures/example.csv');
|
||||
$parser->setHasLegend(true);
|
||||
$parser = new CsvParser();
|
||||
$parser->setHasHeaders(true);
|
||||
|
||||
$validator->validate($parser);
|
||||
$validator->validate($parser->parseFile(__DIR__.'/tests/fixtures/example.csv'));
|
||||
|
||||
if ($validator->isValid() === false) {
|
||||
foreach ($validator->getErrors() as $error) {
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
convertWarningsToExceptions = "true"
|
||||
processIsolation = "false"
|
||||
stopOnFailure = "false"
|
||||
syntaxCheck = "false"
|
||||
bootstrap = "vendor/autoload.php" >
|
||||
|
||||
<testsuites>
|
||||
|
|
|
@ -49,7 +49,7 @@ class Validator
|
|||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $expectedLegend = [];
|
||||
protected $expectedHeaders = [];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
|
@ -105,9 +105,9 @@ class Validator
|
|||
*
|
||||
* @return Validator
|
||||
*/
|
||||
public function setExpectedLegend(array $legend)
|
||||
public function setExpectedHeaders(array $legend)
|
||||
{
|
||||
$this->expectedLegend = $legend;
|
||||
$this->expectedHeaders = $legend;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -121,13 +121,13 @@ class Validator
|
|||
{
|
||||
if ($this->parser !== $parser) {
|
||||
$this->parser = $parser;
|
||||
$this->parser->parse();
|
||||
$this->errors = [];
|
||||
$this->hasValidate = false;
|
||||
} elseif ($this->hasValidate) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->validateLegend();
|
||||
$this->validateHeaders();
|
||||
$this->validateDatas();
|
||||
$this->validateFields();
|
||||
|
||||
|
@ -137,17 +137,17 @@ class Validator
|
|||
/**
|
||||
* Validates the legend.
|
||||
*/
|
||||
protected function validateLegend()
|
||||
protected function validateHeaders()
|
||||
{
|
||||
if (!$this->parser->getHasLegend()) {
|
||||
if (!$this->parser->getHasHeaders()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (empty($this->expectedLegend)) {
|
||||
if (empty($this->expectedHeaders)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->parser->getLegend() !== $this->expectedLegend) {
|
||||
if ($this->parser->getHeaders() !== $this->expectedHeaders) {
|
||||
$this->mergeErrorMessage('Invalid legend.', 1);
|
||||
}
|
||||
}
|
||||
|
@ -294,7 +294,7 @@ class Validator
|
|||
*/
|
||||
protected function getTrueLine($line)
|
||||
{
|
||||
if ($this->parser->getHasLegend()) {
|
||||
if ($this->parser->getHasHeaders()) {
|
||||
++$line;
|
||||
}
|
||||
|
||||
|
|
46
tests/ExampleTest.php
Normal file
46
tests/ExampleTest.php
Normal file
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* class ExampleTest.
|
||||
*
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
class ExampleTest extends TestCase
|
||||
{
|
||||
public function testExemple()
|
||||
{
|
||||
$content = shell_exec('php -f '.__DIR__.'/../example.php');
|
||||
|
||||
$this->assertEquals('<ul>
|
||||
<li>Line: 1</li>
|
||||
<li>Column: </li>
|
||||
<li>
|
||||
<p>Invalid legend.</p>
|
||||
</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Line: 4</li>
|
||||
<li>Column: </li>
|
||||
<li>
|
||||
<p>The line must contain 3 columns</p>
|
||||
</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Line: 2</li>
|
||||
<li>Column: 1</li>
|
||||
<li>
|
||||
<p>This value is not a valid email address.</p>
|
||||
</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Line: 3</li>
|
||||
<li>Column: 2</li>
|
||||
<li>
|
||||
<p>This value is not a valid date.</p>
|
||||
</li>
|
||||
</ul>
|
||||
', $content);
|
||||
}
|
||||
}
|
|
@ -1,35 +1,36 @@
|
|||
<?php
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
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
|
||||
class ValidatorTest extends TestCase
|
||||
{
|
||||
public function testViolation()
|
||||
{
|
||||
$parser = $this->generateParser('example.csv');
|
||||
$validator = $this->generateValidator($parser);
|
||||
$this->setExpectedException('\RuntimeException');
|
||||
$this->expectException('\RuntimeException');
|
||||
$validator->isValid();
|
||||
}
|
||||
|
||||
public function testExpectedLegend()
|
||||
public function testExpectedHeaders()
|
||||
{
|
||||
$parser = $this->generateParser('example.csv');
|
||||
$parser->setHasLegend(true);
|
||||
$parser->setHasHeaders(true);
|
||||
|
||||
$validator = $this->generateValidator();
|
||||
$validator->setExpectedLegend(['foo', 'bar', 'boo']);
|
||||
$validator->validate($parser);
|
||||
$validator->setExpectedHeaders(['foo', 'bar', 'boo']);
|
||||
$validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
|
||||
$this->assertEquals(true, $validator->isValid());
|
||||
$this->assertEquals(0, count($validator->getErrors()));
|
||||
|
||||
$validator = $this->generateValidator();
|
||||
$validator->setExpectedLegend(['bad', 'legend']);
|
||||
$validator->validate($parser);
|
||||
$validator->setExpectedHeaders(['bad', 'legend']);
|
||||
$validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
|
||||
$this->assertEquals(false, $validator->isValid());
|
||||
$this->assertEquals(1, count($validator->getErrors()));
|
||||
}
|
||||
|
@ -38,7 +39,7 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
{
|
||||
$parser = $this->generateParser('example.csv');
|
||||
$validator = $this->generateValidator();
|
||||
$validator->validate($parser);
|
||||
$validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
|
||||
$this->assertEquals(true, $validator->isValid());
|
||||
}
|
||||
|
||||
|
@ -47,14 +48,14 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
$parser = $this->generateParser('example.csv');
|
||||
$validator = $this->generateValidator();
|
||||
$validator->addFieldConstraint(0, new NotBlank());
|
||||
$validator->validate($parser);
|
||||
$validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
|
||||
$this->assertEquals(true, $validator->isValid());
|
||||
$this->assertEquals(0, count($validator->getErrors()));
|
||||
|
||||
$parser = $this->generateParser('example.csv');
|
||||
$validator = $this->generateValidator();
|
||||
$validator->addFieldConstraint(1, new NotBlank());
|
||||
$validator->validate($parser);
|
||||
$validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
|
||||
$this->assertEquals(true, $validator->isValid());
|
||||
$this->assertEquals(0, count($validator->getErrors()));
|
||||
|
||||
|
@ -62,24 +63,24 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
$validator = $this->generateValidator();
|
||||
$validator->addFieldConstraint(0, new NotBlank());
|
||||
$validator->addFieldConstraint(1, new NotBlank());
|
||||
$validator->validate($parser);
|
||||
$validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
|
||||
$this->assertEquals(true, $validator->isValid());
|
||||
$this->assertEquals(0, count($validator->getErrors()));
|
||||
}
|
||||
|
||||
public function testFieldContraintsKo()
|
||||
{
|
||||
$parser = $this->generateParser('example.csv');
|
||||
$parser = $this->generateParser();
|
||||
$validator = $this->generateValidator();
|
||||
$validator->addFieldConstraint(0, new Email());
|
||||
$validator->validate($parser);
|
||||
$validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
|
||||
$this->assertEquals(false, $validator->isValid());
|
||||
$this->assertEquals(2, count($validator->getErrors()));
|
||||
|
||||
$parser = $this->generateParser('example.csv');
|
||||
$parser = $this->generateParser();
|
||||
$validator = $this->generateValidator();
|
||||
$validator->addFieldConstraint(1, new Email());
|
||||
$validator->validate($parser);
|
||||
$validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
|
||||
$this->assertEquals(false, $validator->isValid());
|
||||
$this->assertEquals(5, count($validator->getErrors()));
|
||||
|
||||
|
@ -87,14 +88,14 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
$validator = $this->generateValidator();
|
||||
$validator->addFieldConstraint(0, new Email());
|
||||
$validator->addFieldConstraint(1, new Email());
|
||||
$validator->validate($parser);
|
||||
$validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
|
||||
$this->assertEquals(false, $validator->isValid());
|
||||
$this->assertEquals(7, count($validator->getErrors()));
|
||||
}
|
||||
|
||||
protected function generateParser($file)
|
||||
protected function generateParser()
|
||||
{
|
||||
return new CsvParser(__DIR__.'/fixtures/'.$file);
|
||||
return new CsvParser();
|
||||
}
|
||||
|
||||
protected function generateValidator()
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
<?php
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Validator\ConstraintViolation;
|
||||
use Deblan\CsvValidator\Violation;
|
||||
|
||||
class ViolationTest extends \PHPUnit_Framework_TestCase
|
||||
class ViolationTest extends TestCase
|
||||
{
|
||||
public function testViolation()
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue