Development

Changeset 19554

You must first sign up to be able to contribute.

Changeset 19554

Show
Ignore:
Timestamp:
06/25/09 17:19:08 (4 years ago)
Author:
bschussek
Message:

Fixed: lime_mock now deals with type hints and default values correctly

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfLimeExtraPlugin/trunk/lib/mock/lime_mock.class.php

    r19546 r19554  
    141141   * @var string 
    142142   */ 
    143   protected static $methodTemplate = '%s function %s() { $args = func_get_args(); return $this->__call(%s, $args); }'; 
     143  protected static $methodTemplate = '%s function %s(%s) { $args = func_get_args(); return $this->__call(%s, $args); }'; 
     144 
     145  protected static $parameterTemplate = '%s %s'; 
     146 
     147  protected static $parameterWithDefaultTemplate = '%s %s = %s'; 
    144148 
    145149  /** 
     
    177181      $modifiers = array_diff($modifiers, array('abstract')); 
    178182      $modifiers = implode(' ', $modifiers); 
    179       $methods .= sprintf(self::$methodTemplate, $modifiers, $method->getName(), $method->getName()); 
     183 
     184      $parameters = array(); 
     185 
     186      foreach ($method->getParameters() as $parameter) 
     187      { 
     188        /* @var $parameter ReflectionParameter */ 
     189        if ($parameter->getClass()) 
     190        { 
     191          $typeHint = $parameter->getClass()->getName(); 
     192        } 
     193        else if ($parameter->isArray()) 
     194        { 
     195          $typeHint = 'array'; 
     196        } 
     197 
     198        $name = '$'.$parameter->getName(); 
     199 
     200        if ($parameter->isOptional()) 
     201        { 
     202          $default = var_export($parameter->getDefaultValue(), true); 
     203          $parameters[] = sprintf(self::$parameterWithDefaultTemplate, $typeHint, $name, $default); 
     204        } 
     205        else 
     206        { 
     207          $parameters[] = sprintf(self::$parameterTemplate, $typeHint, $name); 
     208        } 
     209      } 
     210 
     211      $methods .= sprintf(self::$methodTemplate, $modifiers, $method->getName(), 
     212          implode(', ', $parameters), $method->getName()); 
    180213    } 
    181214 
  • plugins/sfLimeExtraPlugin/trunk/test/unit/mock/lime_mockTest.php

    r19546 r19554  
    1515interface TestInterface 
    1616{ 
    17   public function testMethod(); 
     17  public function testMethod($parameter); 
     18
     19 
     20interface TestInterfaceWithTypeHints 
     21
     22  public function testMethod(stdClass $object, array $array); 
     23
     24 
     25interface TestInterfaceWithDefaultValues 
     26
     27  public function testMethod($null = null, $int = 1, $bool = true, $string = 'String', $float = 1.1); 
    1828} 
    1929 
    2030abstract class TestClassAbstract 
    2131{ 
    22   abstract public function testMethod(); 
     32  abstract public function testMethod($parameter); 
    2333} 
    2434 
     
    3646 
    3747 
    38 $t = new lime_test(45, new lime_output_color()); 
     48$t = new lime_test(47, new lime_output_color()); 
    3949 
    4050 
     
    6373  $t->ok($m instanceof FoobarClass, 'The mock generates and inherits the class'); 
    6474  $t->ok($m instanceof lime_mock_interface, 'The mock implements "lime_mock_interface"'); 
     75 
     76 
     77$t->comment('Methods with type hints can be mocked'); 
     78 
     79  // test 
     80  $m = lime_mock::create('TestInterfaceWithTypeHints'); 
     81  // assertions 
     82  $t->ok($m instanceof TestInterfaceWithTypeHints, 'The mock implements the interface'); 
     83 
     84 
     85$t->comment('Methods with default values can be mocked'); 
     86 
     87  // test 
     88  $m = lime_mock::create('TestInterfaceWithDefaultValues'); 
     89  // assertions 
     90  $t->ok($m instanceof TestInterfaceWithDefaultValues, 'The mock implements the interface'); 
    6591 
    6692