Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor.

  • Loading branch information...
commit 9de08ced7f28356f6b2000d038451526738da63b 1 parent eb64b6d
@sebastianbergmann authored
View
128 PHPUnit/Framework/MockObject/Generator.php
@@ -130,6 +130,134 @@ class PHPUnit_Framework_MockObject_Generator
protected static $soapLoaded = NULL;
/**
+ * Returns a mock object for the specified class.
+ *
+ * @param string $originalClassName
+ * @param array $methods
+ * @param array $arguments
+ * @param string $mockClassName
+ * @param boolean $callOriginalConstructor
+ * @param boolean $callOriginalClone
+ * @param boolean $callAutoload
+ * @return object
+ * @throws InvalidArgumentException
+ * @since Method available since Release 3.5.0
+ */
+ public static function getMock($originalClassName, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE)
+ {
+ if (!is_string($originalClassName)) {
+ throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
+ }
+
+ if (!is_string($mockClassName)) {
+ throw PHPUnit_Util_InvalidArgumentHelper::factory(4, 'string');
+ }
+
+ if (!is_array($methods) && !is_null($methods)) {
+ throw new InvalidArgumentException;
+ }
+
+ if ($mockClassName != '' && class_exists($mockClassName, FALSE)) {
+ throw new PHPUnit_Framework_Exception(
+ sprintf(
+ 'Class "%s" already exists.',
+ $mockClassName
+ )
+ );
+ }
+
+ $mock = self::generate(
+ $originalClassName,
+ $methods,
+ $mockClassName,
+ $callOriginalClone,
+ $callAutoload
+ );
+
+ if (!class_exists($mock['mockClassName'], FALSE)) {
+ eval($mock['code']);
+ }
+
+ if ($callOriginalConstructor &&
+ !interface_exists($originalClassName, $callAutoload)) {
+ if (count($arguments) == 0) {
+ $mockObject = new $mock['mockClassName'];
+ } else {
+ $mockClass = new ReflectionClass($mock['mockClassName']);
+ $mockObject = $mockClass->newInstanceArgs($arguments);
+ }
+ } else {
+ // Use a trick to create a new object of a class
+ // without invoking its constructor.
+ $mockObject = unserialize(
+ sprintf(
+ 'O:%d:"%s":0:{}',
+ strlen($mock['mockClassName']), $mock['mockClassName']
+ )
+ );
+ }
+
+ return $mockObject;
+ }
+
+ /**
+ * Returns a mock object for the specified abstract class with all abstract
+ * methods of the class mocked. Concrete methods are not mocked.
+ *
+ * @param string $originalClassName
+ * @param array $arguments
+ * @param string $mockClassName
+ * @param boolean $callOriginalConstructor
+ * @param boolean $callOriginalClone
+ * @param boolean $callAutoload
+ * @return object
+ * @since Method available since Release 3.5.0
+ * @throws InvalidArgumentException
+ */
+ public static function getMockForAbstractClass($originalClassName, array $arguments = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE)
+ {
+ if (!is_string($originalClassName)) {
+ throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
+ }
+
+ if (!is_string($mockClassName)) {
+ throw PHPUnit_Util_InvalidArgumentHelper::factory(3, 'string');
+ }
+
+ if (class_exists($originalClassName, $callAutoload)) {
+ $methods = array();
+ $reflector = new ReflectionClass($originalClassName);
+
+ foreach ($reflector->getMethods() as $method) {
+ if ($method->isAbstract()) {
+ $methods[] = $method->getName();
+ }
+ }
+
+ if (empty($methods)) {
+ $methods = NULL;
+ }
+
+ return self::getMock(
+ $originalClassName,
+ $methods,
+ $arguments,
+ $mockClassName,
+ $callOriginalConstructor,
+ $callOriginalClone,
+ $callAutoload
+ );
+ } else {
+ throw new PHPUnit_Framework_Exception(
+ sprintf(
+ 'Class "%s" does not exist.',
+ $originalClassName
+ )
+ );
+ }
+ }
+
+ /**
* @param string $originalClassName
* @param array $methods
* @param string $mockClassName
View
94 PHPUnit/Framework/TestCase.php
@@ -1087,57 +1087,16 @@ protected function setLocale()
*/
protected function getMock($originalClassName, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE)
{
- if (!is_string($originalClassName)) {
- throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
- }
-
- if (!is_string($mockClassName)) {
- throw PHPUnit_Util_InvalidArgumentHelper::factory(4, 'string');
- }
-
- if (!is_array($methods) && !is_null($methods)) {
- throw new InvalidArgumentException;
- }
-
- if ($mockClassName != '' && class_exists($mockClassName, FALSE)) {
- throw new PHPUnit_Framework_Exception(
- sprintf(
- 'Class "%s" already exists.',
- $mockClassName
- )
- );
- }
-
- $mock = PHPUnit_Framework_MockObject_Generator::generate(
+ $mockObject = PHPUnit_Framework_MockObject_Generator::getMock(
$originalClassName,
$methods,
+ $arguments,
$mockClassName,
+ $callOriginalConstructor,
$callOriginalClone,
$callAutoload
);
- if (!class_exists($mock['mockClassName'], FALSE)) {
- eval($mock['code']);
- }
-
- if ($callOriginalConstructor && !interface_exists($originalClassName, $callAutoload)) {
- if (count($arguments) == 0) {
- $mockObject = new $mock['mockClassName'];
- } else {
- $mockClass = new ReflectionClass($mock['mockClassName']);
- $mockObject = $mockClass->newInstanceArgs($arguments);
- }
- } else {
- // Use a trick to create a new object of a class
- // without invoking its constructor.
- $mockObject = unserialize(
- sprintf(
- 'O:%d:"%s":0:{}',
- strlen($mock['mockClassName']), $mock['mockClassName']
- )
- );
- }
-
$this->mockObjects[] = $mockObject;
return $mockObject;
@@ -1159,45 +1118,18 @@ protected function getMock($originalClassName, $methods = array(), array $argume
*/
protected function getMockForAbstractClass($originalClassName, array $arguments = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE)
{
- if (!is_string($originalClassName)) {
- throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
- }
-
- if (!is_string($mockClassName)) {
- throw PHPUnit_Util_InvalidArgumentHelper::factory(3, 'string');
- }
-
- if (class_exists($originalClassName, $callAutoload)) {
- $methods = array();
- $reflector = new ReflectionClass($originalClassName);
-
- foreach ($reflector->getMethods() as $method) {
- if ($method->isAbstract()) {
- $methods[] = $method->getName();
- }
- }
+ $mockObject = PHPUnit_Framework_MockObject_Generator::getMockForAbstractClass(
+ $originalClassName,
+ $arguments,
+ $mockClassName,
+ $callOriginalConstructor,
+ $callOriginalClone,
+ $callAutoload
+ );
- if (empty($methods)) {
- $methods = NULL;
- }
+ $this->mockObjects[] = $mockObject;
- return $this->getMock(
- $originalClassName,
- $methods,
- $arguments,
- $mockClassName,
- $callOriginalConstructor,
- $callOriginalClone,
- $callAutoload
- );
- } else {
- throw new PHPUnit_Framework_Exception(
- sprintf(
- 'Class "%s" does not exist.',
- $originalClassName
- )
- );
- }
+ return $mockObject;
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.