Kaynağa Gözat

Merge pull request #1633 from pulzarraider/datagarid_test

Added DatagridMapperTest, ListMapperTest. Improved ShowMapperTest.
Thomas 12 yıl önce
ebeveyn
işleme
bfc61bbe47

+ 5 - 23
Admin/Admin.php

@@ -1687,11 +1687,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     }
 
     /**
-     * Returns true if the admin has a FieldDescription with the given $name
-     *
-     * @param string $name
-     *
-     * @return bool
+     * {@inheritdoc}
      */
     public function hasShowFieldDescription($name)
     {
@@ -1733,11 +1729,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     }
 
     /**
-     * Returns true if the list FieldDescription exists
-     *
-     * @param string $name
-     *
-     * @return bool
+     * {@inheritdoc}
      */
     public function hasListFieldDescription($name)
     {
@@ -1755,11 +1747,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     }
 
     /**
-     * remove a list FieldDescription
-     *
-     * @param string $name
-     *
-     * @return void
+     * {@inheritdoc}
      */
     public function removeListFieldDescription($name)
     {
@@ -1779,11 +1767,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     }
 
     /**
-     * Returns true if the filter FieldDescription exists
-     *
-     * @param string $name
-     *
-     * @return bool
+     * {@inheritdoc}
      */
     public function hasFilterFieldDescription($name)
     {
@@ -1799,9 +1783,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     }
 
     /**
-     * remove a filter FieldDescription
-     *
-     * @param string $name
+     * {@inheritdoc}
      */
     public function removeFilterFieldDescription($name)
     {

+ 43 - 0
Admin/AdminInterface.php

@@ -256,6 +256,15 @@ interface AdminInterface
      */
     public function getRouterIdParameter();
 
+    /**
+     * Returns true if the admin has a FieldDescription with the given $name
+     *
+     * @param string $name
+     *
+     * @return bool
+     */
+    public function hasShowFieldDescription($name);
+
     /**
      * add a FieldDescription
      *
@@ -283,6 +292,24 @@ interface AdminInterface
      */
     public function addListFieldDescription($name, FieldDescriptionInterface $fieldDescription);
 
+    /**
+     * Remove a list FieldDescription
+     *
+     * @param string $name
+     *
+     * @return void
+     */
+    public function removeListFieldDescription($name);
+
+    /**
+     * Returns true if the filter FieldDescription exists
+     *
+     * @param string $name
+     *
+     * @return bool
+     */
+    public function hasFilterFieldDescription($name);
+
     /**
      * add a filter FieldDescription
      *
@@ -293,6 +320,13 @@ interface AdminInterface
      */
     public function addFilterFieldDescription($name, FieldDescriptionInterface $fieldDescription);
 
+    /**
+     * Remove a filter FieldDescription
+     *
+     * @param string $name
+     */
+    public function removeFilterFieldDescription($name);
+
     /**
      * Returns the filter FieldDescription collection
      *
@@ -514,6 +548,15 @@ interface AdminInterface
      */
     public function getListFieldDescription($name);
 
+    /**
+     * Returns true if the list FieldDescription exists
+     *
+     * @param string $name
+     *
+     * @return bool
+     */
+    public function hasListFieldDescription($name);
+
     /**
      * Returns the collection of list FieldDescriptions
      *

+ 14 - 1
CHANGELOG.md

@@ -1,7 +1,20 @@
 CHANGELOG
 =========
 
-### 2013-09-5
+### 2013-09-11
+
+* [BC BREAK] added ``hasShowFieldDescription``, ``hasListFieldDescription``,
+  ``removeListFieldDescription``, ``removeFilterFieldDescription``,
+  ``hasFilterFieldDescription`` to the AdminInterface
+  If you do not extend the Admin class, you need to add these methods to
+  your admin.
+
+* [BC BREAK] added ``reorderFilters`` to the DatagridInterface
+  If you do not extend the Datagrid class, you need to add this method to
+  your Datagrid.
+
+### 2013-09-05
+
 * [BC BREAK] added ``getListBuilder``, ``getDatagridBuilder``, ``setBaseControllerName``,
   ``getBaseControllerName``, ``getFormFieldDescriptions``, ``getRoutes``, ``getFilterFieldDescriptions``,
   ``getListFieldDescriptions``, ``isChild`` to the AdminInterface

+ 3 - 0
Datagrid/Datagrid.php

@@ -178,6 +178,9 @@ class Datagrid implements DatagridInterface
         return $this->filters;
     }
 
+    /**
+     * {@inheritdoc}
+     */
     public function reorderFilters(array $keys)
     {
         $this->filters = array_merge(array_flip($keys), $this->filters);

+ 5 - 0
Datagrid/DatagridInterface.php

@@ -46,6 +46,11 @@ interface DatagridInterface
      */
     public function getFilters();
 
+    /**
+     * Reorder filters
+     */
+    public function reorderFilters(array $keys);
+
     /**
      * @return array
      */

+ 234 - 0
Tests/Datagrid/DatagridMapperTest.php

@@ -0,0 +1,234 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ */
+
+namespace Sonata\AdminBundle\Tests\Datagrid;
+
+use Sonata\AdminBundle\Admin\AdminInterface;
+use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
+use Sonata\AdminBundle\Datagrid\DatagridMapper;
+use Sonata\AdminBundle\Datagrid\Datagrid;
+use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
+use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
+use Sonata\AdminBundle\Datagrid\PagerInterface;
+use Sonata\AdminBundle\Filter\Filter;
+use Sonata\AdminBundle\Filter\FilterInterface;
+use Sonata\AdminBundle\Translator\NoopLabelTranslatorStrategy;
+
+/**
+ * @author Andrej Hudec <pulzarraider@gmail.com>
+ */
+class DatagridMapperTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * @var DatagridMapper
+     */
+    private $datagridMapper;
+
+    /**
+     * @var Datagrid
+     */
+    private $datagrid;
+
+    public function setUp()
+    {
+        $datagridBuilder = $this->getMock('Sonata\AdminBundle\Builder\DatagridBuilderInterface');
+
+        $proxyQuery = $this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface');
+        $pager = $this->getMock('Sonata\AdminBundle\Datagrid\PagerInterface');
+        $fieldDescriptionCollection = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionCollection');
+        $formBuilder = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
+                     ->disableOriginalConstructor()
+                     ->getMock();
+
+        $this->datagrid = new Datagrid($proxyQuery, $fieldDescriptionCollection, $pager, $formBuilder, array());
+
+        $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
+
+        //php 5.3 BC
+        $filter = $this->getMockForAbstractClass('Sonata\AdminBundle\Filter\Filter');
+
+        $filter->expects($this->any())
+            ->method('getDefaultOptions')
+            ->will($this->returnValue(array('foo_default_option'=>'bar_default')));
+
+        $datagridBuilder->expects($this->any())
+            ->method('addFilter')
+            ->will($this->returnCallback(function($datagrid, $type, $fieldDescription, $admin) use ($filter) {
+                $fieldDescription->setType($type);
+
+                $filterClone = clone $filter;
+                $filterClone->initialize($fieldDescription->getName(), $fieldDescription->getOptions());
+                $datagrid->addFilter($filterClone);
+            }));
+
+        $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
+
+        //php 5.3 BC
+        $fieldDescription = $this->getFieldDescriptionMock();
+
+        $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;
+            }));
+
+        $admin->expects($this->any())
+            ->method('getModelManager')
+            ->will($this->returnValue($modelManager));
+
+        $this->datagridMapper = new DatagridMapper($datagridBuilder, $this->datagrid, $admin);
+    }
+
+    public function testFluidInterface()
+    {
+        $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
+
+        $this->assertEquals($this->datagridMapper, $this->datagridMapper->add($fieldDescription, null, array('field_name'=>'fooFilterName')));
+        $this->assertEquals($this->datagridMapper, $this->datagridMapper->remove('fooName'));
+        $this->assertEquals($this->datagridMapper, $this->datagridMapper->reorder(array()));
+    }
+
+    public function testGet()
+    {
+        $this->assertFalse($this->datagridMapper->has('fooName'));
+
+        $fieldDescription = $this->getFieldDescriptionMock('foo.name', 'fooLabel');
+
+        $this->datagridMapper->add($fieldDescription, null, array('field_name'=>'fooFilterName'));
+
+        $filter = $this->datagridMapper->get('foo.name');
+        $this->assertInstanceOf('Sonata\AdminBundle\Filter\FilterInterface', $filter);
+        $this->assertEquals('foo.name', $filter->getName());
+        $this->assertEquals('foo__name', $filter->getFormName());
+        $this->assertEquals('text', $filter->getFieldType());
+        $this->assertEquals('fooLabel', $filter->getLabel());
+        $this->assertEquals(array('required'=>false), $filter->getFieldOptions());
+        $this->assertEquals(array(
+            'foo_default_option' => 'bar_default',
+            'label' => 'fooLabel',
+            'field_name' => 'fooFilterName',
+            'placeholder' => 'short_object_description_placeholder',
+        ), $filter->getOptions());
+    }
+
+    public function testGet2()
+    {
+        $this->assertFalse($this->datagridMapper->has('fooName'));
+
+        $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
+
+        $this->datagridMapper->add($fieldDescription, 'foo_type', array('field_name'=>'fooFilterName', 'foo_filter_option'=>'foo_filter_option_value', 'foo_default_option'=>'bar_custom'), 'foo_field_type', array('foo_field_option'=>'baz'));
+
+        $filter = $this->datagridMapper->get('fooName');
+        $this->assertInstanceOf('Sonata\AdminBundle\Filter\FilterInterface', $filter);
+        $this->assertEquals('fooName', $filter->getName());
+        $this->assertEquals('fooName', $filter->getFormName());
+        $this->assertEquals('foo_field_type', $filter->getFieldType());
+        $this->assertEquals('fooLabel', $filter->getLabel());
+        $this->assertEquals(array('foo_field_option'=>'baz'), $filter->getFieldOptions());
+        $this->assertEquals(array(
+            'foo_default_option' => 'bar_custom',
+            'label' => 'fooLabel',
+            'field_name' => 'fooFilterName',
+            'field_options' => array ('foo_field_option' => 'baz'),
+            'field_type' => 'foo_field_type',
+            'placeholder' => 'short_object_description_placeholder',
+            'foo_filter_option' => 'foo_filter_option_value',
+        ), $filter->getOptions());
+    }
+
+    public function testAdd()
+    {
+        $this->datagridMapper->add('fooName');
+
+        $this->assertTrue($this->datagridMapper->has('fooName'));
+
+        $fieldDescription = $this->datagridMapper->get('fooName');
+
+        $this->assertInstanceOf('Sonata\AdminBundle\Filter\FilterInterface', $fieldDescription);
+        $this->assertEquals('fooName', $fieldDescription->getName());
+    }
+
+    public function testAddRemove()
+    {
+        $this->assertFalse($this->datagridMapper->has('fooName'));
+
+        $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
+
+        $this->datagridMapper->add($fieldDescription, null, array('field_name'=>'fooFilterName'));
+        $this->assertTrue($this->datagridMapper->has('fooName'));
+
+        $this->datagridMapper->remove('fooName');
+        $this->assertFalse($this->datagridMapper->has('fooName'));
+    }
+
+    public function testAddException()
+    {
+        try {
+            $this->datagridMapper->add(12345);
+        } catch (\RuntimeException $e) {
+            $this->assertContains('invalid state', $e->getMessage());
+
+            return;
+        }
+
+        $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
+    }
+
+    public function testReorder()
+    {
+        $fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');
+        $fieldDescription2 = $this->getFieldDescriptionMock('fooName2', 'fooLabel2');
+        $fieldDescription3 = $this->getFieldDescriptionMock('fooName3', 'fooLabel3');
+        $fieldDescription4 = $this->getFieldDescriptionMock('fooName4', 'fooLabel4');
+
+        $this->datagridMapper->add($fieldDescription1, null, array('field_name'=>'fooFilterName1'));
+        $this->datagridMapper->add($fieldDescription2, null, array('field_name'=>'fooFilterName2'));
+        $this->datagridMapper->add($fieldDescription3, null, array('field_name'=>'fooFilterName3'));
+        $this->datagridMapper->add($fieldDescription4, null, array('field_name'=>'fooFilterName4'));
+
+        $this->assertEquals(array(
+            'fooName1',
+            'fooName2',
+            'fooName3',
+            'fooName4',
+       ), array_keys($this->datagrid->getFilters()));
+
+        $this->datagridMapper->reorder(array('fooName3', 'fooName2', 'fooName1', 'fooName4'));
+
+        $this->assertEquals(array(
+            'fooName3',
+            'fooName2',
+            'fooName1',
+            'fooName4',
+       ), array_keys($this->datagrid->getFilters()));
+    }
+
+    private function getFieldDescriptionMock($name=null, $label=null)
+    {
+        $fieldDescription = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\BaseFieldDescription');
+
+        if ($name !== null) {
+            $fieldDescription->setName($name);
+        }
+
+        if ($label !== null) {
+            $fieldDescription->setOption('label', $label);
+        }
+
+        return $fieldDescription;
+    }
+}

+ 190 - 0
Tests/Datagrid/ListMapperTest.php

@@ -0,0 +1,190 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ */
+
+namespace Sonata\AdminBundle\Tests\Datagrid;
+
+use Sonata\AdminBundle\Admin\AdminInterface;
+use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
+use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
+use Sonata\AdminBundle\Builder\ListBuilderInterface;
+use Sonata\AdminBundle\Datagrid\ListMapper;
+use Sonata\AdminBundle\Translator\NoopLabelTranslatorStrategy;
+
+/**
+ * @author Andrej Hudec <pulzarraider@gmail.com>
+ */
+class ListMapperTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * @var ListMapper
+     */
+    private $listMapper;
+
+    /**
+     * @var FieldDescriptionCollection
+     */
+    private $fieldDescriptionCollection;
+
+    public function setUp()
+    {
+        $listBuilder = $this->getMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
+        $this->fieldDescriptionCollection = new FieldDescriptionCollection();
+        $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
+
+        $listBuilder->expects($this->any())
+            ->method('addField')
+            ->will($this->returnCallback(function($list, $type, $fieldDescription, $admin) {
+                $list->add($fieldDescription);
+            }));
+
+        $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
+
+        //php 5.3 BC
+        $fieldDescription = $this->getFieldDescriptionMock();
+
+        $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;
+            }));
+
+        $admin->expects($this->any())
+            ->method('getModelManager')
+            ->will($this->returnValue($modelManager));
+
+        $labelTranslatorStrategy = new NoopLabelTranslatorStrategy;
+
+        $admin->expects($this->any())
+            ->method('getLabelTranslatorStrategy')
+            ->will($this->returnValue($labelTranslatorStrategy));
+
+        $this->listMapper = new ListMapper($listBuilder, $this->fieldDescriptionCollection, $admin);
+    }
+
+    public function testFluidInterface()
+    {
+        $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
+
+        $this->assertEquals($this->listMapper, $this->listMapper->add($fieldDescription));
+        $this->assertEquals($this->listMapper, $this->listMapper->remove('fooName'));
+        $this->assertEquals($this->listMapper, $this->listMapper->reorder(array()));
+    }
+
+    public function testGet()
+    {
+        $this->assertFalse($this->listMapper->has('fooName'));
+
+        $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
+
+        $this->listMapper->add($fieldDescription);
+        $this->assertEquals($fieldDescription, $this->listMapper->get('fooName'));
+    }
+
+    public function testAddIdentifier()
+    {
+        $this->assertFalse($this->listMapper->has('fooName'));
+
+        $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
+
+        $this->listMapper->addIdentifier($fieldDescription);
+        $this->assertTrue($this->listMapper->has('fooName'));
+    }
+
+    public function testAdd()
+    {
+        $this->listMapper->add('fooName');
+
+        $this->assertTrue($this->listMapper->has('fooName'));
+
+        $fieldDescription = $this->listMapper->get('fooName');
+
+        $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionInterface', $fieldDescription);
+        $this->assertEquals('fooName', $fieldDescription->getName());
+        $this->assertEquals('fooName', $fieldDescription->getOption('label'));
+    }
+
+    public function testAddRemove()
+    {
+        $this->assertFalse($this->listMapper->has('fooName'));
+
+        $fieldDescription = $this->getFieldDescriptionMock('fooName', 'fooLabel');
+
+        $this->listMapper->add($fieldDescription);
+        $this->assertTrue($this->listMapper->has('fooName'));
+
+        $this->listMapper->remove('fooName');
+        $this->assertFalse($this->listMapper->has('fooName'));
+    }
+
+    public function testAddException()
+    {
+        try {
+            $this->listMapper->add(12345);
+        } catch (\RuntimeException $e) {
+            $this->assertContains('Unknown or duplicate field name in list mapper. Field name should be either of FieldDescriptionInterface interface or string. Names should be unique.', $e->getMessage());
+
+            return;
+        }
+
+        $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
+    }
+
+    public function testReorder()
+    {
+        $fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');
+        $fieldDescription2 = $this->getFieldDescriptionMock('fooName2', 'fooLabel2');
+        $fieldDescription3 = $this->getFieldDescriptionMock('fooName3', 'fooLabel3');
+        $fieldDescription4 = $this->getFieldDescriptionMock('fooName4', 'fooLabel4');
+
+        $this->listMapper->add($fieldDescription1);
+        $this->listMapper->add($fieldDescription2);
+        $this->listMapper->add($fieldDescription3);
+        $this->listMapper->add($fieldDescription4);
+
+        $this->assertEquals(array(
+            'fooName1'=>$fieldDescription1,
+            'fooName2'=>$fieldDescription2,
+            'fooName3'=>$fieldDescription3,
+            'fooName4'=>$fieldDescription4,
+       ), $this->fieldDescriptionCollection->getElements());
+
+        $this->listMapper->reorder(array('fooName3', 'fooName2', 'fooName1', 'fooName4'));
+
+        //print_r is used to compare order of items in associative arrays
+        $this->assertEquals(print_r(array(
+            'fooName3'=>$fieldDescription3,
+            'fooName2'=>$fieldDescription2,
+            'fooName1'=>$fieldDescription1,
+            'fooName4'=>$fieldDescription4,
+       ), true), print_r($this->fieldDescriptionCollection->getElements(), true));
+
+    }
+
+    private function getFieldDescriptionMock($name=null, $label=null)
+    {
+        $fieldDescription = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\BaseFieldDescription');
+
+        if ($name !== null) {
+            $fieldDescription->setName($name);
+        }
+
+        if ($label !== null) {
+            $fieldDescription->setOption('label', $label);
+        }
+
+        return $fieldDescription;
+    }
+}

+ 53 - 14
Tests/Show/ShowMapperTest.php

@@ -16,6 +16,7 @@ use Sonata\AdminBundle\Admin\AdminInterface;
 use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
 use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
 use Sonata\AdminBundle\Builder\ShowBuilderInterface;
+use Sonata\AdminBundle\Translator\NoopLabelTranslatorStrategy;
 
 /**
  * Test for ShowMapper
@@ -27,27 +28,27 @@ class ShowMapperTest extends \PHPUnit_Framework_TestCase
     /**
      * @var ShowMapper
      */
-    protected $showMapper;
+    private $showMapper;
 
     /**
      * @var AdminInterface
      */
-    protected $admin;
+    private $admin;
 
     /**
      * @var Sonata\AdminBundle\Builder\ShowBuilderInterface
      */
-    protected $showBuilder;
+    private $showBuilder;
 
     /**
      * @var FieldDescriptionCollection
      */
-    protected $fieldDescriptionCollection;
+    private $fieldDescriptionCollection;
 
     /**
      * @var array
      */
-    protected $groups;
+    private $groups;
 
     public function setUp()
     {
@@ -76,7 +77,7 @@ class ShowMapperTest extends \PHPUnit_Framework_TestCase
                 $groups = $showGroups;
             }));
 
-      $this->admin->expects($this->any())
+        $this->admin->expects($this->any())
             ->method('reorderShowGroup')
             ->will($this->returnCallback(function($group, $keys) use (&$groups) {
                 $showGroups = $groups;
@@ -84,6 +85,31 @@ class ShowMapperTest extends \PHPUnit_Framework_TestCase
                 $groups = $showGroups;
             }));
 
+        $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
+
+        //php 5.3 BC
+        $fieldDescription = $this->getFieldDescriptionMock();
+
+        $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->expects($this->any())
+            ->method('getModelManager')
+            ->will($this->returnValue($modelManager));
+
+        $labelTranslatorStrategy = new NoopLabelTranslatorStrategy;
+
+        $this->admin->expects($this->any())
+            ->method('getLabelTranslatorStrategy')
+            ->will($this->returnValue($labelTranslatorStrategy));
+
         $this->showBuilder->expects($this->any())
             ->method('addField')
             ->will($this->returnCallback(function($list, $type, $fieldDescription, $admin) {
@@ -112,6 +138,19 @@ class ShowMapperTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals($fieldDescription, $this->showMapper->get('fooName'));
     }
 
+    public function testAdd()
+    {
+        $this->showMapper->add('fooName');
+
+        $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 testAddRemove()
     {
         $this->assertFalse($this->showMapper->has('fooName'));
@@ -173,17 +212,17 @@ class ShowMapperTest extends \PHPUnit_Framework_TestCase
        )), true), print_r($this->admin->getShowGroups(), true));
     }
 
-    protected function getFieldDescriptionMock($name, $label)
+    private function getFieldDescriptionMock($name=null, $label=null)
     {
-        $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
+        $fieldDescription = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\BaseFieldDescription');
 
-        $fieldDescription->expects($this->any())
-            ->method('getName')
-            ->will($this->returnValue($name));
+        if ($name !== null) {
+            $fieldDescription->setName($name);
+        }
 
-        $fieldDescription->expects($this->any())
-            ->method('getLabel')
-            ->will($this->returnValue($label));
+        if ($label !== null) {
+            $fieldDescription->setOption('label', $label);
+        }
 
         return $fieldDescription;
     }