protected function generateClassMethodCode(\reflectionClass $class)
{
$mockedMethods = '';
$mockedMethodNames = array();
$className = $class->getName();
$constructor = $class->getConstructor();
if ($constructor === null) {
$mockedMethods .= self::generateDefaultConstructor();
$mockedMethodNames[] = '__construct';
} else {
if ($constructor->isFinal() === false) {
$constructorName = $constructor->getName();
$overload = $this->getOverload($constructorName);
if ($constructor->isPublic() === false) {
$this->shuntParentClassCalls();
if ($overload === null) {
$this->overload(new php\method('__construct'));
$overload = $this->getOverload('__construct');
}
}
$parameters = $this->getParameters($constructor);
if ($overload === null) {
$mockedMethods .= "\t" . 'public function __construct(' . $this->getParametersSignature($constructor) . ')';
} else {
$overload->addArgument(php\method\argument::get('mockController')->isObject('\\' . __NAMESPACE__ . '\\controller')->setDefaultValue(null));
$mockedMethods .= "\t" . $overload;
}
$mockedMethods .= PHP_EOL;
$mockedMethods .= "\t" . '{' . PHP_EOL;
$mockedMethods .= "\t\t" . '$arguments = array_merge(array(' . join(', ', $parameters) . '), array_slice(func_get_args(), ' . sizeof($parameters) . ', -1));' . PHP_EOL;
$mockedMethods .= "\t\t" . 'if ($mockController === null)' . PHP_EOL;
$mockedMethods .= "\t\t" . '{' . PHP_EOL;
$mockedMethods .= "\t\t\t" . '$mockController = \\mageekguy\\atoum\\mock\\controller::get();' . PHP_EOL;
$mockedMethods .= "\t\t" . '}' . PHP_EOL;
$mockedMethods .= "\t\t" . 'if ($mockController !== null)' . PHP_EOL;
$mockedMethods .= "\t\t" . '{' . PHP_EOL;
$mockedMethods .= "\t\t\t" . '$this->setMockController($mockController);' . PHP_EOL;
$mockedMethods .= "\t\t" . '}' . PHP_EOL;
if ($constructor->isAbstract() === true || $this->isShunted('__construct') === true || $this->isShunted($className) === true) {
$methodName = $this->isShunted($className) === true ? $className : '__construct';
$mockedMethods .= "\t\t" . 'if (isset($this->getMockController()->' . $methodName . ') === false)' . PHP_EOL;
$mockedMethods .= "\t\t" . '{' . PHP_EOL;
$mockedMethods .= "\t\t\t" . '$this->getMockController()->' . $methodName . ' = function() {};' . PHP_EOL;
$mockedMethods .= "\t\t" . '}' . PHP_EOL;
$mockedMethods .= "\t\t" . '$this->getMockController()->invoke(\'' . $methodName . '\', $arguments);' . PHP_EOL;
} else {
$mockedMethods .= "\t\t" . 'if (isset($this->getMockController()->' . $constructorName . ') === true)' . PHP_EOL;
$mockedMethods .= "\t\t" . '{' . PHP_EOL;
$mockedMethods .= "\t\t\t" . '$this->getMockController()->invoke(\'' . $constructorName . '\', $arguments);' . PHP_EOL;
$mockedMethods .= "\t\t" . '}' . PHP_EOL;
$mockedMethods .= "\t\t" . 'else' . PHP_EOL;
$mockedMethods .= "\t\t" . '{' . PHP_EOL;
$mockedMethods .= "\t\t\t" . '$this->getMockController()->addCall(\'' . $constructorName . '\', $arguments);' . PHP_EOL;
if ($this->shuntParentClassCalls === false) {
$mockedMethods .= "\t\t\t" . 'call_user_func_array(\'parent::' . $constructorName . '\', $arguments);' . PHP_EOL;
}
$mockedMethods .= "\t\t" . '}' . PHP_EOL;
}
$mockedMethods .= "\t" . '}' . PHP_EOL;
$mockedMethodNames[] = $constructorName;
}
}
foreach ($class->getMethods() as $method) {
if ($method->isConstructor() === false && $this->methodIsMockable($method) === true) {
$methodName = $method->getName();
$mockedMethodNames[] = strtolower($methodName);
$overload = $this->getOverload($methodName);
$parameters = $this->getParameters($method);
if ($overload !== null) {
$mockedMethods .= "\t" . $overload;
} else {
$mockedMethods .= "\t" . ($method->isPublic() === true ? 'public' : 'protected') . ' function' . ($method->returnsReference() === false ? '' : ' &') . ' ' . $methodName . '(' . $this->getParametersSignature($method) . ')';
}
$mockedMethods .= PHP_EOL . "\t" . '{' . PHP_EOL;
$mockedMethods .= "\t\t" . '$arguments = array_merge(array(' . join(', ', $parameters) . '), array_slice(func_get_args(), ' . sizeof($parameters) . '));' . PHP_EOL;
if ($this->isShunted($methodName) === true || $method->isAbstract() === true) {
$mockedMethods .= "\t\t" . 'if (isset($this->getMockController()->' . $methodName . ') === false)' . PHP_EOL;
$mockedMethods .= "\t\t" . '{' . PHP_EOL;
$mockedMethods .= "\t\t\t" . '$this->getMockController()->' . $methodName . ' = function() {};' . PHP_EOL;
$mockedMethods .= "\t\t" . '}' . PHP_EOL;
$mockedMethods .= "\t\t" . 'return $this->getMockController()->invoke(\'' . $methodName . '\', $arguments);' . PHP_EOL;
} else {
$mockedMethods .= "\t\t" . 'if (isset($this->getMockController()->' . $methodName . ') === true)' . PHP_EOL;
$mockedMethods .= "\t\t" . '{' . PHP_EOL;
$mockedMethods .= "\t\t\t" . 'return $this->getMockController()->invoke(\'' . $methodName . '\', $arguments);' . PHP_EOL;
$mockedMethods .= "\t\t" . '}' . PHP_EOL;
$mockedMethods .= "\t\t" . 'else' . PHP_EOL;
$mockedMethods .= "\t\t" . '{' . PHP_EOL;
if ($methodName === '__call') {
$mockedMethods .= "\t\t\t" . '$this->getMockController()->addCall(current(array_slice($arguments, 0, 1)), current(array_slice($arguments, 1)));' . PHP_EOL;
}
$mockedMethods .= "\t\t\t" . '$this->getMockController()->addCall(\'' . $methodName . '\', $arguments);' . PHP_EOL;
if ($this->shuntParentClassCalls === false) {
$mockedMethods .= "\t\t\t" . 'return call_user_func_array(\'parent::' . $methodName . '\', $arguments);' . PHP_EOL;
}
$mockedMethods .= "\t\t" . '}' . PHP_EOL;
}
$mockedMethods .= "\t" . '}' . PHP_EOL;
}
}
return $mockedMethods . self::generateGetMockedMethod($mockedMethodNames);
}