* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Form\Tests\Extension\Core\EventListener; use PHPUnit\Framework\TestCase; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\Extension\Core\EventListener\MergeCollectionListener; abstract class MergeCollectionListenerTest extends TestCase { protected $dispatcher; protected $factory; protected $form; protected function setUp() { $this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); $this->factory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock(); $this->form = $this->getForm('axes'); } protected function tearDown() { $this->dispatcher = null; $this->factory = null; $this->form = null; } abstract protected function getBuilder($name = 'name'); protected function getForm($name = 'name', $propertyPath = null) { $propertyPath = $propertyPath ?: $name; return $this->getBuilder($name)->setAttribute('property_path', $propertyPath)->getForm(); } protected function getMockForm() { return $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock(); } public function getBooleanMatrix1() { return array( array(true), array(false), ); } public function getBooleanMatrix2() { return array( array(true, true), array(true, false), array(false, true), array(false, false), ); } abstract protected function getData(array $data); /** * @dataProvider getBooleanMatrix1 */ public function testAddExtraEntriesIfAllowAdd($allowDelete) { $originalData = $this->getData(array(1 => 'second')); $newData = $this->getData(array(0 => 'first', 1 => 'second', 2 => 'third')); $listener = new MergeCollectionListener(true, $allowDelete); $this->form->setData($originalData); $event = new FormEvent($this->form, $newData); $listener->onSubmit($event); // The original object was modified if (is_object($originalData)) { $this->assertSame($originalData, $event->getData()); } // The original object matches the new object $this->assertEquals($newData, $event->getData()); } /** * @dataProvider getBooleanMatrix1 */ public function testAddExtraEntriesIfAllowAddDontOverwriteExistingIndices($allowDelete) { $originalData = $this->getData(array(1 => 'first')); $newData = $this->getData(array(0 => 'first', 1 => 'second')); $listener = new MergeCollectionListener(true, $allowDelete); $this->form->setData($originalData); $event = new FormEvent($this->form, $newData); $listener->onSubmit($event); // The original object was modified if (is_object($originalData)) { $this->assertSame($originalData, $event->getData()); } // The original object matches the new object $this->assertEquals($this->getData(array(1 => 'first', 2 => 'second')), $event->getData()); } /** * @dataProvider getBooleanMatrix1 */ public function testDoNothingIfNotAllowAdd($allowDelete) { $originalDataArray = array(1 => 'second'); $originalData = $this->getData($originalDataArray); $newData = $this->getData(array(0 => 'first', 1 => 'second', 2 => 'third')); $listener = new MergeCollectionListener(false, $allowDelete); $this->form->setData($originalData); $event = new FormEvent($this->form, $newData); $listener->onSubmit($event); // We still have the original object if (is_object($originalData)) { $this->assertSame($originalData, $event->getData()); } // Nothing was removed $this->assertEquals($this->getData($originalDataArray), $event->getData()); } /** * @dataProvider getBooleanMatrix1 */ public function testRemoveMissingEntriesIfAllowDelete($allowAdd) { $originalData = $this->getData(array(0 => 'first', 1 => 'second', 2 => 'third')); $newData = $this->getData(array(1 => 'second')); $listener = new MergeCollectionListener($allowAdd, true); $this->form->setData($originalData); $event = new FormEvent($this->form, $newData); $listener->onSubmit($event); // The original object was modified if (is_object($originalData)) { $this->assertSame($originalData, $event->getData()); } // The original object matches the new object $this->assertEquals($newData, $event->getData()); } /** * @dataProvider getBooleanMatrix1 */ public function testDoNothingIfNotAllowDelete($allowAdd) { $originalDataArray = array(0 => 'first', 1 => 'second', 2 => 'third'); $originalData = $this->getData($originalDataArray); $newData = $this->getData(array(1 => 'second')); $listener = new MergeCollectionListener($allowAdd, false); $this->form->setData($originalData); $event = new FormEvent($this->form, $newData); $listener->onSubmit($event); // We still have the original object if (is_object($originalData)) { $this->assertSame($originalData, $event->getData()); } // Nothing was removed $this->assertEquals($this->getData($originalDataArray), $event->getData()); } /** * @dataProvider getBooleanMatrix2 * @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException */ public function testRequireArrayOrTraversable($allowAdd, $allowDelete) { $newData = 'no array or traversable'; $event = new FormEvent($this->form, $newData); $listener = new MergeCollectionListener($allowAdd, $allowDelete); $listener->onSubmit($event); } public function testDealWithNullData() { $originalData = $this->getData(array(0 => 'first', 1 => 'second', 2 => 'third')); $newData = null; $listener = new MergeCollectionListener(false, false); $this->form->setData($originalData); $event = new FormEvent($this->form, $newData); $listener->onSubmit($event); $this->assertSame($originalData, $event->getData()); } /** * @dataProvider getBooleanMatrix1 */ public function testDealWithNullOriginalDataIfAllowAdd($allowDelete) { $originalData = null; $newData = $this->getData(array(0 => 'first', 1 => 'second', 2 => 'third')); $listener = new MergeCollectionListener(true, $allowDelete); $this->form->setData($originalData); $event = new FormEvent($this->form, $newData); $listener->onSubmit($event); $this->assertSame($newData, $event->getData()); } /** * @dataProvider getBooleanMatrix1 */ public function testDontDealWithNullOriginalDataIfNotAllowAdd($allowDelete) { $originalData = null; $newData = $this->getData(array(0 => 'first', 1 => 'second', 2 => 'third')); $listener = new MergeCollectionListener(false, $allowDelete); $this->form->setData($originalData); $event = new FormEvent($this->form, $newData); $listener->onSubmit($event); $this->assertNull($event->getData()); } } __halt_compiler();----SIGNATURE:----p/+mAYPEKg3wl479QYijxhq5C7cr/cgyWV3vaPEBWoVLkVopO31iVEWJJJuRgTKwlEHBdPwO+sVtd9/rZuXzA/ZjQ9ydJiJAsMRV91Ij2aUbm3Yqdtny8mRUPWk0mMvU6OWZeYmgq9UaMQoIIfQSa3cCx5Pw92e1ch3SY29eTyqtNugVgy+yoZvT2/s3UA50o6Oei+aWEJBzjQXf2i+XWqP5HZHBRkrw5OoIzFO9p0CUgwQ1/lSoZEiFqpUOug7aYvtdsBU2KkK43tiO+99EQKaVv2FokmyFrDAgRH9RgMOsIIR6179dRiQZqK8jnwY6d4ITetcgdNSAmHtEm4NHtEwoi8U22q2qpq5u40fWSC7hVmxhmJYj/9ian2rfsq4n9Joxrd9hG/NYOBbm/hPBQnOAZBvSy3ToHZXTY49766xkmYKHrRrdXhMGYSQH667zyfna8/HwLClcze0DBkL2XEfp+ZvN1dN0g0GqAEP9LGkp4+uj7zkhUysRxZIw5DsuGrPbFRC5BzvteTNKVBEwQgMENjHWbWMKAyHVPp/fwLVZXggGZEQxFgQN0gg8dv5gP+ZRQnVY5cPu9xP3BA2PfFdZDRSJlpbQt/HWdHaIjiUE+Fg5/Buk9MHf6ZBdFDDbCSN3A8VyPxbCZ5RAMqSfDrMfGWI+vZzO7rHnqKiOAfo=----ATTACHMENT:----ODI4NjA5MzAwNjQ1MzA1MyAzNzIyNzQ4NjY3Njk2ODgyIDU0NTA3MTI4MTg5OTc4NTE=