protected function PHPUnit_Framework_MockObject_Generator::generateMock in Zircon Profile 8
Same name and namespace in other branches
- 8.0 vendor/phpunit/phpunit-mock-objects/src/Framework/MockObject/Generator.php \PHPUnit_Framework_MockObject_Generator::generateMock()
Parameters
array|string $type:
array|null $methods:
string $mockClassName:
bool $callOriginalClone:
bool $callAutoload:
bool $cloneArguments:
bool $callOriginalMethods:
Return value
array
Throws
1 call to PHPUnit_Framework_MockObject_Generator::generateMock()
- PHPUnit_Framework_MockObject_Generator::generate in vendor/
phpunit/ phpunit-mock-objects/ src/ Framework/ MockObject/ Generator.php
File
- vendor/
phpunit/ phpunit-mock-objects/ src/ Framework/ MockObject/ Generator.php, line 624
Class
- PHPUnit_Framework_MockObject_Generator
- Mock Object Code Generator
Code
protected function generateMock($type, $methods, $mockClassName, $callOriginalClone, $callAutoload, $cloneArguments, $callOriginalMethods) {
$templateDir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Generator' . DIRECTORY_SEPARATOR;
$classTemplate = new Text_Template($templateDir . 'mocked_class.tpl');
$additionalInterfaces = array();
$cloneTemplate = '';
$isClass = false;
$isInterface = false;
$mockClassName = $this
->generateClassName($type, $mockClassName, 'Mock_');
if (is_array($type)) {
foreach ($type as $_type) {
if (!interface_exists($_type, $callAutoload)) {
throw new PHPUnit_Framework_Exception(sprintf('Interface "%s" does not exist.', $_type));
}
$additionalInterfaces[] = $_type;
foreach ($this
->getClassMethods($_type) as $method) {
if (in_array($method, $methods)) {
throw new PHPUnit_Framework_Exception(sprintf('Duplicate method "%s" not allowed.', $method));
}
$methods[] = $method;
}
}
}
if (class_exists($mockClassName['fullClassName'], $callAutoload)) {
$isClass = true;
}
else {
if (interface_exists($mockClassName['fullClassName'], $callAutoload)) {
$isInterface = true;
}
}
if (!class_exists($mockClassName['fullClassName'], $callAutoload) && !interface_exists($mockClassName['fullClassName'], $callAutoload)) {
$prologue = 'class ' . $mockClassName['originalClassName'] . "\n{\n}\n\n";
if (!empty($mockClassName['namespaceName'])) {
$prologue = 'namespace ' . $mockClassName['namespaceName'] . " {\n\n" . $prologue . "}\n\n" . "namespace {\n\n";
$epilogue = "\n\n}";
}
$cloneTemplate = new Text_Template($templateDir . 'mocked_clone.tpl');
}
else {
$class = new ReflectionClass($mockClassName['fullClassName']);
if ($class
->isFinal()) {
throw new PHPUnit_Framework_Exception(sprintf('Class "%s" is declared "final" and cannot be mocked.', $mockClassName['fullClassName']));
}
if ($class
->hasMethod('__clone')) {
$cloneMethod = $class
->getMethod('__clone');
if (!$cloneMethod
->isFinal()) {
if ($callOriginalClone && !$isInterface) {
$cloneTemplate = new Text_Template($templateDir . 'unmocked_clone.tpl');
}
else {
$cloneTemplate = new Text_Template($templateDir . 'mocked_clone.tpl');
}
}
}
else {
$cloneTemplate = new Text_Template($templateDir . 'mocked_clone.tpl');
}
}
if (is_object($cloneTemplate)) {
$cloneTemplate = $cloneTemplate
->render();
}
if (is_array($methods) && empty($methods) && ($isClass || $isInterface)) {
$methods = $this
->getClassMethods($mockClassName['fullClassName']);
}
if (!is_array($methods)) {
$methods = array();
}
$mockedMethods = '';
if (isset($class)) {
// https://github.com/sebastianbergmann/phpunit-mock-objects/issues/103
if ($isInterface && $class
->implementsInterface('Traversable') && !$class
->implementsInterface('Iterator') && !$class
->implementsInterface('IteratorAggregate')) {
$additionalInterfaces[] = 'Iterator';
$methods = array_merge($methods, $this
->getClassMethods('Iterator'));
}
foreach ($methods as $methodName) {
try {
$method = $class
->getMethod($methodName);
if ($this
->canMockMethod($method)) {
$mockedMethods .= $this
->generateMockedMethodDefinitionFromExisting($templateDir, $method, $cloneArguments, $callOriginalMethods);
}
} catch (ReflectionException $e) {
$mockedMethods .= $this
->generateMockedMethodDefinition($templateDir, $mockClassName['fullClassName'], $methodName, $cloneArguments);
}
}
}
else {
foreach ($methods as $methodName) {
$mockedMethods .= $this
->generateMockedMethodDefinition($templateDir, $mockClassName['fullClassName'], $methodName, $cloneArguments);
}
}
$method = '';
if (!in_array('method', $methods)) {
$methodTemplate = new Text_Template($templateDir . 'mocked_class_method.tpl');
$method = $methodTemplate
->render();
}
$classTemplate
->setVar(array(
'prologue' => isset($prologue) ? $prologue : '',
'epilogue' => isset($epilogue) ? $epilogue : '',
'class_declaration' => $this
->generateMockClassDeclaration($mockClassName, $isInterface, $additionalInterfaces),
'clone' => $cloneTemplate,
'mock_class_name' => $mockClassName['className'],
'mocked_methods' => $mockedMethods,
'method' => $method,
));
return array(
'code' => $classTemplate
->render(),
'mockClassName' => $mockClassName['className'],
);
}