From 7de3c8a9e875c839cef1d8ee876858fbd9ef65b0 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Fri, 2 Dec 2016 13:50:07 +0100 Subject: [PATCH 01/19] PSR2 compliance --- src/Deblan/CsvValidator/Validator.php | 121 ++++++++++++++------------ src/Deblan/CsvValidator/Violation.php | 18 ++-- 2 files changed, 75 insertions(+), 64 deletions(-) diff --git a/src/Deblan/CsvValidator/Validator.php b/src/Deblan/CsvValidator/Validator.php index e6650d0..ee22932 100644 --- a/src/Deblan/CsvValidator/Validator.php +++ b/src/Deblan/CsvValidator/Validator.php @@ -10,7 +10,8 @@ use Symfony\Component\Validator\ConstraintViolation; use Symfony\Component\Validator\Validation; /** - * Class Validator + * Class Validator. + * * @author Simon Vieille */ class Validator @@ -36,7 +37,7 @@ class Validator protected $dataConstraints = []; /** - * @var boolean + * @var bool */ protected $hasValidate = false; @@ -44,14 +45,14 @@ class Validator * @var array */ protected $errors = []; - + /** * @var array */ protected $expectedLegend = []; /** - * Constructor + * Constructor. * * @param RecursiveValidator $validator */ @@ -59,16 +60,17 @@ class Validator { if ($validator === null) { $validator = Validation::createValidator(); - } - + } + $this->validator = $validator; } /** - * Append a constraint to a specific column + * Append a constraint to a specific column. * - * @param integer $key The column number + * @param int $key The column number * @param Constraint $constraint The constraint + * * @return Validator */ public function addFieldConstraint($key, Constraint $constraint) @@ -83,9 +85,10 @@ class Validator } /** - * Append a constraint to a specific line + * Append a constraint to a specific line. * * @param Constraint $constraint The constraint + * * @return Validator */ public function addDataConstraint(Constraint $constraint) @@ -96,12 +99,13 @@ class Validator } /** - * Set the expected legend + * Set the expected legend. * * @param array $legend Expected legend + * * @return Validator */ - public function setExpectedLegend(array $legend) + public function setExpectedLegend(array $legend) { $this->expectedLegend = $legend; @@ -109,7 +113,8 @@ class Validator } /** - * Run the validation + * Run the validation. + * * @param CsvParser $parser */ public function validate(CsvParser $parser) @@ -118,23 +123,21 @@ class Validator $this->parser = $parser; $this->parser->parse(); $this->errors = []; - } - elseif ($this->hasValidate) { + } elseif ($this->hasValidate) { return; } - + $this->validateLegend(); $this->validateDatas(); $this->validateFields(); - $this->hasValidate = true; } - + /** - * Validates the legend + * Validates the legend. */ - protected function validateLegend() + protected function validateLegend() { if (!$this->parser->getHasLegend()) { return; @@ -143,16 +146,16 @@ class Validator if (empty($this->expectedLegend)) { return; } - + if ($this->parser->getLegend() !== $this->expectedLegend) { $this->mergeErrorMessage('Invalid legend.', 1); } } /** - * Validates datas + * Validates datas. */ - protected function validateDatas() + protected function validateDatas() { if (empty($this->dataConstraints)) { return; @@ -168,21 +171,21 @@ class Validator } /** - * Validates fields + * Validates fields. */ - protected function validateFields() + protected function validateFields() { if (empty($this->fieldConstraints)) { return; } - + foreach ($this->parser->getDatas() as $line => $data) { foreach ($this->fieldConstraints as $key => $constraints) { if (!isset($data[$key])) { $column = $this->getTrueColunm($key); $this->mergeErrorMessage( - sprintf('Field "%s" does not exist.', $column), - $this->getTrueLine($line), + sprintf('Field "%s" does not exist.', $column), + $this->getTrueLine($line), $column ); } else { @@ -190,8 +193,8 @@ class Validator $violations = $this->validator->validate($data[$key], $constraint); $this->mergeViolationsMessages( - $violations, - $this->getTrueLine($line), + $violations, + $this->getTrueLine($line), $this->getTrueColunm($key) ); } @@ -201,11 +204,11 @@ class Validator } /** - * Add violations + * Add violations. * * @param ConstraintViolationList $violations - * @param integer $line The line of the violations - * @param integer|null $key The column of the violations + * @param int $line The line of the violations + * @param int|null $key The column of the violations */ protected function mergeViolationsMessages(ConstraintViolationList $violations, $line, $key = null) { @@ -219,11 +222,11 @@ class Validator } /** - * Create and append a violation from a string error + * Create and append a violation from a string error. * - * @param string $message The error message - * @param integer $line The line of the violations - * @param integer|null $key The column of the violations + * @param string $message The error message + * @param int $line The line of the violations + * @param int|null $key The column of the violations */ protected function mergeErrorMessage($message, $line, $key = null) { @@ -232,9 +235,9 @@ class Validator } /** - * Returns the validation status + * Returns the validation status. * - * @return boolean + * @return bool * @throw RuntimeException No validation yet */ public function isValid() @@ -247,7 +250,7 @@ class Validator } /** - * Returns the errors + * Returns the errors. * * @return array */ @@ -257,51 +260,55 @@ class Validator } /** - * Generate a ConstraintViolation + * Generate a ConstraintViolation. * * @param string $message The error message + * * @return ConstraintViolation */ - protected function generateConstraintViolation($message) + protected function generateConstraintViolation($message) { return new ConstraintViolation($message, $message, [], null, '', null); } - + /** - * Generate a Violation + * Generate a Violation. + * + * @param string $message The error message + * @param int $line The line of the violations + * @param int|null $key The column of the violations * - * @param string $message The error message - * @param integer $line The line of the violations - * @param integer|null $key The column of the violations * @return Violation */ - protected function generateViolation($line, $key, ConstraintViolation $violation) + protected function generateViolation($line, $key, ConstraintViolation $violation) { return new Violation($line, $key, $violation); } /** - * Get the true line number of an error + * Get the true line number of an error. * - * @param integer $line - * @return integer + * @param int $line + * + * @return int */ - protected function getTrueLine($line) + protected function getTrueLine($line) { if ($this->parser->getHasLegend()) { - $line++; + ++$line; } return ++$line; } - + /** - * Get the true culumn number of an error + * Get the true culumn number of an error. * - * @param integer $key - * @return integer + * @param int $key + * + * @return int */ - protected function getTrueColunm($key) + protected function getTrueColunm($key) { return ++$key; } diff --git a/src/Deblan/CsvValidator/Violation.php b/src/Deblan/CsvValidator/Violation.php index 1989c8a..a6a6e78 100644 --- a/src/Deblan/CsvValidator/Violation.php +++ b/src/Deblan/CsvValidator/Violation.php @@ -5,31 +5,32 @@ namespace Deblan\CsvValidator; use Symfony\Component\Validator\ConstraintViolation; /** - * Class Violation + * Class Violation. + * * @author Simon Vieille */ class Violation { /** - * @var integer + * @var int */ protected $line; /** - * @var integer + * @var int */ protected $column; - + /** * @var ConstraintViolation */ protected $violation; /** - * Constructor + * Constructor. * - * @param integer $line The line of the violation - * @param integer $column The column of the violation + * @param int $line The line of the violation + * @param int $column The column of the violation * @param ConstraintViolation $violation The violation */ public function __construct($line, $column, ConstraintViolation $violation) @@ -41,6 +42,7 @@ class Violation /** * @param int $line + * * @return Violation */ public function setLine($line) @@ -60,6 +62,7 @@ class Violation /** * @param int $column + * * @return Violation */ public function setColumn($column) @@ -83,6 +86,7 @@ class Violation /** * @param ConstraintViolation $violation + * * @return Violation */ public function setViolation(ConstraintViolation $violation) From ac7ae6ba9ac7d6d7be476740d923862faf1c92e1 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Fri, 2 Dec 2016 14:26:50 +0100 Subject: [PATCH 02/19] tests --- tests/ValidatorTest.php | 52 ++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index 50f28df..5c0e69f 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -15,63 +15,63 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase $this->setExpectedException('\RuntimeException'); $validator->isValid(); } - + public function testNoConstraint() { $parser = $this->generateParser('example.csv'); - $validator = $this->generateValidator($parser); - $validator->validate(); + $validator = $this->generateValidator(); + $validator->validate($parser); $this->assertEquals(true, $validator->isValid()); } - + public function testFieldContraintsOk() { $parser = $this->generateParser('example.csv'); - $validator = $this->generateValidator($parser); + $validator = $this->generateValidator(); $validator->addFieldConstraint(0, new NotBlank()); - $validator->validate(); + $validator->validate($parser); $this->assertEquals(true, $validator->isValid()); $this->assertEquals(0, count($validator->getErrors())); - + $parser = $this->generateParser('example.csv'); - $validator = $this->generateValidator($parser); + $validator = $this->generateValidator(); $validator->addFieldConstraint(1, new NotBlank()); - $validator->validate(); + $validator->validate($parser); $this->assertEquals(true, $validator->isValid()); $this->assertEquals(0, count($validator->getErrors())); - + $parser = $this->generateParser('example.csv'); - $validator = $this->generateValidator($parser); + $validator = $this->generateValidator(); $validator->addFieldConstraint(0, new NotBlank()); $validator->addFieldConstraint(1, new NotBlank()); - $validator->validate(); + $validator->validate($parser); $this->assertEquals(true, $validator->isValid()); $this->assertEquals(0, count($validator->getErrors())); } - + public function testFieldContraintsKo() { $parser = $this->generateParser('example.csv'); - $validator = $this->generateValidator($parser); + $validator = $this->generateValidator(); $validator->addFieldConstraint(0, new Email()); - $validator->validate(); + $validator->validate($parser); $this->assertEquals(false, $validator->isValid()); - $this->assertEquals(4, count($validator->getErrors())); - + $this->assertEquals(2, count($validator->getErrors())); + $parser = $this->generateParser('example.csv'); - $validator = $this->generateValidator($parser); + $validator = $this->generateValidator(); $validator->addFieldConstraint(1, new Email()); - $validator->validate(); + $validator->validate($parser); $this->assertEquals(false, $validator->isValid()); - $this->assertEquals(4, count($validator->getErrors())); - + $this->assertEquals(5, count($validator->getErrors())); + $parser = $this->generateParser('example.csv'); - $validator = $this->generateValidator($parser); + $validator = $this->generateValidator(); $validator->addFieldConstraint(0, new Email()); $validator->addFieldConstraint(1, new Email()); - $validator->validate(); + $validator->validate($parser); $this->assertEquals(false, $validator->isValid()); - $this->assertEquals(8, count($validator->getErrors())); + $this->assertEquals(7, count($validator->getErrors())); } protected function generateParser($file) @@ -79,8 +79,8 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase return new CsvParser(__DIR__.'/fixtures/'.$file); } - protected function generateValidator(CsvParser $parser) + protected function generateValidator() { - return new Validator($parser, Validation::createValidator()); + return new Validator(Validation::createValidator()); } } From c3f4921c2682b5fde6547805df054db23b51aded Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Fri, 2 Dec 2016 14:27:30 +0100 Subject: [PATCH 03/19] README --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index fa1e271..d1b21d8 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Installation You need [composer](https://getcomposer.org/): - composer require deblan/csv-validator dev-master + composer require deblan/csv-validator Example @@ -56,10 +56,10 @@ $validator->validate($parser); if ($validator->isValid() === false) { foreach ($validator->getErrors() as $error) { - $line = $error->getLine(); + $line = $error->getLine(); $column = $error->getColumn(); $message = $error->getViolation()->getMessage(); - + echo <<
  • Line: $line
  • From 4858f4a948519eb8493ba04fa7967ea4c6cf3549 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Fri, 2 Dec 2016 14:31:27 +0100 Subject: [PATCH 04/19] README --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index d1b21d8..3a76ce9 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,10 @@ 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](#installation) +* [Example](#example) +* [Contributors](#contributors) + Installation ------------ @@ -73,3 +77,9 @@ EOF; } } ``` + +Contributors +------------ + +* Simon Vieille +* Gautier Deruette From d6f4e098a4c6163428da445f00ee044c0758c291 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Fri, 2 Dec 2016 15:18:33 +0100 Subject: [PATCH 05/19] tests --- tests/ValidatorTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index 5c0e69f..4c8c617 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -16,6 +16,24 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase $validator->isValid(); } + public function testExpectedLegend() + { + $parser = $this->generateParser('example.csv'); + $parser->setHasLegend(true); + + $validator = $this->generateValidator(); + $validator->setExpectedLegend(['foo', 'bar', 'boo']); + $validator->validate($parser); + $this->assertEquals(true, $validator->isValid()); + $this->assertEquals(0, count($validator->getErrors())); + + $validator = $this->generateValidator(); + $validator->setExpectedLegend(['bad', 'legend']); + $validator->validate($parser); + $this->assertEquals(false, $validator->isValid()); + $this->assertEquals(1, count($validator->getErrors())); + } + public function testNoConstraint() { $parser = $this->generateParser('example.csv'); From 3cb4894bd0643cb4b53482636b50c20cfd35d429 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sat, 3 Dec 2016 18:41:54 +0100 Subject: [PATCH 06/19] phpci file --- phpci.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 phpci.yml diff --git a/phpci.yml b/phpci.yml new file mode 100644 index 0000000..01be34a --- /dev/null +++ b/phpci.yml @@ -0,0 +1,14 @@ +build_settings: + verbose: false + prefer_symlink: false + +setup: + composer: + action: "install" + +test: + php_unit: + directory: "tests/" + args: "--configuration 'phpunit.xml'" + +complete: From 104b25d17c6aab0981fda760ab85c46fefce9a4a Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sat, 3 Dec 2016 18:46:28 +0100 Subject: [PATCH 07/19] README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 3a76ce9..60a693c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ csv-validator ============= +![](https://phpci.gitnet.fr//build-status/image/1) + 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). From 31adc51188d52a4ddb630e94c8371ea75caadd1f Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sun, 12 Mar 2017 19:22:52 +0100 Subject: [PATCH 08/19] deblan/csv upgraded to the v2 and more units tests --- README.md | 41 ++++++++++++++++++++++--- composer.json | 2 +- example.php | 12 ++++---- src/Deblan/CsvValidator/Validator.php | 19 ++++++------ tests/ExampleTest.php | 44 +++++++++++++++++++++++++++ tests/ValidatorTest.php | 34 ++++++++++----------- 6 files changed, 114 insertions(+), 38 deletions(-) create mode 100644 tests/ExampleTest.php diff --git a/README.md b/README.md index 60a693c..3b2a3d3 100644 --- a/README.md +++ b/README.md @@ -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: + +``` +
      +
    • Line: 1
    • +
    • Column:
    • +
    • +

      Invalid legend.

      +
    • +
    +
      +
    • Line: 4
    • +
    • Column:
    • +
    • +

      The line must contain 3 columns

      +
    • +
    +
      +
    • Line: 2
    • +
    • Column: 1
    • +
    • +

      This value is not a valid email address.

      +
    • +
    +
      +
    • Line: 3
    • +
    • Column: 2
    • +
    • +

      This value is not a valid date.

      +
    • +
    +``` + Contributors ------------ diff --git a/composer.json b/composer.json index c9aa77f..4e8a836 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,6 @@ "require": { "php": ">=5.6.0", "symfony/validator": "2.*", - "deblan/csv": "v1.1" + "deblan/csv": "v2.0.*" } } diff --git a/example.php b/example.php index 48250f6..595e5c5 100644 --- a/example.php +++ b/example.php @@ -20,7 +20,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) { @@ -30,17 +30,17 @@ $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) { - $line = $error->getLine(); + $line = $error->getLine(); $column = $error->getColumn(); $message = $error->getViolation()->getMessage(); - + echo <<
  • Line: $line
  • diff --git a/src/Deblan/CsvValidator/Validator.php b/src/Deblan/CsvValidator/Validator.php index ee22932..6374910 100644 --- a/src/Deblan/CsvValidator/Validator.php +++ b/src/Deblan/CsvValidator/Validator.php @@ -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,12 @@ class Validator { if ($this->parser !== $parser) { $this->parser = $parser; - $this->parser->parse(); $this->errors = []; } elseif ($this->hasValidate) { return; } - $this->validateLegend(); + $this->validateHeaders(); $this->validateDatas(); $this->validateFields(); @@ -137,17 +136,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 +293,7 @@ class Validator */ protected function getTrueLine($line) { - if ($this->parser->getHasLegend()) { + if ($this->parser->getHasHeaders()) { ++$line; } diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php new file mode 100644 index 0000000..4d0705f --- /dev/null +++ b/tests/ExampleTest.php @@ -0,0 +1,44 @@ + + */ +class ExampleTest extends \PHPUnit_Framework_TestCase +{ + public function testExemple() + { + $content = shell_exec('php -f '.__DIR__.'/../example.php'); + + $this->assertEquals('
      +
    • Line: 1
    • +
    • Column:
    • +
    • +

      Invalid legend.

      +
    • +
    +
      +
    • Line: 4
    • +
    • Column:
    • +
    • +

      The line must contain 3 columns

      +
    • +
    +
      +
    • Line: 2
    • +
    • Column: 1
    • +
    • +

      This value is not a valid email address.

      +
    • +
    +
      +
    • Line: 3
    • +
    • Column: 2
    • +
    • +

      This value is not a valid date.

      +
    • +
    +', $content); + } +} diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index 4c8c617..f3d75ac 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -16,20 +16,20 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase $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 +38,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 +47,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 +62,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 +87,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() From 1e6ed3d204fc6fdc38891a75c3008ea09123980f Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Mon, 13 Mar 2017 15:53:14 +0100 Subject: [PATCH 09/19] Insight analyse fixes --- .gitignore | 2 -- LICENSE | 3 ++- example.php | 3 +-- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 3e52175..22d0d82 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1 @@ -*.swp -tags vendor diff --git a/LICENSE b/LICENSE index b57a6c2..80445fb 100644 --- a/LICENSE +++ b/LICENSE @@ -20,4 +20,5 @@ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 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. \ No newline at end of file +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + diff --git a/example.php b/example.php index 595e5c5..e764726 100644 --- a/example.php +++ b/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; @@ -23,7 +22,7 @@ $validator->addFieldConstraint(1, new Date()); $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'); } From 8ee01fc2098a8b704c840245304eb76a74587a33 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Wed, 15 Mar 2017 00:39:46 +0100 Subject: [PATCH 10/19] CI --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3b2a3d3..683aa00 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ csv-validator ============= -![](https://phpci.gitnet.fr//build-status/image/1) +[![](https://phpci.gitnet.fr/build-status/image/2)](https://phpci.gitnet.fr/build-status/view/2) CSV validator library From 78610d50ea3f35de57d7f4d72acf88c2e1b0ebea Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Wed, 15 Mar 2017 00:42:28 +0100 Subject: [PATCH 11/19] CI --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 683aa00..a83c640 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ csv-validator ============= -[![](https://phpci.gitnet.fr/build-status/image/2)](https://phpci.gitnet.fr/build-status/view/2) +[![](https://phpci.gitnet.fr/build-status/image/2?branch=master&label=PHPCensor&style=flat-square)](https://phpci.gitnet.fr/build-status/view/2) CSV validator library From 95c470e4045cc7571212650ce77a82cca767ba6c Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 29 May 2018 15:37:17 +0200 Subject: [PATCH 12/19] symfony/validator v3.* --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 4e8a836..4e561ff 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "minimum-stability": "dev", "require": { "php": ">=5.6.0", - "symfony/validator": "2.*", + "symfony/validator": "3.*", "deblan/csv": "v2.0.*" } } From 6169083b5e7cc698df303f70af4e62180a543888 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Wed, 6 Jun 2018 15:55:17 +0200 Subject: [PATCH 13/19] CI --- README.md | 2 +- phpunit.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a83c640..f93e91c 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ csv-validator ============= -[![](https://phpci.gitnet.fr/build-status/image/2?branch=master&label=PHPCensor&style=flat-square)](https://phpci.gitnet.fr/build-status/view/2) +[![](https://phpci.gitnet.fr/build-status/image/1?branch=master&label=PHPCensor&style=flat-square)](https://phpci.gitnet.fr/build-status/view/1) CSV validator library diff --git a/phpunit.xml b/phpunit.xml index eabb967..4f465be 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,7 @@ convertWarningsToExceptions = "true" processIsolation = "false" stopOnFailure = "false" - syntaxCheck = "false" + syntaxCheck = "true" bootstrap = "vendor/autoload.php" > From af0c45c6bcf9fb5ece330c28c4cea1cec8b877aa Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Wed, 6 Jun 2018 19:03:13 +0200 Subject: [PATCH 14/19] CI --- phpci.yml => .php-censor.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename phpci.yml => .php-censor.yml (100%) diff --git a/phpci.yml b/.php-censor.yml similarity index 100% rename from phpci.yml rename to .php-censor.yml From cd43e2fed963fb79c33689024e6b9bd0d804e1d1 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Mon, 20 Jan 2020 11:04:04 +0100 Subject: [PATCH 15/19] upgrade dependencies (php7) --- Makefile | 4 ++++ composer.json | 4 ++-- phpunit.xml | 1 - tests/ExampleTest.php | 4 +++- tests/ValidatorTest.php | 5 +++-- tests/ViolationTest.php | 3 ++- 6 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..990a451 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +.PHONY: tests + +tests: + phpunit diff --git a/composer.json b/composer.json index 4e561ff..5b8f1c4 100644 --- a/composer.json +++ b/composer.json @@ -15,8 +15,8 @@ }, "minimum-stability": "dev", "require": { - "php": ">=5.6.0", - "symfony/validator": "3.*", + "php": "^7.1.3", + "symfony/validator": "^4", "deblan/csv": "v2.0.*" } } diff --git a/phpunit.xml b/phpunit.xml index 4f465be..eb8c372 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,6 @@ convertWarningsToExceptions = "true" processIsolation = "false" stopOnFailure = "false" - syntaxCheck = "true" bootstrap = "vendor/autoload.php" > diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php index 4d0705f..0be5792 100644 --- a/tests/ExampleTest.php +++ b/tests/ExampleTest.php @@ -1,11 +1,13 @@ */ -class ExampleTest extends \PHPUnit_Framework_TestCase +class ExampleTest extends TestCase { public function testExemple() { diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index f3d75ac..13d6462 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -1,18 +1,19 @@ generateParser('example.csv'); $validator = $this->generateValidator($parser); - $this->setExpectedException('\RuntimeException'); + $this->expectException('\RuntimeException'); $validator->isValid(); } diff --git a/tests/ViolationTest.php b/tests/ViolationTest.php index 1fd0484..c798ab4 100644 --- a/tests/ViolationTest.php +++ b/tests/ViolationTest.php @@ -1,9 +1,10 @@ Date: Tue, 1 Sep 2020 16:24:15 +0200 Subject: [PATCH 16/19] add compatibility with deblan/csv@v3 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 5b8f1c4..dd0a7c3 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,6 @@ "require": { "php": "^7.1.3", "symfony/validator": "^4", - "deblan/csv": "v2.0.*" + "deblan/csv": "v2.0.*|v3.*" } } From fac4371d69f36ee50f918dca99d8e560009d84cd Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 1 Sep 2020 16:24:29 +0200 Subject: [PATCH 17/19] fix issue with validation --- src/Deblan/CsvValidator/Validator.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Deblan/CsvValidator/Validator.php b/src/Deblan/CsvValidator/Validator.php index 6374910..b590c9f 100644 --- a/src/Deblan/CsvValidator/Validator.php +++ b/src/Deblan/CsvValidator/Validator.php @@ -122,6 +122,7 @@ class Validator if ($this->parser !== $parser) { $this->parser = $parser; $this->errors = []; + $this->hasValidate = false; } elseif ($this->hasValidate) { return; } From 2d06d4adc5df0f319593b76ea770c206b0b8b2f8 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 1 Sep 2020 16:25:29 +0200 Subject: [PATCH 18/19] add jenkins tests --- Jenkinsfile | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..6e0cddf --- /dev/null +++ b/Jenkinsfile @@ -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' + } + } + } +} + From a2e1ac418c5992be2279049d0003833e800b3f57 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 1 Sep 2020 16:28:22 +0200 Subject: [PATCH 19/19] add jenkins status --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f93e91c..48d866e 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ csv-validator ============= -[![](https://phpci.gitnet.fr/build-status/image/1?branch=master&label=PHPCensor&style=flat-square)](https://phpci.gitnet.fr/build-status/view/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