Skip to content

Commit d618fa3

Browse files
DFoxinatorsebastianbergmann
authored andcommitted
Add onlyMethods, addMethods + deprecate setMethods
1 parent bbd09bb commit d618fa3

File tree

3 files changed

+59
-10
lines changed

3 files changed

+59
-10
lines changed

.psalm/baseline.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
<PossiblyNullPropertyAssignmentValue occurrences="1">
202202
<code>null</code>
203203
</PossiblyNullPropertyAssignmentValue>
204-
<ArgumentTypeCoercion occurrences="1">
204+
<ArgumentTypeCoercion occurrences="2">
205205
<code>$this->type</code>
206206
</ArgumentTypeCoercion>
207207
</file>

src/Framework/MockObject/MockBuilder.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ public function getMockForTrait(): MockObject
181181

182182
/**
183183
* Specifies the subset of methods to mock. Default is to mock none of them.
184+
*
185+
* @deprecated https://github.com/sebastianbergmann/phpunit/pull/3687
184186
*/
185187
public function setMethods(array $methods = null): self
186188
{
@@ -196,15 +198,43 @@ public function setMethods(array $methods = null): self
196198
*
197199
* @throws RuntimeException
198200
*/
199-
public function setRealMethods(array $methods): self
201+
public function onlyMethods(array $methods): self
200202
{
201203
$reflection = new \ReflectionClass($this->type);
202204

203205
foreach ($methods as $method) {
204206
if (!$reflection->hasMethod($method)) {
205207
throw new RuntimeException(
206208
\sprintf(
207-
'Trying to set mock method "%s", but it does not exist in class "%s"',
209+
'Trying to set mock method "%s" with onlyMethods, but it does not exist in class "%s". Use addMethods() for methods that don\'t exist in the class.',
210+
$method,
211+
$this->type
212+
)
213+
);
214+
}
215+
}
216+
217+
$this->methods = $methods;
218+
219+
return $this;
220+
}
221+
222+
/**
223+
* Specifies methods that don't exist in the class which you want to mock
224+
*
225+
* @param string[] $methods
226+
*
227+
* @throws RuntimeException
228+
*/
229+
public function addMethods(array $methods): self
230+
{
231+
$reflection = new \ReflectionClass($this->type);
232+
233+
foreach ($methods as $method) {
234+
if ($reflection->hasMethod($method)) {
235+
throw new RuntimeException(
236+
\sprintf(
237+
'Trying to set mock method "%s" with addMethod, but it exists in class "%s". Use onlyMethods() for methods that exist in the class.',
208238
$method,
209239
$this->type
210240
)

tests/unit/Framework/MockObject/MockBuilderTest.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,49 @@ public function testSetMethodsAllowsNonExistentMethodNames(): void
6060
$this->assertNull($mock->mockableMethodWithCrazyName());
6161
}
6262

63-
public function testSetRealMethodsWithNonExistentMethodNames(): void
63+
public function testOnlyMethodsWithNonExistentMethodNames(): void
6464
{
6565
$this->expectException(RuntimeException::class);
6666

6767
$this->getMockBuilder(Mockable::class)
68-
->setRealMethods(['mockableMethodWithCrazyName'])
68+
->onlyMethods(['mockableMethodWithCrazyName'])
6969
->getMock();
7070
}
7171

72-
public function testSetRealMethodsWithExistingMethodNames(): void
72+
public function testOnlyMethodsWithExistingMethodNames(): void
7373
{
7474
$mock = $this->getMockBuilder(Mockable::class)
75-
->setRealMethods(['mockableMethod'])
76-
->getMock();
75+
->onlyMethods(['mockableMethod'])
76+
->getMock();
7777

7878
$this->assertNull($mock->mockableMethod());
7979
$this->assertTrue($mock->anotherMockableMethod());
8080
}
8181

82+
public function testAddMethodsWithNonExistentMethodNames(): void
83+
{
84+
$this->expectException(RuntimeException::class);
85+
86+
$this->getMockBuilder(Mockable::class)
87+
->addMethods(['mockableMethod'])
88+
->getMock();
89+
}
90+
91+
public function testAddMethodsWithExistingMethodNames(): void
92+
{
93+
$mock = $this->getMockBuilder(Mockable::class)
94+
->addMethods(['mockableMethodWithFakeMethod'])
95+
->getMock();
96+
97+
$this->assertNull($mock->mockableMethodWithFakeMethod());
98+
$this->assertTrue($mock->anotherMockableMethod());
99+
}
100+
82101
public function testEmptyMethodExceptionsToMockCanBeSpecified(): void
83102
{
84103
$mock = $this->getMockBuilder(Mockable::class)
85-
->setMethodsExcept()
86-
->getMock();
104+
->setMethodsExcept()
105+
->getMock();
87106

88107
$this->assertNull($mock->mockableMethod());
89108
$this->assertNull($mock->anotherMockableMethod());

0 commit comments

Comments
 (0)