|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of phpDocumentor. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + * |
| 11 | + * @link http://phpdoc.org |
| 12 | + */ |
| 13 | + |
| 14 | +namespace phpDocumentor\Reflection\DocBlock\Tags\Factory; |
| 15 | + |
| 16 | +use Exception; |
| 17 | +use Mockery as m; |
| 18 | +use phpDocumentor\Reflection\DocBlock\Tag; |
| 19 | +use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag; |
| 20 | +use PHPStan\PhpDocParser\Parser\ParserException; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | +use RuntimeException; |
| 23 | + |
| 24 | +/** |
| 25 | + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Factory\AbstractPHPStanFactory |
| 26 | + * @uses \phpDocumentor\Reflection\DocBlock\Tags\InvalidTag |
| 27 | + * |
| 28 | + * @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\Factory\AbstractPHPStanFactory |
| 29 | + * @covers ::<private> |
| 30 | + */ |
| 31 | +class AbstractPHPStanFactoryTest extends TestCase |
| 32 | +{ |
| 33 | + /** |
| 34 | + * Call Mockery::close after each test. |
| 35 | + */ |
| 36 | + public function tearDown(): void |
| 37 | + { |
| 38 | + m::close(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @covers ::create |
| 43 | + */ |
| 44 | + public function testCreateReturnsTagFromSupportingFactory(): void |
| 45 | + { |
| 46 | + $tag = m::mock(Tag::class); |
| 47 | + $factory = m::mock(PHPStanFactory::class); |
| 48 | + $factory->shouldReceive('supports')->andReturn(true); |
| 49 | + $factory->shouldReceive('create')->andReturn($tag); |
| 50 | + |
| 51 | + $sut = new AbstractPHPStanFactory($factory); |
| 52 | + |
| 53 | + $result = $sut->create('@param string $param'); |
| 54 | + |
| 55 | + self::assertSame($tag, $result); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @covers ::create |
| 60 | + */ |
| 61 | + public function testCreateReturnsInvalidTagWhenNoFactorySupports(): void |
| 62 | + { |
| 63 | + $factory = m::mock(PHPStanFactory::class); |
| 64 | + $factory->shouldReceive('supports')->andReturn(false); |
| 65 | + |
| 66 | + $sut = new AbstractPHPStanFactory($factory); |
| 67 | + |
| 68 | + $result = $sut->create('@unknown string $param'); |
| 69 | + |
| 70 | + self::assertInstanceOf(InvalidTag::class, $result); |
| 71 | + self::assertEquals('@unknown', $result->getName()); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @covers ::create |
| 76 | + */ |
| 77 | + public function testCreateReturnsInvalidTagWithErrorOnFactoryRuntimeException(): void |
| 78 | + { |
| 79 | + $factory = m::mock(PHPStanFactory::class); |
| 80 | + $factory->shouldReceive('supports')->andReturn(true); |
| 81 | + $factory->shouldReceive('create')->andThrow(new RuntimeException('Factory error')); |
| 82 | + |
| 83 | + $sut = new AbstractPHPStanFactory($factory); |
| 84 | + |
| 85 | + $result = $sut->create('@param string $param'); |
| 86 | + |
| 87 | + self::assertInstanceOf(InvalidTag::class, $result); |
| 88 | + self::assertInstanceOf(Exception::class, $result->getException()); |
| 89 | + self::assertEquals('Factory error', $result->getException()->getMessage()); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @covers ::create |
| 94 | + */ |
| 95 | + public function testCreateReturnsInvalidTagWithErrorOnFactoryParserException(): void |
| 96 | + { |
| 97 | + $exception = m::mock(ParserException::class); |
| 98 | + $exception->shouldReceive('getMessage')->andReturn('Parser error'); |
| 99 | + |
| 100 | + $factory = m::mock(PHPStanFactory::class); |
| 101 | + $factory->shouldReceive('supports')->andReturn(true); |
| 102 | + $factory->shouldReceive('create')->andThrow($exception); |
| 103 | + |
| 104 | + $sut = new AbstractPHPStanFactory($factory); |
| 105 | + |
| 106 | + $result = $sut->create('@param string $param'); |
| 107 | + |
| 108 | + self::assertInstanceOf(InvalidTag::class, $result); |
| 109 | + self::assertSame($exception, $result->getException()); |
| 110 | + } |
| 111 | +} |
0 commit comments