Skip to content

Commit

Permalink
Merge pull request #1467 from kukulich/mutants
Browse files Browse the repository at this point in the history
Kill some mutants
  • Loading branch information
Ocramius authored Dec 12, 2024
2 parents 9cfae68 + f453c9d commit 75447b4
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 93 deletions.
13 changes: 0 additions & 13 deletions test/unit/Fixture/Php71NullableReturnTypeDeclarations.php

This file was deleted.

25 changes: 0 additions & 25 deletions test/unit/Fixture/Php7ReturnTypeDeclarations.php

This file was deleted.

45 changes: 45 additions & 0 deletions test/unit/Fixture/ReturnTypeDeclarations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

function returnsInt(): int
{
}

function returnsString(): string
{
}

function returnsObject(): \stdClass
{
}

function returnsNull(): null
{
}

function returnsNothing()
{
}

function returnsVoid(): void
{
}

function returnsNullableInt(): ?int
{
}

function returnsNullableString(): ?string
{
}

function returnsNullableObject(): ?\stdClass
{
}

function returnsUnion(): int|string
{
}

function returnsIntersection(): DateTime&DateTimeImmutable
{
}
1 change: 1 addition & 0 deletions test/unit/NodeCompiler/CompileNodeToValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,7 @@ class Bat {
]));
$classInfo = $reflector->reflectClass('Bat');

$this->expectException(UnableToCompileNode::class);
$this->expectExceptionMessage('An enum expression Foo::ONE is not supported in class Bat in file ');
$classInfo->getConstant('ONE_VALUE')->getValue();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Roave\BetterReflectionTest\Reflection\Adapter;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Roave\BetterReflection\Reflection\Adapter\Exception\NotImplementedBecauseItTriggersAutoloading;

#[CoversClass(NotImplementedBecauseItTriggersAutoloading::class)]
class NotImplementedBecauseItTriggersAutoloadingTest extends TestCase
{
public function testCreate(): void
{
$exception = NotImplementedBecauseItTriggersAutoloading::create();

self::assertInstanceOf(NotImplementedBecauseItTriggersAutoloading::class, $exception);
self::assertSame('Not implemented because it triggers autoloading', $exception->getMessage());
}
}
39 changes: 29 additions & 10 deletions test/unit/Reflection/Adapter/ReflectionNamedTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,27 +111,46 @@ public function testAdapterMethods(string $methodName, string|null $expectedExce
}

/** @return list<array{0: string}> */
public static function dataNotBuildin(): array
public static function dataIsBuildin(): array
{
return [
['self'],
['sElF'],
['static'],
['sTaTiC'],
['parent'],
['PaReNt'],
['string', true],
['int', true],
['self', false],
['sElF', false],
['static', false],
['sTaTiC', false],
['parent', false],
['PaReNt', false],
];
}

#[DataProvider('dataNotBuildin')]
public function testIsNotBuiltin(string $type): void
#[DataProvider('dataIsBuildin')]
public function testIsBuiltin(string $type, bool $isBuiltin): void
{
$reflector = $this->createMock(Reflector::class);
$owner = $this->createMock(BetterReflectionMethod::class);

$betterReflectionNamedType = new BetterReflectionNamedType($reflector, $owner, new Node\Name($type));
$reflectionTypeAdapter = new ReflectionNamedTypeAdapter($betterReflectionNamedType, false);

self::assertFalse($reflectionTypeAdapter->isBuiltin());
self::assertSame($isBuiltin, $reflectionTypeAdapter->isBuiltin());
}

public function testTypeWithoutBetterReflection(): void
{
$reflectionTypeAdapter = new ReflectionNamedTypeAdapter('never');

self::assertSame('never', $reflectionTypeAdapter->getName());
self::assertSame('never', $reflectionTypeAdapter->__toString());
self::assertTrue($reflectionTypeAdapter->isBuiltin());
self::assertFalse($reflectionTypeAdapter->allowsNull());
}

public function testDefaultAllowsNull(): void
{
$reflectionTypeAdapter = new ReflectionNamedTypeAdapter('never');

self::assertFalse($reflectionTypeAdapter->allowsNull());
}
}
64 changes: 20 additions & 44 deletions test/unit/Reflection/ReflectionFunctionAbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ public function testGetLocatedSource(): void
self::assertSame($locatedSource, $functionInfo->getLocatedSource());
}

/** @return list<array{0: string, 1: string|class-string}> */
/** @return list<array{0: string, 1: string|class-string|null}> */
public static function returnTypeFunctionProvider(): array
{
return [
Expand All @@ -480,46 +480,30 @@ public static function returnTypeFunctionProvider(): array
['returnsNull', 'null'],
['returnsObject', stdClass::class],
['returnsVoid', 'void'],
['returnsVoid', 'void'],
['returnsNothing', null],
['returnsUnion', 'int|string'],
['returnsIntersection', 'DateTime&DateTimeImmutable'],
];
}

#[DataProvider('returnTypeFunctionProvider')]
public function testGetReturnTypeWithDeclaredType(string $functionToReflect, string $expectedType): void
public function testGetReturnTypeWithDeclaredType(string $functionToReflect, string|null $expectedType): void
{
$functionInfo = (new DefaultReflector(
new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php7ReturnTypeDeclarations.php', $this->astLocator),
new SingleFileSourceLocator(__DIR__ . '/../Fixture/ReturnTypeDeclarations.php', $this->astLocator),
))->reflectFunction($functionToReflect);

$reflectionType = $functionInfo->getReturnType();
self::assertInstanceOf(ReflectionType::class, $reflectionType);
self::assertSame($expectedType, (string) $reflectionType);
}

public function testGetReturnTypeReturnsNullWhenTypeIsNotDeclared(): void
{
$functionInfo = (new DefaultReflector(
new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php7ReturnTypeDeclarations.php', $this->astLocator),
))->reflectFunction('returnsNothing');

self::assertNull($functionInfo->getReturnType());
}

public function testHasReturnTypeWhenTypeDeclared(): void
{
$functionInfo = (new DefaultReflector(
new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php7ReturnTypeDeclarations.php', $this->astLocator),
))->reflectFunction('returnsString');
if ($expectedType === null) {
self::assertFalse($functionInfo->hasReturnType());
self::assertNull($functionInfo->getReturnType());
} else {
self::assertTrue($functionInfo->hasReturnType());

self::assertTrue($functionInfo->hasReturnType());
}

public function testHasReturnTypeWhenTypeIsNotDeclared(): void
{
$functionInfo = (new DefaultReflector(
new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php7ReturnTypeDeclarations.php', $this->astLocator),
))->reflectFunction('returnsNothing');

self::assertFalse($functionInfo->hasReturnType());
$reflectionType = $functionInfo->getReturnType();
self::assertInstanceOf(ReflectionType::class, $reflectionType);
self::assertSame($expectedType, (string) $reflectionType);
}
}

/** @return list<array{0: string, 1: string}> */
Expand All @@ -536,7 +520,7 @@ public static function nullableReturnTypeFunctionProvider(): array
public function testGetNullableReturnTypeWithDeclaredType(string $functionToReflect, string $expectedType): void
{
$functionInfo = (new DefaultReflector(
new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php71NullableReturnTypeDeclarations.php', $this->astLocator),
new SingleFileSourceLocator(__DIR__ . '/../Fixture/ReturnTypeDeclarations.php', $this->astLocator),
))->reflectFunction($functionToReflect);

$reflectionType = $functionInfo->getReturnType();
Expand All @@ -557,11 +541,13 @@ public function testHasTentativeReturnType(): void
public function testHasNotTentativeReturnType(): void
{
$functionInfo = (new DefaultReflector(
new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php7ReturnTypeDeclarations.php', $this->astLocator),
new SingleFileSourceLocator(__DIR__ . '/../Fixture/ReturnTypeDeclarations.php', $this->astLocator),
))->reflectFunction('returnsString');

self::assertFalse($functionInfo->hasTentativeReturnType());
self::assertNull($functionInfo->getTentativeReturnType());
self::assertTrue($functionInfo->hasReturnType());
self::assertNotNull($functionInfo->getReturnType());
}

public function testGetTentativeReturnType(): void
Expand All @@ -576,16 +562,6 @@ public function testGetTentativeReturnType(): void
self::assertNull($methodInfo->getReturnType());
}

public function testNoTentativeReturnType(): void
{
$functionInfo = (new DefaultReflector(
new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php7ReturnTypeDeclarations.php', $this->astLocator),
))->reflectFunction('returnsString');

self::assertNull($functionInfo->getTentativeReturnType());
self::assertNotNull($functionInfo->getReturnType());
}

#[DataProvider('deprecatedDocCommentsProvider')]
public function testFunctionsCanBeDeprecated(string $comment): void
{
Expand Down
26 changes: 25 additions & 1 deletion test/unit/Reflection/ReflectionPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ public static function asymetricVisibilityModifierProvider(): array

/** @param non-empty-string $propertyName */
#[DataProvider('asymetricVisibilityModifierProvider')]
public function testGetAsymetricVisibilityModifiers(string $propertyName, int $expectedModifier): void
public function testGetAsymetricVisibilityMethods(string $propertyName, int $expectedModifier): void
{
$reflector = new DefaultReflector(new SingleFileSourceLocator(__DIR__ . '/../Fixture/AsymetricVisibilityClass.php', $this->astLocator));
$classInfo = $reflector->reflectClass('Roave\BetterReflectionTest\Fixture\AsymetricVisibilityClass');
Expand All @@ -926,6 +926,30 @@ public function testGetAsymetricVisibilityModifiers(string $propertyName, int $e
self::assertSame($expectedModifier, $property->getModifiers());
}

public function testIsProtectedSet(): void
{
$reflector = new DefaultReflector(new SingleFileSourceLocator(__DIR__ . '/../Fixture/AsymetricVisibilityClass.php', $this->astLocator));
$classInfo = $reflector->reflectClass('Roave\BetterReflectionTest\Fixture\AsymetricVisibilityClass');

$publicPublicSetProperty = $classInfo->getProperty('publicPublicSet');
$publicProtectedSetProperty = $classInfo->getProperty('publicProtectedSet');

self::assertFalse($publicPublicSetProperty->isProtectedSet());
self::assertTrue($publicProtectedSetProperty->isProtectedSet());
}

public function testIsPrivateSet(): void
{
$reflector = new DefaultReflector(new SingleFileSourceLocator(__DIR__ . '/../Fixture/AsymetricVisibilityClass.php', $this->astLocator));
$classInfo = $reflector->reflectClass('Roave\BetterReflectionTest\Fixture\AsymetricVisibilityClass');

$protectedProtectedSet = $classInfo->getProperty('protectedProtectedSet');
$protectedPrivateSet = $classInfo->getProperty('protectedPrivateSet');

self::assertFalse($protectedProtectedSet->isPrivateSet());
self::assertTrue($protectedPrivateSet->isPrivateSet());
}

public function testIsAbstract(): void
{
$reflector = new DefaultReflector(new SingleFileSourceLocator(__DIR__ . '/../Fixture/PropertyHooks.php', $this->astLocator));
Expand Down
8 changes: 8 additions & 0 deletions test/unit/SourceLocator/Ast/FindReflectionsInTreeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\TestCase;
use Roave\BetterReflection\Identifier\Identifier;
use Roave\BetterReflection\Identifier\IdentifierType;
Expand Down Expand Up @@ -215,6 +216,12 @@ public function testInvokeCallsReflectNodesForConstantByDefine(): void

$strategy->expects($this->once())
->method('__invoke')
->with(
$this->isInstanceOf(Reflector::class),
new Callback(
static fn (Node $node): bool => $node instanceof Node\Expr\FuncCall && $node->getDocComment() !== null,
),
)
->willReturn($mockReflection);

$reflector = $this->createMock(Reflector::class);
Expand All @@ -224,6 +231,7 @@ public function testInvokeCallsReflectNodesForConstantByDefine(): void

$source = <<<'PHP'
<?php
/** @var int */
define("FOO", 1);
PHP;
$locatedSource = new LocatedSource($source, 'FOO');
Expand Down

0 comments on commit 75447b4

Please sign in to comment.