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
@@ -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)
);
}
}