Browse Source

request: Add $parameter option given to 'call_user_func'
-fix indentation
-parameter renamed in parameters
-call_user_func_array is used instead of call_user_func
-default value for parameters is defined (array())
-some tests and documentation added

Brice LALU 12 năm trước cách đây
mục cha
commit
2efdc9d4c4
2 tập tin đã thay đổi với 38 bổ sung8 xóa
  1. 8 7
      Admin/BaseFieldDescription.php
  2. 30 1
      Tests/Admin/BaseFieldDescriptionTest.php

+ 8 - 7
Admin/BaseFieldDescription.php

@@ -315,21 +315,22 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
         $camelizedFieldName = self::camelize($fieldName);
 
         $getters = array();
-
+        $parameters = array();
+        
         // prefer method name given in the code option
         if ($this->getOption('code')) {
             $getters[] = $this->getOption('code');
         }
-        
-        if($this->getOption('parameter')){
-            $parameter = $this->getOption('parameter');
+        // parameters for the method given in the code option
+        if($this->getOption('parameters')){
+            $parameters = $this->getOption('parameters');
         }
-            $getters[] = 'get' . $camelizedFieldName;
-            $getters[] = 'is' . $camelizedFieldName;
+        $getters[] = 'get' . $camelizedFieldName;
+        $getters[] = 'is' . $camelizedFieldName;
         
         foreach ($getters as $getter) {
             if (method_exists($object, $getter)) {
-                return call_user_func(array($object, $getter),$parameter);
+                return call_user_func_array(array($object, $getter),$parameters);
             }
         }
 

+ 30 - 1
Tests/Admin/BaseFieldDescriptionTest.php

@@ -90,9 +90,38 @@ class BaseFieldDescriptionTest extends \PHPUnit_Framework_TestCase
         $description->setOption('code', 'getFoo');
 
         $mock = $this->getMock('stdClass', array('getFoo'));
-        $mock->expects($this->once())->method('getFoo')->will($this->returnValue(42));
+        $mock->expects($this->any())->method('getFoo')->will($this->returnValue(42));
 
         $this->assertEquals(42, $description->getFieldValue($mock, 'fake'));
+        
+        /**
+         * Test with One parameter int
+         */
+        $arg1 = 38;
+        $oneParameter = array($arg1);
+        $description1 = new FieldDescription();
+        $description1->setOption('code', 'getWithOneParameter');
+        $description1->setOption('parameters', $oneParameter);
+        
+        $mock1 = $this->getMock('stdClass', array('getWithOneParameter'));
+        $returnValue1 = $arg1 + 2;
+        $mock1->expects($this->once())->method('getWithOneParameter')->with($this->equalTo($arg1))->will($this->returnValue($returnValue1));
+        
+        $this->assertEquals(40, $description1->getFieldValue($mock1, 'fake'));
+        
+        /**
+         * Test with Two parameters int
+         */
+        $arg2 = 4;
+        $twoParameters = array($arg1,$arg2);
+        $description2 = new FieldDescription();        
+        $description2->setOption('code', 'getWithTwoParameters');
+        $description2->setOption('parameters', $twoParameters);
+        
+        $mock2 = $this->getMock('stdClass', array('getWithTwoParameters'));
+        $returnValue2 = $arg1 + $arg2;
+        $mock2->expects($this->any())->method('getWithTwoParameters')->with($this->equalTo($arg1),$this->equalTo($arg2))->will($this->returnValue($returnValue2));
+        $this->assertEquals(42, $description2->getFieldValue($mock2, 'fake'));
     }
 
     /**