Browse Source

Added support for the ‘has’ getter prefix (#4653)

Updated the unit tests so both ‘has’ and ‘is’ methods get tested, besides ‘get’ methods.
Leon Boot 7 years ago
parent
commit
c21d0f88c5

+ 1 - 0
Admin/BaseFieldDescription.php

@@ -340,6 +340,7 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
         }
         $getters[] = 'get'.$camelizedFieldName;
         $getters[] = 'is'.$camelizedFieldName;
+        $getters[] = 'has'.$camelizedFieldName;
 
         foreach ($getters as $getter) {
             if (method_exists($object, $getter)) {

+ 18 - 7
Tests/Admin/BaseFieldDescriptionTest.php

@@ -14,6 +14,7 @@ namespace Sonata\AdminBundle\Tests\Admin;
 use Sonata\AdminBundle\Admin\BaseFieldDescription;
 use Sonata\AdminBundle\Tests\Fixtures\Admin\FieldDescription;
 use Sonata\AdminBundle\Tests\Fixtures\Entity\Foo;
+use Sonata\AdminBundle\Tests\Fixtures\Entity\FooBoolean;
 use Sonata\AdminBundle\Tests\Fixtures\Entity\FooCall;
 use Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
 
@@ -145,13 +146,15 @@ class BaseFieldDescriptionTest extends PHPUnit_Framework_TestCase
         /*
          * Test with underscored attribute name
          */
-        $description3 = new FieldDescription();
-        $mock3 = $this->getMockBuilder('stdClass')
-            ->setMethods(array('getFake'))
-            ->getMock();
-
-        $mock3->expects($this->once())->method('getFake')->will($this->returnValue(42));
-        $this->assertSame(42, $description3->getFieldValue($mock3, '_fake'));
+        foreach (array('getFake', 'isFake', 'hasFake') as $method) {
+            $description3 = new FieldDescription();
+            $mock3 = $this->getMockBuilder('stdClass')
+                ->setMethods(array($method))
+                ->getMock();
+
+            $mock3->expects($this->once())->method($method)->will($this->returnValue(42));
+            $this->assertSame(42, $description3->getFieldValue($mock3, '_fake'));
+        }
     }
 
     /**
@@ -225,6 +228,14 @@ class BaseFieldDescriptionTest extends PHPUnit_Framework_TestCase
         $description = new FieldDescription();
         $this->assertSame('Bar', $description->getFieldValue($foo, 'bar'));
 
+        $foo = new FooBoolean();
+        $foo->setBar(true);
+        $foo->setBaz(false);
+
+        $description = new FieldDescription();
+        $this->assertSame(true, $description->getFieldValue($foo, 'bar'));
+        $this->assertSame(false, $description->getFieldValue($foo, 'baz'));
+
         $this->expectException('Sonata\AdminBundle\Exception\NoValueException');
         $description->getFieldValue($foo, 'inexistantMethod');
     }

+ 30 - 0
Tests/Fixtures/Entity/FooBoolean.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace Sonata\AdminBundle\Tests\Fixtures\Entity;
+
+class FooBoolean
+{
+    private $bar;
+
+    private $baz;
+
+    public function hasBar()
+    {
+        return $this->bar;
+    }
+
+    public function setBar($bar)
+    {
+        $this->bar = $bar;
+    }
+
+    public function isBaz()
+    {
+        return $this->baz;
+    }
+
+    public function setBaz($baz)
+    {
+        $this->baz = $baz;
+    }
+}