Changeset 19554
- Timestamp:
- 06/25/09 17:19:08 (4 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfLimeExtraPlugin/trunk/lib/mock/lime_mock.class.php
r19546 r19554 141 141 * @var string 142 142 */ 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'; 144 148 145 149 /** … … 177 181 $modifiers = array_diff($modifiers, array('abstract')); 178 182 $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()); 180 213 } 181 214 plugins/sfLimeExtraPlugin/trunk/test/unit/mock/lime_mockTest.php
r19546 r19554 15 15 interface TestInterface 16 16 { 17 public function testMethod(); 17 public function testMethod($parameter); 18 } 19 20 interface TestInterfaceWithTypeHints 21 { 22 public function testMethod(stdClass $object, array $array); 23 } 24 25 interface TestInterfaceWithDefaultValues 26 { 27 public function testMethod($null = null, $int = 1, $bool = true, $string = 'String', $float = 1.1); 18 28 } 19 29 20 30 abstract class TestClassAbstract 21 31 { 22 abstract public function testMethod( );32 abstract public function testMethod($parameter); 23 33 } 24 34 … … 36 46 37 47 38 $t = new lime_test(4 5, new lime_output_color());48 $t = new lime_test(47, new lime_output_color()); 39 49 40 50 … … 63 73 $t->ok($m instanceof FoobarClass, 'The mock generates and inherits the class'); 64 74 $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'); 65 91 66 92