Add test for DocBlock class

This commit is contained in:
Mike van Riel
2015-06-13 18:01:00 +02:00
committed by Mike van Riel
parent b87a111eb2
commit 5749b814fb
4 changed files with 160 additions and 325 deletions
+49 -56
View File
@@ -13,8 +13,7 @@
namespace phpDocumentor\Reflection; namespace phpDocumentor\Reflection;
use phpDocumentor\Reflection\DocBlock\Tag; use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Context; use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\DocBlock\Location;
final class DocBlock final class DocBlock
{ {
@@ -40,12 +39,6 @@ final class DocBlock
private $isTemplateEnd = false; private $isTemplateEnd = false;
/** /**
* Parses the given docblock and populates the member fields.
*
* The constructor may also receive namespace information such as the
* current namespace and aliases. This information is used by some tags
* (e.g. return, param, etc.) to turn a relative Type into a FQCN.
*
* @param string $summary * @param string $summary
* @param DocBlock\Description $description * @param DocBlock\Description $description
* @param DocBlock\Tag[] $tags * @param DocBlock\Tag[] $tags
@@ -77,6 +70,42 @@ final class DocBlock
$this->isTemplateStart = $isTemplateStart; $this->isTemplateStart = $isTemplateStart;
} }
/**
* @return string
*/
public function getSummary()
{
return $this->summary;
}
/**
* @return DocBlock\Description
*/
public function getDescription()
{
return $this->description;
}
/**
* Returns the current context.
*
* @return Context
*/
public function getContext()
{
return $this->context;
}
/**
* Returns the current location.
*
* @return Location
*/
public function getLocation()
{
return $this->location;
}
/** /**
* Returns whether this DocBlock is the start of a Template section. * Returns whether this DocBlock is the start of a Template section.
* *
@@ -115,54 +144,6 @@ final class DocBlock
return $this->isTemplateEnd; return $this->isTemplateEnd;
} }
/**
* Returns the current context.
*
* @return Context
*/
public function getContext()
{
return $this->context;
}
/**
* Returns the current location.
*
* @return Location
*/
public function getLocation()
{
return $this->location;
}
/**
* @return string
*/
public function getSummary()
{
return $this->summary;
}
/**
* @return DocBlock\Description
*/
public function getDescription()
{
return $this->description;
}
/**
* Adds a tag to this DocBlock.
*
* @param Tag $tag The tag to add.
*
* @return void
*/
public function addTag(Tag $tag)
{
$this->tags[] = $tag;
}
/** /**
* Returns the tags for this DocBlock. * Returns the tags for this DocBlock.
* *
@@ -215,4 +196,16 @@ final class DocBlock
return false; return false;
} }
/**
* Adds a tag to this DocBlock.
*
* @param Tag $tag The tag to add.
*
* @return void
*/
private function addTag(Tag $tag)
{
$this->tags[] = $tag;
}
} }
+3 -3
View File
@@ -63,12 +63,12 @@ final class DocBlockFactory implements DocBlockFactoryInterface
/** /**
* @param $docblock * @param $docblock
* @param DocBlock\Context $context * @param Types\Context $context
* @param DocBlock\Location $location * @param Location $location
* *
* @return DocBlock * @return DocBlock
*/ */
public function create($docblock, DocBlock\Context $context = null, DocBlock\Location $location = null) public function create($docblock, Types\Context $context = null, Location $location = null)
{ {
if (is_object($docblock)) { if (is_object($docblock)) {
if (!method_exists($docblock, 'getDocComment')) { if (!method_exists($docblock, 'getDocComment')) {
+3 -3
View File
@@ -14,10 +14,10 @@ interface DocBlockFactoryInterface
/** /**
* @param string $docblock * @param string $docblock
* @param DocBlock\Context $context * @param Types\Context $context
* @param DocBlock\Location $location * @param Location $location
* *
* @return DocBlock * @return DocBlock
*/ */
public function create($docblock, DocBlock\Context $context = null, DocBlock\Location $location = null); public function create($docblock, Types\Context $context = null, Location $location = null);
} }
+107 -265
View File
@@ -1,337 +1,179 @@
<?php <?php
/** /**
* phpDocumentor DocBlock Test * This file is part of phpDocumentor.
* *
* PHP Version 5.3 * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* *
* @author Mike van Riel <mike[email protected]> * @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @copyright 2010-2011 Mike van Riel / Naenius. (http://www.naenius.com)
* @license http://www.opensource.org/licenses/mit-license.php MIT * @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org * @link http://phpdoc.org
*/ */
namespace phpDocumentor\Reflection; namespace phpDocumentor\Reflection;
use phpDocumentor\Reflection\DocBlock\Context; use Mockery as m;
use phpDocumentor\Reflection\DocBlock\Location; use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\DocBlock\Tag\Return_;
/** /**
* Test class for phpDocumentor\Reflection\DocBlock * @coversDefaultClass phpDocumentor\Reflection\DocBlock
* * @covers ::<private>
* @author Mike van Riel <[email protected]>
* @copyright 2010-2011 Mike van Riel / Naenius. (http://www.naenius.com)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/ */
class DocBlockTest extends \PHPUnit_Framework_TestCase class DocBlockTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @covers \phpDocumentor\Reflection\DocBlock * @covers ::__construct
* @covers ::getSummary
* *
* @return void * @uses \phpDocumentor\Reflection\DocBlock\Description
*/ */
public function testConstruct() public function testDocBlockCanHaveASummary()
{ {
$fixture = <<<DOCBLOCK $summary = 'This is a summary';
/**
* This is a short description
*
* This is a long description
*
* @see \MyClass
* @return void
*/
DOCBLOCK;
$object = new DocBlock(
$fixture,
new Context('\MyNamespace', array('PHPDoc' => '\phpDocumentor')),
new Location(2)
);
$this->assertEquals(
'This is a short description',
$object->getShortDescription()
);
$this->assertEquals(
'This is a long description',
$object->getLongDescription()->getContents()
);
$this->assertCount(2, $object->getTags());
$this->assertTrue($object->hasTag('see'));
$this->assertTrue($object->hasTag('return'));
$this->assertFalse($object->hasTag('category'));
$this->assertSame('MyNamespace', $object->getContext()->getNamespace()); $fixture = new DocBlock($summary);
$this->assertSame(
array('PHPDoc' => '\phpDocumentor'), $this->assertSame($summary, $fixture->getSummary());
$object->getContext()->getNamespaceAliases()
);
$this->assertSame(2, $object->getLocation()->getLineNumber());
} }
/** /**
* @covers \phpDocumentor\Reflection\DocBlock::splitDocBlock * @covers ::__construct
* @covers ::getDescription
* *
* @return void * @uses \phpDocumentor\Reflection\DocBlock\Description
*/ */
public function testConstructWithTagsOnly() public function testDocBlockCanHaveADescription()
{ {
$fixture = <<<DOCBLOCK $description = new DocBlock\Description('');
/**
* @see \MyClass $fixture = new DocBlock('', $description);
* @return void
*/ $this->assertSame($description, $fixture->getDescription());
DOCBLOCK;
$object = new DocBlock($fixture);
$this->assertEquals('', $object->getShortDescription());
$this->assertEquals('', $object->getLongDescription()->getContents());
$this->assertCount(2, $object->getTags());
$this->assertTrue($object->hasTag('see'));
$this->assertTrue($object->hasTag('return'));
$this->assertFalse($object->hasTag('category'));
} }
/** /**
* @covers \phpDocumentor\Reflection\DocBlock::isTemplateStart * @covers ::__construct
*/ * @covers ::getTags
public function testIfStartOfTemplateIsDiscovered()
{
$fixture = <<<DOCBLOCK
/**#@+
* @see \MyClass
* @return void
*/
DOCBLOCK;
$object = new DocBlock($fixture);
$this->assertEquals('', $object->getShortDescription());
$this->assertEquals('', $object->getLongDescription()->getContents());
$this->assertCount(2, $object->getTags());
$this->assertTrue($object->hasTag('see'));
$this->assertTrue($object->hasTag('return'));
$this->assertFalse($object->hasTag('category'));
$this->assertTrue($object->isTemplateStart());
}
/**
* @covers \phpDocumentor\Reflection\DocBlock::isTemplateEnd
*/
public function testIfEndOfTemplateIsDiscovered()
{
$fixture = <<<DOCBLOCK
/**#@-*/
DOCBLOCK;
$object = new DocBlock($fixture);
$this->assertEquals('', $object->getShortDescription());
$this->assertEquals('', $object->getLongDescription()->getContents());
$this->assertTrue($object->isTemplateEnd());
}
/**
* @covers \phpDocumentor\Reflection\DocBlock::cleanInput
* *
* @return void * @uses \phpDocumentor\Reflection\DocBlock\Description
* @uses \phpDocumentor\Reflection\DocBlock\Tag
*/ */
public function testConstructOneLiner() public function testDocBlockCanHaveTags()
{ {
$fixture = '/** Short description and nothing more. */'; $tags = [
$object = new DocBlock($fixture); m::mock(DocBlock\Tag::class)
$this->assertEquals( ];
'Short description and nothing more.',
$object->getShortDescription() $fixture = new DocBlock('', null, $tags);
);
$this->assertEquals('', $object->getLongDescription()->getContents()); $this->assertSame($tags, $fixture->getTags());
$this->assertCount(0, $object->getTags());
} }
/** /**
* @covers \phpDocumentor\Reflection\DocBlock::__construct * @covers ::__construct
* @covers ::getTagsByName
* *
* @return void * @uses \phpDocumentor\Reflection\DocBlock::getTags
* @uses \phpDocumentor\Reflection\DocBlock\Description
* @uses \phpDocumentor\Reflection\DocBlock\Tag
*/ */
public function testConstructFromReflector() public function testFindTagsInDocBlockByName()
{ {
$object = new DocBlock(new \ReflectionClass($this)); $tag1 = m::mock(DocBlock\Tag::class);
$this->assertEquals( $tag2 = m::mock(DocBlock\Tag::class);
'Test class for phpDocumentor\Reflection\DocBlock', $tag3 = m::mock(DocBlock\Tag::class);
$object->getShortDescription() $tags = [$tag1, $tag2, $tag3];
);
$this->assertEquals('', $object->getLongDescription()->getContents()); $tag1->shouldReceive('getName')->andReturn('abc');
$this->assertCount(4, $object->getTags()); $tag2->shouldReceive('getName')->andReturn('abcd');
$this->assertTrue($object->hasTag('author')); $tag3->shouldReceive('getName')->never();
$this->assertTrue($object->hasTag('copyright'));
$this->assertTrue($object->hasTag('license')); $fixture = new DocBlock('', null, $tags);
$this->assertTrue($object->hasTag('link'));
$this->assertFalse($object->hasTag('category')); $this->assertSame([$tag2], $fixture->getTagsByName('abcd'));
$this->assertSame([], $fixture->getTagsByName('Ebcd'));
} }
/** /**
* @expectedException \InvalidArgumentException * @covers ::__construct
* @covers ::hasTag
* *
* @return void * @uses \phpDocumentor\Reflection\DocBlock::getTags
* @uses \phpDocumentor\Reflection\DocBlock\Description
* @uses \phpDocumentor\Reflection\DocBlock\Tag
*/ */
public function testExceptionOnInvalidObject() public function testCheckIfThereAreTagsWithAGivenName()
{ {
new DocBlock($this); $tag1 = m::mock(DocBlock\Tag::class);
} $tag2 = m::mock(DocBlock\Tag::class);
$tag3 = m::mock(DocBlock\Tag::class);
$tags = [$tag1, $tag2, $tag3];
public function testDotSeperation() $tag1->shouldReceive('getName')->andReturn('abc');
{ $tag2->shouldReceive('getName')->andReturn('abcd');
$fixture = <<<DOCBLOCK $tag3->shouldReceive('getName')->never();
/**
* This is a short description. $fixture = new DocBlock('', null, $tags);
* This is a long description.
* This is a continuation of the long description. $this->assertTrue($fixture->hasTag('abcd'));
*/ $this->assertFalse($fixture->hasTag('Ebcd'));
DOCBLOCK;
$object = new DocBlock($fixture);
$this->assertEquals(
'This is a short description.',
$object->getShortDescription()
);
$this->assertEquals(
"This is a long description.\nThis is a continuation of the long "
."description.",
$object->getLongDescription()->getContents()
);
} }
/** /**
* @covers \phpDocumentor\Reflection\DocBlock::parseTags * @covers ::__construct
* @expectedException \LogicException * @covers ::getContext
* *
* @return void * @uses \phpDocumentor\Reflection\DocBlock\Description
* @uses \phpDocumentor\Reflection\Types\Context
*/ */
public function testInvalidTagBlock() public function testDocBlockKnowsInWhichNamespaceItIsAndWhichAliasesThereAre()
{ {
if (0 == ini_get('allow_url_include')) { $context = new Context('');
$this->markTestSkipped('"data" URIs for includes are required.');
$fixture = new DocBlock('', null, [], $context);
$this->assertSame($context, $fixture->getContext());
} }
include 'data:text/plain;base64,'. base64_encode(
<<<DOCBLOCK_EXTENSION
<?php
class MyReflectionDocBlock extends \phpDocumentor\Reflection\DocBlock {
protected function splitDocBlock(\$comment) {
return array('', '', 'Invalid tag block');
}
}
DOCBLOCK_EXTENSION
);
new \MyReflectionDocBlock('');
}
public function testTagCaseSensitivity()
{
$fixture = <<<DOCBLOCK
/**
* This is a short description.
*
* This is a long description.
*
* @method null something()
* @Method({"GET", "POST"})
*/
DOCBLOCK;
$object = new DocBlock($fixture);
$this->assertEquals(
'This is a short description.',
$object->getShortDescription()
);
$this->assertEquals(
'This is a long description.',
$object->getLongDescription()->getContents()
);
$tags = $object->getTags();
$this->assertCount(2, $tags);
$this->assertTrue($object->hasTag('method'));
$this->assertTrue($object->hasTag('Method'));
$this->assertInstanceOf(
__NAMESPACE__ . '\DocBlock\Tag\MethodTag',
$tags[0]
);
$this->assertInstanceOf(
__NAMESPACE__ . '\DocBlock\Tag',
$tags[1]
);
$this->assertNotInstanceOf(
__NAMESPACE__ . '\DocBlock\Tag\MethodTag',
$tags[1]
);
}
/** /**
* @depends testConstructFromReflector * @covers ::__construct
* @covers \phpDocumentor\Reflection\DocBlock::getTagsByName * @covers ::getLocation
* *
* @return void * @uses \phpDocumentor\Reflection\DocBlock\Description
* @uses \phpDocumentor\Reflection\Location
*/ */
public function testGetTagsByNameZeroAndOneMatch() public function testDocBlockKnowsAtWhichLineItIs()
{ {
$object = new DocBlock(new \ReflectionClass($this)); $location = new Location(10);
$this->assertEmpty($object->getTagsByName('category'));
$this->assertCount(1, $object->getTagsByName('author'));
}
/** $fixture = new DocBlock('', null, [], null, $location);
* @depends testConstructWithTagsOnly
* @covers \phpDocumentor\Reflection\DocBlock::parseTags $this->assertSame($location, $fixture->getLocation());
*
* @return void
*/
public function testParseMultilineTag()
{
$fixture = <<<DOCBLOCK
/**
* @return void Content on
* multiple lines.
*/
DOCBLOCK;
$object = new DocBlock($fixture);
$this->assertCount(1, $object->getTags());
} }
/** /**
* @depends testConstructWithTagsOnly * @covers ::__construct
* @covers \phpDocumentor\Reflection\DocBlock::parseTags * @covers ::isTemplateStart
* *
* @return void * @uses \phpDocumentor\Reflection\DocBlock\Description
*/ */
public function testParseMultilineTagWithLineBreaks() public function testDocBlockKnowsIfItIsTheStartOfADocBlockTemplate()
{ {
$fixture = <<<DOCBLOCK $fixture = new DocBlock('', null, [], null, null, true);
/**
* @return void Content on $this->assertTrue($fixture->isTemplateStart());
* multiple lines.
*
* One more, after the break.
*/
DOCBLOCK;
$object = new DocBlock($fixture);
$this->assertCount(1, $tags = $object->getTags());
/** @var Return_ $tag */
$tag = reset($tags);
$this->assertEquals("Content on\n multiple lines.\n\n One more, after the break.", $tag->getDescription());
} }
/** /**
* @depends testConstructWithTagsOnly * @covers ::__construct
* @covers \phpDocumentor\Reflection\DocBlock::getTagsByName * @covers ::isTemplateEnd
* *
* @return void * @uses \phpDocumentor\Reflection\DocBlock\Description
*/ */
public function testGetTagsByNameMultipleMatch() public function testDocBlockKnowsIfItIsTheEndOfADocBlockTemplate()
{ {
$fixture = <<<DOCBLOCK $fixture = new DocBlock('', null, [], null, null, false, true);
/**
* @param string $this->assertTrue($fixture->isTemplateEnd());
* @param int
* @return void
*/
DOCBLOCK;
$object = new DocBlock($fixture);
$this->assertEmpty($object->getTagsByName('category'));
$this->assertCount(1, $object->getTagsByName('return'));
$this->assertCount(2, $object->getTagsByName('param'));
} }
} }