]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/form/Symfony/Component/Form/Tests/FormBuilderTest.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / Tests / FormBuilderTest.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\FormBuilder;
15
16 class FormBuilderTest extends \PHPUnit_Framework_TestCase
17 {
18 private $dispatcher;
19
20 private $factory;
21
22 private $builder;
23
24 protected function setUp()
25 {
26 if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
27 $this->markTestSkipped('The "EventDispatcher" component is not available');
28 }
29
30 $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
31 $this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
32 $this->builder = new FormBuilder('name', null, $this->dispatcher, $this->factory);
33 }
34
35 protected function tearDown()
36 {
37 $this->dispatcher = null;
38 $this->factory = null;
39 $this->builder = null;
40 }
41
42 /**
43 * Changing the name is not allowed, otherwise the name and property path
44 * are not synchronized anymore
45 *
46 * @see FormType::buildForm
47 */
48 public function testNoSetName()
49 {
50 $this->assertFalse(method_exists($this->builder, 'setName'));
51 }
52
53 public function testAddNameNoStringAndNoInteger()
54 {
55 $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
56 $this->builder->add(true);
57 }
58
59 public function testAddTypeNoString()
60 {
61 $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
62 $this->builder->add('foo', 1234);
63 }
64
65 public function testAddWithGuessFluent()
66 {
67 $this->builder = new FormBuilder('name', 'stdClass', $this->dispatcher, $this->factory);
68 $builder = $this->builder->add('foo');
69 $this->assertSame($builder, $this->builder);
70 }
71
72 public function testAddIsFluent()
73 {
74 $builder = $this->builder->add('foo', 'text', array('bar' => 'baz'));
75 $this->assertSame($builder, $this->builder);
76 }
77
78 public function testAdd()
79 {
80 $this->assertFalse($this->builder->has('foo'));
81 $this->builder->add('foo', 'text');
82 $this->assertTrue($this->builder->has('foo'));
83 }
84
85 public function testAddIntegerName()
86 {
87 $this->assertFalse($this->builder->has(0));
88 $this->builder->add(0, 'text');
89 $this->assertTrue($this->builder->has(0));
90 }
91
92 public function testAll()
93 {
94 $this->factory->expects($this->once())
95 ->method('createNamedBuilder')
96 ->with('foo', 'text')
97 ->will($this->returnValue(new FormBuilder('foo', null, $this->dispatcher, $this->factory)));
98
99 $this->assertCount(0, $this->builder->all());
100 $this->assertFalse($this->builder->has('foo'));
101
102 $this->builder->add('foo', 'text');
103 $children = $this->builder->all();
104
105 $this->assertTrue($this->builder->has('foo'));
106 $this->assertCount(1, $children);
107 $this->assertArrayHasKey('foo', $children);
108 }
109
110 /*
111 * https://github.com/symfony/symfony/issues/4693
112 */
113 public function testMaintainOrderOfLazyAndExplicitChildren()
114 {
115 $this->builder->add('foo', 'text');
116 $this->builder->add($this->getFormBuilder('bar'));
117 $this->builder->add('baz', 'text');
118
119 $children = $this->builder->all();
120
121 $this->assertSame(array('foo', 'bar', 'baz'), array_keys($children));
122 }
123
124 public function testAddFormType()
125 {
126 $this->assertFalse($this->builder->has('foo'));
127 $this->builder->add('foo', $this->getMock('Symfony\Component\Form\FormTypeInterface'));
128 $this->assertTrue($this->builder->has('foo'));
129 }
130
131 public function testRemove()
132 {
133 $this->builder->add('foo', 'text');
134 $this->builder->remove('foo');
135 $this->assertFalse($this->builder->has('foo'));
136 }
137
138 public function testRemoveUnknown()
139 {
140 $this->builder->remove('foo');
141 $this->assertFalse($this->builder->has('foo'));
142 }
143
144 // https://github.com/symfony/symfony/pull/4826
145 public function testRemoveAndGetForm()
146 {
147 $this->builder->add('foo', 'text');
148 $this->builder->remove('foo');
149 $form = $this->builder->getForm();
150 $this->assertInstanceOf('Symfony\Component\Form\Form', $form);
151 }
152
153 public function testCreateNoTypeNo()
154 {
155 $this->factory->expects($this->once())
156 ->method('createNamedBuilder')
157 ->with('foo', 'text', null, array())
158 ;
159
160 $this->builder->create('foo');
161 }
162
163 public function testGetUnknown()
164 {
165 $this->setExpectedException('Symfony\Component\Form\Exception\InvalidArgumentException', 'The child with the name "foo" does not exist.');
166 $this->builder->get('foo');
167 }
168
169 public function testGetExplicitType()
170 {
171 $expectedType = 'text';
172 $expectedName = 'foo';
173 $expectedOptions = array('bar' => 'baz');
174
175 $this->factory->expects($this->once())
176 ->method('createNamedBuilder')
177 ->with($expectedName, $expectedType, null, $expectedOptions)
178 ->will($this->returnValue($this->getFormBuilder()));
179
180 $this->builder->add($expectedName, $expectedType, $expectedOptions);
181 $builder = $this->builder->get($expectedName);
182
183 $this->assertNotSame($builder, $this->builder);
184 }
185
186 public function testGetGuessedType()
187 {
188 $expectedName = 'foo';
189 $expectedOptions = array('bar' => 'baz');
190
191 $this->factory->expects($this->once())
192 ->method('createBuilderForProperty')
193 ->with('stdClass', $expectedName, null, $expectedOptions)
194 ->will($this->returnValue($this->getFormBuilder()));
195
196 $this->builder = new FormBuilder('name', 'stdClass', $this->dispatcher, $this->factory);
197 $this->builder->add($expectedName, null, $expectedOptions);
198 $builder = $this->builder->get($expectedName);
199
200 $this->assertNotSame($builder, $this->builder);
201 }
202
203 public function testGetFormConfigErasesReferences()
204 {
205 $builder = new FormBuilder('name', null, $this->dispatcher, $this->factory);
206 $builder->add(new FormBuilder('child', null, $this->dispatcher, $this->factory));
207
208 $config = $builder->getFormConfig();
209 $reflClass = new \ReflectionClass($config);
210 $children = $reflClass->getProperty('children');
211 $unresolvedChildren = $reflClass->getProperty('unresolvedChildren');
212
213 $children->setAccessible(true);
214 $unresolvedChildren->setAccessible(true);
215
216 $this->assertEmpty($children->getValue($config));
217 $this->assertEmpty($unresolvedChildren->getValue($config));
218 }
219
220 private function getFormBuilder($name = 'name')
221 {
222 $mock = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
223 ->disableOriginalConstructor()
224 ->getMock();
225
226 $mock->expects($this->any())
227 ->method('getName')
228 ->will($this->returnValue($name));
229
230 return $mock;
231 }
232 }