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

+182
-0
lines changed

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.');

Tests/MockObject/invocation_object_clone_object.phpt

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

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

Tests/MockObject/invocation_static_clone_object.phpt

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

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

Tests/MockObject/namespaced_class.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ class MockFoo extends NS\Foo implements PHPUnit_Framework_MockObject_MockObject
9191
return $this->__phpunit_getInvocationMocker()->expects($matcher);
9292
}
9393

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

Tests/MockObject/namespaced_class_call_parent_clone.phpt

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

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

Tests/MockObject/namespaced_class_call_parent_constructor.phpt

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

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

Tests/MockObject/namespaced_class_dont_call_parent_clone.phpt

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

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

Tests/MockObject/namespaced_class_dont_call_parent_constructor.phpt

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

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

Tests/MockObject/namespaced_class_implementing_interface_call_parent_constructor.phpt

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

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

Tests/MockObject/namespaced_class_implementing_interface_dont_call_parent_constructor.phpt

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

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

Tests/MockObject/namespaced_class_partial.phpt

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

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

Tests/MockObject/namespaced_interface.phpt

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

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

Tests/MockObject/nonexistent_class.phpt

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

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

Tests/MockObject/nonexistent_class_with_namespace.phpt

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

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

Tests/MockObject/nonexistent_class_with_namespace_starting_with_separator.phpt

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

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

Tests/MockObject/proxy.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ class ProxyFoo extends Foo implements PHPUnit_Framework_MockObject_MockObject
8484
return $this->__phpunit_getInvocationMocker()->expects($matcher);
8585
}
8686

87+
public function method()
88+
{
89+
$any = new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
90+
$expects = $this->expects($any);
91+
return call_user_func_array(array($expects, 'method'), func_get_args());
92+
}
93+
8794
public static function staticExpects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
8895
{
8996
PHPUnit_Util_DeprecatedFeature_Logger::log('The stubbing and mocking of static methods is deprecated and will be removed in PHPUnit 3.9.');

Tests/MockObjectTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ public function testMockedMethodIsNotCalledWhenExpectsAnyWithParameter()
8686
->with('someArg');
8787
}
8888

89+
public function testMockedMethodIsNotCalledWhenMethodSpecifiedDirectlyWithParameter()
90+
{
91+
$mock = $this->getMock('SomeClass');
92+
$mock->method('doSomethingElse')
93+
->with('someArg');
94+
}
95+
8996
public function testMockedMethodIsCalledAtLeastOnce()
9097
{
9198
$mock = $this->getMock('AnInterface');

0 commit comments

Comments
 (0)