Development

Changeset 19781

You must first sign up to be able to contribute.

Changeset 19781

Show
Ignore:
Timestamp:
07/01/09 12:59:46 (7 months ago)
Author:
bschussek
Message:

Fixed: Classes with construtors can be mocked

Files:

Legend:

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

    r19555 r19781  
    177177    foreach ($class->getMethods() as $method) 
    178178    { 
    179       /* @var $method ReflectionMethod */ 
    180       $modifiers = Reflection::getModifierNames($method->getModifiers()); 
    181       $modifiers = array_diff($modifiers, array('abstract')); 
    182       $modifiers = implode(' ', $modifiers); 
    183  
    184       $parameters = array(); 
    185  
    186       foreach ($method->getParameters() as $parameter) 
     179      if (!in_array($method->getName(), array('__construct', '__call', '__lime_getControl'))) 
    187180      { 
    188         /* @var $parameter ReflectionParameter */ 
    189         if ($parameter->getClass()) 
     181        /* @var $method ReflectionMethod */ 
     182        $modifiers = Reflection::getModifierNames($method->getModifiers()); 
     183        $modifiers = array_diff($modifiers, array('abstract')); 
     184        $modifiers = implode(' ', $modifiers); 
     185   
     186        $parameters = array(); 
     187   
     188        foreach ($method->getParameters() as $parameter) 
    190189        { 
    191           $typeHint = $parameter->getClass()->getName(); 
     190          /* @var $parameter ReflectionParameter */ 
     191          if ($parameter->getClass()) 
     192          { 
     193            $typeHint = $parameter->getClass()->getName(); 
     194          } 
     195          else if ($parameter->isArray()) 
     196          { 
     197            $typeHint = 'array'; 
     198          } 
     199          else 
     200          { 
     201            $typeHint = ''; 
     202          } 
     203   
     204          $name = '$'.$parameter->getName(); 
     205   
     206          if ($parameter->isOptional()) 
     207          { 
     208            $default = var_export($parameter->getDefaultValue(), true); 
     209            $parameters[] = sprintf(self::$parameterWithDefaultTemplate, $typeHint, $name, $default); 
     210          } 
     211          else 
     212          { 
     213            $parameters[] = sprintf(self::$parameterTemplate, $typeHint, $name); 
     214          } 
    192215        } 
    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         } 
     216 
     217        $methods .= sprintf(self::$methodTemplate, $modifiers, $method->getName(), 
     218            implode(', ', $parameters), $method->getName()); 
    209219      } 
    210  
    211       $methods .= sprintf(self::$methodTemplate, $modifiers, $method->getName(), 
    212           implode(', ', $parameters), $method->getName()); 
    213220    } 
    214221 
  • plugins/sfLimeExtraPlugin/trunk/test/unit/mock/lime_mockTest.php

    r19743 r19781  
    3333} 
    3434 
     35class TestClassWithConstructor 
     36{ 
     37  public static $calls = 0; 
     38   
     39  public function __construct() 
     40  { 
     41    self::$calls++; 
     42  } 
     43} 
     44 
    3545class TestClass 
    3646{ 
     
    4656 
    4757 
    48 $t = new lime_test_simple(47, new lime_output_color()); 
     58$t = new lime_test_simple(50, new lime_output_color()); 
    4959 
    5060 
     
    8595  $t->ok($m instanceof FoobarClass, 'The mock generates and inherits the class'); 
    8696  $t->ok($m instanceof lime_mock_interface, 'The mock implements "lime_mock_interface"'); 
     97   
     98   
     99// @Test: Classes with constructors can be mocked 
     100 
     101  $m = lime_mocK::create('TestClassWithConstructor'); 
     102  // assertions 
     103  $t->ok($m instanceof TestClassWithConstructor, 'The mock generates and inherits the class'); 
     104  $t->ok($m instanceof lime_mock_interface, 'The mock implements "lime_mock_interface"'); 
     105  $t->is(TestClassWithConstructor::$calls, 0, 'The original constructor was not called'); 
    87106 
    88107 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting.
Sensio Labs also supports several large Open-Source projects.