Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6eb3873

Browse files
committed
[DoctrineBridge] deprecated MergeDoctrineCollectionListener::onBind()
1 parent 0b67fa3 commit 6eb3873

File tree

3 files changed

+112
-3
lines changed

3 files changed

+112
-3
lines changed

src/Symfony/Bridge/Doctrine/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ CHANGELOG
44
3.1.0
55
-----
66

7-
* added "{{ value }}" message placeholder to UniqueEntityValidator
7+
* added "{{ value }}" message placeholder to UniqueEntityValidator
8+
* deprecated `MergeDoctrineCollectionListener::onBind` in favor of
9+
`MergeDoctrineListener::onSubmit`
810

911
3.0.0
1012
-----

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

Lines changed: 29 additions & 2 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('onBind', 10));
37+
return array(
38+
FormEvents::SUBMIT => array(
39+
// BC
40+
array('onBind', 10),
41+
array('onSubmit', 5),
42+
),
43+
);
3544
}
3645

37-
public function onBind(FormEvent $event)
46+
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

@@ -45,4 +61,15 @@ public function onBind(FormEvent $event)
4561
$collection->clear();
4662
}
4763
}
64+
65+
/**
66+
* Alias of {@link onSubmit()}.
67+
*
68+
* @deprecated since version 3.1, to be removed in 4.0.
69+
* Use {@link onSubmit()} instead.
70+
*/
71+
public function onBind()
72+
{
73+
$this->bc = false;
74+
}
4875
}
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 <[email protected]>
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)