|
@@ -11,13 +11,9 @@
|
|
|
|
|
|
namespace Sonata\AdminBundle\Tests\Form;
|
|
|
|
|
|
-use Sonata\AdminBundle\Admin\Admin;
|
|
|
use Sonata\AdminBundle\Form\FormMapper;
|
|
|
-
|
|
|
-class TestAdmin extends Admin
|
|
|
-{
|
|
|
-
|
|
|
-}
|
|
|
+use Sonata\AdminBundle\Tests\Fixtures\Admin\CleanAdmin;
|
|
|
+use Symfony\Component\Form\FormBuilder;
|
|
|
|
|
|
class FormMapperTest extends \PHPUnit_Framework_TestCase
|
|
|
{
|
|
@@ -26,11 +22,6 @@ class FormMapperTest extends \PHPUnit_Framework_TestCase
|
|
|
*/
|
|
|
protected $contractor;
|
|
|
|
|
|
- /**
|
|
|
- * @var \Symfony\Component\Form\FormBuilder
|
|
|
- */
|
|
|
- protected $builder;
|
|
|
-
|
|
|
/**
|
|
|
* @var \Sonata\AdminBundle\Admin\AdminInterface
|
|
|
*/
|
|
@@ -41,16 +32,6 @@ class FormMapperTest extends \PHPUnit_Framework_TestCase
|
|
|
*/
|
|
|
protected $modelManager;
|
|
|
|
|
|
- /**
|
|
|
- * @var \Sonata\AdminBundle\Admin\FieldDescriptionInterface
|
|
|
- */
|
|
|
- protected $fieldDescription;
|
|
|
-
|
|
|
- /**
|
|
|
- * @var \Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface
|
|
|
- */
|
|
|
- protected $labelTranslatorStrategy;
|
|
|
-
|
|
|
/**
|
|
|
* @var FormMapper
|
|
|
*/
|
|
@@ -59,19 +40,37 @@ class FormMapperTest extends \PHPUnit_Framework_TestCase
|
|
|
public function setUp()
|
|
|
{
|
|
|
$this->contractor = $this->getMock('Sonata\AdminBundle\Builder\FormContractorInterface');
|
|
|
- $this->builder = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
|
|
|
- ->disableOriginalConstructor()
|
|
|
- ->getMock();
|
|
|
|
|
|
- $this->admin = new TestAdmin('code', 'class', 'controller');
|
|
|
+ $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
|
|
|
+ $eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
|
|
+
|
|
|
+ $formBuilder = new FormBuilder('test', 'stdClass', $eventDispatcher, $formFactory);
|
|
|
+
|
|
|
+ $this->admin = new CleanAdmin('code', 'class', 'controller');
|
|
|
|
|
|
$this->modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
|
|
|
- $this->fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
|
|
|
- $this->labelTranslatorStrategy = $this->getMock('Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface');
|
|
|
+
|
|
|
+ // php 5.3 BC
|
|
|
+ $fieldDescription = $this->getFieldDescriptionMock();
|
|
|
+
|
|
|
+ $this->modelManager->expects($this->any())
|
|
|
+ ->method('getNewFieldDescriptionInstance')
|
|
|
+ ->will($this->returnCallback(function($class, $name, array $options = array()) use ($fieldDescription) {
|
|
|
+ $fieldDescriptionClone = clone $fieldDescription;
|
|
|
+ $fieldDescriptionClone->setName($name);
|
|
|
+ $fieldDescriptionClone->setOptions($options);
|
|
|
+
|
|
|
+ return $fieldDescriptionClone;
|
|
|
+ }));
|
|
|
+
|
|
|
+ $this->admin->setModelManager($this->modelManager);
|
|
|
+
|
|
|
+ $labelTranslatorStrategy = $this->getMock('Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface');
|
|
|
+ $this->admin->setLabelTranslatorStrategy($labelTranslatorStrategy);
|
|
|
|
|
|
$this->formMapper = new FormMapper(
|
|
|
$this->contractor,
|
|
|
- $this->builder,
|
|
|
+ $formBuilder,
|
|
|
$this->admin
|
|
|
);
|
|
|
}
|
|
@@ -130,43 +129,23 @@ class FormMapperTest extends \PHPUnit_Framework_TestCase
|
|
|
|
|
|
public function testWithFieldsCascadeTranslationDomain()
|
|
|
{
|
|
|
- $formGroups = array(
|
|
|
- 'foobar' => array(
|
|
|
- 'collapsed' => false,
|
|
|
- 'fields' => array(),
|
|
|
- 'description' => false,
|
|
|
- 'translation_domain' => 'Foobar',
|
|
|
- 'class' => false,
|
|
|
- 'name' => 'foobar'
|
|
|
- )
|
|
|
- );
|
|
|
-
|
|
|
- $this->admin->setModelManager($this->modelManager);
|
|
|
-
|
|
|
- $this->modelManager->expects($this->once())
|
|
|
- ->method('getNewFieldDescriptionInstance')
|
|
|
- ->with(
|
|
|
- 'class',
|
|
|
- 'foo',
|
|
|
- array(
|
|
|
- 'translation_domain' => 'Foobar',
|
|
|
- 'type' => 'bar',
|
|
|
- )
|
|
|
- )
|
|
|
- ->will($this->returnValue($this->fieldDescription));
|
|
|
-
|
|
|
$this->contractor->expects($this->once())
|
|
|
->method('getDefaultOptions')
|
|
|
->will($this->returnValue(array()));
|
|
|
|
|
|
- $this->admin->setLabelTranslatorStrategy($this->labelTranslatorStrategy);
|
|
|
-
|
|
|
$this->formMapper->with('foobar', array(
|
|
|
'translation_domain' => 'Foobar'
|
|
|
))
|
|
|
->add('foo', 'bar')
|
|
|
->end();
|
|
|
|
|
|
+ $fieldDescription = $this->admin->getFormFieldDescription('foo');
|
|
|
+ $this->assertEquals('foo', $fieldDescription->getName());
|
|
|
+ $this->assertEquals('bar', $fieldDescription->getType());
|
|
|
+ $this->assertEquals('Foobar', $fieldDescription->getTranslationDomain());
|
|
|
+
|
|
|
+ $this->assertTrue($this->formMapper->has('foo'));
|
|
|
+
|
|
|
$this->assertEquals(array('default' => array (
|
|
|
'collapsed' => false,
|
|
|
'class' => false,
|
|
@@ -195,4 +174,169 @@ class FormMapperTest extends \PHPUnit_Framework_TestCase
|
|
|
$this->formMapper->with('foo');
|
|
|
$this->formMapper->remove('foo');
|
|
|
}
|
|
|
+
|
|
|
+ public function testIfTrueApply()
|
|
|
+ {
|
|
|
+ $this->contractor->expects($this->once())
|
|
|
+ ->method('getDefaultOptions')
|
|
|
+ ->will($this->returnValue(array()));
|
|
|
+
|
|
|
+ $this->formMapper
|
|
|
+ ->ifTrue(true)
|
|
|
+ ->add('foo', 'bar')
|
|
|
+ ->ifEnd()
|
|
|
+ ;
|
|
|
+
|
|
|
+ $this->assertTrue($this->formMapper->has('foo'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testIfTrueNotApply()
|
|
|
+ {
|
|
|
+ $this->formMapper
|
|
|
+ ->ifTrue(false)
|
|
|
+ ->add('foo', 'bar')
|
|
|
+ ->ifEnd()
|
|
|
+ ;
|
|
|
+
|
|
|
+ $this->assertFalse($this->formMapper->has('foo'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testIfTrueCombination()
|
|
|
+ {
|
|
|
+ $this->contractor->expects($this->once())
|
|
|
+ ->method('getDefaultOptions')
|
|
|
+ ->will($this->returnValue(array()));
|
|
|
+
|
|
|
+ $this->formMapper
|
|
|
+ ->ifTrue(false)
|
|
|
+ ->add('foo', 'bar')
|
|
|
+ ->ifEnd()
|
|
|
+ ->add('baz', 'foobaz')
|
|
|
+ ;
|
|
|
+
|
|
|
+ $this->assertFalse($this->formMapper->has('foo'));
|
|
|
+ $this->assertTrue($this->formMapper->has('baz'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testIfFalseApply()
|
|
|
+ {
|
|
|
+ $this->contractor->expects($this->once())
|
|
|
+ ->method('getDefaultOptions')
|
|
|
+ ->will($this->returnValue(array()));
|
|
|
+
|
|
|
+ $this->formMapper
|
|
|
+ ->ifFalse(false)
|
|
|
+ ->add('foo', 'bar')
|
|
|
+ ->ifEnd()
|
|
|
+ ;
|
|
|
+
|
|
|
+ $this->assertTrue($this->formMapper->has('foo'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testIfFalseNotApply()
|
|
|
+ {
|
|
|
+ $this->formMapper
|
|
|
+ ->ifFalse(true)
|
|
|
+ ->add('foo', 'bar')
|
|
|
+ ->ifEnd()
|
|
|
+ ;
|
|
|
+
|
|
|
+ $this->assertFalse($this->formMapper->has('foo'));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testIfFalseCombination()
|
|
|
+ {
|
|
|
+ $this->contractor->expects($this->once())
|
|
|
+ ->method('getDefaultOptions')
|
|
|
+ ->will($this->returnValue(array()));
|
|
|
+
|
|
|
+ $this->formMapper
|
|
|
+ ->ifFalse(true)
|
|
|
+ ->add('foo', 'bar')
|
|
|
+ ->ifEnd()
|
|
|
+ ->add('baz', 'foobaz')
|
|
|
+ ;
|
|
|
+
|
|
|
+ $this->assertFalse($this->formMapper->has('foo'));
|
|
|
+ $this->assertTrue($this->formMapper->has('baz'));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @expectedException RuntimeException
|
|
|
+ * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
|
|
|
+ */
|
|
|
+ public function testIfTrueNested()
|
|
|
+ {
|
|
|
+ $this->formMapper->ifTrue(true);
|
|
|
+ $this->formMapper->ifTrue(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @expectedException RuntimeException
|
|
|
+ * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
|
|
|
+ */
|
|
|
+ public function testIfFalseNested()
|
|
|
+ {
|
|
|
+ $this->formMapper->ifFalse(false);
|
|
|
+ $this->formMapper->ifFalse(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @expectedException RuntimeException
|
|
|
+ * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
|
|
|
+ */
|
|
|
+ public function testIfCombinationNested()
|
|
|
+ {
|
|
|
+ $this->formMapper->ifTrue(true);
|
|
|
+ $this->formMapper->ifFalse(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @expectedException RuntimeException
|
|
|
+ * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
|
|
|
+ */
|
|
|
+ public function testIfFalseCombinationNested2()
|
|
|
+ {
|
|
|
+ $this->formMapper->ifFalse(false);
|
|
|
+ $this->formMapper->ifTrue(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @expectedException RuntimeException
|
|
|
+ * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
|
|
|
+ */
|
|
|
+ public function testIfFalseCombinationNested3()
|
|
|
+ {
|
|
|
+ $this->formMapper->ifFalse(true);
|
|
|
+ $this->formMapper->ifTrue(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @expectedException RuntimeException
|
|
|
+ * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
|
|
|
+ */
|
|
|
+ public function testIfFalseCombinationNested4()
|
|
|
+ {
|
|
|
+ $this->formMapper->ifTrue(false);
|
|
|
+ $this->formMapper->ifFalse(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function getFieldDescriptionMock($name = null, $label = null, $translationDomain = null)
|
|
|
+ {
|
|
|
+ $fieldDescription = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\BaseFieldDescription');
|
|
|
+
|
|
|
+ if ($name !== null) {
|
|
|
+ $fieldDescription->setName($name);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($label !== null) {
|
|
|
+ $fieldDescription->setOption('label', $label);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($translationDomain !== null) {
|
|
|
+ $fieldDescription->setOption('translation_domain', $translationDomain);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $fieldDescription;
|
|
|
+ }
|
|
|
}
|