|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | +* This file is part of the Symfony package. |
| 5 | +* |
| 6 | +* (c) Fabien Potencier <fabien@symfony.com> |
| 7 | +* |
| 8 | +* For the full copyright and license information, please view the LICENSE |
| 9 | +* file that was distributed with this source code. |
| 10 | +*/ |
| 11 | + |
| 12 | +namespace Symfony\Component\Form\Tests\Extension\Validator; |
| 13 | + |
| 14 | +use Symfony\Component\Form\Extension\Validator\ValidatorExtension; |
| 15 | + |
| 16 | +class ValidatorExtensionTest extends \PHPUnit_Framework_TestCase |
| 17 | +{ |
| 18 | + public function testValidatorInterfaceSinceSymfony25() |
| 19 | + { |
| 20 | + $classMetaData = $this->createClassMetaDataMock(); |
| 21 | + |
| 22 | + // Mock of ValidatorInterface since apiVersion 2.5 |
| 23 | + $validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface'); |
| 24 | + |
| 25 | + $validator |
| 26 | + ->expects($this->once()) |
| 27 | + ->method('getMetadataFor') |
| 28 | + ->with($this->identicalTo('Symfony\Component\Form\Form')) |
| 29 | + ->will($this->returnValue($classMetaData)) |
| 30 | + ; |
| 31 | + |
| 32 | + $validatorExtension = new ValidatorExtension($validator); |
| 33 | + $this->assertAttributeSame($validator, 'validator', $validatorExtension); |
| 34 | + } |
| 35 | + |
| 36 | + public function testValidatorInterfaceUntilSymfony24() |
| 37 | + { |
| 38 | + $classMetaData = $this->createClassMetaDataMock(); |
| 39 | + |
| 40 | + $metaDataFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface'); |
| 41 | + |
| 42 | + $metaDataFactory |
| 43 | + ->expects($this->once()) |
| 44 | + ->method('getMetadataFor') |
| 45 | + ->with($this->identicalTo('Symfony\Component\Form\Form')) |
| 46 | + ->will($this->returnValue($classMetaData)) |
| 47 | + ; |
| 48 | + |
| 49 | + // Mock of ValidatorInterface until apiVersion 2.4 |
| 50 | + $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface'); |
| 51 | + |
| 52 | + $validator |
| 53 | + ->expects($this->once()) |
| 54 | + ->method('getMetadataFactory') |
| 55 | + ->will($this->returnValue($metaDataFactory)) |
| 56 | + ; |
| 57 | + |
| 58 | + $validatorExtension = new ValidatorExtension($validator); |
| 59 | + $this->assertAttributeSame($validator, 'validator', $validatorExtension); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @expectedException \InvalidArgumentException |
| 64 | + */ |
| 65 | + public function testInvalidValidatorInterface() |
| 66 | + { |
| 67 | + new ValidatorExtension(null); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @return mixed |
| 72 | + */ |
| 73 | + private function createClassMetaDataMock() |
| 74 | + { |
| 75 | + $classMetaData = $this->getMockBuilder('Symfony\Component\Validator\Mapping\ClassMetadata') |
| 76 | + ->disableOriginalConstructor() |
| 77 | + ->getMock(); |
| 78 | + |
| 79 | + $classMetaData |
| 80 | + ->expects($this->once()) |
| 81 | + ->method('addConstraint') |
| 82 | + ->with($this->isInstanceOf('Symfony\Component\Form\Extension\Validator\Constraints\Form')); |
| 83 | + $classMetaData |
| 84 | + ->expects($this->once()) |
| 85 | + ->method('addPropertyConstraint') |
| 86 | + ->with( |
| 87 | + $this->identicalTo('children'), |
| 88 | + $this->isInstanceOf('Symfony\Component\Validator\Constraints\Valid') |
| 89 | + ); |
| 90 | + |
| 91 | + return $classMetaData; |
| 92 | + } |
| 93 | +} |
0 commit comments