Browse Source

Change exception message if duplicate field name in show mapper.
Add test function "testAddDuplicateFieldNameException" to ShowMapperTest.

Alexander Timofeev 10 years ago
parent
commit
73e9ea7157
2 changed files with 45 additions and 6 deletions
  1. 10 6
      Show/ShowMapper.php
  2. 35 0
      Tests/Show/ShowMapperTest.php

+ 10 - 6
Show/ShowMapper.php

@@ -60,12 +60,16 @@ class ShowMapper extends BaseGroupedMapper
         if ($name instanceof FieldDescriptionInterface) {
             $fieldDescription = $name;
             $fieldDescription->mergeOptions($fieldDescriptionOptions);
-        } elseif (is_string($name) && !$this->admin->hasShowFieldDescription($name)) {
-            $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
-                $this->admin->getClass(),
-                $name,
-                $fieldDescriptionOptions
-            );
+        } elseif (is_string($name)) {
+            if (!$this->admin->hasShowFieldDescription($name)) {
+                $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
+                    $this->admin->getClass(),
+                    $name,
+                    $fieldDescriptionOptions
+                );
+            } else {
+                throw new \RuntimeException(sprintf('Duplicate field name "%s" in show mapper. Names should be unique.', $name));
+            }
         } else {
             throw new \RuntimeException('invalid state');
         }

+ 35 - 0
Tests/Show/ShowMapperTest.php

@@ -50,6 +50,11 @@ class ShowMapperTest extends \PHPUnit_Framework_TestCase
      */
     private $groups;
 
+    /**
+     * @var array
+     */
+    private $listShowFields;
+
     public function setUp()
     {
         $this->showBuilder = $this->getMock('Sonata\AdminBundle\Builder\ShowBuilderInterface');
@@ -65,9 +70,11 @@ class ShowMapperTest extends \PHPUnit_Framework_TestCase
             ->will($this->returnValue(array()));
 
         $this->groups = array();
+        $this->listShowFields = array();
 
         // php 5.3 BC
         $groups = &$this->groups;
+        $listShowFields = &$this->listShowFields;
 
         $this->admin->expects($this->any())
             ->method('getShowGroups')
@@ -114,6 +121,18 @@ class ShowMapperTest extends \PHPUnit_Framework_TestCase
             ->method('getLabelTranslatorStrategy')
             ->will($this->returnValue($labelTranslatorStrategy));
 
+        $this->admin->expects($this->any())
+            ->method('hasShowFieldDescription')
+            ->will($this->returnCallback(function ($name) use (&$listShowFields) {
+                if (isset($listShowFields[$name])) {
+                    return true;
+                } else {
+                    $listShowFields[$name] = true;
+
+                    return false;
+                }
+            }));
+
         $this->showBuilder->expects($this->any())
             ->method('addField')
             ->will($this->returnCallback(function ($list, $type, $fieldDescription, $admin) {
@@ -319,6 +338,22 @@ class ShowMapperTest extends \PHPUnit_Framework_TestCase
         $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
     }
 
+    public function testAddDuplicateFieldNameException()
+    {
+        $name = 'name';
+
+        try {
+            $this->showMapper->add($name);
+            $this->showMapper->add($name);
+        } catch (\RuntimeException $e) {
+            $this->assertContains(sprintf('Duplicate field name "%s" in show mapper. Names should be unique.', $name), $e->getMessage());
+
+            return;
+        }
+
+        $this->fail('Failed asserting that duplicate field name exception of type "\RuntimeException" is thrown.');
+    }
+
     public function testReorder()
     {
         $this->assertSame(array(), $this->admin->getShowGroups());