ValidValidatorTest.php 3.8 KB

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