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\Extension\Core\EventListener
;
14 use Symfony\Component\Form\FormEvent
;
15 use Symfony\Component\Form\Extension\Core\EventListener\MergeCollectionListener
;
17 abstract class MergeCollectionListenerTest
extends \PHPUnit_Framework_TestCase
19 protected $dispatcher;
23 protected function setUp()
25 if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
26 $this->markTestSkipped('The "EventDispatcher" component is not available');
29 $this->dispatcher
= $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
30 $this->factory
= $this->getMock('Symfony\Component\Form\FormFactoryInterface');
31 $this->form
= $this->getForm('axes');
34 protected function tearDown()
36 $this->dispatcher
= null;
37 $this->factory
= null;
41 abstract protected function getBuilder($name = 'name');
43 protected function getForm($name = 'name', $propertyPath = null)
45 $propertyPath = $propertyPath ?: $name;
47 return $this->getBuilder($name)->setAttribute('property_path', $propertyPath)->getForm();
50 protected function getMockForm()
52 return $this->getMock('Symfony\Component\Form\Test\FormInterface');
55 public function getBooleanMatrix1()
63 public function getBooleanMatrix2()
73 abstract protected function getData(array $data);
76 * @dataProvider getBooleanMatrix1
78 public function testAddExtraEntriesIfAllowAdd($allowDelete)
80 $originalData = $this->getData(array(1 => 'second'));
81 $newData = $this->getData(array(0 => 'first', 1 => 'second', 2 => 'third'));
83 $listener = new MergeCollectionListener(true, $allowDelete);
85 $this->form
->setData($originalData);
87 $event = new FormEvent($this->form
, $newData);
88 $listener->onSubmit($event);
90 // The original object was modified
91 if (is_object($originalData)) {
92 $this->assertSame($originalData, $event->getData());
95 // The original object matches the new object
96 $this->assertEquals($newData, $event->getData());
100 * @dataProvider getBooleanMatrix1
102 public function testAddExtraEntriesIfAllowAddDontOverwriteExistingIndices($allowDelete)
104 $originalData = $this->getData(array(1 => 'first'));
105 $newData = $this->getData(array(0 => 'first', 1 => 'second'));
107 $listener = new MergeCollectionListener(true, $allowDelete);
109 $this->form
->setData($originalData);
111 $event = new FormEvent($this->form
, $newData);
112 $listener->onSubmit($event);
114 // The original object was modified
115 if (is_object($originalData)) {
116 $this->assertSame($originalData, $event->getData());
119 // The original object matches the new object
120 $this->assertEquals($this->getData(array(1 => 'first', 2 => 'second')), $event->getData());
124 * @dataProvider getBooleanMatrix1
126 public function testDoNothingIfNotAllowAdd($allowDelete)
128 $originalDataArray = array(1 => 'second');
129 $originalData = $this->getData($originalDataArray);
130 $newData = $this->getData(array(0 => 'first', 1 => 'second', 2 => 'third'));
132 $listener = new MergeCollectionListener(false, $allowDelete);
134 $this->form
->setData($originalData);
136 $event = new FormEvent($this->form
, $newData);
137 $listener->onSubmit($event);
139 // We still have the original object
140 if (is_object($originalData)) {
141 $this->assertSame($originalData, $event->getData());
144 // Nothing was removed
145 $this->assertEquals($this->getData($originalDataArray), $event->getData());
149 * @dataProvider getBooleanMatrix1
151 public function testRemoveMissingEntriesIfAllowDelete($allowAdd)
153 $originalData = $this->getData(array(0 => 'first', 1 => 'second', 2 => 'third'));
154 $newData = $this->getData(array(1 => 'second'));
156 $listener = new MergeCollectionListener($allowAdd, true);
158 $this->form
->setData($originalData);
160 $event = new FormEvent($this->form
, $newData);
161 $listener->onSubmit($event);
163 // The original object was modified
164 if (is_object($originalData)) {
165 $this->assertSame($originalData, $event->getData());
168 // The original object matches the new object
169 $this->assertEquals($newData, $event->getData());
173 * @dataProvider getBooleanMatrix1
175 public function testDoNothingIfNotAllowDelete($allowAdd)
177 $originalDataArray = array(0 => 'first', 1 => 'second', 2 => 'third');
178 $originalData = $this->getData($originalDataArray);
179 $newData = $this->getData(array(1 => 'second'));
181 $listener = new MergeCollectionListener($allowAdd, false);
183 $this->form
->setData($originalData);
185 $event = new FormEvent($this->form
, $newData);
186 $listener->onSubmit($event);
188 // We still have the original object
189 if (is_object($originalData)) {
190 $this->assertSame($originalData, $event->getData());
193 // Nothing was removed
194 $this->assertEquals($this->getData($originalDataArray), $event->getData());
198 * @dataProvider getBooleanMatrix2
199 * @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
201 public function testRequireArrayOrTraversable($allowAdd, $allowDelete)
203 $newData = 'no array or traversable';
204 $event = new FormEvent($this->form
, $newData);
205 $listener = new MergeCollectionListener($allowAdd, $allowDelete);
206 $listener->onSubmit($event);
209 public function testDealWithNullData()
211 $originalData = $this->getData(array(0 => 'first', 1 => 'second', 2 => 'third'));
214 $listener = new MergeCollectionListener(false, false);
216 $this->form
->setData($originalData);
218 $event = new FormEvent($this->form
, $newData);
219 $listener->onSubmit($event);
221 $this->assertSame($originalData, $event->getData());
225 * @dataProvider getBooleanMatrix1
227 public function testDealWithNullOriginalDataIfAllowAdd($allowDelete)
229 $originalData = null;
230 $newData = $this->getData(array(0 => 'first', 1 => 'second', 2 => 'third'));
232 $listener = new MergeCollectionListener(true, $allowDelete);
234 $this->form
->setData($originalData);
236 $event = new FormEvent($this->form
, $newData);
237 $listener->onSubmit($event);
239 $this->assertSame($newData, $event->getData());
243 * @dataProvider getBooleanMatrix1
245 public function testDontDealWithNullOriginalDataIfNotAllowAdd($allowDelete)
247 $originalData = null;
248 $newData = $this->getData(array(0 => 'first', 1 => 'second', 2 => 'third'));
250 $listener = new MergeCollectionListener(false, $allowDelete);
252 $this->form
->setData($originalData);
254 $event = new FormEvent($this->form
, $newData);
255 $listener->onSubmit($event);
257 $this->assertNull($event->getData());