[DoctrineBridge] fixed bc layer from #18069

This commit is contained in:
Jules Pietri 2016-04-29 23:39:44 +02:00
parent 6857c36576
commit 8a6cf9d93e
2 changed files with 46 additions and 7 deletions

View File

@ -27,8 +27,9 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
*/
class MergeDoctrineCollectionListener implements EventSubscriberInterface
{
// Keeps BC. To be removed in 4.0
// Keep BC. To be removed in 4.0
private $bc = true;
private $bcLayer = false;
public static function getSubscribedEvents()
{
@ -36,8 +37,7 @@ class MergeDoctrineCollectionListener implements EventSubscriberInterface
// is called before
return array(
FormEvents::SUBMIT => array(
// BC
array('onBind', 10),
array('onBind', 10), // deprecated
array('onSubmit', 5),
),
);
@ -45,11 +45,14 @@ class MergeDoctrineCollectionListener implements EventSubscriberInterface
public function onSubmit(FormEvent $event)
{
// If onBind() is overridden then logic has been executed
if ($this->bc) {
// onBind() has been overridden from a child class
@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);
return;
if (!$this->bcLayer) {
// If parent::onBind() has not been called, then logic has been executed
return;
}
}
$collection = $event->getForm()->getData();
@ -68,10 +71,13 @@ class MergeDoctrineCollectionListener implements EventSubscriberInterface
* @deprecated since version 3.1, to be removed in 4.0.
* Use {@link onSubmit()} instead.
*/
public function onBind()
public function onBind(FormEvent $event)
{
if (__CLASS__ === get_class($this)) {
$this->bc = false;
} else {
// parent::onBind() has been called
$this->bcLayer = true;
}
}
}

View File

@ -18,7 +18,7 @@ use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class MergeDoctrineCollectionTest extends \PHPUnit_Framework_TestCase
class MergeDoctrineCollectionListenerTest extends \PHPUnit_Framework_TestCase
{
/** @var \Doctrine\Common\Collections\ArrayCollection */
private $collection;
@ -77,4 +77,37 @@ class MergeDoctrineCollectionTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($this->collection->isEmpty());
}
/**
* @group legacy
*/
public function testLegacyChildClassOnSubmitCallParent()
{
$form = $this->getBuilder('name')
->setData($this->collection)
->addEventSubscriber(new TestClassExtendingMergeDoctrineCollectionListener())
->getForm();
$submittedData = array();
$event = new FormEvent($form, $submittedData);
$this->dispatcher->dispatch(FormEvents::SUBMIT, $event);
$this->assertTrue($this->collection->isEmpty());
$this->assertTrue(TestClassExtendingMergeDoctrineCollectionListener::$onBindCalled);
}
}
/**
* @group legacy
*/
class TestClassExtendingMergeDoctrineCollectionListener extends MergeDoctrineCollectionListener
{
public static $onBindCalled = false;
public function onBind(FormEvent $event)
{
self::$onBindCalled = true;
parent::onBind($event);
}
}