Removed DocBlock::expandType() and associated tests, as previously advised by @mvriel;

Added tests for generic Tag objects, and a few others;
Increased total coverage by more appropriate use of @covers annotations.
This commit is contained in:
Vasil Rangelov
2012-11-10 23:19:36 +02:00
parent 6e1bb192b3
commit f11936eaae
13 changed files with 231 additions and 182 deletions
+1 -80
View File
@@ -66,7 +66,7 @@ class DocBlock implements \Reflector
public function __construct( public function __construct(
$docblock, $docblock,
$namespace = '\\', $namespace = '\\',
$namespace_aliases = array() array $namespace_aliases = array()
) { ) {
if (is_object($docblock)) { if (is_object($docblock)) {
if (!method_exists($docblock, 'getDocComment')) { if (!method_exists($docblock, 'getDocComment')) {
@@ -304,85 +304,6 @@ class DocBlock implements \Reflector
return false; return false;
} }
/**
* Tries to expand a type to it's full namespaced equivalent (FQCN).
*
* This method will take the given type and examine the current namespace
* and namespace aliases to see whether it should expand it into a FQCN
* as defined by the rules in PHP.
*
* @param string $type Type to expand into full namespaced
* equivalent.
* @param string[] $ignore_keywords Whether to ignore given keywords, when
* null it will use the default keywords:
* 'string', 'int', 'integer', 'bool', 'boolean', 'float', 'double',
* 'object', 'mixed', 'array', 'resource', 'void', 'null', 'callback',
* 'false', 'true', 'self', '$this', 'callable'.
* Default value for this parameter is null.
*
* @return string
*/
public function expandType($type, $ignore_keywords = null)
{
if ($type === null) {
return null;
}
if ($ignore_keywords === null) {
$ignore_keywords = array(
'string', 'int', 'integer', 'bool', 'boolean', 'float',
'double', 'object', 'mixed', 'array', 'resource', 'void',
'null', 'callback', 'false', 'true', 'self', '$this', 'callable'
);
}
$namespace = '\\';
if ($this->namespace != 'default' && $this->namespace != 'global') {
$namespace = rtrim($this->namespace, '\\') . '\\';
}
$type = explode('|', $type);
foreach ($type as &$item) {
$item = trim($item);
// add support for array notation
$is_array = false;
if (substr($item, -2) == '[]') {
$item = substr($item, 0, -2);
$is_array = true;
}
if ((substr($item, 0, 1) != '\\')
&& (!in_array(strtolower($item), $ignore_keywords))
) {
$type_parts = explode('\\', $item);
// if the first segment is an alias; replace with full name
if (isset($this->namespace_aliases[$type_parts[0]])) {
$type_parts[0] = $this->namespace_aliases[$type_parts[0]];
$item = implode('\\', $type_parts);
} else {
// otherwise prepend the current namespace
$item = $namespace . $item;
}
}
// full paths always start with a slash
if (isset($item[0]) && ($item[0] !== '\\')
&& (!in_array(strtolower($item), $ignore_keywords))
) {
$item = '\\' . $item;
}
// re-add the array notation markers
if ($is_array) {
$item .= '[]';
}
}
return implode('|', $type);
}
/** /**
* Builds a string representation of this object. * Builds a string representation of this object.
* *
@@ -121,6 +121,9 @@ class LongDescription implements \Reflector
* @todo this should become a more intelligent piece of code where the * @todo this should become a more intelligent piece of code where the
* configuration contains a setting what format long descriptions are. * configuration contains a setting what format long descriptions are.
* *
* @codeCoverageIgnore Will be removed soon, in favor of adapters at
* PhpDocumentor itself that will process text in various formats.
*
* @return string * @return string
*/ */
public function getFormattedContents() public function getFormattedContents()
@@ -28,7 +28,6 @@ class CoversTagTest extends \PHPUnit_Framework_TestCase
* *
* @param string $type * @param string $type
* @param string $content * @param string $content
* @param string $exName
* @param string $exContent * @param string $exContent
* @param string $exReference * @param string $exReference
* *
@@ -59,7 +58,7 @@ class CoversTagTest extends \PHPUnit_Framework_TestCase
*/ */
public function provideDataForConstuctor() public function provideDataForConstuctor()
{ {
// $type, $content, $exName, $exContent, $exDescription, $exReference // $type, $content, $exContent, $exDescription, $exReference
return array( return array(
array( array(
'covers', 'covers',
@@ -28,7 +28,6 @@ class LinkTagTest extends \PHPUnit_Framework_TestCase
* *
* @param string $type * @param string $type
* @param string $content * @param string $content
* @param string $exName
* @param string $exContent * @param string $exContent
* @param string $exDescription * @param string $exDescription
* @param string $exLink * @param string $exLink
@@ -61,7 +60,7 @@ class LinkTagTest extends \PHPUnit_Framework_TestCase
*/ */
public function provideDataForConstuctor() public function provideDataForConstuctor()
{ {
// $type, $content, $exName, $exContent, $exDescription, $exLink // $type, $content, $exContent, $exDescription, $exLink
return array( return array(
array( array(
'link', 'link',
@@ -23,19 +23,20 @@ namespace phpDocumentor\Reflection\DocBlock\Tag;
class MethodTagTest extends \PHPUnit_Framework_TestCase class MethodTagTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @param string $signature The signature to test * @param string $signature The signature to test.
* @param bool $valid Whether the given signature is expected to * @param bool $valid Whether the given signature is expected to
* be valid. * be valid.
* @param string $expected_name The method name that is expected from this * @param string $expected_name The method name that is expected from this
* signature * signature.
* @param string $expected_return The return type that is expected from this * @param string $expected_return The return type that is expected from this
* signature * signature.
* @param bool $has_params whether this signature features parameters. * @param bool $paramCount Number of parameters in the signature.
* @param string $description The short description mentioned in the * @param string $description The short description mentioned in the
* signature. * signature.
* *
* @covers \phpDocumentor\Reflection\DocBlock\Tag\MethodTag::__construct * @covers \phpDocumentor\Reflection\DocBlock\Tag\MethodTag::__construct
* @covers \phpDocumentor\Reflection\DocBlock\Tag\MethodTag::getMethodName * @covers \phpDocumentor\Reflection\DocBlock\Tag\MethodTag::getMethodName
* @covers \phpDocumentor\Reflection\DocBlock\Tag\MethodTag::getArguments
* *
* @dataProvider getTestSignatures * @dataProvider getTestSignatures
* *
@@ -46,7 +47,7 @@ class MethodTagTest extends \PHPUnit_Framework_TestCase
$valid, $valid,
$expected_name, $expected_name,
$expected_return, $expected_return,
$has_params, $paramCount,
$description $description
) { ) {
ob_start(); ob_start();
@@ -66,11 +67,7 @@ class MethodTagTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected_name, $tag->getMethodName()); $this->assertEquals($expected_name, $tag->getMethodName());
$this->assertEquals($expected_return, $tag->getType()); $this->assertEquals($expected_return, $tag->getType());
$this->assertEquals($description, $tag->getDescription()); $this->assertEquals($description, $tag->getDescription());
$this->assertSame( $this->assertCount($paramCount, $tag->getArguments());
$has_params,
(bool)(count($tag->getArguments()) > 0),
'Number of found arguments should exceed 0'
);
} }
public function getTestSignatures() public function getTestSignatures()
@@ -78,55 +75,55 @@ class MethodTagTest extends \PHPUnit_Framework_TestCase
return array( return array(
array( array(
'foo', 'foo',
false, 'foo', '', false, '' false, 'foo', '', 0, ''
), ),
array( array(
'foo()', 'foo()',
true, 'foo', 'void', false, '' true, 'foo', 'void', 0, ''
), ),
array( array(
'foo() description', 'foo() description',
true, 'foo', 'void', false, 'description' true, 'foo', 'void', 0, 'description'
), ),
array( array(
'int foo()', 'int foo()',
true, 'foo', 'int', false, '' true, 'foo', 'int', 0, ''
), ),
array( array(
'int foo() description', 'int foo() description',
true, 'foo', 'int', false, 'description' true, 'foo', 'int', 0, 'description'
), ),
array( array(
'int foo($a, $b)', 'int foo($a, $b)',
true, 'foo', 'int', true, '' true, 'foo', 'int', 2, ''
), ),
array( array(
'int foo() foo(int $a, int $b)', 'int foo() foo(int $a, int $b)',
true, 'foo', 'int', true, '' true, 'foo', 'int', 2, ''
), ),
array( array(
'int foo(int $a, int $b)', 'int foo(int $a, int $b)',
true, 'foo', 'int', true, '' true, 'foo', 'int', 2, ''
), ),
array( array(
'null|int foo(int $a, int $b)', 'null|int foo(int $a, int $b)',
true, 'foo', 'null|int', true, '' true, 'foo', 'null|int', 2, ''
), ),
array( array(
'int foo(null|int $a, int $b)', 'int foo(null|int $a, int $b)',
true, 'foo', 'int', true, '' true, 'foo', 'int', 2, ''
), ),
array( array(
'\Exception foo() foo(Exception $a, Exception $b)', '\Exception foo() foo(Exception $a, Exception $b)',
true, 'foo', '\Exception', true, '' true, 'foo', '\Exception', 2, ''
), ),
array( array(
'int foo() foo(Exception $a, Exception $b) description', 'int foo() foo(Exception $a, Exception $b) description',
true, 'foo', 'int', true, 'description' true, 'foo', 'int', 2, 'description'
), ),
array( array(
'int foo() foo(\Exception $a, \Exception $b) description', 'int foo() foo(\Exception $a, \Exception $b) description',
true, 'foo', 'int', true, 'description' true, 'foo', 'int', 2, 'description'
), ),
); );
} }
@@ -29,6 +29,7 @@ class ParamTagTest extends \PHPUnit_Framework_TestCase
* @param string $type * @param string $type
* @param string $content * @param string $content
* @param string $extractedType * @param string $extractedType
* @param string $extractedTypes
* @param string $extractedVarName * @param string $extractedVarName
* @param string $extractedDescription * @param string $extractedDescription
* *
@@ -20,7 +20,7 @@ namespace phpDocumentor\Reflection\DocBlock\Tag;
* @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
*/ */
class ReturnTagTest extends ParamTagTest class ReturnTagTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* Test that the \phpDocumentor\Reflection\DocBlock\Tag\ReturnTag can * Test that the \phpDocumentor\Reflection\DocBlock\Tag\ReturnTag can
@@ -28,6 +28,7 @@ class ReturnTagTest extends ParamTagTest
* *
* @param string $content * @param string $content
* @param string $extractedType * @param string $extractedType
* @param string $extractedTypes
* @param string $extractedDescription * @param string $extractedDescription
* *
* @covers \phpDocumentor\Reflection\DocBlock\Tag\ReturnTag::__construct * @covers \phpDocumentor\Reflection\DocBlock\Tag\ReturnTag::__construct
@@ -28,7 +28,6 @@ class SeeTagTest extends \PHPUnit_Framework_TestCase
* *
* @param string $type * @param string $type
* @param string $content * @param string $content
* @param string $exName
* @param string $exContent * @param string $exContent
* @param string $exReference * @param string $exReference
* *
@@ -28,7 +28,6 @@ class UsesTagTest extends \PHPUnit_Framework_TestCase
* *
* @param string $type * @param string $type
* @param string $content * @param string $content
* @param string $exName
* @param string $exContent * @param string $exContent
* @param string $exReference * @param string $exReference
* *
@@ -59,7 +58,7 @@ class UsesTagTest extends \PHPUnit_Framework_TestCase
*/ */
public function provideDataForConstuctor() public function provideDataForConstuctor()
{ {
// $type, $content, $exName, $exContent, $exDescription, $exReference // $type, $content, $exContent, $exDescription, $exReference
return array( return array(
array( array(
'uses', 'uses',
@@ -59,7 +59,7 @@ class VarTagTest extends \PHPUnit_Framework_TestCase
*/ */
public function provideDataForConstuctor() public function provideDataForConstuctor()
{ {
// $type, $content // $type, $content, $exType, $exVariable, $exDescription
return array( return array(
array( array(
'var', 'var',
@@ -0,0 +1,86 @@
<?php
/**
* phpDocumentor Var Tag Test
*
* PHP version 5.3
*
* @author Daniel O'Connor <[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
*/
namespace phpDocumentor\Reflection\DocBlock;
/**
* Test class for \phpDocumentor\Reflection\DocBlock\Tag\VarTag
*
* @author Daniel O'Connor <[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 TagTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidTagLine()
{
Tag::createInstance('Invalid tag line');
}
/**
* Test that the \phpDocumentor\Reflection\DocBlock\Tag\VarTag can
* understand the @var doc block.
*
* @param string $type
* @param string $content
* @param string $exDescription
*
* @covers \phpDocumentor\Reflection\DocBlock\Tag::__construct
* @covers \phpDocumentor\Reflection\DocBlock\Tag::getDescription
* @covers \phpDocumentor\Reflection\DocBlock\Tag::getContent
* @dataProvider provideDataForConstuctor
*
* @return void
*/
public function testConstructorParesInputsIntoCorrectFields(
$type,
$content,
$exDescription
) {
$tag = new Tag($type, $content);
$this->assertEquals($type, $tag->getName());
$this->assertEquals($content, $tag->getContent());
$this->assertEquals($exDescription, $tag->getDescription());
}
/**
* Data provider for testConstructorParesInputsIntoCorrectFields
*
* @return array
*/
public function provideDataForConstuctor()
{
// $type, $content, $exDescription
return array(
array(
'unknown',
'some content',
'some content',
),
array(
'unknown',
'',
'',
),
array(
'',
'unknown',
'unknown',
)
);
}
}
@@ -39,6 +39,23 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
$this->assertCount(0, $collection->getNamespaceAliases()); $this->assertCount(0, $collection->getNamespaceAliases());
} }
/**
* @covers phpDocumentor\Reflection\DocBlock\Type\Collection::__construct
* @covers phpDocumentor\Reflection\DocBlock\Type\Collection::setNamespace
* @covers phpDocumentor\Reflection\DocBlock\Type\Collection::getNamespace
* @covers phpDocumentor\Reflection\DocBlock\Type\Collection::getNamespaceAliases
*
* @return void
*/
public function testGlobalIgnore()
{
$collection = new Collection();
$collection->setNamespace('global');
$this->assertCount(0, $collection);
$this->assertEquals('\\', $collection->getNamespace());
$this->assertCount(0, $collection->getNamespaceAliases());
}
/** /**
* @covers phpDocumentor\Reflection\DocBlock\Type\Collection::__construct * @covers phpDocumentor\Reflection\DocBlock\Type\Collection::__construct
* *
+97 -70
View File
@@ -34,7 +34,11 @@ class DocBlockTest extends \PHPUnit_Framework_TestCase
* @return void * @return void
*/ */
DOCBLOCK; DOCBLOCK;
$object = new DocBlock($fixture); $object = new DocBlock(
$fixture,
'\MyNamespace',
array('PHPDoc' => '\phpDocumentor')
);
$this->assertEquals( $this->assertEquals(
'This is a short description.', 'This is a short description.',
$object->getShortDescription() $object->getShortDescription()
@@ -43,10 +47,55 @@ DOCBLOCK;
'This is a long description.', 'This is a long description.',
$object->getLongDescription()->getContents() $object->getLongDescription()->getContents()
); );
$this->assertEquals(2, count($object->getTags())); $this->assertCount(2, $object->getTags());
$this->assertTrue($object->hasTag('see')); $this->assertTrue($object->hasTag('see'));
$this->assertTrue($object->hasTag('return')); $this->assertTrue($object->hasTag('return'));
$this->assertFalse($object->hasTag('category')); $this->assertFalse($object->hasTag('category'));
$this->assertSame('\MyNamespace', $object->getNamespace());
$this->assertSame(
array('PHPDoc' => '\phpDocumentor'),
$object->getNamespaceAliases()
);
}
/**
* @covers \phpDocumentor\Reflection\DocBlock::splitDocBlock
*
* @return void
*/
public function testConstructWithTagsOnly()
{
$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'));
}
/**
* @covers \phpDocumentor\Reflection\DocBlock::cleanInput
*
* @return void
*/
public function testConstructOneLiner()
{
$fixture = '/** Short description and nothing more. */';
$object = new DocBlock($fixture);
$this->assertEquals(
'Short description and nothing more.',
$object->getShortDescription()
);
$this->assertEquals('', $object->getLongDescription()->getContents());
$this->assertCount(0, $object->getTags());
} }
/** /**
@@ -60,7 +109,7 @@ DOCBLOCK;
$object->getShortDescription() $object->getShortDescription()
); );
$this->assertEquals('', $object->getLongDescription()->getContents()); $this->assertEquals('', $object->getLongDescription()->getContents());
$this->assertEquals(4, count($object->getTags())); $this->assertCount(4, $object->getTags());
$this->assertTrue($object->hasTag('author')); $this->assertTrue($object->hasTag('author'));
$this->assertTrue($object->hasTag('copyright')); $this->assertTrue($object->hasTag('copyright'));
$this->assertTrue($object->hasTag('license')); $this->assertTrue($object->hasTag('license'));
@@ -118,7 +167,7 @@ DOCBLOCK;
$object->getLongDescription()->getContents() $object->getLongDescription()->getContents()
); );
$tags = $object->getTags(); $tags = $object->getTags();
$this->assertEquals(2, count($tags)); $this->assertCount(2, $tags);
$this->assertTrue($object->hasTag('method')); $this->assertTrue($object->hasTag('method'));
$this->assertTrue($object->hasTag('Method')); $this->assertTrue($object->hasTag('Method'));
$this->assertInstanceOf( $this->assertInstanceOf(
@@ -136,88 +185,66 @@ DOCBLOCK;
} }
/** /**
* Tests whether a type is expanded with the given namespace and that a * @depends testConstructFromReflector
* keyword is not expanded. * @covers \phpDocumentor\Reflection\DocBlock::getTagsByName
*
* @covers \phpDocumentor\Reflection\DocBlock::expandType()
*
* @return void
*/ */
public function testExpandTypeUsingNamespace() public function testGetTagsByNameZeroAndOneMatch()
{ {
$docblock = new DocBlock('', '\My\Namespace'); $object = new DocBlock(new \ReflectionClass($this));
$this->assertEquals('\My\Namespace\Mine', $docblock->expandType('Mine')); $this->assertEmpty($object->getTagsByName('category'));
$this->assertCount(1, $object->getTagsByName('author'));
} }
/** /**
* Tests whether a type is expanded when no namespace is given. * @depends testConstructWithTagsOnly
* * @covers \phpDocumentor\Reflection\DocBlock::parseTags
* @covers \phpDocumentor\Reflection\DocBlock::expandType()
*
* @return void
*/ */
public function testExpandTypeWithoutNamespace() public function testParseMultilineTag()
{ {
$docblock = new DocBlock(''); $fixture = <<<DOCBLOCK
$this->assertEquals('\Mine', $docblock->expandType('Mine')); /**
* @return void Content on
* multiple lines.
*/
DOCBLOCK;
$object = new DocBlock($fixture);
$this->assertCount(1, $object->getTags());
} }
/** /**
* Tests whether a type is expanded with the given namespace when an alias * @depends testConstructWithTagsOnly
* is provided. * @covers \phpDocumentor\Reflection\DocBlock::parseTags
*
* @covers \phpDocumentor\Reflection\DocBlock::expandType()
*
* @return void
*/ */
public function testExpandTypeUsingNamespaceAlias() public function testParseMultilineTagWithLineBreaks()
{ {
$docblock = new DocBlock( $fixture = <<<DOCBLOCK
'', /**
'\My\Namespace', * @return void Content on
array('Alias' => '\My\Namespace\Alias') * multiple lines.
); *
* One more, after the break.
// first try a normal resolution without alias */
$this->assertEquals( DOCBLOCK;
'\My\Namespace\Al', $object = new DocBlock($fixture);
$docblock->expandType('Al') $this->assertCount(1, $object->getTags());
);
// try to use the alias
$this->assertEquals(
'\My\Namespace\Alias\Al',
$docblock->expandType('Alias\Al')
);
} }
/** /**
* Tests whether the keywords that should not be converted are not converted. * @depends testConstructWithTagsOnly
* * @covers \phpDocumentor\Reflection\DocBlock::getTagsByName
* @param string $keyword The keyword that is to be tested; this is provided
* by the dataprovider.
*
* @covers \phpDocumentor\Reflection\DocBlock::expandType()
*
* @dataProvider getNonExpandableKeywordsForExpandType
*
* @return void
*/ */
public function testThatExpandTypeDoesNotExpandAllKeywords($keyword) public function testGetTagsByNameMultipleMatch()
{ {
$docblock = new DocBlock('', '\My\Namespace'); $fixture = <<<DOCBLOCK
$this->assertSame($keyword, $docblock->expandType($keyword)); /**
} * @param string
* @param int
public function getNonExpandableKeywordsForExpandType() * @return void
{ */
return array( DOCBLOCK;
array(null), $object = new DocBlock($fixture);
array('string'), array('int'), array('integer'), array('bool'), $this->assertEmpty($object->getTagsByName('category'));
array('boolean'), array('float'), array('double'), array('object'), $this->assertCount(1, $object->getTagsByName('return'));
array('mixed'), array('array'), array('resource'), array('void'), $this->assertCount(2, $object->getTagsByName('param'));
array('null'), array('callback'), array('false'), array('true'),
array('self'), array('$this'), array('callable')
);
} }
} }