CSV validator library.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ValidatorTest.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. use Deblan\Csv\CsvParser;
  3. use Deblan\CsvValidator\Validator;
  4. use Symfony\Component\Validator\Validation;
  5. use Symfony\Component\Validator\Constraints\NotBlank;
  6. use Symfony\Component\Validator\Constraints\Email;
  7. class ValidatorTest extends \PHPUnit_Framework_TestCase
  8. {
  9. public function testViolation()
  10. {
  11. $parser = $this->generateParser('example.csv');
  12. $validator = $this->generateValidator($parser);
  13. $this->setExpectedException('\RuntimeException');
  14. $validator->isValid();
  15. }
  16. public function testExpectedHeaders()
  17. {
  18. $parser = $this->generateParser('example.csv');
  19. $parser->setHasHeaders(true);
  20. $validator = $this->generateValidator();
  21. $validator->setExpectedHeaders(['foo', 'bar', 'boo']);
  22. $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
  23. $this->assertEquals(true, $validator->isValid());
  24. $this->assertEquals(0, count($validator->getErrors()));
  25. $validator = $this->generateValidator();
  26. $validator->setExpectedHeaders(['bad', 'legend']);
  27. $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
  28. $this->assertEquals(false, $validator->isValid());
  29. $this->assertEquals(1, count($validator->getErrors()));
  30. }
  31. public function testNoConstraint()
  32. {
  33. $parser = $this->generateParser('example.csv');
  34. $validator = $this->generateValidator();
  35. $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
  36. $this->assertEquals(true, $validator->isValid());
  37. }
  38. public function testFieldContraintsOk()
  39. {
  40. $parser = $this->generateParser('example.csv');
  41. $validator = $this->generateValidator();
  42. $validator->addFieldConstraint(0, new NotBlank());
  43. $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
  44. $this->assertEquals(true, $validator->isValid());
  45. $this->assertEquals(0, count($validator->getErrors()));
  46. $parser = $this->generateParser('example.csv');
  47. $validator = $this->generateValidator();
  48. $validator->addFieldConstraint(1, new NotBlank());
  49. $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
  50. $this->assertEquals(true, $validator->isValid());
  51. $this->assertEquals(0, count($validator->getErrors()));
  52. $parser = $this->generateParser('example.csv');
  53. $validator = $this->generateValidator();
  54. $validator->addFieldConstraint(0, new NotBlank());
  55. $validator->addFieldConstraint(1, new NotBlank());
  56. $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
  57. $this->assertEquals(true, $validator->isValid());
  58. $this->assertEquals(0, count($validator->getErrors()));
  59. }
  60. public function testFieldContraintsKo()
  61. {
  62. $parser = $this->generateParser();
  63. $validator = $this->generateValidator();
  64. $validator->addFieldConstraint(0, new Email());
  65. $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
  66. $this->assertEquals(false, $validator->isValid());
  67. $this->assertEquals(2, count($validator->getErrors()));
  68. $parser = $this->generateParser();
  69. $validator = $this->generateValidator();
  70. $validator->addFieldConstraint(1, new Email());
  71. $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
  72. $this->assertEquals(false, $validator->isValid());
  73. $this->assertEquals(5, count($validator->getErrors()));
  74. $parser = $this->generateParser('example.csv');
  75. $validator = $this->generateValidator();
  76. $validator->addFieldConstraint(0, new Email());
  77. $validator->addFieldConstraint(1, new Email());
  78. $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv'));
  79. $this->assertEquals(false, $validator->isValid());
  80. $this->assertEquals(7, count($validator->getErrors()));
  81. }
  82. protected function generateParser()
  83. {
  84. return new CsvParser();
  85. }
  86. protected function generateValidator()
  87. {
  88. return new Validator(Validation::createValidator());
  89. }
  90. }