From 8a6cf9d93eda5d4c7685fd794a3733f787368055 Mon Sep 17 00:00:00 2001 From: Jules Pietri Date: Fri, 29 Apr 2016 23:39:44 +0200 Subject: [PATCH] [DoctrineBridge] fixed bc layer from #18069 --- .../MergeDoctrineCollectionListener.php | 18 ++++++---- ...> MergeDoctrineCollectionListenerTest.php} | 35 ++++++++++++++++++- 2 files changed, 46 insertions(+), 7 deletions(-) rename src/Symfony/Bridge/Doctrine/Tests/Form/EventListener/{MergeDoctrineCollectionTest.php => MergeDoctrineCollectionListenerTest.php} (70%) diff --git a/src/Symfony/Bridge/Doctrine/Form/EventListener/MergeDoctrineCollectionListener.php b/src/Symfony/Bridge/Doctrine/Form/EventListener/MergeDoctrineCollectionListener.php index e1cdfd8372..ca837e6428 100644 --- a/src/Symfony/Bridge/Doctrine/Form/EventListener/MergeDoctrineCollectionListener.php +++ b/src/Symfony/Bridge/Doctrine/Form/EventListener/MergeDoctrineCollectionListener.php @@ -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; } } } diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/EventListener/MergeDoctrineCollectionTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/EventListener/MergeDoctrineCollectionListenerTest.php similarity index 70% rename from src/Symfony/Bridge/Doctrine/Tests/Form/EventListener/MergeDoctrineCollectionTest.php rename to src/Symfony/Bridge/Doctrine/Tests/Form/EventListener/MergeDoctrineCollectionListenerTest.php index 6d48fd2b16..8c4ec7a215 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Form/EventListener/MergeDoctrineCollectionTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/EventListener/MergeDoctrineCollectionListenerTest.php @@ -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); + } }