From 0c63d0ae5b4c6c7a2317047b0489b0b55eb864dd Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Mon, 15 Jun 2015 07:04:42 +0200 Subject: [PATCH] Rename Other tag class to Generic and write test --- src/DocBlock/TagFactory.php | 4 +- src/DocBlock/Tags/{Other.php => Generic.php} | 24 ++- tests/unit/DocBlock/DescriptionTest.php | 6 +- tests/unit/DocBlock/SerializerTest.php | 8 +- .../Formatter/PassthroughFormatterTest.php | 4 +- tests/unit/DocBlock/Tags/GenericTest.php | 146 ++++++++++++++++++ 6 files changed, 175 insertions(+), 17 deletions(-) rename src/DocBlock/Tags/{Other.php => Generic.php} (75%) create mode 100644 tests/unit/DocBlock/Tags/GenericTest.php diff --git a/src/DocBlock/TagFactory.php b/src/DocBlock/TagFactory.php index 2d83b26..01b6829 100644 --- a/src/DocBlock/TagFactory.php +++ b/src/DocBlock/TagFactory.php @@ -12,7 +12,7 @@ namespace phpDocumentor\Reflection\DocBlock; -use phpDocumentor\Reflection\DocBlock\Tags\Other; +use phpDocumentor\Reflection\DocBlock\Tags\Generic; use phpDocumentor\Reflection\FqsenResolver; use phpDocumentor\Reflection\Types\Context; @@ -85,7 +85,7 @@ final class TagFactory } list($tagName, $tagBody) = $this->extractTagParts($tagLine); - $handler = Other::class; + $handler = Generic::class; if (isset($this->tagHandlerMappings[$tagName])) { $handler = $this->tagHandlerMappings[$tagName]; } elseif ($this->isAnnotation($tagName)) { diff --git a/src/DocBlock/Tags/Other.php b/src/DocBlock/Tags/Generic.php similarity index 75% rename from src/DocBlock/Tags/Other.php rename to src/DocBlock/Tags/Generic.php index 50ce765..634fdb3 100644 --- a/src/DocBlock/Tags/Other.php +++ b/src/DocBlock/Tags/Generic.php @@ -12,16 +12,16 @@ namespace phpDocumentor\Reflection\DocBlock\Tags; -use Doctrine\Instantiator\Exception\InvalidArgumentException; use phpDocumentor\Reflection\DocBlock\Description; use phpDocumentor\Reflection\DocBlock\DescriptionFactory; use phpDocumentor\Reflection\DocBlock\TagFactory; use phpDocumentor\Reflection\Types\Context; +use Webmozart\Assert\Assert; /** * Parses a tag definition for a DocBlock. */ -class Other extends BaseTag +class Generic extends BaseTag { /** * Parses a tag and populates the member variables. @@ -37,14 +37,26 @@ class Other extends BaseTag $this->description = $description; } + /** + * Creates a new tag that represents any unknown tag type. + * + * @param string $body + * @param string $name + * @param DescriptionFactory $descriptionFactory + * @param Context $context + * + * @return static + */ public static function create( $body, $name = '', DescriptionFactory $descriptionFactory = null, Context $context = null - ) - { - $description = $descriptionFactory ? $descriptionFactory->create($body, $context) : null; + ) { + Assert::string($body); + Assert::stringNotEmpty($name); + + $description = $descriptionFactory && $body ? $descriptionFactory->create($body, $context) : null; return new static($name, $description); } @@ -56,7 +68,7 @@ class Other extends BaseTag */ public function __toString() { - return "@{$this->getName()} {$this->description->render()}"; + return $this->getName() . ($this->description ? ' ' . $this->description->render() : ''); } /** diff --git a/tests/unit/DocBlock/DescriptionTest.php b/tests/unit/DocBlock/DescriptionTest.php index ece7f1a..e266f76 100644 --- a/tests/unit/DocBlock/DescriptionTest.php +++ b/tests/unit/DocBlock/DescriptionTest.php @@ -14,7 +14,7 @@ namespace phpDocumentor\Reflection\DocBlock; use Mockery as m; use phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter; -use phpDocumentor\Reflection\DocBlock\Tags\Other; +use phpDocumentor\Reflection\DocBlock\Tags\Generic; /** * @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Description @@ -32,7 +32,7 @@ class DescriptionTest extends \PHPUnit_Framework_TestCase { $body = 'This is a %1$s body.'; $expected = 'This is a {@internal significant } body.'; - $tags = [new Other('internal', new Description('significant '))]; + $tags = [new Generic('internal', new Description('significant '))]; $fixture = new Description($body, $tags); @@ -57,7 +57,7 @@ class DescriptionTest extends \PHPUnit_Framework_TestCase { $body = 'This is a %1$s body.'; $expected = 'This is a {@internal significant } body.'; - $tags = [new Other('internal', new Description('significant '))]; + $tags = [new Generic('internal', new Description('significant '))]; $fixture = new Description($body, $tags); diff --git a/tests/unit/DocBlock/SerializerTest.php b/tests/unit/DocBlock/SerializerTest.php index 2dfc420..8f6ab17 100644 --- a/tests/unit/DocBlock/SerializerTest.php +++ b/tests/unit/DocBlock/SerializerTest.php @@ -48,7 +48,7 @@ DOCCOMMENT; 'This is a summary', new Description('This is a description'), [ - new DocBlock\Tags\Other('unknown-tag', new Description('Test description for the unknown tag')) + new DocBlock\Tags\Generic('unknown-tag', new Description('Test description for the unknown tag')) ] ); @@ -82,7 +82,7 @@ DOCCOMMENT; 'This is a summary', new Description('This is a description'), [ - new DocBlock\Tags\Other('unknown-tag', new Description('Test description for the unknown tag')) + new DocBlock\Tags\Generic('unknown-tag', new Description('Test description for the unknown tag')) ] ); @@ -116,7 +116,7 @@ DOCCOMMENT; 'This is a summary', new Description('This is a description'), [ - new DocBlock\Tags\Other('unknown-tag', new Description('Test description for the unknown tag')) + new DocBlock\Tags\Generic('unknown-tag', new Description('Test description for the unknown tag')) ] ); @@ -156,7 +156,7 @@ DOCCOMMENT; 'This is a summary', new Description('This is a description'), [ - new DocBlock\Tags\Other('unknown-tag', new Description('Test description for the unknown tag')) + new DocBlock\Tags\Generic('unknown-tag', new Description('Test description for the unknown tag')) ] ); diff --git a/tests/unit/DocBlock/Tags/Formatter/PassthroughFormatterTest.php b/tests/unit/DocBlock/Tags/Formatter/PassthroughFormatterTest.php index 66edfa2..8f87b99 100644 --- a/tests/unit/DocBlock/Tags/Formatter/PassthroughFormatterTest.php +++ b/tests/unit/DocBlock/Tags/Formatter/PassthroughFormatterTest.php @@ -14,7 +14,7 @@ namespace phpDocumentor\Reflection\DocBlock\Tags\Formatter; use Mockery as m; use phpDocumentor\Reflection\DocBlock\Description; -use phpDocumentor\Reflection\DocBlock\Tags\Other; +use phpDocumentor\Reflection\DocBlock\Tags\Generic; /** * @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter @@ -35,7 +35,7 @@ class PassthroughFormatterTest extends \PHPUnit_Framework_TestCase $this->assertSame( $expected, - $fixture->format(new Other('unknown-tag', new Description('This is a description'))) + $fixture->format(new Generic('unknown-tag', new Description('This is a description'))) ); } } diff --git a/tests/unit/DocBlock/Tags/GenericTest.php b/tests/unit/DocBlock/Tags/GenericTest.php new file mode 100644 index 0000000..a844f68 --- /dev/null +++ b/tests/unit/DocBlock/Tags/GenericTest.php @@ -0,0 +1,146 @@ + + * @license http://www.opensource.org/licenses/mit-license.php MIT + * @generic http://phpdoc.org + */ + +namespace phpDocumentor\Reflection\DocBlock\Tags; + +use Mockery as m; +use phpDocumentor\Reflection\DocBlock\Description; +use phpDocumentor\Reflection\DocBlock\DescriptionFactory; +use phpDocumentor\Reflection\Types\Context; + +/** + * @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\Generic + * @covers :: + */ +class GenericTest extends \PHPUnit_Framework_TestCase +{ + /** + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Generic::__construct + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName + */ + public function testIfCorrectTagNameIsReturned() + { + $fixture = new Generic('generic', new Description('Description')); + + $this->assertSame('generic', $fixture->getName()); + } + + /** + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Generic::__construct + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Generic::__toString + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName + * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render + */ + public function testIfTagCanBeRenderedUsingDefaultFormatter() + { + $fixture = new Generic('generic', new Description('Description')); + + $this->assertSame('generic Description', $fixture->render()); + } + + /** + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Generic::__construct + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render + */ + public function testIfTagCanBeRenderedUsingSpecificFormatter() + { + $fixture = new Generic('generic', new Description('Description')); + + $formatter = m::mock(Formatter::class); + $formatter->shouldReceive('format')->with($fixture)->andReturn('Rendered output'); + + $this->assertSame('Rendered output', $fixture->render($formatter)); + } + + /** + * @covers ::__construct + * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getDescription + * @uses \phpDocumentor\Reflection\DocBlock\Description + */ + public function testHasDescription() + { + $expected = new Description('Description'); + + $fixture = new Generic('generic', $expected); + + $this->assertSame($expected, $fixture->getDescription()); + } + + /** + * @covers ::__construct + * @covers ::__toString + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName + */ + public function testStringRepresentationIsReturned() + { + $fixture = new Generic('generic', new Description('Description')); + + $this->assertSame('generic Description', (string)$fixture); + } + + /** + * @covers ::create + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Generic:: + * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\Types\Context + */ + public function testFactoryMethod() + { + $descriptionFactory = m::mock(DescriptionFactory::class); + $context = new Context(''); + + $generics = 'generic'; + $description = new Description('My Description'); + + $descriptionFactory->shouldReceive('create')->with('My Description', $context)->andReturn($description); + + $fixture = Generic::create('My Description', 'generic', $descriptionFactory, $context); + + $this->assertSame('generic My Description', (string)$fixture); + $this->assertSame($generics, $fixture->getName()); + $this->assertSame($description, $fixture->getDescription()); + } + + /** + * @covers ::create + * @expectedException \InvalidArgumentException + */ + public function testFactoryMethodFailsIfNameIsNotString() + { + Generic::create('', []); + } + + /** + * @covers ::create + * @expectedException \InvalidArgumentException + */ + public function testFactoryMethodFailsIfNameIsNotEmpty() + { + Generic::create('', ''); + } + + /** + * @covers ::create + * @covers ::__construct + * @expectedException \InvalidArgumentException + */ + public function testFactoryMethodFailsIfNameContainsIllegalCharacters() + { + Generic::create('', 'name/myname'); + } +}