]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/form/Symfony/Component/Form/Tests/FormFactoryTest.php
gitignore vendor
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / Tests / FormFactoryTest.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\FormTypeGuesserChain;
15 use Symfony\Component\Form\FormFactory;
16 use Symfony\Component\Form\Guess\Guess;
17 use Symfony\Component\Form\Guess\ValueGuess;
18 use Symfony\Component\Form\Guess\TypeGuess;
19 use Symfony\Component\Form\Tests\Fixtures\Author;
20 use Symfony\Component\Form\Tests\Fixtures\FooType;
21 use Symfony\Component\Form\Tests\Fixtures\FooSubType;
22 use Symfony\Component\Form\Tests\Fixtures\FooSubTypeWithParentInstance;
23
24 /**
25 * @author Bernhard Schussek <bschussek@gmail.com>
26 */
27 class FormFactoryTest extends \PHPUnit_Framework_TestCase
28 {
29 /**
30 * @var \PHPUnit_Framework_MockObject_MockObject
31 */
32 private $guesser1;
33
34 /**
35 * @var \PHPUnit_Framework_MockObject_MockObject
36 */
37 private $guesser2;
38
39 /**
40 * @var \PHPUnit_Framework_MockObject_MockObject
41 */
42 private $registry;
43
44 /**
45 * @var \PHPUnit_Framework_MockObject_MockObject
46 */
47 private $resolvedTypeFactory;
48
49 /**
50 * @var FormFactory
51 */
52 private $factory;
53
54 protected function setUp()
55 {
56 if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
57 $this->markTestSkipped('The "EventDispatcher" component is not available');
58 }
59
60 $this->resolvedTypeFactory = $this->getMock('Symfony\Component\Form\ResolvedFormTypeFactoryInterface');
61 $this->guesser1 = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
62 $this->guesser2 = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
63 $this->registry = $this->getMock('Symfony\Component\Form\FormRegistryInterface');
64 $this->factory = new FormFactory($this->registry, $this->resolvedTypeFactory);
65
66 $this->registry->expects($this->any())
67 ->method('getTypeGuesser')
68 ->will($this->returnValue(new FormTypeGuesserChain(array(
69 $this->guesser1,
70 $this->guesser2,
71 ))));
72 }
73
74 public function testCreateNamedBuilderWithTypeName()
75 {
76 $options = array('a' => '1', 'b' => '2');
77 $resolvedType = $this->getMockResolvedType();
78
79 $this->registry->expects($this->once())
80 ->method('getType')
81 ->with('type')
82 ->will($this->returnValue($resolvedType));
83
84 $resolvedType->expects($this->once())
85 ->method('createBuilder')
86 ->with($this->factory, 'name', $options)
87 ->will($this->returnValue('BUILDER'));
88
89 $this->assertSame('BUILDER', $this->factory->createNamedBuilder('name', 'type', null, $options));
90 }
91
92 public function testCreateNamedBuilderWithTypeInstance()
93 {
94 $options = array('a' => '1', 'b' => '2');
95 $type = new FooType();
96 $resolvedType = $this->getMockResolvedType();
97
98 $this->resolvedTypeFactory->expects($this->once())
99 ->method('createResolvedType')
100 ->with($type)
101 ->will($this->returnValue($resolvedType));
102
103 $resolvedType->expects($this->once())
104 ->method('createBuilder')
105 ->with($this->factory, 'name', $options)
106 ->will($this->returnValue('BUILDER'));
107
108 $this->assertSame('BUILDER', $this->factory->createNamedBuilder('name', $type, null, $options));
109 }
110
111 public function testCreateNamedBuilderWithTypeInstanceWithParentType()
112 {
113 $options = array('a' => '1', 'b' => '2');
114 $type = new FooSubType();
115 $resolvedType = $this->getMockResolvedType();
116 $parentResolvedType = $this->getMockResolvedType();
117
118 $this->registry->expects($this->once())
119 ->method('getType')
120 ->with('foo')
121 ->will($this->returnValue($parentResolvedType));
122
123 $this->resolvedTypeFactory->expects($this->once())
124 ->method('createResolvedType')
125 ->with($type, array(), $parentResolvedType)
126 ->will($this->returnValue($resolvedType));
127
128 $resolvedType->expects($this->once())
129 ->method('createBuilder')
130 ->with($this->factory, 'name', $options)
131 ->will($this->returnValue('BUILDER'));
132
133 $this->assertSame('BUILDER', $this->factory->createNamedBuilder('name', $type, null, $options));
134 }
135
136 public function testCreateNamedBuilderWithTypeInstanceWithParentTypeInstance()
137 {
138 $options = array('a' => '1', 'b' => '2');
139 $type = new FooSubTypeWithParentInstance();
140 $resolvedType = $this->getMockResolvedType();
141 $parentResolvedType = $this->getMockResolvedType();
142
143 $this->resolvedTypeFactory->expects($this->at(0))
144 ->method('createResolvedType')
145 ->with($type->getParent())
146 ->will($this->returnValue($parentResolvedType));
147
148 $this->resolvedTypeFactory->expects($this->at(1))
149 ->method('createResolvedType')
150 ->with($type, array(), $parentResolvedType)
151 ->will($this->returnValue($resolvedType));
152
153 $resolvedType->expects($this->once())
154 ->method('createBuilder')
155 ->with($this->factory, 'name', $options)
156 ->will($this->returnValue('BUILDER'));
157
158 $this->assertSame('BUILDER', $this->factory->createNamedBuilder('name', $type, null, $options));
159 }
160
161 public function testCreateNamedBuilderWithResolvedTypeInstance()
162 {
163 $options = array('a' => '1', 'b' => '2');
164 $resolvedType = $this->getMockResolvedType();
165
166 $resolvedType->expects($this->once())
167 ->method('createBuilder')
168 ->with($this->factory, 'name', $options)
169 ->will($this->returnValue('BUILDER'));
170
171 $this->assertSame('BUILDER', $this->factory->createNamedBuilder('name', $resolvedType, null, $options));
172 }
173
174 public function testCreateNamedBuilderFillsDataOption()
175 {
176 $givenOptions = array('a' => '1', 'b' => '2');
177 $expectedOptions = array_merge($givenOptions, array('data' => 'DATA'));
178 $resolvedType = $this->getMockResolvedType();
179
180 $this->registry->expects($this->once())
181 ->method('getType')
182 ->with('type')
183 ->will($this->returnValue($resolvedType));
184
185 $resolvedType->expects($this->once())
186 ->method('createBuilder')
187 ->with($this->factory, 'name', $expectedOptions)
188 ->will($this->returnValue('BUILDER'));
189
190 $this->assertSame('BUILDER', $this->factory->createNamedBuilder('name', 'type', 'DATA', $givenOptions));
191 }
192
193 public function testCreateNamedBuilderDoesNotOverrideExistingDataOption()
194 {
195 $options = array('a' => '1', 'b' => '2', 'data' => 'CUSTOM');
196 $resolvedType = $this->getMockResolvedType();
197
198 $this->registry->expects($this->once())
199 ->method('getType')
200 ->with('type')
201 ->will($this->returnValue($resolvedType));
202
203 $resolvedType->expects($this->once())
204 ->method('createBuilder')
205 ->with($this->factory, 'name', $options)
206 ->will($this->returnValue('BUILDER'));
207
208 $this->assertSame('BUILDER', $this->factory->createNamedBuilder('name', 'type', 'DATA', $options));
209 }
210
211 /**
212 * @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
213 * @expectedExceptionMessage Expected argument of type "string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface", "stdClass" given
214 */
215 public function testCreateNamedBuilderThrowsUnderstandableException()
216 {
217 $this->factory->createNamedBuilder('name', new \stdClass());
218 }
219
220 public function testCreateUsesTypeNameIfTypeGivenAsString()
221 {
222 $options = array('a' => '1', 'b' => '2');
223 $resolvedType = $this->getMockResolvedType();
224 $builder = $this->getMockFormBuilder();
225
226 $this->registry->expects($this->once())
227 ->method('getType')
228 ->with('TYPE')
229 ->will($this->returnValue($resolvedType));
230
231 $resolvedType->expects($this->once())
232 ->method('createBuilder')
233 ->with($this->factory, 'TYPE', $options)
234 ->will($this->returnValue($builder));
235
236 $builder->expects($this->once())
237 ->method('getForm')
238 ->will($this->returnValue('FORM'));
239
240 $this->assertSame('FORM', $this->factory->create('TYPE', null, $options));
241 }
242
243 public function testCreateUsesTypeNameIfTypeGivenAsObject()
244 {
245 $options = array('a' => '1', 'b' => '2');
246 $resolvedType = $this->getMockResolvedType();
247 $builder = $this->getMockFormBuilder();
248
249 $resolvedType->expects($this->once())
250 ->method('getName')
251 ->will($this->returnValue('TYPE'));
252
253 $resolvedType->expects($this->once())
254 ->method('createBuilder')
255 ->with($this->factory, 'TYPE', $options)
256 ->will($this->returnValue($builder));
257
258 $builder->expects($this->once())
259 ->method('getForm')
260 ->will($this->returnValue('FORM'));
261
262 $this->assertSame('FORM', $this->factory->create($resolvedType, null, $options));
263 }
264
265 public function testCreateNamed()
266 {
267 $options = array('a' => '1', 'b' => '2');
268 $resolvedType = $this->getMockResolvedType();
269 $builder = $this->getMockFormBuilder();
270
271 $this->registry->expects($this->once())
272 ->method('getType')
273 ->with('type')
274 ->will($this->returnValue($resolvedType));
275
276 $resolvedType->expects($this->once())
277 ->method('createBuilder')
278 ->with($this->factory, 'name', $options)
279 ->will($this->returnValue($builder));
280
281 $builder->expects($this->once())
282 ->method('getForm')
283 ->will($this->returnValue('FORM'));
284
285 $this->assertSame('FORM', $this->factory->createNamed('name', 'type', null, $options));
286 }
287
288 public function testCreateBuilderForPropertyWithoutTypeGuesser()
289 {
290 $registry = $this->getMock('Symfony\Component\Form\FormRegistryInterface');
291 $factory = $this->getMockBuilder('Symfony\Component\Form\FormFactory')
292 ->setMethods(array('createNamedBuilder'))
293 ->setConstructorArgs(array($registry, $this->resolvedTypeFactory))
294 ->getMock();
295
296 $factory->expects($this->once())
297 ->method('createNamedBuilder')
298 ->with('firstName', 'text', null, array())
299 ->will($this->returnValue('builderInstance'));
300
301 $builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
302
303 $this->assertEquals('builderInstance', $builder);
304 }
305
306 public function testCreateBuilderForPropertyCreatesFormWithHighestConfidence()
307 {
308 $this->guesser1->expects($this->once())
309 ->method('guessType')
310 ->with('Application\Author', 'firstName')
311 ->will($this->returnValue(new TypeGuess(
312 'text',
313 array('max_length' => 10),
314 Guess::MEDIUM_CONFIDENCE
315 )));
316
317 $this->guesser2->expects($this->once())
318 ->method('guessType')
319 ->with('Application\Author', 'firstName')
320 ->will($this->returnValue(new TypeGuess(
321 'password',
322 array('max_length' => 7),
323 Guess::HIGH_CONFIDENCE
324 )));
325
326 $factory = $this->getMockFactory(array('createNamedBuilder'));
327
328 $factory->expects($this->once())
329 ->method('createNamedBuilder')
330 ->with('firstName', 'password', null, array('max_length' => 7))
331 ->will($this->returnValue('builderInstance'));
332
333 $builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
334
335 $this->assertEquals('builderInstance', $builder);
336 }
337
338 public function testCreateBuilderCreatesTextFormIfNoGuess()
339 {
340 $this->guesser1->expects($this->once())
341 ->method('guessType')
342 ->with('Application\Author', 'firstName')
343 ->will($this->returnValue(null));
344
345 $factory = $this->getMockFactory(array('createNamedBuilder'));
346
347 $factory->expects($this->once())
348 ->method('createNamedBuilder')
349 ->with('firstName', 'text')
350 ->will($this->returnValue('builderInstance'));
351
352 $builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
353
354 $this->assertEquals('builderInstance', $builder);
355 }
356
357 public function testOptionsCanBeOverridden()
358 {
359 $this->guesser1->expects($this->once())
360 ->method('guessType')
361 ->with('Application\Author', 'firstName')
362 ->will($this->returnValue(new TypeGuess(
363 'text',
364 array('max_length' => 10),
365 Guess::MEDIUM_CONFIDENCE
366 )));
367
368 $factory = $this->getMockFactory(array('createNamedBuilder'));
369
370 $factory->expects($this->once())
371 ->method('createNamedBuilder')
372 ->with('firstName', 'text', null, array('max_length' => 11))
373 ->will($this->returnValue('builderInstance'));
374
375 $builder = $factory->createBuilderForProperty(
376 'Application\Author',
377 'firstName',
378 null,
379 array('max_length' => 11)
380 );
381
382 $this->assertEquals('builderInstance', $builder);
383 }
384
385 public function testCreateBuilderUsesMaxLengthIfFound()
386 {
387 $this->guesser1->expects($this->once())
388 ->method('guessMaxLength')
389 ->with('Application\Author', 'firstName')
390 ->will($this->returnValue(new ValueGuess(
391 15,
392 Guess::MEDIUM_CONFIDENCE
393 )));
394
395 $this->guesser2->expects($this->once())
396 ->method('guessMaxLength')
397 ->with('Application\Author', 'firstName')
398 ->will($this->returnValue(new ValueGuess(
399 20,
400 Guess::HIGH_CONFIDENCE
401 )));
402
403 $factory = $this->getMockFactory(array('createNamedBuilder'));
404
405 $factory->expects($this->once())
406 ->method('createNamedBuilder')
407 ->with('firstName', 'text', null, array('max_length' => 20))
408 ->will($this->returnValue('builderInstance'));
409
410 $builder = $factory->createBuilderForProperty(
411 'Application\Author',
412 'firstName'
413 );
414
415 $this->assertEquals('builderInstance', $builder);
416 }
417
418 public function testCreateBuilderUsesRequiredSettingWithHighestConfidence()
419 {
420 $this->guesser1->expects($this->once())
421 ->method('guessRequired')
422 ->with('Application\Author', 'firstName')
423 ->will($this->returnValue(new ValueGuess(
424 true,
425 Guess::MEDIUM_CONFIDENCE
426 )));
427
428 $this->guesser2->expects($this->once())
429 ->method('guessRequired')
430 ->with('Application\Author', 'firstName')
431 ->will($this->returnValue(new ValueGuess(
432 false,
433 Guess::HIGH_CONFIDENCE
434 )));
435
436 $factory = $this->getMockFactory(array('createNamedBuilder'));
437
438 $factory->expects($this->once())
439 ->method('createNamedBuilder')
440 ->with('firstName', 'text', null, array('required' => false))
441 ->will($this->returnValue('builderInstance'));
442
443 $builder = $factory->createBuilderForProperty(
444 'Application\Author',
445 'firstName'
446 );
447
448 $this->assertEquals('builderInstance', $builder);
449 }
450
451 public function testCreateBuilderUsesPatternIfFound()
452 {
453 $this->guesser1->expects($this->once())
454 ->method('guessPattern')
455 ->with('Application\Author', 'firstName')
456 ->will($this->returnValue(new ValueGuess(
457 '[a-z]',
458 Guess::MEDIUM_CONFIDENCE
459 )));
460
461 $this->guesser2->expects($this->once())
462 ->method('guessPattern')
463 ->with('Application\Author', 'firstName')
464 ->will($this->returnValue(new ValueGuess(
465 '[a-zA-Z]',
466 Guess::HIGH_CONFIDENCE
467 )));
468
469 $factory = $this->getMockFactory(array('createNamedBuilder'));
470
471 $factory->expects($this->once())
472 ->method('createNamedBuilder')
473 ->with('firstName', 'text', null, array('pattern' => '[a-zA-Z]'))
474 ->will($this->returnValue('builderInstance'));
475
476 $builder = $factory->createBuilderForProperty(
477 'Application\Author',
478 'firstName'
479 );
480
481 $this->assertEquals('builderInstance', $builder);
482 }
483
484 private function getMockFactory(array $methods = array())
485 {
486 return $this->getMockBuilder('Symfony\Component\Form\FormFactory')
487 ->setMethods($methods)
488 ->setConstructorArgs(array($this->registry, $this->resolvedTypeFactory))
489 ->getMock();
490 }
491
492 private function getMockResolvedType()
493 {
494 return $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
495 }
496
497 private function getMockType()
498 {
499 return $this->getMock('Symfony\Component\Form\FormTypeInterface');
500 }
501
502 private function getMockFormBuilder()
503 {
504 return $this->getMock('Symfony\Component\Form\Test\FormBuilderInterface');
505 }
506 }