Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0aef32f

Browse files
Improve logic MockMethodCallRule to search for method even on wrong type
1 parent 4d2b44b commit 0aef32f

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

‎src/Rules/PHPUnit/MockMethodCallRule.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function processNode(Node $node, Scope $scope): array
4848
$method = $constantString->getValue();
4949
$type = $scope->getType($node->var);
5050

51-
$error = $this->checkCallOnType($type, $method);
51+
$error = $this->checkCallOnType($scope, $type, $method);
5252
if ($error !== null) {
5353
$errors[] = $error;
5454
continue;
@@ -67,7 +67,7 @@ public function processNode(Node $node, Scope $scope): array
6767
}
6868

6969
$varType = $scope->getType($node->var->var);
70-
$error = $this->checkCallOnType($varType, $method);
70+
$error = $this->checkCallOnType($scope, $varType, $method);
7171
if ($error === null) {
7272
continue;
7373
}
@@ -78,14 +78,16 @@ public function processNode(Node $node, Scope $scope): array
7878
return $errors;
7979
}
8080

81-
private function checkCallOnType(Type $type, string $method): ?IdentifierRuleError
81+
private function checkCallOnType(Scope$scope, Type $type, string $method): ?IdentifierRuleError
8282
{
83+
$methodReflection = $scope->getMethodReflection($type, $method);
84+
if ($methodReflection !== null) {
85+
return null;
86+
}
87+
8388
if (
84-
(
85-
in_array(MockObject::class, $type->getObjectClassNames(), true)
86-
|| in_array(Stub::class, $type->getObjectClassNames(), true)
87-
)
88-
&& !$type->hasMethod($method)->yes()
89+
in_array(MockObject::class, $type->getObjectClassNames(), true)
90+
|| in_array(Stub::class, $type->getObjectClassNames(), true)
8991
) {
9092
$mockClasses = array_filter($type->getObjectClassNames(), static fn (string $class): bool => $class !== MockObject::class && $class !== Stub::class);
9193
if (count($mockClasses) === 0) {

‎tests/Rules/PHPUnit/MockMethodCallRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public function testRule(): void
3636
$this->analyse([__DIR__ . '/data/mock-method-call.php'], $expectedErrors);
3737
}
3838

39+
public function testBug227(): void
40+
{
41+
$this->analyse([__DIR__ . '/data/bug-227.php'], []);
42+
}
43+
3944
/**
4045
* @return string[]
4146
*/

‎tests/Rules/PHPUnit/data/bug-227.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Bug227;
4+
5+
use PHPUnit\Framework\MockObject\MockObject;
6+
use PHPUnit\Framework\TestCase;
7+
use stdClass;
8+
9+
class Foo
10+
{
11+
12+
public function addCacheTags(array $tags)
13+
{
14+
15+
}
16+
17+
public function getLanguage(): stdClass
18+
{
19+
20+
}
21+
22+
}
23+
24+
class SomeTest extends TestCase
25+
{
26+
27+
protected MockObject|Foo $tsfe;
28+
29+
protected function setUp(): void
30+
{
31+
$this->tsfe = $this->getMockBuilder(Foo::class)
32+
->onlyMethods(['addCacheTags', 'getLanguage'])
33+
->disableOriginalConstructor()
34+
->getMock();
35+
$this->tsfe->method('getLanguage')->willReturn('aaa');
36+
}
37+
38+
public function testSometest(): void
39+
{
40+
$this->tsfe->expects(self::once())->method('addCacheTags');
41+
}
42+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /