From 82108384859ad691d842447a568e7a9396193791 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sun, 22 May 2016 18:37:12 +0200 Subject: [PATCH] Refactoring, tests --- src/Deblan/CsvValidator/Validator.php | 70 +++++++++++++++++++++++---- tests/CsvTest.php | 8 --- tests/ViolationTest.php | 23 +++++++++ 3 files changed, 83 insertions(+), 18 deletions(-) delete mode 100644 tests/CsvTest.php create mode 100644 tests/ViolationTest.php diff --git a/src/Deblan/CsvValidator/Validator.php b/src/Deblan/CsvValidator/Validator.php index d1bd4f5..b7024c3 100644 --- a/src/Deblan/CsvValidator/Validator.php +++ b/src/Deblan/CsvValidator/Validator.php @@ -27,7 +27,7 @@ class Validator /** * @var array */ - protected $fieldsConstraints = []; + protected $fieldConstraints = []; /** * @var array @@ -66,11 +66,11 @@ class Validator */ public function addFieldConstraint($key, Constraint $constraint) { - if (!array_key_exists($key, $this->fieldsConstraints)) { - $this->fieldsConstraints[$key] = []; + if (!array_key_exists($key, $this->fieldConstraints)) { + $this->fieldConstraints[$key] = []; } - $this->fieldsConstraints[$key][] = $constraint; + $this->fieldConstraints[$key][] = $constraint; return $this; } @@ -98,14 +98,42 @@ class Validator return; } + $this->validateDatas(); + $this->validateFields(); + + + $this->hasValidate = true; + } + + /** + * Validates datas + */ + protected function validateDatas() + { + if (empty($this->dataConstraints)) { + return; + } + foreach ($this->parser->getDatas() as $line => $data) { foreach ($this->dataConstraints as $constraint) { $violations = $this->validator->validateValue($data, $constraint); $this->mergeViolationsMessages($violations, $line); } + } + } - foreach ($this->fieldsConstraints as $key => $constraints) { + /** + * Validates fields + */ + 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])) { $this->mergeErrorMessage(sprintf('Field "%s" does not exist.', $key + 1), $line, $key); } else { @@ -117,8 +145,6 @@ class Validator } } } - - $this->hasValidate = true; } /** @@ -139,7 +165,7 @@ class Validator } foreach ($violations as $violation) { - $this->errors[] = new Violation($line + 1, $key, $violation); + $this->errors[] = $this->generateViolation($line + 1, $key, $violation); } } @@ -160,8 +186,8 @@ class Validator $key++; } - $violation = new ConstraintViolation($message, $message, [], null, '', null); - $this->errors[] = new Violation($line + 1, $key, $violation); + $violation = $this->generateConstraintViolation($message); + $this->errors[] = $this->generateViolation($line + 1, $key, $violation); } /** @@ -188,4 +214,28 @@ class Validator { return $this->errors; } + + /** + * Generate a ConstraintViolation + * + * @param string $message The error message + * @return ConstraintViolation + */ + protected function generateConstraintViolation($message) + { + return new ConstraintViolation($message, $message, [], null, '', null); + } + + /** + * Generate a Violation + * + * @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) + { + return new Violation($line, $key, $violation); + } } diff --git a/tests/CsvTest.php b/tests/CsvTest.php deleted file mode 100644 index 4a7e33c..0000000 --- a/tests/CsvTest.php +++ /dev/null @@ -1,8 +0,0 @@ -generateConstraintViolation(); + + $violation = new Violation(1, 2, $constraintViolation); + + $this->assertEquals(1, $violation->getLine()); + $this->assertEquals(2, $violation->getColumn()); + $this->assertEquals($constraintViolation, $violation->getViolation()); + } + + protected function generateConstraintViolation() + { + return new ConstraintViolation('foo', 'foo', [], null, '', null); + } +}