Skip to content

Commit 39c77aa

Browse files
committed
Support for new names of MockObject and MockBuilder in PHPUnit 6.5
1 parent 4d0e94f commit 39c77aa

5 files changed

+47
-18
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
"require": {
1313
"php": "~7.0",
14-
"phpstan/phpstan": "^0.9",
14+
"phpstan/phpstan": "^0.9.1",
1515
"phpunit/phpunit": "^6.3"
1616
},
1717
"require-dev": {

src/Type/PHPUnit/CreateMockDynamicReturnTypeExtension.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
5656
$class = $scope->getClassReflection()->getName();
5757
}
5858

59-
$mockedClassType = new ObjectType($class);
60-
$mockType = new ObjectType(\PHPUnit_Framework_MockObject_MockObject::class);
61-
62-
return TypeCombinator::intersect($mockedClassType, $mockType);
59+
return TypeCombinator::intersect(
60+
new ObjectType($class),
61+
$methodReflection->getReturnType()
62+
);
6363
}
6464

6565
}

src/Type/PHPUnit/GetMockBuilderDynamicReturnTypeExtension.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\MethodReflection;
88
use PHPStan\Type\Type;
9+
use PHPStan\Type\TypeWithClassName;
910

1011
class GetMockBuilderDynamicReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension
1112
{
@@ -22,29 +23,34 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
2223

2324
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
2425
{
26+
$mockBuilderType = $methodReflection->getReturnType();
2527
if (count($methodCall->args) === 0) {
26-
return $methodReflection->getReturnType();
28+
return $mockBuilderType;
2729
}
2830
$arg = $methodCall->args[0]->value;
2931
if (!($arg instanceof \PhpParser\Node\Expr\ClassConstFetch)) {
30-
return $methodReflection->getReturnType();
32+
return $mockBuilderType;
3133
}
3234

3335
$class = $arg->class;
3436
if (!($class instanceof \PhpParser\Node\Name)) {
35-
return $methodReflection->getReturnType();
37+
return $mockBuilderType;
3638
}
3739

3840
$class = (string) $class;
3941
if ($class === 'static') {
40-
return $methodReflection->getReturnType();
42+
return $mockBuilderType;
4143
}
4244

4345
if ($class === 'self') {
4446
$class = $scope->getClassReflection()->getName();
4547
}
4648

47-
return new MockBuilderType($class);
49+
if (!$mockBuilderType instanceof TypeWithClassName) {
50+
throw new \PHPStan\ShouldNotHappenException();
51+
}
52+
53+
return new MockBuilderType($mockBuilderType, $class);
4854
}
4955

5056
}

src/Type/PHPUnit/MockBuilderDynamicReturnTypeExtension.php

+24-6
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,35 @@
44

55
use PhpParser\Node\Expr\MethodCall;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\Broker\Broker;
78
use PHPStan\Reflection\MethodReflection;
89
use PHPStan\Type\ObjectType;
910
use PHPStan\Type\Type;
1011
use PHPStan\Type\TypeCombinator;
12+
use PHPStan\Type\TypeWithClassName;
1113

12-
class MockBuilderDynamicReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension
14+
class MockBuilderDynamicReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension, \PHPStan\Reflection\BrokerAwareExtension
1315
{
1416

17+
/**
18+
* @var \PHPStan\Broker\Broker
19+
*/
20+
private $broker;
21+
22+
public function setBroker(Broker $broker)
23+
{
24+
$this->broker = $broker;
25+
}
26+
1527
public function getClass(): string
1628
{
17-
return \PHPUnit_Framework_MockObject_MockBuilder::class;
29+
$testCase = $this->broker->getClass(\PHPUnit\Framework\TestCase::class);
30+
$mockBuilderType = $testCase->getNativeMethod('getMockBuilder')->getReturnType();
31+
if (!$mockBuilderType instanceof TypeWithClassName) {
32+
throw new \PHPStan\ShouldNotHappenException();
33+
}
34+
35+
return $mockBuilderType->getClassName();
1836
}
1937

2038
public function isMethodSupported(MethodReflection $methodReflection): bool
@@ -40,10 +58,10 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
4058
return $methodReflection->getReturnType();
4159
}
4260

43-
$mockedClassType = new ObjectType($calledOnType->getMockedClass());
44-
$mockType = new ObjectType(\PHPUnit_Framework_MockObject_MockObject::class);
45-
46-
return TypeCombinator::intersect($mockedClassType, $mockType);
61+
return TypeCombinator::intersect(
62+
new ObjectType($calledOnType->getMockedClass()),
63+
$methodReflection->getReturnType()
64+
);
4765
}
4866

4967
}

src/Type/PHPUnit/MockBuilderType.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PHPStan\Type\PHPUnit;
44

5+
use PHPStan\Type\TypeWithClassName;
6+
57
class MockBuilderType extends \PHPStan\Type\ObjectType
68
{
79

@@ -10,9 +12,12 @@ class MockBuilderType extends \PHPStan\Type\ObjectType
1012
*/
1113
private $mockedClass;
1214

13-
public function __construct(string $mockedClass)
15+
public function __construct(
16+
TypeWithClassName $mockBuilderType,
17+
string $mockedClass
18+
)
1419
{
15-
parent::__construct(\PHPUnit_Framework_MockObject_MockBuilder::class);
20+
parent::__construct($mockBuilderType->getClassName());
1621
$this->mockedClass = $mockedClass;
1722
}
1823

0 commit comments

Comments
 (0)