Skip to content

Commit

Permalink
Improved tests for ReflectionClass adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Nov 9, 2021
1 parent 9327be8 commit a86c17a
Showing 1 changed file with 78 additions and 54 deletions.
132 changes: 78 additions & 54 deletions test/unit/Reflection/Adapter/ReflectionClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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],
];
}

Expand All @@ -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
Expand Down

0 comments on commit a86c17a

Please sign in to comment.