[Form] Fixed: Custom add and remove method are not invoked if disallowed

This commit is contained in:
Bernhard Schussek 2012-02-02 13:53:44 +01:00
parent 02f61adcc5
commit 8ab982afc8
2 changed files with 64 additions and 2 deletions

View File

@ -130,7 +130,7 @@ class MergeCollectionListener implements EventSubscriberInterface
}
// Set preconfigured adder
if ($this->addMethod) {
if ($this->allowAdd && $this->addMethod) {
$addMethod = $this->checkMethod($reflClass, $this->addMethod);
if (!$addMethod) {
@ -143,7 +143,7 @@ class MergeCollectionListener implements EventSubscriberInterface
}
// Set preconfigured remover
if ($this->removeMethod) {
if ($this->allowDelete && $this->removeMethod) {
$removeMethod = $this->checkMethod($reflClass, $this->removeMethod);
if (!$removeMethod) {

View File

@ -475,6 +475,68 @@ abstract class MergeCollectionListenerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($this->getData($originalDataArray), $event->getData());
}
public function testDontCallAdderWithCustomNameIfDisallowed()
{
$parentData = $this->getMock(__CLASS__ . '_CarCustomNames');
$parentForm = $this->getForm('car');
$parentForm->setData($parentData);
$parentForm->add($this->form);
$originalDataArray = array(1 => 'second');
$originalData = $this->getData($originalDataArray);
$newData = $this->getData(array(0 => 'first'));
$this->form->setData($originalData);
$parentData->expects($this->never())
->method('foo');
$parentData->expects($this->once())
->method('bar')
->with('second');
$event = new FilterDataEvent($this->form, $newData);
$listener = new MergeCollectionListener(false, true, true, 'foo', 'bar');
$listener->onBindNormData($event);
if (is_object($originalData)) {
$this->assertSame($originalData, $event->getData());
}
// The data was not modified
$this->assertEquals($this->getData($originalDataArray), $event->getData());
}
public function testDontCallRemoverWithCustomNameIfDisallowed()
{
$parentData = $this->getMock(__CLASS__ . '_CarCustomNames');
$parentForm = $this->getForm('car');
$parentForm->setData($parentData);
$parentForm->add($this->form);
$originalDataArray = array(1 => 'second');
$originalData = $this->getData($originalDataArray);
$newData = $this->getData(array(0 => 'first'));
$this->form->setData($originalData);
$parentData->expects($this->once())
->method('foo')
->with('first');
$parentData->expects($this->never())
->method('bar');
$event = new FilterDataEvent($this->form, $newData);
$listener = new MergeCollectionListener(true, false, true, 'foo', 'bar');
$listener->onBindNormData($event);
if (is_object($originalData)) {
$this->assertSame($originalData, $event->getData());
}
// The data was not modified
$this->assertEquals($this->getData($originalDataArray), $event->getData());
}
/**
* @expectedException Symfony\Component\Form\Exception\FormException
*/