From a86c17abd2aa10ed3af12543c9b089851557cca7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Tue, 9 Nov 2021 11:01:18 +0100 Subject: [PATCH] Improved tests for ReflectionClass adapter --- .../Adapter/ReflectionClassTest.php | 132 +++++++++++------- 1 file changed, 78 insertions(+), 54 deletions(-) diff --git a/test/unit/Reflection/Adapter/ReflectionClassTest.php b/test/unit/Reflection/Adapter/ReflectionClassTest.php index 9a958afb0..c3d574140 100644 --- a/test/unit/Reflection/Adapter/ReflectionClassTest.php +++ b/test/unit/Reflection/Adapter/ReflectionClassTest.php @@ -14,6 +14,8 @@ use Roave\BetterReflection\Reflection\Adapter\ReflectionAttribute as ReflectionAttributeAdapter; use Roave\BetterReflection\Reflection\Adapter\ReflectionClass as ReflectionClassAdapter; use Roave\BetterReflection\Reflection\Adapter\ReflectionClassConstant as ReflectionClassConstantAdapter; +use Roave\BetterReflection\Reflection\Adapter\ReflectionMethod as ReflectionMethodAdapter; +use Roave\BetterReflection\Reflection\Adapter\ReflectionProperty as ReflectionPropertyAdapter; use Roave\BetterReflection\Reflection\ReflectionAttribute as BetterReflectionAttribute; use Roave\BetterReflection\Reflection\ReflectionClass as BetterReflectionClass; use Roave\BetterReflection\Reflection\ReflectionClassConstant as BetterReflectionClassConstant; @@ -28,6 +30,7 @@ use function array_combine; use function array_map; use function get_class_methods; +use function is_array; /** * @covers \Roave\BetterReflection\Reflection\Adapter\ReflectionClass @@ -63,55 +66,55 @@ public function methodExpectationProvider(): array $mockConstant = $this->createMock(BetterReflectionClassConstant::class); return [ - ['__toString', null, '', []], - ['getName', null, '', []], - ['isAnonymous', null, true, []], - ['isInternal', null, true, []], - ['isUserDefined', null, true, []], - ['isInstantiable', null, true, []], - ['isCloneable', null, true, []], - ['getFileName', null, '', []], - ['getStartLine', null, 123, []], - ['getEndLine', null, 123, []], - ['getDocComment', null, '', []], - ['getConstructor', null, $mockMethod, []], - ['hasMethod', null, true, ['foo']], - ['getMethod', null, $mockMethod, ['foo']], - ['getMethods', null, [$mockMethod], []], - ['hasProperty', null, true, ['foo']], - ['getProperty', null, $mockProperty, ['foo']], - ['getProperties', null, [$mockProperty], []], - ['hasConstant', null, true, ['foo']], - ['getConstant', null, 'a', ['foo']], - ['getReflectionConstant', null, $mockConstant, ['foo']], - ['getReflectionConstants', null, [$mockConstant], []], - ['getInterfaces', null, [$mockClassLike], []], - ['getInterfaceNames', null, ['a', 'b'], []], - ['isInterface', null, true, []], - ['getTraits', null, [$mockClassLike], []], - ['getTraitNames', null, ['a', 'b'], []], - ['getTraitAliases', null, ['a', 'b'], []], - ['isTrait', null, true, []], - ['isAbstract', null, true, []], - ['isFinal', null, true, []], - ['getModifiers', null, 123, []], - ['isInstance', null, true, [new stdClass()]], - ['newInstance', NotImplemented::class, null, []], - ['newInstanceWithoutConstructor', NotImplemented::class, null, []], - ['newInstanceArgs', NotImplemented::class, null, []], - ['getParentClass', null, $mockClassLike, []], - ['isSubclassOf', null, true, ['\stdClass']], - ['getStaticProperties', null, [], []], - ['getDefaultProperties', null, ['foo' => 'bar'], []], - ['isIterateable', null, true, []], - ['implementsInterface', null, true, ['\Traversable']], - ['getExtension', NotImplemented::class, null, []], - ['getExtensionName', null, null, []], - ['inNamespace', null, true, []], - ['getNamespaceName', null, '', []], - ['getShortName', null, '', []], - ['getAttributes', null, [], []], - ['isEnum', null, true, []], + ['__toString', [], 'string', null, 'string', null], + ['getName', [], 'name', null, 'name', null], + ['isAnonymous', [], true, null, true, null], + ['isInternal', [], true, null, true, null], + ['isUserDefined', [], true, null, true, null], + ['isInstantiable', [], true, null, true, null], + ['isCloneable', [], true, null, true, null], + ['getFileName', [], 'filename', null, 'filename', null], + ['getStartLine', [], 123, null, 123, null], + ['getEndLine', [], 123, null, 123, null], + ['getDocComment', [], '', null, false, null], + ['getConstructor', [], $mockMethod, null, null, null], + ['hasMethod', ['foo'], true, null, true, null], + ['getMethod', ['foo'], $mockMethod, null, null, ReflectionMethodAdapter::class], + ['getMethods', [], [$mockMethod], null, null, ReflectionMethodAdapter::class], + ['hasProperty', ['foo'], true, null, true, null], + ['getProperty', ['foo'], $mockProperty, null, null, ReflectionPropertyAdapter::class], + ['getProperties', [], [$mockProperty], null, null, ReflectionPropertyAdapter::class], + ['hasConstant', ['foo'], true, null, true, null], + ['getConstant', ['foo'], 'a', null, 'a', null], + ['getReflectionConstant', ['foo'], $mockConstant, null, null, ReflectionClassConstantAdapter::class], + ['getReflectionConstants', [], [$mockConstant], null, null, ReflectionClassConstantAdapter::class], + ['getInterfaces', [], [$mockClassLike], null, null, ReflectionClassAdapter::class], + ['getInterfaceNames', [], ['a', 'b'], null, ['a', 'b'], null], + ['isInterface', [], true, null, true, null], + ['getTraits', [], [$mockClassLike], null, null, ReflectionClassAdapter::class], + ['getTraitNames', [], ['a', 'b'], null, ['a', 'b'], null], + ['getTraitAliases', [], ['a', 'b'], null, ['a', 'b'], null], + ['isTrait', [], true, null, true, null], + ['isAbstract', [], true, null, true, null], + ['isFinal', [], true, null, true, null], + ['getModifiers', [], 123, null, 123, null], + ['isInstance', [new stdClass()], true, null, true, null], + ['newInstance', [], null, NotImplemented::class, null, null], + ['newInstanceWithoutConstructor', [], null, NotImplemented::class, null, null], + ['newInstanceArgs', [], null, NotImplemented::class, null, null], + ['getParentClass', [], $mockClassLike, null, null, ReflectionClassAdapter::class], + ['isSubclassOf', ['\stdClass'], true, null, true, null], + ['getStaticProperties', [], [], null, [], null], + ['getDefaultProperties', [], ['foo' => 'bar'], null, null, null], + ['isIterateable', [], true, null, true, null], + ['implementsInterface', ['\Traversable'], true, null, true, null], + ['getExtension', [], null, NotImplemented::class, null, null], + ['getExtensionName', [], null, null, '', null], + ['inNamespace', [], true, null, true, null], + ['getNamespaceName', [], '', null, '', null], + ['getShortName', [], 'shortName', null, 'shortName', null], + ['getAttributes', [], [], null, [], null], + ['isEnum', [], true, null, true, null], ]; } @@ -120,23 +123,44 @@ public function methodExpectationProvider(): array * * @dataProvider methodExpectationProvider */ - public function testAdapterMethods(string $methodName, ?string $expectedException, mixed $returnValue, array $args): void - { + public function testAdapterMethods( + string $methodName, + array $args, + mixed $returnValue, + ?string $expectedException, + mixed $expectedReturnValue, + ?string $expectedReturnValueInstance, + ): void { $reflectionStub = $this->createMock(BetterReflectionClass::class); if ($expectedException === null) { $reflectionStub->expects($this->once()) ->method($methodName) ->with(...$args) - ->will($this->returnValue($returnValue)); + ->willReturn($returnValue); } + $adapter = new ReflectionClassAdapter($reflectionStub); + if ($expectedException !== null) { $this->expectException($expectedException); } - $adapter = new ReflectionClassAdapter($reflectionStub); - $adapter->{$methodName}(...$args); + $actualReturnValue = $adapter->{$methodName}(...$args); + + if ($expectedReturnValue !== null) { + self::assertSame($expectedReturnValue, $actualReturnValue); + } + + if ($expectedReturnValueInstance === null) { + return; + } + + if (is_array($actualReturnValue)) { + self::assertContainsOnlyInstancesOf($expectedReturnValueInstance, $actualReturnValue); + } else { + self::assertInstanceOf($expectedReturnValueInstance, $actualReturnValue); + } } public function testGetFileNameReturnsFalseWhenNoFileName(): void