ValidatorStepTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace Ddeboer\DataImport\Tests\Step;
  3. use Ddeboer\DataImport\Step\ValidatorStep;
  4. use Symfony\Component\Validator\Constraints;
  5. use Symfony\Component\Validator\ConstraintViolation;
  6. use Symfony\Component\Validator\ConstraintViolationList;
  7. class ValidatorStepTest extends \PHPUnit_Framework_TestCase
  8. {
  9. protected function setUp()
  10. {
  11. $this->validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');
  12. $this->filter = new ValidatorStep($this->validator);
  13. }
  14. public function testProcess()
  15. {
  16. $data = ['title' => null];
  17. $this->filter->add('title', $constraint = new Constraints\NotNull());
  18. $list = new ConstraintViolationList();
  19. $list->add($this->buildConstraintViolation());
  20. $this->validator->expects($this->once())
  21. ->method('validate')
  22. ->willReturn($list);
  23. $this->assertFalse($this->filter->process($data));
  24. $this->assertEquals([1 => $list], $this->filter->getViolations());
  25. }
  26. /**
  27. * @expectedException Ddeboer\DataImport\Exception\ValidationException
  28. */
  29. public function testProcessWithExceptions()
  30. {
  31. $data = ['title' => null];
  32. $this->filter->add('title', $constraint = new Constraints\NotNull());
  33. $this->filter->throwExceptions();
  34. $list = new ConstraintViolationList();
  35. $list->add($this->buildConstraintViolation());
  36. $this->validator->expects($this->once())
  37. ->method('validate')
  38. ->willReturn($list);
  39. $this->assertFalse($this->filter->process($data));
  40. }
  41. public function testPriority()
  42. {
  43. $this->assertEquals(128, $this->filter->getPriority());
  44. }
  45. private function buildConstraintViolation()
  46. {
  47. return $this->getMockBuilder('Symfony\Component\Validator\ConstraintViolation')
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. }
  51. }