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

Commit 85a7600

Browse files
author
Ciaran McNulty
committed
Ability to call ::method() on a stub without calling ::expects()
When using a Test Double as a Stub, we don't typically care whether it is called. Currently the mocking library forces the test author to specify an expectation of how many times the method will be called. This makes it harder to visually scan source code to see which Doubles have expectations set, and which don't care. This change allows calls like $this->getMock()->method('foo')->will($this->returnValue('bar')); which are clearer to identify as Stubbing behaviour.
1 parent 73e7b07 commit 85a7600

26 files changed

Lines changed: 182 additions & 0 deletions

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
return $this->__phpunit_getInvocationMocker()->expects($matcher);
1111
}
1212

13+
public function method()
14+
{
15+
$any = new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
16+
$expects = $this->expects($any);
17+
return call_user_func_array(array($expects, 'method'), func_get_args());
18+
}
19+
1320
public static function staticExpects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
1421
{
1522
PHPUnit_Util_DeprecatedFeature_Logger::log('The stubbing and mocking of static methods is deprecated and will be removed in PHPUnit 3.9.');

Tests/MockObject/class.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ class MockFoo extends Foo implements PHPUnit_Framework_MockObject_MockObject
8888
return $this->__phpunit_getInvocationMocker()->expects($matcher);
8989
}
9090

91+
public function method()
92+
{
93+
$any = new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
94+
$expects = $this->expects($any);
95+
return call_user_func_array(array($expects, 'method'), func_get_args());
96+
}
97+
9198
public static function staticExpects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
9299
{
93100
PHPUnit_Util_DeprecatedFeature_Logger::log('The stubbing and mocking of static methods is deprecated and will be removed in PHPUnit 3.9.');

Tests/MockObject/class_call_parent_clone.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ class MockFoo extends Foo implements PHPUnit_Framework_MockObject_MockObject
4141
return $this->__phpunit_getInvocationMocker()->expects($matcher);
4242
}
4343

44+
public function method()
45+
{
46+
$any = new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
47+
$expects = $this->expects($any);
48+
return call_user_func_array(array($expects, 'method'), func_get_args());
49+
}
50+
4451
public static function staticExpects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
4552
{
4653
PHPUnit_Util_DeprecatedFeature_Logger::log('The stubbing and mocking of static methods is deprecated and will be removed in PHPUnit 3.9.');

Tests/MockObject/class_call_parent_constructor.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ class MockFoo extends Foo implements PHPUnit_Framework_MockObject_MockObject
4040
return $this->__phpunit_getInvocationMocker()->expects($matcher);
4141
}
4242

43+
public function method()
44+
{
45+
$any = new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
46+
$expects = $this->expects($any);
47+
return call_user_func_array(array($expects, 'method'), func_get_args());
48+
}
49+
4350
public static function staticExpects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
4451
{
4552
PHPUnit_Util_DeprecatedFeature_Logger::log('The stubbing and mocking of static methods is deprecated and will be removed in PHPUnit 3.9.');

Tests/MockObject/class_dont_call_parent_clone.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ class MockFoo extends Foo implements PHPUnit_Framework_MockObject_MockObject
4040
return $this->__phpunit_getInvocationMocker()->expects($matcher);
4141
}
4242

43+
public function method()
44+
{
45+
$any = new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
46+
$expects = $this->expects($any);
47+
return call_user_func_array(array($expects, 'method'), func_get_args());
48+
}
49+
4350
public static function staticExpects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
4451
{
4552
PHPUnit_Util_DeprecatedFeature_Logger::log('The stubbing and mocking of static methods is deprecated and will be removed in PHPUnit 3.9.');

Tests/MockObject/class_dont_call_parent_constructor.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ class MockFoo extends Foo implements PHPUnit_Framework_MockObject_MockObject
4040
return $this->__phpunit_getInvocationMocker()->expects($matcher);
4141
}
4242

43+
public function method()
44+
{
45+
$any = new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
46+
$expects = $this->expects($any);
47+
return call_user_func_array(array($expects, 'method'), func_get_args());
48+
}
49+
4350
public static function staticExpects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
4451
{
4552
PHPUnit_Util_DeprecatedFeature_Logger::log('The stubbing and mocking of static methods is deprecated and will be removed in PHPUnit 3.9.');

Tests/MockObject/class_implementing_interface_call_parent_constructor.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ class MockFoo extends Foo implements PHPUnit_Framework_MockObject_MockObject
4545
return $this->__phpunit_getInvocationMocker()->expects($matcher);
4646
}
4747

48+
public function method()
49+
{
50+
$any = new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
51+
$expects = $this->expects($any);
52+
return call_user_func_array(array($expects, 'method'), func_get_args());
53+
}
54+
4855
public static function staticExpects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
4956
{
5057
PHPUnit_Util_DeprecatedFeature_Logger::log('The stubbing and mocking of static methods is deprecated and will be removed in PHPUnit 3.9.');

Tests/MockObject/class_implementing_interface_dont_call_parent_constructor.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ class MockFoo extends Foo implements PHPUnit_Framework_MockObject_MockObject
4545
return $this->__phpunit_getInvocationMocker()->expects($matcher);
4646
}
4747

48+
public function method()
49+
{
50+
$any = new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
51+
$expects = $this->expects($any);
52+
return call_user_func_array(array($expects, 'method'), func_get_args());
53+
}
54+
4855
public static function staticExpects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
4956
{
5057
PHPUnit_Util_DeprecatedFeature_Logger::log('The stubbing and mocking of static methods is deprecated and will be removed in PHPUnit 3.9.');

Tests/MockObject/class_partial.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ class MockFoo extends Foo implements PHPUnit_Framework_MockObject_MockObject
6767
return $this->__phpunit_getInvocationMocker()->expects($matcher);
6868
}
6969

70+
public function method()
71+
{
72+
$any = new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
73+
$expects = $this->expects($any);
74+
return call_user_func_array(array($expects, 'method'), func_get_args());
75+
}
76+
7077
public static function staticExpects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
7178
{
7279
PHPUnit_Util_DeprecatedFeature_Logger::log('The stubbing and mocking of static methods is deprecated and will be removed in PHPUnit 3.9.');

Tests/MockObject/interface.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ class MockFoo implements PHPUnit_Framework_MockObject_MockObject, Foo
6161
return $this->__phpunit_getInvocationMocker()->expects($matcher);
6262
}
6363

64+
public function method()
65+
{
66+
$any = new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
67+
$expects = $this->expects($any);
68+
return call_user_func_array(array($expects, 'method'), func_get_args());
69+
}
70+
6471
public static function staticExpects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
6572
{
6673
PHPUnit_Util_DeprecatedFeature_Logger::log('The stubbing and mocking of static methods is deprecated and will be removed in PHPUnit 3.9.');

0 commit comments

Comments
 (0)