Add tests for factories

This commit is contained in:
Jaapio
2026-01-04 15:34:19 +01:00
parent 3f6d648d7d
commit c1111a0b0a
13 changed files with 488 additions and 108 deletions
+12 -27
View File
@@ -1,15 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.0/phpunit.xsd"
colors="true"
convertDeprecationsToExceptions="false"
beStrictAboutOutputDuringTests="false"
forceCoversAnnotation="true"
verbose="true"
bootstrap="vendor/autoload.php"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" colors="true" convertDeprecationsToExceptions="false" beStrictAboutOutputDuringTests="false" forceCoversAnnotation="true" verbose="true" bootstrap="vendor/autoload.php">
<coverage>
<include>
<directory suffix=".php">./src/</directory>
</include>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/coverage" lowUpperBound="35" highLowerBound="70"/>
</report>
</coverage>
<testsuites>
<testsuite name="unit">
<directory>./tests/unit</directory>
@@ -18,22 +17,8 @@
<directory>./tests/integration</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html"
target="build/coverage"
lowUpperBound="35"
highLowerBound="70"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<logging/>
<listeners>
<listener
class="Mockery\Adapter\Phpunit\TestListener"
file="vendor/mockery/mockery/library/Mockery/Adapter/Phpunit/TestListener.php"
/>
<listener class="Mockery\Adapter\Phpunit\TestListener" file="vendor/mockery/mockery/library/Mockery/Adapter/Phpunit/TestListener.php"/>
</listeners>
</phpunit>
+2 -4
View File
@@ -28,9 +28,8 @@ use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyReadFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyWriteFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\ReturnFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\TemplateExtendsFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\TemplateCovariantFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\TemplateFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\TemplateImplementsFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\ThrowsFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\VarFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Generic;
@@ -156,8 +155,7 @@ final class StandardTagFactory implements TagFactory
new ImplementsFactory($typeResolver, $descriptionFactory),
new ExtendsFactory($typeResolver, $descriptionFactory),
new TemplateFactory($typeResolver, $descriptionFactory),
new TemplateImplementsFactory($typeResolver, $descriptionFactory),
new TemplateExtendsFactory($typeResolver, $descriptionFactory),
new TemplateCovariantFactory($typeResolver, $descriptionFactory),
new ThrowsFactory($typeResolver, $descriptionFactory),
);
@@ -18,6 +18,7 @@ use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag;
use phpDocumentor\Reflection\Types\Context as TypeContext;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\ConstExprParser;
use PHPStan\PhpDocParser\Parser\ParserException;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\PhpDocParser\Parser\TypeParser;
@@ -59,6 +60,7 @@ class AbstractPHPStanFactory implements Factory
public function create(string $tagLine, ?TypeContext $context = null): Tag
{
try {
$tokens = $this->tokenizeLine($tagLine . "\n");
$ast = $this->parser->parseTag($tokens);
if (property_exists($ast->value, 'description') === true) {
@@ -67,6 +69,9 @@ class AbstractPHPStanFactory implements Factory
rtrim($ast->value->description . $tokens->joinUntil(Lexer::TOKEN_END), "\n")
);
}
} catch (ParserException $e) {
return InvalidTag::create($tagLine, '')->withError($e);
}
if ($context === null) {
$context = new TypeContext('');
@@ -80,6 +85,8 @@ class AbstractPHPStanFactory implements Factory
}
} catch (RuntimeException $e) {
return InvalidTag::create((string) $ast->value, 'method')->withError($e);
} catch (ParserException $e) {
return InvalidTag::create((string) $ast->value, $ast->name)->withError($e);
}
return InvalidTag::create(
@@ -6,11 +6,12 @@ namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Tags\TemplateExtends;
use phpDocumentor\Reflection\DocBlock\Tags\TemplateCovariant;
use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Context;
use PHPStan\PhpDocParser\Ast\PhpDoc\ExtendsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Webmozart\Assert\Assert;
use function is_string;
@@ -18,7 +19,7 @@ use function is_string;
/**
* @internal This class is not part of the BC promise of this library.
*/
final class TemplateExtendsFactory implements PHPStanFactory
final class TemplateCovariantFactory implements PHPStanFactory
{
private DescriptionFactory $descriptionFactory;
private TypeResolver $typeResolver;
@@ -31,21 +32,21 @@ final class TemplateExtendsFactory implements PHPStanFactory
public function supports(PhpDocTagNode $node, Context $context): bool
{
return $node->value instanceof ExtendsTagValueNode && $node->name === '@template-extends';
return $node->value instanceof TemplateTagValueNode && $node->name === '@template-covariant';
}
public function create(PhpDocTagNode $node, Context $context): Tag
{
$tagValue = $node->value;
Assert::isInstanceOf($tagValue, ExtendsTagValueNode::class);
Assert::isInstanceOf($tagValue, TemplateTagValueNode::class);
$description = $tagValue->getAttribute('description');
if (is_string($description) === false) {
$description = $tagValue->description;
}
return new TemplateExtends(
$this->typeResolver->createType($tagValue->type, $context),
return new TemplateCovariant(
$this->typeResolver->createType(new IdentifierTypeNode($tagValue->name), $context),
$this->descriptionFactory->create($description, $context)
);
}
@@ -1,52 +0,0 @@
<?php
declare(strict_types=1);
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Tags\TemplateImplements;
use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Context;
use PHPStan\PhpDocParser\Ast\PhpDoc\ImplementsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Webmozart\Assert\Assert;
use function is_string;
/**
* @internal This class is not part of the BC promise of this library.
*/
final class TemplateImplementsFactory implements PHPStanFactory
{
private DescriptionFactory $descriptionFactory;
private TypeResolver $typeResolver;
public function __construct(TypeResolver $typeResolver, DescriptionFactory $descriptionFactory)
{
$this->descriptionFactory = $descriptionFactory;
$this->typeResolver = $typeResolver;
}
public function supports(PhpDocTagNode $node, Context $context): bool
{
return $node->value instanceof ImplementsTagValueNode && $node->name === '@template-implements';
}
public function create(PhpDocTagNode $node, Context $context): Tag
{
$tagValue = $node->value;
Assert::isInstanceOf($tagValue, ImplementsTagValueNode::class);
$description = $tagValue->getAttribute('description');
if (is_string($description) === false) {
$description = $tagValue->description;
}
return new TemplateImplements(
$this->typeResolver->createType($tagValue->type, $context),
$this->descriptionFactory->create($description, $context)
);
}
}
@@ -0,0 +1,111 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
use Exception;
use Mockery as m;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag;
use PHPStan\PhpDocParser\Parser\ParserException;
use PHPUnit\Framework\TestCase;
use RuntimeException;
/**
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Factory\AbstractPHPStanFactory
* @uses \phpDocumentor\Reflection\DocBlock\Tags\InvalidTag
*
* @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\Factory\AbstractPHPStanFactory
* @covers ::<private>
*/
class AbstractPHPStanFactoryTest extends TestCase
{
/**
* Call Mockery::close after each test.
*/
public function tearDown(): void
{
m::close();
}
/**
* @covers ::create
*/
public function testCreateReturnsTagFromSupportingFactory(): void
{
$tag = m::mock(Tag::class);
$factory = m::mock(PHPStanFactory::class);
$factory->shouldReceive('supports')->andReturn(true);
$factory->shouldReceive('create')->andReturn($tag);
$sut = new AbstractPHPStanFactory($factory);
$result = $sut->create('@param string $param');
self::assertSame($tag, $result);
}
/**
* @covers ::create
*/
public function testCreateReturnsInvalidTagWhenNoFactorySupports(): void
{
$factory = m::mock(PHPStanFactory::class);
$factory->shouldReceive('supports')->andReturn(false);
$sut = new AbstractPHPStanFactory($factory);
$result = $sut->create('@unknown string $param');
self::assertInstanceOf(InvalidTag::class, $result);
self::assertEquals('@unknown', $result->getName());
}
/**
* @covers ::create
*/
public function testCreateReturnsInvalidTagWithErrorOnFactoryRuntimeException(): void
{
$factory = m::mock(PHPStanFactory::class);
$factory->shouldReceive('supports')->andReturn(true);
$factory->shouldReceive('create')->andThrow(new RuntimeException('Factory error'));
$sut = new AbstractPHPStanFactory($factory);
$result = $sut->create('@param string $param');
self::assertInstanceOf(InvalidTag::class, $result);
self::assertInstanceOf(Exception::class, $result->getException());
self::assertEquals('Factory error', $result->getException()->getMessage());
}
/**
* @covers ::create
*/
public function testCreateReturnsInvalidTagWithErrorOnFactoryParserException(): void
{
$exception = m::mock(ParserException::class);
$exception->shouldReceive('getMessage')->andReturn('Parser error');
$factory = m::mock(PHPStanFactory::class);
$factory->shouldReceive('supports')->andReturn(true);
$factory->shouldReceive('create')->andThrow($exception);
$sut = new AbstractPHPStanFactory($factory);
$result = $sut->create('@param string $param');
self::assertInstanceOf(InvalidTag::class, $result);
self::assertSame($exception, $result->getException());
}
}
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\Tags\Extends_;
use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\PseudoTypes\Generic;
use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\Object_;
final class ExtendsFactoryTest extends TagFactoryTestCase
{
/**
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ExtendsFactory::__construct
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ExtendsFactory::create
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ExtendsFactory::supports
*/
public function testExtendsIsCreated(): void
{
$ast = $this->parseTag('@extends SomeClass<OtherType>');
$factory = new ExtendsFactory($this->giveTypeResolver(), $this->givenDescriptionFactory());
$context = new Context('global');
self::assertTrue($factory->supports($ast, $context));
self::assertEquals(
new Extends_(
new Generic(new Fqsen('\\SomeClass'), [new Object_(new Fqsen('\\OtherType'))]),
new Description('')
),
$factory->create($ast, $context)
);
}
}
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\Tags\Implements_;
use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\PseudoTypes\Generic;
use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\Object_;
final class ImplementsFactoryTest extends TagFactoryTestCase
{
/**
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ImplementsFactory::__construct
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ImplementsFactory::create
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ImplementsFactory::supports
*/
public function testImplementsIsCreated(): void
{
$ast = $this->parseTag('@implements SomeClass<OtherType>');
$factory = new ImplementsFactory($this->giveTypeResolver(), $this->givenDescriptionFactory());
$context = new Context('global');
self::assertTrue($factory->supports($ast, $context));
self::assertEquals(
new Implements_(
new Generic(new Fqsen('\\SomeClass'), [new Object_(new Fqsen('\\OtherType'))]),
new Description('')
),
$factory->create($ast, $context)
);
}
}
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\Tags\Mixin;
use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\String_;
final class MixinFactoryTest extends TagFactoryTestCase
{
/**
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\MixinFactory::__construct
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\MixinFactory::create
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\MixinFactory::supports
*/
public function testMixinIsCreated(): void
{
$ast = $this->parseTag('@mixin string');
$factory = new MixinFactory($this->giveTypeResolver(), $this->givenDescriptionFactory());
$context = new Context('global');
self::assertTrue($factory->supports($ast, $context));
self::assertEquals(
new Mixin(
new String_(),
new Description('')
),
$factory->create($ast, $context)
);
}
}
@@ -33,7 +33,7 @@ abstract class TagFactoryTestCase extends TestCase
{
public function parseTag(string $tag): PhpDocTagNode
{
$config = new ParserConfig([]);
$config = new ParserConfig(['indexes' => true, 'lines' => true]);
$lexer = new Lexer($config);
$constParser = new ConstExprParser($config);
$phpDocParser = new PhpDocParser($config, new TypeParser($config, $constParser), $constParser);
@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Tags\TemplateCovariant;
use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\Object_;
use phpDocumentor\Reflection\Types\String_;
final class TemplateCovariantFactoryTest extends TagFactoryTestCase
{
/**
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\TemplateCovariantFactory::__construct
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\TemplateCovariantFactory::create
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\TemplateCovariantFactory::supports
* @dataProvider templateCovariantInputProvider
*/
public function testTemplateCovariantIsCreated(string $input, Tag $expected): void
{
$ast = $this->parseTag($input);
$factory = new TemplateCovariantFactory($this->giveTypeResolver(), $this->givenDescriptionFactory());
$context = new Context('global');
self::assertTrue($factory->supports($ast, $context));
self::assertEquals(
$expected,
$factory->create($ast, $context)
);
}
/**
* @return array<int, array<int, string|TemplateCovariant>>
*/
public function templateCovariantInputProvider(): array
{
return [
[
'@template-covariant string',
new TemplateCovariant(
new String_(),
new Description('')
),
],
[
'@template-covariant SomeClass Description',
new TemplateCovariant(
new Object_(new Fqsen('\SomeClass')),
new Description('Description')
),
],
[
'@template-covariant SomeClass',
new TemplateCovariant(
new Object_(new Fqsen('\SomeClass')),
new Description('')
),
],
];
}
}
@@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Tags\Template;
use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\Mixed_;
use phpDocumentor\Reflection\Types\Object_;
final class TemplateFactoryTest extends TagFactoryTestCase
{
/**
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\TemplateFactory::__construct
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\TemplateFactory::create
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\TemplateFactory::supports
* @dataProvider templateInputProvider
*/
public function testTemplateIsCreated(string $input, Tag $expected): void
{
$ast = $this->parseTag($input);
$factory = new TemplateFactory($this->giveTypeResolver(), $this->givenDescriptionFactory());
$context = new Context('global');
self::assertTrue($factory->supports($ast, $context));
self::assertEquals(
$expected,
$factory->create($ast, $context)
);
}
/**
* @return array<int, array<int, string|Template>>
*/
public function templateInputProvider(): array
{
return [
[
'@template T',
new Template(
'T',
new Mixed_(),
new Mixed_(),
new Description('')
),
],
[
'@template T of SomeClass Description',
new Template(
'T',
new Object_(new Fqsen('\SomeClass')),
new Mixed_(),
new Description('Description')
),
],
[
'@template T of SomeClass = Default',
new Template(
'T',
new Object_(new Fqsen('\SomeClass')),
new Object_(new Fqsen('\Default')),
new Description('')
),
],
];
}
}
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\Tags\Throws;
use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\String_;
final class ThrowsFactoryTest extends TagFactoryTestCase
{
/**
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ThrowsFactory::__construct
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ThrowsFactory::create
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ThrowsFactory::supports
*/
public function testThrowsIsCreated(): void
{
$ast = $this->parseTag('@throws string');
$factory = new ThrowsFactory($this->giveTypeResolver(), $this->givenDescriptionFactory());
$context = new Context('global');
self::assertTrue($factory->supports($ast, $context));
self::assertEquals(
new Throws(
new String_(),
new Description('')
),
$factory->create($ast, $context)
);
}
}