]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/EventListener/MergeCollectionListenerTest.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / Tests / Extension / Core / EventListener / MergeCollectionListenerTest.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\Extension\Core\EventListener;
13
14 use Symfony\Component\Form\FormEvent;
15 use Symfony\Component\Form\Extension\Core\EventListener\MergeCollectionListener;
16
17 abstract class MergeCollectionListenerTest extends \PHPUnit_Framework_TestCase
18 {
19 protected $dispatcher;
20 protected $factory;
21 protected $form;
22
23 protected function setUp()
24 {
25 if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
26 $this->markTestSkipped('The "EventDispatcher" component is not available');
27 }
28
29 $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
30 $this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
31 $this->form = $this->getForm('axes');
32 }
33
34 protected function tearDown()
35 {
36 $this->dispatcher = null;
37 $this->factory = null;
38 $this->form = null;
39 }
40
41 abstract protected function getBuilder($name = 'name');
42
43 protected function getForm($name = 'name', $propertyPath = null)
44 {
45 $propertyPath = $propertyPath ?: $name;
46
47 return $this->getBuilder($name)->setAttribute('property_path', $propertyPath)->getForm();
48 }
49
50 protected function getMockForm()
51 {
52 return $this->getMock('Symfony\Component\Form\Test\FormInterface');
53 }
54
55 public function getBooleanMatrix1()
56 {
57 return array(
58 array(true),
59 array(false),
60 );
61 }
62
63 public function getBooleanMatrix2()
64 {
65 return array(
66 array(true, true),
67 array(true, false),
68 array(false, true),
69 array(false, false),
70 );
71 }
72
73 abstract protected function getData(array $data);
74
75 /**
76 * @dataProvider getBooleanMatrix1
77 */
78 public function testAddExtraEntriesIfAllowAdd($allowDelete)
79 {
80 $originalData = $this->getData(array(1 => 'second'));
81 $newData = $this->getData(array(0 => 'first', 1 => 'second', 2 => 'third'));
82
83 $listener = new MergeCollectionListener(true, $allowDelete);
84
85 $this->form->setData($originalData);
86
87 $event = new FormEvent($this->form, $newData);
88 $listener->onSubmit($event);
89
90 // The original object was modified
91 if (is_object($originalData)) {
92 $this->assertSame($originalData, $event->getData());
93 }
94
95 // The original object matches the new object
96 $this->assertEquals($newData, $event->getData());
97 }
98
99 /**
100 * @dataProvider getBooleanMatrix1
101 */
102 public function testAddExtraEntriesIfAllowAddDontOverwriteExistingIndices($allowDelete)
103 {
104 $originalData = $this->getData(array(1 => 'first'));
105 $newData = $this->getData(array(0 => 'first', 1 => 'second'));
106
107 $listener = new MergeCollectionListener(true, $allowDelete);
108
109 $this->form->setData($originalData);
110
111 $event = new FormEvent($this->form, $newData);
112 $listener->onSubmit($event);
113
114 // The original object was modified
115 if (is_object($originalData)) {
116 $this->assertSame($originalData, $event->getData());
117 }
118
119 // The original object matches the new object
120 $this->assertEquals($this->getData(array(1 => 'first', 2 => 'second')), $event->getData());
121 }
122
123 /**
124 * @dataProvider getBooleanMatrix1
125 */
126 public function testDoNothingIfNotAllowAdd($allowDelete)
127 {
128 $originalDataArray = array(1 => 'second');
129 $originalData = $this->getData($originalDataArray);
130 $newData = $this->getData(array(0 => 'first', 1 => 'second', 2 => 'third'));
131
132 $listener = new MergeCollectionListener(false, $allowDelete);
133
134 $this->form->setData($originalData);
135
136 $event = new FormEvent($this->form, $newData);
137 $listener->onSubmit($event);
138
139 // We still have the original object
140 if (is_object($originalData)) {
141 $this->assertSame($originalData, $event->getData());
142 }
143
144 // Nothing was removed
145 $this->assertEquals($this->getData($originalDataArray), $event->getData());
146 }
147
148 /**
149 * @dataProvider getBooleanMatrix1
150 */
151 public function testRemoveMissingEntriesIfAllowDelete($allowAdd)
152 {
153 $originalData = $this->getData(array(0 => 'first', 1 => 'second', 2 => 'third'));
154 $newData = $this->getData(array(1 => 'second'));
155
156 $listener = new MergeCollectionListener($allowAdd, true);
157
158 $this->form->setData($originalData);
159
160 $event = new FormEvent($this->form, $newData);
161 $listener->onSubmit($event);
162
163 // The original object was modified
164 if (is_object($originalData)) {
165 $this->assertSame($originalData, $event->getData());
166 }
167
168 // The original object matches the new object
169 $this->assertEquals($newData, $event->getData());
170 }
171
172 /**
173 * @dataProvider getBooleanMatrix1
174 */
175 public function testDoNothingIfNotAllowDelete($allowAdd)
176 {
177 $originalDataArray = array(0 => 'first', 1 => 'second', 2 => 'third');
178 $originalData = $this->getData($originalDataArray);
179 $newData = $this->getData(array(1 => 'second'));
180
181 $listener = new MergeCollectionListener($allowAdd, false);
182
183 $this->form->setData($originalData);
184
185 $event = new FormEvent($this->form, $newData);
186 $listener->onSubmit($event);
187
188 // We still have the original object
189 if (is_object($originalData)) {
190 $this->assertSame($originalData, $event->getData());
191 }
192
193 // Nothing was removed
194 $this->assertEquals($this->getData($originalDataArray), $event->getData());
195 }
196
197 /**
198 * @dataProvider getBooleanMatrix2
199 * @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
200 */
201 public function testRequireArrayOrTraversable($allowAdd, $allowDelete)
202 {
203 $newData = 'no array or traversable';
204 $event = new FormEvent($this->form, $newData);
205 $listener = new MergeCollectionListener($allowAdd, $allowDelete);
206 $listener->onSubmit($event);
207 }
208
209 public function testDealWithNullData()
210 {
211 $originalData = $this->getData(array(0 => 'first', 1 => 'second', 2 => 'third'));
212 $newData = null;
213
214 $listener = new MergeCollectionListener(false, false);
215
216 $this->form->setData($originalData);
217
218 $event = new FormEvent($this->form, $newData);
219 $listener->onSubmit($event);
220
221 $this->assertSame($originalData, $event->getData());
222 }
223
224 /**
225 * @dataProvider getBooleanMatrix1
226 */
227 public function testDealWithNullOriginalDataIfAllowAdd($allowDelete)
228 {
229 $originalData = null;
230 $newData = $this->getData(array(0 => 'first', 1 => 'second', 2 => 'third'));
231
232 $listener = new MergeCollectionListener(true, $allowDelete);
233
234 $this->form->setData($originalData);
235
236 $event = new FormEvent($this->form, $newData);
237 $listener->onSubmit($event);
238
239 $this->assertSame($newData, $event->getData());
240 }
241
242 /**
243 * @dataProvider getBooleanMatrix1
244 */
245 public function testDontDealWithNullOriginalDataIfNotAllowAdd($allowDelete)
246 {
247 $originalData = null;
248 $newData = $this->getData(array(0 => 'first', 1 => 'second', 2 => 'third'));
249
250 $listener = new MergeCollectionListener(false, $allowDelete);
251
252 $this->form->setData($originalData);
253
254 $event = new FormEvent($this->form, $newData);
255 $listener->onSubmit($event);
256
257 $this->assertNull($event->getData());
258 }
259 }