]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/form/Symfony/Component/Form/Tests/FormRegistryTest.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / Tests / FormRegistryTest.php
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;
13
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;
22
23 /**
24 * @author Bernhard Schussek <bschussek@gmail.com>
25 */
26 class FormRegistryTest extends \PHPUnit_Framework_TestCase
27 {
28 /**
29 * @var FormRegistry
30 */
31 private $registry;
32
33 /**
34 * @var \PHPUnit_Framework_MockObject_MockObject
35 */
36 private $resolvedTypeFactory;
37
38 /**
39 * @var \PHPUnit_Framework_MockObject_MockObject
40 */
41 private $guesser1;
42
43 /**
44 * @var \PHPUnit_Framework_MockObject_MockObject
45 */
46 private $guesser2;
47
48 /**
49 * @var TestExtension
50 */
51 private $extension1;
52
53 /**
54 * @var TestExtension
55 */
56 private $extension2;
57
58 protected function setUp()
59 {
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(
66 $this->extension1,
67 $this->extension2,
68 ), $this->resolvedTypeFactory);
69 }
70
71 public function testGetTypeFromExtension()
72 {
73 $type = new FooType();
74 $resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
75
76 $this->extension2->addType($type);
77
78 $this->resolvedTypeFactory->expects($this->once())
79 ->method('createResolvedType')
80 ->with($type)
81 ->will($this->returnValue($resolvedType));
82
83 $resolvedType->expects($this->any())
84 ->method('getName')
85 ->will($this->returnValue('foo'));
86
87 $resolvedType = $this->registry->getType('foo');
88
89 $this->assertSame($resolvedType, $this->registry->getType('foo'));
90 }
91
92 public function testGetTypeWithTypeExtensions()
93 {
94 $type = new FooType();
95 $ext1 = new FooTypeBarExtension();
96 $ext2 = new FooTypeBazExtension();
97 $resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
98
99 $this->extension2->addType($type);
100 $this->extension1->addTypeExtension($ext1);
101 $this->extension2->addTypeExtension($ext2);
102
103 $this->resolvedTypeFactory->expects($this->once())
104 ->method('createResolvedType')
105 ->with($type, array($ext1, $ext2))
106 ->will($this->returnValue($resolvedType));
107
108 $resolvedType->expects($this->any())
109 ->method('getName')
110 ->will($this->returnValue('foo'));
111
112 $this->assertSame($resolvedType, $this->registry->getType('foo'));
113 }
114
115 public function testGetTypeConnectsParent()
116 {
117 $parentType = new FooType();
118 $type = new FooSubType();
119 $parentResolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
120 $resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
121
122 $this->extension1->addType($parentType);
123 $this->extension2->addType($type);
124
125 $this->resolvedTypeFactory->expects($this->at(0))
126 ->method('createResolvedType')
127 ->with($parentType)
128 ->will($this->returnValue($parentResolvedType));
129
130 $this->resolvedTypeFactory->expects($this->at(1))
131 ->method('createResolvedType')
132 ->with($type, array(), $parentResolvedType)
133 ->will($this->returnValue($resolvedType));
134
135 $parentResolvedType->expects($this->any())
136 ->method('getName')
137 ->will($this->returnValue('foo'));
138
139 $resolvedType->expects($this->any())
140 ->method('getName')
141 ->will($this->returnValue('foo_sub_type'));
142
143 $this->assertSame($resolvedType, $this->registry->getType('foo_sub_type'));
144 }
145
146 public function testGetTypeConnectsParentIfGetParentReturnsInstance()
147 {
148 $type = new FooSubTypeWithParentInstance();
149 $parentResolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
150 $resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
151
152 $this->extension1->addType($type);
153
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));
158
159 $this->resolvedTypeFactory->expects($this->at(1))
160 ->method('createResolvedType')
161 ->with($type, array(), $parentResolvedType)
162 ->will($this->returnValue($resolvedType));
163
164 $parentResolvedType->expects($this->any())
165 ->method('getName')
166 ->will($this->returnValue('foo'));
167
168 $resolvedType->expects($this->any())
169 ->method('getName')
170 ->will($this->returnValue('foo_sub_type_parent_instance'));
171
172 $this->assertSame($resolvedType, $this->registry->getType('foo_sub_type_parent_instance'));
173 }
174
175 /**
176 * @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
177 */
178 public function testGetTypeThrowsExceptionIfParentNotFound()
179 {
180 $type = new FooSubType();
181
182 $this->extension1->addType($type);
183
184 $this->registry->getType($type);
185 }
186
187 /**
188 * @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException
189 */
190 public function testGetTypeThrowsExceptionIfTypeNotFound()
191 {
192 $this->registry->getType('bar');
193 }
194
195 /**
196 * @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
197 */
198 public function testGetTypeThrowsExceptionIfNoString()
199 {
200 $this->registry->getType(array());
201 }
202
203 public function testHasTypeAfterLoadingFromExtension()
204 {
205 $type = new FooType();
206 $resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
207
208 $this->resolvedTypeFactory->expects($this->once())
209 ->method('createResolvedType')
210 ->with($type)
211 ->will($this->returnValue($resolvedType));
212
213 $resolvedType->expects($this->any())
214 ->method('getName')
215 ->will($this->returnValue('foo'));
216
217 $this->assertFalse($this->registry->hasType('foo'));
218
219 $this->extension2->addType($type);
220
221 $this->assertTrue($this->registry->hasType('foo'));
222 }
223
224 public function testGetTypeGuesser()
225 {
226 $expectedGuesser = new FormTypeGuesserChain(array($this->guesser1, $this->guesser2));
227
228 $this->assertEquals($expectedGuesser, $this->registry->getTypeGuesser());
229
230 $registry = new FormRegistry(
231 array($this->getMock('Symfony\Component\Form\FormExtensionInterface')),
232 $this->resolvedTypeFactory);
233
234 $this->assertNull($registry->getTypeGuesser());
235 }
236
237 public function testGetExtensions()
238 {
239 $expectedExtensions = array($this->extension1, $this->extension2);
240
241 $this->assertEquals($expectedExtensions, $this->registry->getExtensions());
242 }
243 }