Skip to content

Commit

Permalink
Merge pull request #5 from jdecool/fix-get-variants
Browse files Browse the repository at this point in the history
Fix data return by EnumMethodReflection::getVariants
  • Loading branch information
samizdam committed Aug 9, 2018
2 parents cc4dce0 + 8f9f18a commit 6dffb12
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 27 deletions.
6 changes: 5 additions & 1 deletion src/Reflection/EnumMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use PHPStan\Reflection\ClassMemberReflection;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\FunctionVariant;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\ObjectType;

class EnumMethodReflection implements MethodReflection
{
Expand Down Expand Up @@ -58,6 +60,8 @@ public function getPrototype(): ClassMemberReflection

public function getVariants(): array
{
return [];
return [
new FunctionVariant([], false, new ObjectType($this->classReflection->getName())),
];
}
}
72 changes: 46 additions & 26 deletions tests/Reflection/EnumMethodsClassReflectionExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

use MyCLabs\Enum\Enum;
use PHPStan\Reflection\ClassReflection;
use PHPUnit\Framework\TestCase;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Testing\TestCase;
use PHPStan\Type\VerbosityLevel;
use Timeweb\PHPStan\Reflection\EnumMethodReflection;
use Timeweb\PHPStan\Reflection\EnumMethodsClassReflectionExtension;

Expand All @@ -13,13 +13,19 @@
*/
class EnumMethodsClassReflectionExtensionTest extends TestCase
{
/**
* @var \PHPStan\Broker\Broker
*/
protected $broker;

/**
* @var EnumMethodsClassReflectionExtension
*/
protected $reflectionExtension;

public function setUp()
{
$this->broker = $this->createBroker();
$this->reflectionExtension = new EnumMethodsClassReflectionExtension();
}

Expand All @@ -29,19 +35,7 @@ public function setUp()
*/
public function testEnumMethodsCanBeFoundInEnumSubclasses(bool $expected, string $methodName)
{
$classReflection = $this->createMock(ClassReflection::class);

$classReflection->expects($this->once())
->method('getNativeReflection')
->will($this->returnValue(new ReflectionClass(EnumFixture::class)))
;

$classReflection->expects($this->once())
->method('isSubclassOf')
->with($this->equalTo(Enum::class))
->will($this->returnValue(true))
;

$classReflection = $this->broker->getClass(EnumFixture::class);
$hasMethod = $this->reflectionExtension->hasMethod($classReflection, $methodName);

$this->assertEquals($expected, $hasMethod);
Expand All @@ -60,14 +54,7 @@ public function methodNameDataProvider(): array
*/
public function testEnumMethodsCannotBeFoundInNonEnumSubclasses()
{
$classReflection = $this->createMock(ClassReflection::class);

$classReflection->expects($this->once())
->method('isSubclassOf')
->with($this->equalTo(Enum::class))
->will($this->returnValue(false))
;

$classReflection = $this->broker->getClass(EnumFixture::class);
$hasMethod = $this->reflectionExtension->hasMethod($classReflection, 'SOME_NAME');

$this->assertFalse($hasMethod);
Expand All @@ -79,10 +66,43 @@ public function testEnumMethodsCannotBeFoundInNonEnumSubclasses()
*/
public function testEnumMethodReflectionCanBeObtained()
{
$classReflection = $this->createMock(ClassReflection::class);

$classReflection = $this->broker->getClass(EnumFixture::class);
$methodReflection = $this->reflectionExtension->getMethod($classReflection, 'SOME_NAME');

$this->assertInstanceOf(EnumMethodReflection::class, $methodReflection);
}

/**
* @covers \Timeweb\PHPStan\Reflection\EnumMethodReflection::getName
* @covers \Timeweb\PHPStan\Reflection\EnumMethodReflection::getDeclaringClass
* @covers \Timeweb\PHPStan\Reflection\EnumMethodReflection::isStatic
* @covers \Timeweb\PHPStan\Reflection\EnumMethodReflection::isPrivate
* @covers \Timeweb\PHPStan\Reflection\EnumMethodReflection::isPublic
* @covers \Timeweb\PHPStan\Reflection\EnumMethodReflection::getPrototype
* @covers \Timeweb\PHPStan\Reflection\EnumMethodReflection::getVariants
* @uses Timeweb\PHPStan\Reflection\EnumMethodReflection
* @dataProvider enumFixtureProperties
*/
public function testEnumMethodProperties(string $propertyName)
{
$classReflection = $this->broker->getClass(EnumFixture::class);
$methodReflection = $this->reflectionExtension->getMethod($classReflection, $propertyName);
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());

$this->assertSame($propertyName, $methodReflection->getName());
$this->assertSame($classReflection, $methodReflection->getDeclaringClass());
$this->assertTrue($methodReflection->isStatic());
$this->assertFalse($methodReflection->isPrivate());
$this->assertTrue($methodReflection->isPublic());
$this->assertSame(EnumFixture::class, $parametersAcceptor->getReturnType()->describe(VerbosityLevel::value()));
}

public function enumFixtureProperties(): array
{
return [
['MEMBER'],
['PUBLIC_CONST'],
['PRIVATE_CONST'],
];
}
}
3 changes: 3 additions & 0 deletions tests/_fixture/EnumFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
class EnumFixture extends MyCLabs\Enum\Enum
{
const MEMBER = 'member';

public const PUBLIC_CONST = 'public';
private const PRIVATE_CONST = 'private';
}

0 comments on commit 6dffb12

Please sign in to comment.