public function PHPUnit_Framework_MockObject_Generator::getMock in Zircon Profile 8.0
Same name and namespace in other branches
- 8 vendor/phpunit/phpunit-mock-objects/src/Framework/MockObject/Generator.php \PHPUnit_Framework_MockObject_Generator::getMock()
Returns a mock object for the specified class.
@since Method available since Release 1.0.0
Parameters
array|string $type:
array $methods:
array $arguments:
string $mockClassName:
bool $callOriginalConstructor:
bool $callOriginalClone:
bool $callAutoload:
bool $cloneArguments:
bool $callOriginalMethods:
object $proxyTarget:
Return value
object
Throws
InvalidArgumentException
PHPUnit_Framework_MockObject_RuntimeException
1 call to PHPUnit_Framework_MockObject_Generator::getMock()
- PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass in vendor/
phpunit/ phpunit-mock-objects/ src/ Framework/ MockObject/ Generator.php - Returns a mock object for the specified abstract class with all abstract methods of the class mocked. Concrete methods to mock can be specified with the last parameter
File
- vendor/
phpunit/ phpunit-mock-objects/ src/ Framework/ MockObject/ Generator.php, line 134
Class
- PHPUnit_Framework_MockObject_Generator
- Mock Object Code Generator
Code
public function getMock($type, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $cloneArguments = true, $callOriginalMethods = false, $proxyTarget = null) {
if (!is_array($type) && !is_string($type)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'array or string');
}
if (!is_string($mockClassName)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(4, 'string');
}
if (!is_array($methods) && !is_null($methods)) {
throw new InvalidArgumentException();
}
if ($type === 'Traversable' || $type === '\\Traversable') {
$type = 'Iterator';
}
if (is_array($type)) {
$type = array_unique(array_map(function ($type) {
if ($type === 'Traversable' || $type === '\\Traversable' || $type === '\\Iterator') {
return 'Iterator';
}
return $type;
}, $type));
}
if (null !== $methods) {
foreach ($methods as $method) {
if (!preg_match('~[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*~', $method)) {
throw new PHPUnit_Framework_Exception(sprintf('Cannot stub or mock method with invalid name "%s"', $method));
}
}
if ($methods != array_unique($methods)) {
throw new PHPUnit_Framework_MockObject_RuntimeException(sprintf('Cannot stub or mock using a method list that contains duplicates: "%s"', implode(', ', $methods)));
}
}
if ($mockClassName != '' && class_exists($mockClassName, false)) {
$reflect = new ReflectionClass($mockClassName);
if (!$reflect
->implementsInterface('PHPUnit_Framework_MockObject_MockObject')) {
throw new PHPUnit_Framework_MockObject_RuntimeException(sprintf('Class "%s" already exists.', $mockClassName));
}
}
$mock = $this
->generate($type, $methods, $mockClassName, $callOriginalClone, $callAutoload, $cloneArguments, $callOriginalMethods);
return $this
->getObject($mock['code'], $mock['mockClassName'], $type, $callOriginalConstructor, $callAutoload, $arguments, $callOriginalMethods, $proxyTarget);
}