From 849a9f3e5649197c0c7752f2c6756a7910561b72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20DECOOL?= Date: Fri, 3 Aug 2018 23:47:09 +0200 Subject: [PATCH 1/3] Fix data return by EnumMethodReflection::getVariants --- src/Reflection/EnumMethodReflection.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Reflection/EnumMethodReflection.php b/src/Reflection/EnumMethodReflection.php index 34dbbc8..d75a9a9 100644 --- a/src/Reflection/EnumMethodReflection.php +++ b/src/Reflection/EnumMethodReflection.php @@ -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 { @@ -58,6 +60,8 @@ public function getPrototype(): ClassMemberReflection public function getVariants(): array { - return []; + return [ + new FunctionVariant([], false, new ObjectType($this->classReflection->getName())), + ]; } } From b2083c43144e37e385b35a5dee7e721709a6f8cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20DECOOL?= Date: Mon, 6 Aug 2018 16:06:47 +0200 Subject: [PATCH 2/3] Remove mocking usage in tests --- ...numMethodsClassReflectionExtensionTest.php | 36 ++++++------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/tests/Reflection/EnumMethodsClassReflectionExtensionTest.php b/tests/Reflection/EnumMethodsClassReflectionExtensionTest.php index e57561b..cfae4ef 100644 --- a/tests/Reflection/EnumMethodsClassReflectionExtensionTest.php +++ b/tests/Reflection/EnumMethodsClassReflectionExtensionTest.php @@ -2,9 +2,7 @@ declare(strict_types=1); -use MyCLabs\Enum\Enum; -use PHPStan\Reflection\ClassReflection; -use PHPUnit\Framework\TestCase; +use PHPStan\Testing\TestCase; use Timeweb\PHPStan\Reflection\EnumMethodReflection; use Timeweb\PHPStan\Reflection\EnumMethodsClassReflectionExtension; @@ -13,6 +11,11 @@ */ class EnumMethodsClassReflectionExtensionTest extends TestCase { + /** + * @var \PHPStan\Broker\Broker + */ + protected $broker; + /** * @var EnumMethodsClassReflectionExtension */ @@ -20,6 +23,7 @@ class EnumMethodsClassReflectionExtensionTest extends TestCase public function setUp() { + $this->broker = $this->createBroker(); $this->reflectionExtension = new EnumMethodsClassReflectionExtension(); } @@ -29,19 +33,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); @@ -60,14 +52,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); @@ -79,8 +64,7 @@ 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); From 8f9f18ad73ea44e4e21eb54861a80ab074c13ca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20DECOOL?= Date: Mon, 6 Aug 2018 16:20:53 +0200 Subject: [PATCH 3/3] Add test for EnumMethodReflection properties --- ...numMethodsClassReflectionExtensionTest.php | 36 +++++++++++++++++++ tests/_fixture/EnumFixture.php | 3 ++ 2 files changed, 39 insertions(+) diff --git a/tests/Reflection/EnumMethodsClassReflectionExtensionTest.php b/tests/Reflection/EnumMethodsClassReflectionExtensionTest.php index cfae4ef..9b12370 100644 --- a/tests/Reflection/EnumMethodsClassReflectionExtensionTest.php +++ b/tests/Reflection/EnumMethodsClassReflectionExtensionTest.php @@ -2,7 +2,9 @@ declare(strict_types=1); +use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Testing\TestCase; +use PHPStan\Type\VerbosityLevel; use Timeweb\PHPStan\Reflection\EnumMethodReflection; use Timeweb\PHPStan\Reflection\EnumMethodsClassReflectionExtension; @@ -69,4 +71,38 @@ public function testEnumMethodReflectionCanBeObtained() $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'], + ]; + } } diff --git a/tests/_fixture/EnumFixture.php b/tests/_fixture/EnumFixture.php index fae35b5..b42b5dc 100644 --- a/tests/_fixture/EnumFixture.php +++ b/tests/_fixture/EnumFixture.php @@ -3,4 +3,7 @@ class EnumFixture extends MyCLabs\Enum\Enum { const MEMBER = 'member'; + + public const PUBLIC_CONST = 'public'; + private const PRIVATE_CONST = 'private'; }