Permalink
Browse files

Implement option for disallowing the mocking of unknown types

  • Loading branch information...
sebastianbergmann committed Apr 14, 2016
1 parent 313b5c2 commit def4c9df058eaac669a762c000daee0b66e3b163
Showing with 60 additions and 2 deletions.
  1. +29 −1 src/Framework/MockObject/Generator.php
  2. +31 −1 src/Framework/MockObject/MockBuilder.php
@@ -134,6 +134,7 @@ class PHPUnit_Framework_MockObject_Generator
* @param bool $cloneArguments
* @param bool $callOriginalMethods
* @param object $proxyTarget
* @param bool $allowMockingUnknownTypes
*
* @return PHPUnit_Framework_MockObject_MockObject
*
@@ -143,7 +144,7 @@ class PHPUnit_Framework_MockObject_Generator
*
* @since Method available since Release 1.0.0
*/
public function getMock($type, $methods = [], array $arguments = [], $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $cloneArguments = true, $callOriginalMethods = false, $proxyTarget = null)
public function getMock($type, $methods = [], array $arguments = [], $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $cloneArguments = true, $callOriginalMethods = false, $proxyTarget = null, $allowMockingUnknownTypes = true)
{
if (!is_array($type) && !is_string($type)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'array or string');
@@ -178,6 +179,33 @@ function ($type) {
);
}
if (!$allowMockingUnknownTypes) {
if (is_array($type)) {
foreach ($type as $_type) {
if (!class_exists($_type, $callAutoload) &&
!interface_exists($_type, $callAutoload)) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
sprintf(
'Cannot stub or mock class or interface "%s" which does not exist',
$_type
)
);
}
}
} else {
if (!class_exists($type, $callAutoload) &&
!interface_exists($type, $callAutoload)
) {
throw new PHPUnit_Framework_MockObject_RuntimeException(
sprintf(
'Cannot stub or mock class or interface "%s" which does not exist',
$type
)
);
}
}
}
if (null !== $methods) {
foreach ($methods as $method) {
if (!preg_match('~[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*~', $method)) {
@@ -70,6 +70,11 @@ class PHPUnit_Framework_MockObject_MockBuilder
*/
private $proxyTarget = null;
/**
* @var bool
*/
private $allowMockingUnknownTypes = true;
/**
* @param PHPUnit_Framework_TestCase $testCase
* @param array|string $type
@@ -99,7 +104,8 @@ public function getMock()
$this->autoload,
$this->cloneArguments,
$this->callOriginalMethods,
$this->proxyTarget
$this->proxyTarget,
$this->allowMockingUnknownTypes
);
$this->testCase->registerMockObject($object);
@@ -349,4 +355,28 @@ public function setProxyTarget($object)
return $this;
}
/**
* @return PHPUnit_Framework_MockObject_MockBuilder
*
* @since Method available since Release 3.2.0
*/
public function allowMockingUnknownTypes()
{
$this->allowMockingUnknownTypes = true;
return $this;
}
/**
* @return PHPUnit_Framework_MockObject_MockBuilder
*
* @since Method available since Release 3.2.0
*/
public function disallowMockingUnknownTypes()
{
$this->allowMockingUnknownTypes = false;
return $this;
}
}

0 comments on commit def4c9d

Please sign in to comment.