4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Tests
;
14 use Symfony\Component\Form\FormRegistry
;
15 use Symfony\Component\Form\FormTypeGuesserChain
;
16 use Symfony\Component\Form\Tests\Fixtures\TestExtension
;
17 use Symfony\Component\Form\Tests\Fixtures\FooSubTypeWithParentInstance
;
18 use Symfony\Component\Form\Tests\Fixtures\FooSubType
;
19 use Symfony\Component\Form\Tests\Fixtures\FooTypeBazExtension
;
20 use Symfony\Component\Form\Tests\Fixtures\FooTypeBarExtension
;
21 use Symfony\Component\Form\Tests\Fixtures\FooType
;
24 * @author Bernhard Schussek <bschussek@gmail.com>
26 class FormRegistryTest
extends \PHPUnit_Framework_TestCase
34 * @var \PHPUnit_Framework_MockObject_MockObject
36 private $resolvedTypeFactory;
39 * @var \PHPUnit_Framework_MockObject_MockObject
44 * @var \PHPUnit_Framework_MockObject_MockObject
58 protected function setUp()
60 $this->resolvedTypeFactory
= $this->getMock('Symfony\Component\Form\ResolvedFormTypeFactory');
61 $this->guesser1
= $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
62 $this->guesser2
= $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
63 $this->extension1
= new TestExtension($this->guesser1
);
64 $this->extension2
= new TestExtension($this->guesser2
);
65 $this->registry
= new FormRegistry(array(
68 ), $this->resolvedTypeFactory
);
71 public function testGetTypeFromExtension()
73 $type = new FooType();
74 $resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
76 $this->extension2
->addType($type);
78 $this->resolvedTypeFactory
->expects($this->once())
79 ->method('createResolvedType')
81 ->will($this->returnValue($resolvedType));
83 $resolvedType->expects($this->any())
85 ->will($this->returnValue('foo'));
87 $resolvedType = $this->registry
->getType('foo');
89 $this->assertSame($resolvedType, $this->registry
->getType('foo'));
92 public function testGetTypeWithTypeExtensions()
94 $type = new FooType();
95 $ext1 = new FooTypeBarExtension();
96 $ext2 = new FooTypeBazExtension();
97 $resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
99 $this->extension2
->addType($type);
100 $this->extension1
->addTypeExtension($ext1);
101 $this->extension2
->addTypeExtension($ext2);
103 $this->resolvedTypeFactory
->expects($this->once())
104 ->method('createResolvedType')
105 ->with($type, array($ext1, $ext2))
106 ->will($this->returnValue($resolvedType));
108 $resolvedType->expects($this->any())
110 ->will($this->returnValue('foo'));
112 $this->assertSame($resolvedType, $this->registry
->getType('foo'));
115 public function testGetTypeConnectsParent()
117 $parentType = new FooType();
118 $type = new FooSubType();
119 $parentResolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
120 $resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
122 $this->extension1
->addType($parentType);
123 $this->extension2
->addType($type);
125 $this->resolvedTypeFactory
->expects($this->at(0))
126 ->method('createResolvedType')
128 ->will($this->returnValue($parentResolvedType));
130 $this->resolvedTypeFactory
->expects($this->at(1))
131 ->method('createResolvedType')
132 ->with($type, array(), $parentResolvedType)
133 ->will($this->returnValue($resolvedType));
135 $parentResolvedType->expects($this->any())
137 ->will($this->returnValue('foo'));
139 $resolvedType->expects($this->any())
141 ->will($this->returnValue('foo_sub_type'));
143 $this->assertSame($resolvedType, $this->registry
->getType('foo_sub_type'));
146 public function testGetTypeConnectsParentIfGetParentReturnsInstance()
148 $type = new FooSubTypeWithParentInstance();
149 $parentResolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
150 $resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
152 $this->extension1
->addType($type);
154 $this->resolvedTypeFactory
->expects($this->at(0))
155 ->method('createResolvedType')
156 ->with($this->isInstanceOf('Symfony\Component\Form\Tests\Fixtures\FooType'))
157 ->will($this->returnValue($parentResolvedType));
159 $this->resolvedTypeFactory
->expects($this->at(1))
160 ->method('createResolvedType')
161 ->with($type, array(), $parentResolvedType)
162 ->will($this->returnValue($resolvedType));
164 $parentResolvedType->expects($this->any())
166 ->will($this->returnValue('foo'));
168 $resolvedType->expects($this->any())
170 ->will($this->returnValue('foo_sub_type_parent_instance'));
172 $this->assertSame($resolvedType, $this->registry
->getType('foo_sub_type_parent_instance'));
176 * @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
178 public function testGetTypeThrowsExceptionIfParentNotFound()
180 $type = new FooSubType();
182 $this->extension1
->addType($type);
184 $this->registry
->getType($type);
188 * @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException
190 public function testGetTypeThrowsExceptionIfTypeNotFound()
192 $this->registry
->getType('bar');
196 * @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
198 public function testGetTypeThrowsExceptionIfNoString()
200 $this->registry
->getType(array());
203 public function testHasTypeAfterLoadingFromExtension()
205 $type = new FooType();
206 $resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
208 $this->resolvedTypeFactory
->expects($this->once())
209 ->method('createResolvedType')
211 ->will($this->returnValue($resolvedType));
213 $resolvedType->expects($this->any())
215 ->will($this->returnValue('foo'));
217 $this->assertFalse($this->registry
->hasType('foo'));
219 $this->extension2
->addType($type);
221 $this->assertTrue($this->registry
->hasType('foo'));
224 public function testGetTypeGuesser()
226 $expectedGuesser = new FormTypeGuesserChain(array($this->guesser1
, $this->guesser2
));
228 $this->assertEquals($expectedGuesser, $this->registry
->getTypeGuesser());
230 $registry = new FormRegistry(
231 array($this->getMock('Symfony\Component\Form\FormExtensionInterface')),
232 $this->resolvedTypeFactory
);
234 $this->assertNull($registry->getTypeGuesser());
237 public function testGetExtensions()
239 $expectedExtensions = array($this->extension1
, $this->extension2
);
241 $this->assertEquals($expectedExtensions, $this->registry
->getExtensions());