Skip to content
This repository was archived by the owner on Feb 20, 2023. It is now read-only.

Commit 325961c

Browse files
Closes #159
1 parent cd2b9cc commit 325961c

4 files changed

Lines changed: 107 additions & 10 deletions

File tree

src/Framework/MockObject/Generator.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ class PHPUnit_Framework_MockObject_Generator
126126
'interface' => TRUE,
127127
'isset' => TRUE,
128128
'list' => TRUE,
129-
'method' => TRUE,
130129
'namespace' => TRUE,
131130
'new' => TRUE,
132131
'or' => TRUE,
@@ -785,6 +784,16 @@ protected function generateMock($type, $methods, $mockClassName, $callOriginalCl
785784
}
786785
}
787786

787+
$method = '';
788+
789+
if (!in_array('method', $methods)) {
790+
$methodTemplate = new Text_Template(
791+
$templateDir . 'mocked_class_method.tpl'
792+
);
793+
794+
$method = $methodTemplate->render();
795+
}
796+
788797
$classTemplate->setVar(
789798
array(
790799
'prologue' => isset($prologue) ? $prologue : '',
@@ -796,7 +805,8 @@ protected function generateMock($type, $methods, $mockClassName, $callOriginalCl
796805
),
797806
'clone' => $cloneTemplate,
798807
'mock_class_name' => $mockClassName['className'],
799-
'mocked_methods' => $mockedMethods
808+
'mocked_methods' => $mockedMethods,
809+
'method' => $method
800810
)
801811
);
802812

src/Framework/MockObject/Generator/mocked_class.tpl.dist

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,7 @@
88
{
99
return $this->__phpunit_getInvocationMocker()->expects($matcher);
1010
}
11-
12-
public function method()
13-
{
14-
$any = new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
15-
$expects = $this->expects($any);
16-
return call_user_func_array(array($expects, 'method'), func_get_args());
17-
}
18-
11+
{method}
1912
public function __phpunit_setOriginalObject($originalObject)
2013
{
2114
$this->__phpunit_originalObject = $originalObject;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
public function method()
3+
{
4+
$any = new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
5+
$expects = $this->expects($any);
6+
return call_user_func_array(array($expects, 'method'), func_get_args());
7+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
--TEST--
2+
PHPUnit_Framework_MockObject_Generator::generate('Foo', array(), 'MockFoo', TRUE, TRUE)
3+
--FILE--
4+
<?php
5+
class Foo
6+
{
7+
public function method()
8+
{
9+
}
10+
}
11+
12+
require __DIR__ . '/../../vendor/autoload.php';
13+
14+
$generator = new PHPUnit_Framework_MockObject_Generator;
15+
16+
$mock = $generator->generate(
17+
'Foo',
18+
array(),
19+
'MockFoo',
20+
TRUE,
21+
TRUE
22+
);
23+
24+
print $mock['code'];
25+
?>
26+
--EXPECTF--
27+
class MockFoo extends Foo implements PHPUnit_Framework_MockObject_MockObject
28+
{
29+
private $__phpunit_invocationMocker;
30+
private $__phpunit_originalObject;
31+
32+
public function __clone()
33+
{
34+
$this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationMocker();
35+
}
36+
37+
public function method()
38+
{
39+
$arguments = array();
40+
$count = func_num_args();
41+
42+
if ($count > 0) {
43+
$_arguments = func_get_args();
44+
45+
for ($i = 0; $i < $count; $i++) {
46+
$arguments[] = $_arguments[$i];
47+
}
48+
}
49+
50+
$result = $this->__phpunit_getInvocationMocker()->invoke(
51+
new PHPUnit_Framework_MockObject_Invocation_Object(
52+
'Foo', 'method', $arguments, $this, TRUE
53+
)
54+
);
55+
56+
return $result;
57+
}
58+
59+
public function expects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
60+
{
61+
return $this->__phpunit_getInvocationMocker()->expects($matcher);
62+
}
63+
64+
public function __phpunit_setOriginalObject($originalObject)
65+
{
66+
$this->__phpunit_originalObject = $originalObject;
67+
}
68+
69+
public function __phpunit_getInvocationMocker()
70+
{
71+
if ($this->__phpunit_invocationMocker === NULL) {
72+
$this->__phpunit_invocationMocker = new PHPUnit_Framework_MockObject_InvocationMocker;
73+
}
74+
75+
return $this->__phpunit_invocationMocker;
76+
}
77+
78+
public function __phpunit_hasMatchers()
79+
{
80+
return $this->__phpunit_getInvocationMocker()->hasMatchers();
81+
}
82+
83+
public function __phpunit_verify()
84+
{
85+
$this->__phpunit_getInvocationMocker()->verify();
86+
}
87+
}

0 commit comments

Comments
 (0)