[Form] Added test for disabling buttons

This commit is contained in:
Bernhard Schussek 2014-03-31 11:59:34 +02:00
parent 6bb355e2fd
commit ebfee723c9
3 changed files with 93 additions and 21 deletions

View File

@ -252,6 +252,8 @@ class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface
public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;
return $this;
}
/**
@ -260,6 +262,8 @@ class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface
public function setAttributes(array $attributes)
{
$this->attributes = $attributes;
return $this;
}
/**
@ -286,6 +290,8 @@ class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface
public function setDisabled($disabled)
{
$this->disabled = $disabled;
return $this;
}
/**
@ -410,6 +416,8 @@ class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface
public function setType(ResolvedFormTypeInterface $type)
{
$this->type = $type;
return $this;
}
/**

View File

@ -0,0 +1,70 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Tests;
use Symfony\Component\Form\ButtonBuilder;
use Symfony\Component\Form\FormBuilder;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class ButtonTest extends \PHPUnit_Framework_TestCase
{
private $dispatcher;
private $factory;
protected function setUp()
{
$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
}
/**
* @dataProvider getDisabledStates
*/
public function testDisabledIfParentIsDisabled($parentDisabled, $buttonDisabled, $result)
{
$form = $this->getFormBuilder('form')
->setDisabled($parentDisabled)
->getForm();
$button = $this->getButtonBuilder('button')
->setDisabled($buttonDisabled)
->getForm();
$button->setParent($form);
$this->assertSame($result, $button->isDisabled());
}
public function getDisabledStates()
{
return array(
// parent, button, result
array(true, true, true),
array(true, false, true),
array(false, true, true),
array(false, false, false),
);
}
private function getButtonBuilder($name)
{
return new ButtonBuilder($name);
}
private function getFormBuilder($name)
{
return new FormBuilder($name, null, $this->dispatcher, $this->factory);
}
}

View File

@ -170,34 +170,28 @@ class SimpleFormTest extends AbstractFormTest
$this->assertFalse($child->isRequired());
}
public function testAlwaysDisabledIfParentDisabled()
/**
* @dataProvider getDisabledStates
*/
public function testAlwaysDisabledIfParentDisabled($parentDisabled, $disabled, $result)
{
$parent = $this->getBuilder()->setDisabled(true)->getForm();
$child = $this->getBuilder()->setDisabled(false)->getForm();
$parent = $this->getBuilder()->setDisabled($parentDisabled)->getForm();
$child = $this->getBuilder()->setDisabled($disabled)->getForm();
$child->setParent($parent);
$this->assertTrue($child->isDisabled());
$this->assertSame($result, $child->isDisabled());
}
public function testDisabled()
public function getDisabledStates()
{
$parent = $this->getBuilder()->setDisabled(false)->getForm();
$child = $this->getBuilder()->setDisabled(true)->getForm();
$child->setParent($parent);
$this->assertTrue($child->isDisabled());
}
public function testNotDisabled()
{
$parent = $this->getBuilder()->setDisabled(false)->getForm();
$child = $this->getBuilder()->setDisabled(false)->getForm();
$child->setParent($parent);
$this->assertFalse($child->isDisabled());
return array(
// parent, button, result
array(true, true, true),
array(true, false, true),
array(false, true, true),
array(false, false, false),
);
}
public function testGetRootReturnsRootOfParent()