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 e6d2ec7c54
commit a868c60f8e
13 changed files with 231 additions and 182 deletions
+1 -80
View File
@@ -66,7 +66,7 @@ class DocBlock implements \Reflector
public function __construct(
$docblock,
$namespace = '\\',
$namespace_aliases = array()
array $namespace_aliases = array()
) {
if (is_object($docblock)) {
if (!method_exists($docblock, 'getDocComment')) {
@@ -304,85 +304,6 @@ class DocBlock implements \Reflector
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.
*
@@ -120,6 +120,9 @@ class LongDescription implements \Reflector
*
* @todo this should become a more intelligent piece of code where the
* 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
*/
@@ -28,7 +28,6 @@ class CoversTagTest extends \PHPUnit_Framework_TestCase
*
* @param string $type
* @param string $content
* @param string $exName
* @param string $exContent
* @param string $exReference
*
@@ -59,7 +58,7 @@ class CoversTagTest extends \PHPUnit_Framework_TestCase
*/
public function provideDataForConstuctor()
{
// $type, $content, $exName, $exContent, $exDescription, $exReference
// $type, $content, $exContent, $exDescription, $exReference
return array(
array(
'covers',
@@ -28,7 +28,6 @@ class LinkTagTest extends \PHPUnit_Framework_TestCase
*
* @param string $type
* @param string $content
* @param string $exName
* @param string $exContent
* @param string $exDescription
* @param string $exLink
@@ -61,7 +60,7 @@ class LinkTagTest extends \PHPUnit_Framework_TestCase
*/
public function provideDataForConstuctor()
{
// $type, $content, $exName, $exContent, $exDescription, $exLink
// $type, $content, $exContent, $exDescription, $exLink
return array(
array(
'link',
@@ -23,19 +23,20 @@ namespace phpDocumentor\Reflection\DocBlock\Tag;
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
* be valid.
* @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
* signature
* @param bool $has_params whether this signature features parameters.
* signature.
* @param bool $paramCount Number of parameters in the signature.
* @param string $description The short description mentioned in the
* signature.
*
* @covers \phpDocumentor\Reflection\DocBlock\Tag\MethodTag::__construct
* @covers \phpDocumentor\Reflection\DocBlock\Tag\MethodTag::getMethodName
* @covers \phpDocumentor\Reflection\DocBlock\Tag\MethodTag::getArguments
*
* @dataProvider getTestSignatures
*
@@ -46,7 +47,7 @@ class MethodTagTest extends \PHPUnit_Framework_TestCase
$valid,
$expected_name,
$expected_return,
$has_params,
$paramCount,
$description
) {
ob_start();
@@ -66,11 +67,7 @@ class MethodTagTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected_name, $tag->getMethodName());
$this->assertEquals($expected_return, $tag->getType());
$this->assertEquals($description, $tag->getDescription());
$this->assertSame(
$has_params,
(bool)(count($tag->getArguments()) > 0),
'Number of found arguments should exceed 0'
);
$this->assertCount($paramCount, $tag->getArguments());
}
public function getTestSignatures()
@@ -78,55 +75,55 @@ class MethodTagTest extends \PHPUnit_Framework_TestCase
return array(
array(
'foo',
false, 'foo', '', false, ''
false, 'foo', '', 0, ''
),
array(
'foo()',
true, 'foo', 'void', false, ''
true, 'foo', 'void', 0, ''
),
array(
'foo() description',
true, 'foo', 'void', false, 'description'
true, 'foo', 'void', 0, 'description'
),
array(
'int foo()',
true, 'foo', 'int', false, ''
true, 'foo', 'int', 0, ''
),
array(
'int foo() description',
true, 'foo', 'int', false, 'description'
true, 'foo', 'int', 0, 'description'
),
array(
'int foo($a, $b)',
true, 'foo', 'int', true, ''
true, 'foo', 'int', 2, ''
),
array(
'int foo() foo(int $a, int $b)',
true, 'foo', 'int', true, ''
true, 'foo', 'int', 2, ''
),
array(
'int foo(int $a, int $b)',
true, 'foo', 'int', true, ''
true, 'foo', 'int', 2, ''
),
array(
'null|int foo(int $a, int $b)',
true, 'foo', 'null|int', true, ''
true, 'foo', 'null|int', 2, ''
),
array(
'int foo(null|int $a, int $b)',
true, 'foo', 'int', true, ''
true, 'foo', 'int', 2, ''
),
array(
'\Exception foo() foo(Exception $a, Exception $b)',
true, 'foo', '\Exception', true, ''
true, 'foo', '\Exception', 2, ''
),
array(
'int foo() foo(Exception $a, Exception $b) description',
true, 'foo', 'int', true, 'description'
true, 'foo', 'int', 2, 'description'
),
array(
'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 $content
* @param string $extractedType
* @param string $extractedTypes
* @param string $extractedVarName
* @param string $extractedDescription
*
@@ -20,7 +20,7 @@ namespace phpDocumentor\Reflection\DocBlock\Tag;
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
class ReturnTagTest extends ParamTagTest
class ReturnTagTest extends \PHPUnit_Framework_TestCase
{
/**
* Test that the \phpDocumentor\Reflection\DocBlock\Tag\ReturnTag can
@@ -28,6 +28,7 @@ class ReturnTagTest extends ParamTagTest
*
* @param string $content
* @param string $extractedType
* @param string $extractedTypes
* @param string $extractedDescription
*
* @covers \phpDocumentor\Reflection\DocBlock\Tag\ReturnTag::__construct
@@ -28,7 +28,6 @@ class SeeTagTest extends \PHPUnit_Framework_TestCase
*
* @param string $type
* @param string $content
* @param string $exName
* @param string $exContent
* @param string $exReference
*
@@ -28,7 +28,6 @@ class UsesTagTest extends \PHPUnit_Framework_TestCase
*
* @param string $type
* @param string $content
* @param string $exName
* @param string $exContent
* @param string $exReference
*
@@ -59,7 +58,7 @@ class UsesTagTest extends \PHPUnit_Framework_TestCase
*/
public function provideDataForConstuctor()
{
// $type, $content, $exName, $exContent, $exDescription, $exReference
// $type, $content, $exContent, $exDescription, $exReference
return array(
array(
'uses',
@@ -59,7 +59,7 @@ class VarTagTest extends \PHPUnit_Framework_TestCase
*/
public function provideDataForConstuctor()
{
// $type, $content
// $type, $content, $exType, $exVariable, $exDescription
return array(
array(
'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());
}
/**
* @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
*
+97 -70
View File
@@ -34,7 +34,11 @@ class DocBlockTest extends \PHPUnit_Framework_TestCase
* @return void
*/
DOCBLOCK;
$object = new DocBlock($fixture);
$object = new DocBlock(
$fixture,
'\MyNamespace',
array('PHPDoc' => '\phpDocumentor')
);
$this->assertEquals(
'This is a short description.',
$object->getShortDescription()
@@ -43,10 +47,55 @@ DOCBLOCK;
'This is a long description.',
$object->getLongDescription()->getContents()
);
$this->assertEquals(2, count($object->getTags()));
$this->assertCount(2, $object->getTags());
$this->assertTrue($object->hasTag('see'));
$this->assertTrue($object->hasTag('return'));
$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()
);
$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('copyright'));
$this->assertTrue($object->hasTag('license'));
@@ -118,7 +167,7 @@ DOCBLOCK;
$object->getLongDescription()->getContents()
);
$tags = $object->getTags();
$this->assertEquals(2, count($tags));
$this->assertCount(2, $tags);
$this->assertTrue($object->hasTag('method'));
$this->assertTrue($object->hasTag('Method'));
$this->assertInstanceOf(
@@ -136,88 +185,66 @@ DOCBLOCK;
}
/**
* Tests whether a type is expanded with the given namespace and that a
* keyword is not expanded.
*
* @covers \phpDocumentor\Reflection\DocBlock::expandType()
*
* @return void
* @depends testConstructFromReflector
* @covers \phpDocumentor\Reflection\DocBlock::getTagsByName
*/
public function testExpandTypeUsingNamespace()
public function testGetTagsByNameZeroAndOneMatch()
{
$docblock = new DocBlock('', '\My\Namespace');
$this->assertEquals('\My\Namespace\Mine', $docblock->expandType('Mine'));
$object = new DocBlock(new \ReflectionClass($this));
$this->assertEmpty($object->getTagsByName('category'));
$this->assertCount(1, $object->getTagsByName('author'));
}
/**
* Tests whether a type is expanded when no namespace is given.
*
* @covers \phpDocumentor\Reflection\DocBlock::expandType()
*
* @return void
* @depends testConstructWithTagsOnly
* @covers \phpDocumentor\Reflection\DocBlock::parseTags
*/
public function testExpandTypeWithoutNamespace()
public function testParseMultilineTag()
{
$docblock = new DocBlock('');
$this->assertEquals('\Mine', $docblock->expandType('Mine'));
$fixture = <<<DOCBLOCK
/**
* @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
* is provided.
*
* @covers \phpDocumentor\Reflection\DocBlock::expandType()
*
* @return void
* @depends testConstructWithTagsOnly
* @covers \phpDocumentor\Reflection\DocBlock::parseTags
*/
public function testExpandTypeUsingNamespaceAlias()
public function testParseMultilineTagWithLineBreaks()
{
$docblock = new DocBlock(
'',
'\My\Namespace',
array('Alias' => '\My\Namespace\Alias')
);
// first try a normal resolution without alias
$this->assertEquals(
'\My\Namespace\Al',
$docblock->expandType('Al')
);
// try to use the alias
$this->assertEquals(
'\My\Namespace\Alias\Al',
$docblock->expandType('Alias\Al')
);
$fixture = <<<DOCBLOCK
/**
* @return void Content on
* multiple lines.
*
* One more, after the break.
*/
DOCBLOCK;
$object = new DocBlock($fixture);
$this->assertCount(1, $object->getTags());
}
/**
* Tests whether the keywords that should not be converted are not converted.
*
* @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
* @depends testConstructWithTagsOnly
* @covers \phpDocumentor\Reflection\DocBlock::getTagsByName
*/
public function testThatExpandTypeDoesNotExpandAllKeywords($keyword)
public function testGetTagsByNameMultipleMatch()
{
$docblock = new DocBlock('', '\My\Namespace');
$this->assertSame($keyword, $docblock->expandType($keyword));
}
public function getNonExpandableKeywordsForExpandType()
{
return array(
array(null),
array('string'), array('int'), array('integer'), array('bool'),
array('boolean'), array('float'), array('double'), array('object'),
array('mixed'), array('array'), array('resource'), array('void'),
array('null'), array('callback'), array('false'), array('true'),
array('self'), array('$this'), array('callable')
);
$fixture = <<<DOCBLOCK
/**
* @param string
* @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'));
}
}