Skip to content

Commit 28dbfe4

Browse files
committed
fix bc break and add tests
1 parent cfb2f6c commit 28dbfe4

File tree

2 files changed

+99
-5
lines changed

2 files changed

+99
-5
lines changed

src/Symfony/Bridge/Doctrine/Form/EventListener/MergeDoctrineCollectionListener.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,31 @@
2727
*/
2828
class MergeDoctrineCollectionListener implements EventSubscriberInterface
2929
{
30+
// Keeps BC. To be removed in 4.0
31+
private $bc = true;
32+
3033
public static function getSubscribedEvents()
3134
{
3235
// Higher priority than core MergeCollectionListener so that this one
3336
// is called before
34-
return array(FormEvents::SUBMIT => array('onSubmit', 10));
37+
return array(
38+
FormEvents::SUBMIT => array(
39+
// BC
40+
array('onBind', 10),
41+
array('onSubmit', 5),
42+
)
43+
);
3544
}
3645

3746
public function onSubmit(FormEvent $event)
3847
{
48+
// If onBind() is overridden then logic has been executed
49+
if ($this->bc) {
50+
@trigger_error('The onBind() method is deprecated since version 3.1 and will be removed in 4.0. Use the onSubmit() method instead.', E_USER_DEPRECATED);
51+
52+
return;
53+
}
54+
3955
$collection = $event->getForm()->getData();
4056
$data = $event->getData();
4157

@@ -52,10 +68,8 @@ public function onSubmit(FormEvent $event)
5268
* @deprecated since version 3.1, to be removed in 4.0.
5369
* Use {@link onSubmit()} instead.
5470
*/
55-
public function onBind(FormEvent $event)
71+
public function onBind()
5672
{
57-
@trigger_error('The '.__METHOD__.' method is deprecated since version 3.1 and will be removed in 4.0. Use the onSubmit() method instead.', E_USER_DEPRECATED);
58-
59-
$this->onSubmit($event);
73+
$this->bc = false;
6074
}
6175
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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\Bridge\Doctrine\Tests\Form\EventListener;
13+
14+
use Doctrine\Common\Collections\ArrayCollection;
15+
use Symfony\Bridge\Doctrine\Form\EventListener\MergeDoctrineCollectionListener;
16+
use Symfony\Component\EventDispatcher\EventDispatcher;
17+
use Symfony\Component\Form\FormBuilder;
18+
use Symfony\Component\Form\FormEvent;
19+
use Symfony\Component\Form\FormEvents;
20+
21+
class MergeDoctrineCollectionTest extends \PHPUnit_Framework_TestCase
22+
{
23+
/** @var \Doctrine\Common\Collections\ArrayCollection */
24+
private $collection;
25+
/** @var \Symfony\Component\EventDispatcher\EventDispatcher */
26+
private $dispatcher;
27+
private $factory;
28+
private $form;
29+
30+
protected function setUp()
31+
{
32+
$this->collection = new ArrayCollection(array('test'));
33+
$this->dispatcher = new EventDispatcher();
34+
$this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
35+
$this->form = $this->getBuilder()
36+
->getForm();
37+
}
38+
39+
protected function tearDown()
40+
{
41+
$this->collection = null;
42+
$this->dispatcher = null;
43+
$this->factory = null;
44+
$this->form = null;
45+
}
46+
47+
protected function getBuilder($name = 'name')
48+
{
49+
return new FormBuilder($name, null, $this->dispatcher, $this->factory);
50+
}
51+
52+
protected function getForm($name = 'name')
53+
{
54+
return $this->getBuilder($name)
55+
->setData($this->collection)
56+
->addEventSubscriber(new MergeDoctrineCollectionListener())
57+
->getForm();
58+
}
59+
60+
public function testOnSubmitDoNothing()
61+
{
62+
$submittedData = array('test');
63+
$event = new FormEvent($this->getForm(), $submittedData);
64+
65+
$this->dispatcher->dispatch(FormEvents::SUBMIT, $event);
66+
67+
$this->assertTrue($this->collection->contains('test'));
68+
$this->assertSame(1, $this->collection->count());
69+
}
70+
71+
public function testOnSubmitNullClearCollection()
72+
{
73+
$submittedData = array();
74+
$event = new FormEvent($this->getForm(), $submittedData);
75+
76+
$this->dispatcher->dispatch(FormEvents::SUBMIT, $event);
77+
78+
$this->assertTrue($this->collection->isEmpty());
79+
}
80+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy