public function build(\reflectionClass $class, &$instance = null) { $this->factory = null; if ($class->isInterface() === false && $class->isAbstract() === false) { $constructor = $class->getConstructor(); if ($constructor === null || $constructor->isPublic() === true) { $constructorParameters = $closureParameters = array(); if ($constructor !== null) { $this->allArgumentsAreOptional = $constructor->getNumberOfRequiredParameters() === 0; foreach ($constructor->getParameters() as $position => $parameter) { $closureParameters[$position] = ($parameter->isPassedByReference() === false ? '' : '& ') . ($constructorParameters[$position] = '$' . $parameter->getName()); if (self::isVariadic($parameter)) { $closureParameters[$position] = '...' . $closureParameters[$position]; $constructorParameters[$position] = '...' . $constructorParameters[$position]; } switch (true) { case $parameter->isDefaultValueAvailable(): $defaultValue = var_export($parameter->getDefaultValue(), true); break; case $parameter->isOptional() && self::isVariadic($parameter) === false: $defaultValue = 'null'; break; default: $defaultValue = null; } if ($defaultValue !== null) { $closureParameters[$position] .= ' = ' . $defaultValue; } } } if ($constructor === null || sizeof($closureParameters) <= 0) { $this->factory = function () use(&$instance, $class) { return $instance = $class->newInstanceArgs(func_get_args()); }; } else { $this->factory = eval('return function(' . join(', ', $closureParameters) . ') use (& $instance) { return ($instance = new ' . $class->getName() . '(' . join(', ', $constructorParameters) . ')); };'); } } } return $this; }
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); }