Skip to content

Add tests for existing extensions #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
parameters:
ignoreErrors: []
ignoreErrors:
- '#but returns PHPUnit_Framework_MockObject_MockObject#'
- '#PHPUnit_Framework_MockObject_MockObject given#'
- '#Access to an undefined property PHPUnit_Framework_MockObject_MockObject::\$\w+#'

1 change: 1 addition & 0 deletions ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<ruleset name="PHPStan Doctrine extensions">
<rule ref="vendor/consistence/coding-standard/Consistence/ruleset.xml"/>
<rule ref="vendor/slevomat/coding-standard/SlevomatCodingStandard/ruleset.xml">
<exclude name="SlevomatCodingStandard.Classes.ClassConstantVisibility.MissingConstantVisibility"/>
<exclude name="SlevomatCodingStandard.Files.TypeNameMatchesFileName"/>
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameAfterKeyword"/>
<exclude name="SlevomatCodingStandard.Namespaces.UseOnlyWhitelistedNamespaces"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php declare(strict_types = 1);

namespace Tests\PHPStan\Reflection\Doctrine;

use PHPStan\Broker\Broker;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\Doctrine\DoctrineSelectableClassReflectionExtension;
use PHPStan\Reflection\MethodReflection;
use PHPUnit\Framework\TestCase;

final class DoctrineSelectableClassReflectionExtensionTest extends TestCase
{

/** @var \PHPStan\Reflection\Doctrine\DoctrineSelectableClassReflectionExtension */
private $extension;

protected function setUp()
{
$broker = $this->mockBroker();

$this->extension = new DoctrineSelectableClassReflectionExtension();
$this->extension->setBroker($broker);
}

/**
* @return mixed[]
*/
public function dataHasMethod(): array
{
return [
[\Doctrine\Common\Collections\Collection::class, 'matching', true],
[\Doctrine\Common\Collections\Collection::class, 'foo', false],
];
}

/**
* @dataProvider dataHasMethod
* @param string $className
* @param string $method
* @param bool $expectedResult
*/
public function testHasMethod(string $className, string $method, bool $expectedResult)
{
$classReflection = $this->mockClassReflection(new \ReflectionClass($className));
$this->assertSame($expectedResult, $this->extension->hasMethod($classReflection, $method));
}

public function testGetMethod()
{
$classReflection = $this->mockClassReflection(new \ReflectionClass(\Doctrine\Common\Collections\Collection::class));
$methodReflection = $this->extension->getMethod($classReflection, 'matching');
$this->assertSame('matching', $methodReflection->getName());
}

private function mockBroker(): Broker
{
$broker = $this->createMock(Broker::class);

$broker->method('getClass')->willReturnCallback(
function (string $className): ClassReflection {
return $this->mockClassReflection(new \ReflectionClass($className));
}
);

return $broker;
}

private function mockClassReflection(\ReflectionClass $reflectionClass): ClassReflection
{
$classReflection = $this->createMock(ClassReflection::class);
$classReflection->method('getName')->willReturn($reflectionClass->getName());
$classReflection->method('getMethod')->willReturnCallback(
function (string $method) use ($reflectionClass): MethodReflection {
return $this->mockMethodReflection($reflectionClass->getMethod($method));
}
);

return $classReflection;
}

private function mockMethodReflection(\ReflectionMethod $method): MethodReflection
{
$methodReflection = $this->createMock(MethodReflection::class);
$methodReflection->method('getName')->willReturn($method->getName());
return $methodReflection;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php declare(strict_types = 1);

namespace Tests\PHPStan\Type\Doctrine;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\Doctrine\DoctrineSelectableDynamicReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPUnit\Framework\TestCase;

final class DoctrineSelectableDynamicReturnTypeExtensionTest extends TestCase
{

/** @var \PHPStan\Type\Doctrine\DoctrineSelectableDynamicReturnTypeExtension */
private $extension;

protected function setUp()
{
$this->extension = new DoctrineSelectableDynamicReturnTypeExtension();
}

/**
* @return mixed[]
*/
public function dataIsMethodSupported(): array
{
return [
['matching', true],
['filter', false],
['foo', false],
];
}

/**
* @dataProvider dataIsMethodSupported
* @param string $method
* @param bool $expectedResult
*/
public function testIsMethodSupported(string $method, bool $expectedResult)
{
$methodReflection = $this->createMock(MethodReflection::class);
$methodReflection->method('getName')->willReturn($method);
$this->assertSame($expectedResult, $this->extension->isMethodSupported($methodReflection));
}

public function testGetTypeFromMethodCall()
{
$methodReflection = $this->createMock(MethodReflection::class);

$scope = $this->createMock(Scope::class);
$scope->method('getType')->will(
self::returnCallback(
function (\PhpParser\Node\Expr $node): Type {
return new ObjectType($node->getType());
}
)
);

$var = $this->createMock(Expr::class);
$var->method('getType')->willReturn(\Doctrine\Common\Collections\Collection::class);
$methodCall = $this->createMock(MethodCall::class);
$methodCall->var = $var;

$resultType = $this->extension->getTypeFromMethodCall($methodReflection, $methodCall, $scope);

$this->assertInstanceOf(ObjectType::class, $resultType);
$this->assertSame(\Doctrine\Common\Collections\Collection::class, $resultType->describe());
}

}
Loading