ValidValidatorTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Symfony\Tests\Component\Validator;
  3. require_once __DIR__.'/../Fixtures/Entity.php';
  4. use Symfony\Component\Validator\ValidationContext;
  5. use Symfony\Component\Validator\Constraints\Valid;
  6. use Symfony\Component\Validator\Constraints\ValidValidator;
  7. use Symfony\Tests\Component\Validator\Fixtures\Entity;
  8. class ValidValidatorTest extends \PHPUnit_Framework_TestCase
  9. {
  10. const CLASSNAME = 'Symfony\Tests\Component\Validator\Fixtures\Entity';
  11. protected $validator;
  12. protected $factory;
  13. protected $walker;
  14. protected $context;
  15. public function setUp()
  16. {
  17. $this->walker = $this->getMock('Symfony\Component\Validator\GraphWalker', array(), array(), '', false);
  18. $this->factory = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
  19. $messageInterpolator = $this->getMock('Symfony\Component\Validator\MessageInterpolator\MessageInterpolatorInterface');
  20. $this->context = new ValidationContext('Root', $this->walker, $this->factory, $messageInterpolator);
  21. $this->validator = new ValidValidator();
  22. $this->validator->initialize($this->context);
  23. }
  24. public function testNullIsValid()
  25. {
  26. $this->assertTrue($this->validator->isValid(null, new Valid()));
  27. }
  28. public function testThrowsExceptionIfNotObjectOrArray()
  29. {
  30. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  31. $this->validator->isValid('foobar', new Valid());
  32. }
  33. public function testWalkObject()
  34. {
  35. $this->context->setGroup('MyGroup');
  36. $this->context->setPropertyPath('foo');
  37. $metadata = $this->createClassMetadata();
  38. $entity = new Entity();
  39. $this->factory->expects($this->once())
  40. ->method('getClassMetadata')
  41. ->with($this->equalTo(self::CLASSNAME))
  42. ->will($this->returnValue($metadata));
  43. $this->walker->expects($this->once())
  44. ->method('walkClass')
  45. ->with($this->equalTo($metadata), $this->equalTo($entity), 'MyGroup', 'foo');
  46. $this->assertTrue($this->validator->isValid($entity, new Valid()));
  47. }
  48. public function testWalkArray()
  49. {
  50. $this->context->setGroup('MyGroup');
  51. $this->context->setPropertyPath('foo');
  52. $constraint = new Valid();
  53. $entity = new Entity();
  54. // can only test for one object due to PHPUnit's mocking limitations
  55. $array = array('key' => $entity);
  56. $this->walker->expects($this->once())
  57. ->method('walkConstraint')
  58. ->with($this->equalTo($constraint), $this->equalTo($entity), 'MyGroup', 'foo[key]');
  59. $this->assertTrue($this->validator->isValid($array, $constraint));
  60. }
  61. public function testValidateClass_Succeeds()
  62. {
  63. $metadata = $this->createClassMetadata();
  64. $entity = new Entity();
  65. $this->factory->expects($this->any())
  66. ->method('getClassMetadata')
  67. ->with($this->equalTo(self::CLASSNAME))
  68. ->will($this->returnValue($metadata));
  69. $this->assertTrue($this->validator->isValid($entity, new Valid(array('class' => self::CLASSNAME))));
  70. }
  71. public function testValidateClass_Fails()
  72. {
  73. $entity = new \stdClass();
  74. $this->assertFalse($this->validator->isValid($entity, new Valid(array('class' => self::CLASSNAME))));
  75. }
  76. protected function createClassMetadata()
  77. {
  78. return $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadata', array(), array(), '', false);
  79. }
  80. }