Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- Merge [4375].

  • Loading branch information...
commit 9164018d0f45fa2cde4bc9f42032dcb4ce7af086 1 parent 938c8c4
@sebastianbergmann authored
View
51 PHPUnit/Framework/MockObject/Generator.php
@@ -70,12 +70,13 @@ class PHPUnit_Framework_MockObject_Generator
protected static $cache = array();
protected static $soapLoaded = NULL;
- public static function generate($originalClassName, array $methods = array(), $mockClassName = '', $callOriginalClone = TRUE, $callAutoload = TRUE)
+ public static function generate($originalClassName, array $methods = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE)
{
if ($mockClassName == '') {
$key = md5(
$originalClassName .
serialize($methods) .
+ serialize($callOriginalConstructor) .
serialize($callOriginalClone)
);
@@ -88,6 +89,7 @@ public static function generate($originalClassName, array $methods = array(), $m
$originalClassName,
$methods,
$mockClassName,
+ $callOriginalConstructor,
$callOriginalClone,
$callAutoload
);
@@ -156,7 +158,7 @@ public static function generateClassFromWsdl($wsdlFile, $originalClassName, arra
}
}
- protected static function generateMock($originalClassName, array $methods, $mockClassName, $callOriginalClone, $callAutoload)
+ protected static function generateMock($originalClassName, array $methods, $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload)
{
$templateDir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Generator' . DIRECTORY_SEPARATOR;
$classTemplate = new PHPUnit_Util_Template($templateDir . 'mocked_class.tpl');
@@ -170,10 +172,11 @@ protected static function generateMock($originalClassName, array $methods, $mock
if (class_exists($originalClassName, $callAutoload)) {
$isClass = TRUE;
- }
-
- else if (interface_exists($originalClassName, $callAutoload)) {
- $isInterface = TRUE;
+ } else {
+ if (interface_exists($originalClassName, $callAutoload)) {
+ $callOriginalConstructor = FALSE;
+ $isInterface = TRUE;
+ }
}
if (!class_exists($mockClassName['fullClassName'], $callAutoload) &&
@@ -254,6 +257,13 @@ protected static function generateMock($originalClassName, array $methods, $mock
'class_declaration' => self::generateMockClassDeclaration(
$mockClassName, $isInterface
),
+ 'constructor' => self::generateMockConstructor(
+ $templateDir,
+ isset($class) ? $class->getConstructor() : NULL,
+ $originalClassName,
+ $mockClassName['mockClassName'],
+ $callOriginalConstructor
+ ),
'clone' => $cloneTemplate,
'mocked_methods' => $mockedMethods
)
@@ -316,6 +326,35 @@ protected static function generateMockClassDeclaration(array $mockClassName, $is
return $buffer;
}
+ protected static function generateMockConstructor($templateDir, $constructor, $originalClassName, $mockedClassName, $callOriginalConstructor)
+ {
+ if ($constructor !== NULL) {
+ if ($constructor->isFinal()) {
+ throw new RuntimeException(
+ sprintf(
+ 'Constructor of class "%s" is declared "final". The class cannot be mocked.',
+ $mockedClassName
+ )
+ );
+ }
+
+ if ($callOriginalConstructor) {
+ $template = new PHPUnit_Util_Template($templateDir . 'unmocked_constructor.tpl');
+
+ $template->setVar(
+ array(
+ 'arguments' => PHPUnit_Util_Class::getMethodParameters($constructor),
+ 'mocked_class_name' => $mockedClassName
+ )
+ );
+
+ return $template->render();
+ }
+ }
+
+ return '';
+ }
+
protected static function generateMockedMethodDefinitionFromExisting($templateDir, ReflectionMethod $method)
{
if ($method->isPrivate()) {
View
2  PHPUnit/Framework/MockObject/Generator/mocked_class.tpl.dist
@@ -2,7 +2,7 @@
{
public $invocationMocker;
-{clone}{mocked_methods}
+{constructor}{clone}{mocked_methods}
public function expects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
{
return $this->invocationMocker->expects($matcher);
View
10 PHPUnit/Framework/MockObject/Generator/unmocked_constructor.tpl.dist
@@ -0,0 +1,10 @@
+ public function __construct({arguments})
+ {
+ $args = func_get_args();
+
+ $this->invocationMocker = new PHPUnit_Framework_MockObject_InvocationMocker($this);
+
+ $class = new ReflectionClass('{mocked_class_name}');
+ $class->getParentClass()->getConstructor()->invokeArgs($this, $args);
+ }
+
View
6 PHPUnit/Framework/TestCase.php
@@ -876,6 +876,7 @@ protected function getMock($originalClassName, $methods = array(), array $argume
$originalClassName,
$methods,
$mockClassName,
+ $callOriginalConstructor,
$callOriginalClone,
$callAutoload
);
@@ -899,8 +900,11 @@ protected function getMock($originalClassName, $methods = array(), array $argume
);
}
+ if ($mockObject->invocationMocker === NULL) {
+ $mockObject->invocationMocker = new PHPUnit_Framework_MockObject_InvocationMocker($mockObject);
+ }
+
$this->mockObjects[] = $mockObject;
- $mockObject->invocationMocker = new PHPUnit_Framework_MockObject_InvocationMocker($mockObject);
return $mockObject;
}
View
3  PHPUnit/Tests/Framework/MockObject/class.phpt
@@ -1,5 +1,5 @@
--TEST--
-PHPUnit_Framework_MockObject_Generator::generate('Foo', array(), 'MockFoo', TRUE)
+PHPUnit_Framework_MockObject_Generator::generate('Foo', array(), 'MockFoo', TRUE, TRUE)
--FILE--
<?php
class Foo
@@ -19,6 +19,7 @@ $mock = PHPUnit_Framework_MockObject_Generator::generate(
'Foo',
array(),
'MockFoo',
+ TRUE,
TRUE
);
View
3  PHPUnit/Tests/Framework/MockObject/class_call_parent_clone.phpt
@@ -1,5 +1,5 @@
--TEST--
-PHPUnit_Framework_MockObject_Generator::generate('Foo', array(), 'MockFoo', TRUE)
+PHPUnit_Framework_MockObject_Generator::generate('Foo', array(), 'MockFoo', TRUE, TRUE)
--FILE--
<?php
class Foo
@@ -15,6 +15,7 @@ $mock = PHPUnit_Framework_MockObject_Generator::generate(
'Foo',
array(),
'MockFoo',
+ TRUE,
TRUE
);
View
13 PHPUnit/Tests/Framework/MockObject/class_call_parent_constructor.phpt
@@ -1,5 +1,5 @@
--TEST--
-PHPUnit_Framework_MockObject_Generator::generate('Foo', array(), 'MockFoo', TRUE)
+PHPUnit_Framework_MockObject_Generator::generate('Foo', array(), 'MockFoo', TRUE, TRUE)
--FILE--
<?php
class Foo
@@ -15,6 +15,7 @@ $mock = PHPUnit_Framework_MockObject_Generator::generate(
'Foo',
array(),
'MockFoo',
+ TRUE,
TRUE
);
@@ -26,6 +27,16 @@ class MockFoo extends Foo
public static $staticInvocationMocker;
public $invocationMocker;
+ public function __construct()
+ {
+ $args = func_get_args();
+
+ $this->invocationMocker = new PHPUnit_Framework_MockObject_InvocationMocker($this);
+
+ $class = new ReflectionClass('MockFoo');
+ $class->getParentClass()->getConstructor()->invokeArgs($this, $args);
+ }
+
public function __clone()
{
$this->invocationMocker = clone $this->invocationMocker;
View
3  PHPUnit/Tests/Framework/MockObject/class_dont_call_parent_clone.phpt
@@ -1,5 +1,5 @@
--TEST--
-PHPUnit_Framework_MockObject_Generator::generate('Foo', array(), 'MockFoo', FALSE)
+PHPUnit_Framework_MockObject_Generator::generate('Foo', array(), 'MockFoo', TRUE, FALSE)
--FILE--
<?php
class Foo
@@ -15,6 +15,7 @@ $mock = PHPUnit_Framework_MockObject_Generator::generate(
'Foo',
array(),
'MockFoo',
+ TRUE,
FALSE
);
View
3  PHPUnit/Tests/Framework/MockObject/class_dont_call_parent_constructor.phpt
@@ -1,5 +1,5 @@
--TEST--
-PHPUnit_Framework_MockObject_Generator::generate('Foo', array(), 'MockFoo', TRUE)
+PHPUnit_Framework_MockObject_Generator::generate('Foo', array(), 'MockFoo', FALSE, TRUE)
--FILE--
<?php
class Foo
@@ -15,6 +15,7 @@ $mock = PHPUnit_Framework_MockObject_Generator::generate(
'Foo',
array(),
'MockFoo',
+ FALSE,
TRUE
);
View
13 PHPUnit/Tests/Framework/MockObject/class_implementing_interface_call_parent_constructor.phpt
@@ -1,5 +1,5 @@
--TEST--
-PHPUnit_Framework_MockObject_Generator::generate('Foo', array(), 'MockFoo', TRUE)
+PHPUnit_Framework_MockObject_Generator::generate('Foo', array(), 'MockFoo', TRUE, TRUE)
--FILE--
<?php
interface IFoo
@@ -20,6 +20,7 @@ $mock = PHPUnit_Framework_MockObject_Generator::generate(
'Foo',
array(),
'MockFoo',
+ TRUE,
TRUE
);
@@ -31,6 +32,16 @@ class MockFoo extends Foo
public static $staticInvocationMocker;
public $invocationMocker;
+ public function __construct($bar)
+ {
+ $args = func_get_args();
+
+ $this->invocationMocker = new PHPUnit_Framework_MockObject_InvocationMocker($this);
+
+ $class = new ReflectionClass('MockFoo');
+ $class->getParentClass()->getConstructor()->invokeArgs($this, $args);
+ }
+
public function __clone()
{
$this->invocationMocker = clone $this->invocationMocker;
View
3  ...Tests/Framework/MockObject/class_implementing_interface_dont_call_parent_constructor.phpt
@@ -1,5 +1,5 @@
--TEST--
-PHPUnit_Framework_MockObject_Generator::generate('Foo', array(), 'MockFoo', TRUE)
+PHPUnit_Framework_MockObject_Generator::generate('Foo', array(), 'MockFoo', FALSE, TRUE)
--FILE--
<?php
interface IFoo
@@ -20,6 +20,7 @@ $mock = PHPUnit_Framework_MockObject_Generator::generate(
'Foo',
array(),
'MockFoo',
+ FALSE,
TRUE
);
View
3  PHPUnit/Tests/Framework/MockObject/class_partial.phpt
@@ -1,5 +1,5 @@
--TEST--
-PHPUnit_Framework_MockObject_Generator::generate('Foo', array('bar'), 'MockFoo', TRUE)
+PHPUnit_Framework_MockObject_Generator::generate('Foo', array('bar'), 'MockFoo', TRUE, TRUE)
--FILE--
<?php
class Foo
@@ -19,6 +19,7 @@ $mock = PHPUnit_Framework_MockObject_Generator::generate(
'Foo',
array('bar'),
'MockFoo',
+ TRUE,
TRUE
);
View
3  PHPUnit/Tests/Framework/MockObject/interface.phpt
@@ -1,5 +1,5 @@
--TEST--
-PHPUnit_Framework_MockObject_Generator::generate('Foo', array(), 'MockFoo', TRUE)
+PHPUnit_Framework_MockObject_Generator::generate('Foo', array(), 'MockFoo', TRUE, TRUE)
--FILE--
<?php
interface Foo
@@ -13,6 +13,7 @@ $mock = PHPUnit_Framework_MockObject_Generator::generate(
'Foo',
array(),
'MockFoo',
+ TRUE,
TRUE
);
View
1  package.xml
@@ -453,6 +453,7 @@
<file baseinstalldir="/" name="mocked_clone.tpl.dist" role="php" />
<file baseinstalldir="/" name="mocked_method.tpl.dist" role="php" />
<file baseinstalldir="/" name="unmocked_clone.tpl.dist" role="php" />
+ <file baseinstalldir="/" name="unmocked_constructor.tpl.dist" role="php" />
<file baseinstalldir="/" name="wsdl_class.tpl.dist" role="php" />
<file baseinstalldir="/" name="wsdl_method.tpl.dist" role="php" />
</dir>
Please sign in to comment.
Something went wrong with that request. Please try again.