ValidValidatorTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. $this->context = new ValidationContext('Root', $this->walker, $this->factory);
  20. $this->validator = new ValidValidator();
  21. $this->validator->initialize($this->context);
  22. }
  23. public function testNullIsValid()
  24. {
  25. $this->assertTrue($this->validator->isValid(null, new Valid()));
  26. }
  27. public function testThrowsExceptionIfNotObjectOrArray()
  28. {
  29. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  30. $this->validator->isValid('foobar', new Valid());
  31. }
  32. public function testWalkObject()
  33. {
  34. $this->context->setGroup('MyGroup');
  35. $this->context->setPropertyPath('foo');
  36. $metadata = $this->createClassMetadata();
  37. $entity = new Entity();
  38. $this->factory->expects($this->once())
  39. ->method('getClassMetadata')
  40. ->with($this->equalTo(self::CLASSNAME))
  41. ->will($this->returnValue($metadata));
  42. $this->walker->expects($this->once())
  43. ->method('walkClass')
  44. ->with($this->equalTo($metadata), $this->equalTo($entity), 'MyGroup', 'foo');
  45. $this->assertTrue($this->validator->isValid($entity, new Valid()));
  46. }
  47. public function testWalkArray()
  48. {
  49. $this->context->setGroup('MyGroup');
  50. $this->context->setPropertyPath('foo');
  51. $constraint = new Valid();
  52. $entity = new Entity();
  53. // can only test for one object due to PHPUnit's mocking limitations
  54. $array = array('key' => $entity);
  55. $this->walker->expects($this->once())
  56. ->method('walkConstraint')
  57. ->with($this->equalTo($constraint), $this->equalTo($entity), 'MyGroup', 'foo[key]');
  58. $this->assertTrue($this->validator->isValid($array, $constraint));
  59. }
  60. public function testValidateClass_Succeeds()
  61. {
  62. $metadata = $this->createClassMetadata();
  63. $entity = new Entity();
  64. $this->factory->expects($this->any())
  65. ->method('getClassMetadata')
  66. ->with($this->equalTo(self::CLASSNAME))
  67. ->will($this->returnValue($metadata));
  68. $this->assertTrue($this->validator->isValid($entity, new Valid(array('class' => self::CLASSNAME))));
  69. }
  70. public function testValidateClass_Fails()
  71. {
  72. $entity = new \stdClass();
  73. $this->assertFalse($this->validator->isValid($entity, new Valid(array('class' => self::CLASSNAME))));
  74. }
  75. protected function createClassMetadata()
  76. {
  77. return $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadata', array(), array(), '', false);
  78. }
  79. }