Browse Source

Fixed methods if_true, if_false, if_end in BaseGroupedMapper to camelCase, added tests

Andrej Hudec 10 years ago
parent
commit
407d90f972

+ 15 - 13
Mapper/BaseGroupedMapper.php

@@ -138,17 +138,18 @@ abstract class BaseGroupedMapper extends BaseMapper
     }
 
     /**
-     * Only nested add if the condition match FALSE
+     * Only nested add if the condition match true
      *
-     * @param $bool
+     * @param boolean $bool
+     *
+     * @return BaseGroupedMapper
      *
-     * @return $this
      * @throws \RuntimeException
      */
-    public function if_true($bool)
+    public function ifTrue($bool)
     {
-        if (!$this->apply == null) {
-            throw new \RuntimeException('Cannot nest ifTrue call');
+        if ($this->apply !== null) {
+            throw new \RuntimeException('Cannot nest ifTrue or ifFalse call');
         }
 
         $this->apply = ($bool === true);
@@ -157,17 +158,18 @@ abstract class BaseGroupedMapper extends BaseMapper
     }
 
     /**
-     * Only nested add if the condition match FALSE
+     * Only nested add if the condition match false
      *
-     * @param $bool
+     * @param boolean $bool
+     *
+     * @return BaseGroupedMapper
      *
-     * @return $this
      * @throws \RuntimeException
      */
-    public function if_false($bool)
+    public function ifFalse($bool)
     {
-        if (!$this->apply == null) {
-            throw new \RuntimeException('Cannot nest ifTrue call');
+        if ($this->apply !== null) {
+            throw new \RuntimeException('Cannot nest ifTrue or ifFalse call');
         }
 
         $this->apply = ($bool === false);
@@ -178,7 +180,7 @@ abstract class BaseGroupedMapper extends BaseMapper
     /**
      * @return $this
      */
-    public function end_if()
+    public function ifEnd()
     {
         $this->apply = null;
 

+ 9 - 0
Tests/Fixtures/Admin/CleanAdmin.php

@@ -0,0 +1,9 @@
+<?php
+
+namespace Sonata\AdminBundle\Tests\Fixtures\Admin;
+
+use Sonata\AdminBundle\Admin\Admin;
+
+class CleanAdmin extends Admin
+{
+}

+ 199 - 55
Tests/Form/FormMapperTest.php

@@ -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;
+    }
 }

+ 139 - 6
Tests/Show/ShowMapperTest.php

@@ -19,11 +19,6 @@ use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
 use Sonata\AdminBundle\Builder\ShowBuilderInterface;
 use Sonata\AdminBundle\Translator\NoopLabelTranslatorStrategy;
 
-class TestAdmin extends Admin
-{
-
-}
-
 /**
  * Test for ShowMapper
  *
@@ -42,7 +37,7 @@ class ShowMapperTest extends \PHPUnit_Framework_TestCase
     private $admin;
 
     /**
-     * @var Sonata\AdminBundle\Builder\ShowBuilderInterface
+     * @var ShowBuilderInterface
      */
     private $showBuilder;
 
@@ -161,6 +156,144 @@ class ShowMapperTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('fooName', $fieldDescription->getOption('label'));
     }
 
+    public function testIfTrueApply()
+    {
+        $this->showMapper->ifTrue(true);
+        $this->showMapper->add('fooName');
+        $this->showMapper->ifEnd();
+
+        $this->assertTrue($this->showMapper->has('fooName'));
+        $fieldDescription = $this->showMapper->get('fooName');
+
+        $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface', $fieldDescription);
+        $this->assertEquals('fooName', $fieldDescription->getName());
+        $this->assertEquals('fooName', $fieldDescription->getOption('label'));
+    }
+
+    public function testIfTrueNotApply()
+    {
+        $this->showMapper->ifTrue(false);
+        $this->showMapper->add('fooName');
+        $this->showMapper->ifEnd();
+
+        $this->assertFalse($this->showMapper->has('fooName'));
+    }
+
+    public function testIfTrueCombination()
+    {
+        $this->showMapper->ifTrue(false);
+        $this->showMapper->add('fooName');
+        $this->showMapper->ifEnd();
+        $this->showMapper->add('barName');
+
+        $this->assertFalse($this->showMapper->has('fooName'));
+        $this->assertTrue($this->showMapper->has('barName'));
+        $fieldDescription = $this->showMapper->get('barName');
+
+        $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface', $fieldDescription);
+        $this->assertEquals('barName', $fieldDescription->getName());
+        $this->assertEquals('barName', $fieldDescription->getOption('label'));
+    }
+
+    public function testIfFalseApply()
+    {
+        $this->showMapper->ifFalse(false);
+        $this->showMapper->add('fooName');
+        $this->showMapper->ifEnd();
+
+        $this->assertTrue($this->showMapper->has('fooName'));
+        $fieldDescription = $this->showMapper->get('fooName');
+
+        $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface', $fieldDescription);
+        $this->assertEquals('fooName', $fieldDescription->getName());
+        $this->assertEquals('fooName', $fieldDescription->getOption('label'));
+    }
+
+    public function testIfFalseNotApply()
+    {
+        $this->showMapper->ifFalse(true);
+        $this->showMapper->add('fooName');
+        $this->showMapper->ifEnd();
+
+        $this->assertFalse($this->showMapper->has('fooName'));
+    }
+
+    public function testIfFalseCombination()
+    {
+        $this->showMapper->ifFalse(true);
+        $this->showMapper->add('fooName');
+        $this->showMapper->ifEnd();
+        $this->showMapper->add('barName');
+
+        $this->assertFalse($this->showMapper->has('fooName'));
+        $this->assertTrue($this->showMapper->has('barName'));
+        $fieldDescription = $this->showMapper->get('barName');
+
+        $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface', $fieldDescription);
+        $this->assertEquals('barName', $fieldDescription->getName());
+        $this->assertEquals('barName', $fieldDescription->getOption('label'));
+    }
+
+    /**
+     * @expectedException        RuntimeException
+     * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
+     */
+    public function testIfTrueNested()
+    {
+        $this->showMapper->ifTrue(true);
+        $this->showMapper->ifTrue(true);
+    }
+
+    /**
+     * @expectedException        RuntimeException
+     * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
+     */
+    public function testIfFalseNested()
+    {
+        $this->showMapper->ifFalse(false);
+        $this->showMapper->ifFalse(false);
+    }
+
+    /**
+     * @expectedException        RuntimeException
+     * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
+     */
+    public function testIfCombinationNested()
+    {
+        $this->showMapper->ifTrue(true);
+        $this->showMapper->ifFalse(false);
+    }
+
+    /**
+     * @expectedException        RuntimeException
+     * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
+     */
+    public function testIfFalseCombinationNested2()
+    {
+        $this->showMapper->ifFalse(false);
+        $this->showMapper->ifTrue(true);
+    }
+
+    /**
+     * @expectedException        RuntimeException
+     * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
+     */
+    public function testIfFalseCombinationNested3()
+    {
+        $this->showMapper->ifFalse(true);
+        $this->showMapper->ifTrue(false);
+    }
+
+   /**
+     * @expectedException        RuntimeException
+     * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
+     */
+    public function testIfFalseCombinationNested4()
+    {
+        $this->showMapper->ifTrue(false);
+        $this->showMapper->ifFalse(true);
+    }
+
     public function testAddRemove()
     {
         $this->assertFalse($this->showMapper->has('fooName'));