Skip to content

Commit

Permalink
Improved tests for ReflectionProperty adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Nov 9, 2021
1 parent 943b497 commit 2c7483d
Showing 1 changed file with 47 additions and 21 deletions.
68 changes: 47 additions & 21 deletions test/unit/Reflection/Adapter/ReflectionPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ReflectionProperty as CoreReflectionProperty;
use Roave\BetterReflection\Reflection\Adapter\ReflectionAttribute as ReflectionAttributeAdapter;
use Roave\BetterReflection\Reflection\Adapter\ReflectionClass as ReflectionClassAdapter;
use Roave\BetterReflection\Reflection\Adapter\ReflectionNamedType as ReflectionNamedTypeAdapter;
use Roave\BetterReflection\Reflection\Adapter\ReflectionProperty as ReflectionPropertyAdapter;
use Roave\BetterReflection\Reflection\Exception\NoObjectProvided;
use Roave\BetterReflection\Reflection\Exception\NotAnObject;
Expand All @@ -23,6 +24,7 @@
use function array_combine;
use function array_map;
use function get_class_methods;
use function is_array;

/**
* @covers \Roave\BetterReflection\Reflection\Adapter\ReflectionProperty
Expand Down Expand Up @@ -51,23 +53,25 @@ public function methodExpectationProvider(): array
{
$mockType = $this->createMock(BetterReflectionNamedType::class);

$mockAttribute = $this->createMock(BetterReflectionAttribute::class);

return [
['__toString', null, '', []],
['getName', null, '', []],
['isPublic', null, true, []],
['isPrivate', null, true, []],
['isProtected', null, true, []],
['isStatic', null, true, []],
['isDefault', null, true, []],
['getModifiers', null, 123, []],
['getDocComment', null, '', []],
['hasType', null, true, []],
['getType', null, $mockType, []],
['hasDefaultValue', null, true, []],
['getDefaultValue', null, null, []],
['isPromoted', null, true, []],
['getAttributes', null, [], []],
['isReadOnly', null, true, []],
['__toString', [], 'string', null, 'string', null],
['getName', [], 'name', null, 'name', null],
['isPublic', [], true, null, true, null],
['isPrivate', [], true, null, true, null],
['isProtected', [], true, null, true, null],
['isStatic', [], true, null, true, null],
['isDefault', [], true, null, true, null],
['getModifiers', [], 123, null, 123, null],
['getDocComment', [], '', null, false, null],
['hasType', [], true, null, true, null],
['getType', [], $mockType, null, null, ReflectionNamedTypeAdapter::class],
['hasDefaultValue', [], true, null, true, null],
['getDefaultValue', [], null, null, null, null],
['isPromoted', [], true, null, true, null],
['getAttributes', [], [$mockAttribute], null, null, ReflectionAttributeAdapter::class],
['isReadOnly', [], true, null, true, null],
];
}

Expand All @@ -76,23 +80,45 @@ 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(BetterReflectionProperty::class);

if ($expectedException === null) {
$reflectionStub->expects($this->once())
->method($methodName)
->with(...$args)
->will($this->returnValue($returnValue));
->willReturn($returnValue);
}

$adapter = new ReflectionPropertyAdapter($reflectionStub);

if ($expectedException !== null) {
$this->expectException($expectedException);
}

$adapter = new ReflectionPropertyAdapter($reflectionStub);
$adapter->{$methodName}(...$args);
$actualReturnValue = $adapter->{$methodName}(...$args);

if ($expectedReturnValue !== null) {
self::assertSame($expectedReturnValue, $actualReturnValue);
}

if ($expectedReturnValueInstance === null) {
return;
}

if (is_array($actualReturnValue)) {
self::assertNotEmpty($actualReturnValue);
self::assertContainsOnlyInstancesOf($expectedReturnValueInstance, $actualReturnValue);
} else {
self::assertInstanceOf($expectedReturnValueInstance, $actualReturnValue);
}
}

public function testGetDocCommentReturnsFalseWhenNoDocComment(): void
Expand Down

0 comments on commit 2c7483d

Please sign in to comment.