Skip to content

Commit 64f1284

Browse files
vhenzlondrejmirtes
authored andcommitted
Add tests for DoctrineSelectableDynamicReturnTypeExtension
1 parent bad3d76 commit 64f1284

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
parameters:
22
ignoreErrors:
33
- '#but returns PHPUnit_Framework_MockObject_MockObject#'
4+
- '#PHPUnit_Framework_MockObject_MockObject given#'
5+
- '#Access to an undefined property PHPUnit_Framework_MockObject_MockObject::\$\w+#'
46

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Tests\PHPStan\Type\Doctrine;
4+
5+
use PhpParser\Node\Expr;
6+
use PhpParser\Node\Expr\MethodCall;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Reflection\MethodReflection;
9+
use PHPStan\Type\Doctrine\DoctrineSelectableDynamicReturnTypeExtension;
10+
use PHPStan\Type\ObjectType;
11+
use PHPStan\Type\Type;
12+
use PHPUnit\Framework\TestCase;
13+
14+
final class DoctrineSelectableDynamicReturnTypeExtensionTest extends TestCase
15+
{
16+
17+
/** @var \PHPStan\Type\Doctrine\DoctrineSelectableDynamicReturnTypeExtension */
18+
private $extension;
19+
20+
protected function setUp()
21+
{
22+
$this->extension = new DoctrineSelectableDynamicReturnTypeExtension();
23+
}
24+
25+
/**
26+
* @return mixed[]
27+
*/
28+
public function dataIsMethodSupported(): array
29+
{
30+
return [
31+
['matching', true],
32+
['filter', false],
33+
['foo', false],
34+
];
35+
}
36+
37+
/**
38+
* @dataProvider dataIsMethodSupported
39+
* @param string $method
40+
* @param bool $expectedResult
41+
*/
42+
public function testIsMethodSupported(string $method, bool $expectedResult)
43+
{
44+
$methodReflection = $this->createMock(MethodReflection::class);
45+
$methodReflection->method('getName')->willReturn($method);
46+
$this->assertSame($expectedResult, $this->extension->isMethodSupported($methodReflection));
47+
}
48+
49+
public function testGetTypeFromMethodCall()
50+
{
51+
$methodReflection = $this->createMock(MethodReflection::class);
52+
53+
$scope = $this->createMock(Scope::class);
54+
$scope->method('getType')->will(
55+
self::returnCallback(
56+
function (\PhpParser\Node\Expr $node): Type {
57+
return new ObjectType($node->getType());
58+
}
59+
)
60+
);
61+
62+
$var = $this->createMock(Expr::class);
63+
$var->method('getType')->willReturn(\Doctrine\Common\Collections\Collection::class);
64+
$methodCall = $this->createMock(MethodCall::class);
65+
$methodCall->var = $var;
66+
67+
$resultType = $this->extension->getTypeFromMethodCall($methodReflection, $methodCall, $scope);
68+
69+
$this->assertInstanceOf(ObjectType::class, $resultType);
70+
$this->assertSame(\Doctrine\Common\Collections\Collection::class, $resultType->describe());
71+
}
72+
73+
}

0 commit comments

Comments
 (0)