Refactored Uses tag and written test

This commit is contained in:
Mike van Riel
2015-06-22 06:52:05 +02:00
committed by Mike van Riel
parent 6b92695a71
commit 2bab7d9c1f
3 changed files with 411 additions and 6 deletions
+69 -6
View File
@@ -1,20 +1,83 @@
<?php <?php
/** /**
* phpDocumentor * 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\DocBlock\Tags; namespace phpDocumentor\Reflection\DocBlock\Tags;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\FqsenResolver;
use phpDocumentor\Reflection\Types\Context;
use Webmozart\Assert\Assert;
/** /**
* Reflection class for a @uses tag in a Docblock. * Reflection class for a {@}uses tag in a Docblock.
*/ */
class Uses extends See final class Uses extends BaseTag
{ {
protected $name = 'uses';
/** @var Fqsen */
protected $refers = null;
/**
* Initializes this tag.
*
* @param Fqsen $refers
* @param Description $description
*/
public function __construct(Fqsen $refers, Description $description = null)
{
$this->refers = $refers;
$this->description = $description;
}
/**
* {@inheritdoc}
*/
public static function create(
$body,
FqsenResolver $resolver = null,
DescriptionFactory $descriptionFactory = null,
Context $context = null
) {
Assert::string($body);
Assert::allNotNull([$resolver, $descriptionFactory]);
$parts = preg_split('/\s+/Su', $body, 2);
return new static(
$resolver->resolve($parts[0], $context),
$descriptionFactory->create(isset($parts[1]) ? $parts[1] : '', $context)
);
}
/**
* Returns the structural element this tag refers to.
*
* @return Fqsen
*/
public function getReference()
{
return $this->refers;
}
/**
* Returns a string representation of this tag.
*
* @return string
*/
public function __toString()
{
return $this->refers . ' ' . $this->description->render();
}
} }
+169
View File
@@ -0,0 +1,169 @@
<?php
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link 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\TypeResolver;
use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\String_;
/**
* @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\Throws
* @covers ::<private>
*/
class ThrowsTest extends \PHPUnit_Framework_TestCase
{
/**
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Throws::__construct
* @uses \phpDocumentor\Reflection\DocBlock\Description
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName
*/
public function testIfCorrectTagNameIsReturned()
{
$fixture = new Throws(new String_(), new Description('Description'));
$this->assertSame('throws', $fixture->getName());
}
/**
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Throws::__construct
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Throws::__toString
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
* @uses \phpDocumentor\Reflection\DocBlock\Description
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
*/
public function testIfTagCanBeRenderedUsingDefaultFormatter()
{
$fixture = new Throws(new String_(), new Description('Description'));
$this->assertSame('string Description', $fixture->render());
}
/**
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Throws::__construct
* @uses \phpDocumentor\Reflection\DocBlock\Description
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
*/
public function testIfTagCanBeRenderedUsingSpecificFormatter()
{
$fixture = new Throws(new String_(), 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 ::getType
*/
public function testHasType()
{
$expected = new String_();
$fixture = new Throws($expected);
$this->assertSame($expected, $fixture->getType());
}
/**
* @covers ::__construct
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getDescription
* @uses \phpDocumentor\Reflection\DocBlock\Description
*/
public function testHasDescription()
{
$expected = new Description('Description');
$fixture = new Throws(new String_(), $expected);
$this->assertSame($expected, $fixture->getDescription());
}
/**
* @covers ::__construct
* @covers ::__toString
* @uses \phpDocumentor\Reflection\DocBlock\Description
*/
public function testStringRepresentationIsReturned()
{
$fixture = new Throws(new String_(), new Description('Description'));
$this->assertSame('string Description', (string)$fixture);
}
/**
* @covers ::create
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Throws::<public>
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
* @uses \phpDocumentor\Reflection\TypeResolver
* @uses \phpDocumentor\Reflection\DocBlock\Description
* @uses \phpDocumentor\Reflection\Types\String_
* @uses \phpDocumentor\Reflection\Types\Context
*/
public function testFactoryMethod()
{
$descriptionFactory = m::mock(DescriptionFactory::class);
$resolver = new TypeResolver();
$context = new Context('');
$type = new String_();
$description = new Description('My Description');
$descriptionFactory->shouldReceive('create')->with('My Description', $context)->andReturn($description);
$fixture = Throws::create('string My Description', $resolver, $descriptionFactory, $context);
$this->assertSame('string My Description', (string)$fixture);
$this->assertEquals($type, $fixture->getType());
$this->assertSame($description, $fixture->getDescription());
}
/**
* @covers ::create
* @expectedException \InvalidArgumentException
*/
public function testFactoryMethodFailsIfBodyIsNotString()
{
$this->assertNull(Throws::create([]));
}
/**
* @covers ::create
* @expectedException \InvalidArgumentException
*/
public function testFactoryMethodFailsIfBodyIsNotEmpty()
{
$this->assertNull(Throws::create(''));
}
/**
* @covers ::create
* @expectedException \InvalidArgumentException
*/
public function testFactoryMethodFailsIfResolverIsNull()
{
Throws::create('body');
}
/**
* @covers ::create
* @expectedException \InvalidArgumentException
*/
public function testFactoryMethodFailsIfDescriptionFactoryIsNull()
{
Throws::create('body', new TypeResolver());
}
}
+173
View File
@@ -0,0 +1,173 @@
<?php
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link 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\Fqsen;
use phpDocumentor\Reflection\FqsenResolver;
use phpDocumentor\Reflection\Types\Context;
/**
* @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\Uses
* @covers ::<private>
*/
class UsesTest extends \PHPUnit_Framework_TestCase
{
/**
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Uses::__construct
* @uses \phpDocumentor\Reflection\DocBlock\Description
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName
*/
public function testIfCorrectTagNameIsReturned()
{
$fixture = new Uses(new Fqsen('\DateTime'), new Description('Description'));
$this->assertSame('uses', $fixture->getName());
}
/**
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Uses::__construct
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Uses::__toString
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
* @uses \phpDocumentor\Reflection\DocBlock\Description
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
*/
public function testIfTagCanBeRenderedUsingDefaultFormatter()
{
$fixture = new Uses(new Fqsen('\DateTime'), new Description('Description'));
$this->assertSame('\DateTime Description', $fixture->render());
}
/**
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Uses::__construct
* @uses \phpDocumentor\Reflection\DocBlock\Description
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
*/
public function testIfTagCanBeRenderedUsingSpecificFormatter()
{
$fixture = new Uses(new Fqsen('\DateTime'), 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 ::getReference
*/
public function testHasReferenceToFqsen()
{
$expected = new Fqsen('\DateTime');
$fixture = new Uses($expected);
$this->assertSame($expected, $fixture->getReference());
}
/**
* @covers ::__construct
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getDescription
* @uses \phpDocumentor\Reflection\DocBlock\Description
*/
public function testHasDescription()
{
$expected = new Description('Description');
$fixture = new Uses(new Fqsen('\DateTime'), $expected);
$this->assertSame($expected, $fixture->getDescription());
}
/**
* @covers ::__construct
* @covers ::__toString
* @uses \phpDocumentor\Reflection\DocBlock\Description
*/
public function testStringRepresentationIsReturned()
{
$fixture = new Uses(new Fqsen('\DateTime'), new Description('Description'));
$this->assertSame('\DateTime Description', (string)$fixture);
}
/**
* @covers ::create
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Uses::<public>
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
* @uses \phpDocumentor\Reflection\FqsenResolver
* @uses \phpDocumentor\Reflection\DocBlock\Description
* @uses \phpDocumentor\Reflection\Fqsen
* @uses \phpDocumentor\Reflection\Types\Context
*/
public function testFactoryMethod()
{
$descriptionFactory = m::mock(DescriptionFactory::class);
$resolver = m::mock(FqsenResolver::class);
$context = new Context('');
$fqsen = new Fqsen('\DateTime');
$description = new Description('My Description');
$descriptionFactory
->shouldReceive('create')->with('My Description', $context)->andReturn($description)
;
$resolver->shouldReceive('resolve')->with('DateTime', $context)->andReturn($fqsen);
$fixture = Uses::create('DateTime My Description', $resolver, $descriptionFactory, $context);
$this->assertSame('\DateTime My Description', (string)$fixture);
$this->assertSame($fqsen, $fixture->getReference());
$this->assertSame($description, $fixture->getDescription());
}
/**
* @covers ::create
* @expectedException \InvalidArgumentException
*/
public function testFactoryMethodFailsIfBodyIsNotString()
{
$this->assertNull(Uses::create([]));
}
/**
* @covers ::create
* @expectedException \InvalidArgumentException
*/
public function testFactoryMethodFailsIfBodyIsNotEmpty()
{
$this->assertNull(Uses::create(''));
}
/**
* @covers ::create
* @expectedException \InvalidArgumentException
*/
public function testFactoryMethodFailsIfResolverIsNull()
{
Uses::create('body');
}
/**
* @covers ::create
* @expectedException \InvalidArgumentException
*/
public function testFactoryMethodFailsIfDescriptionFactoryIsNull()
{
Uses::create('body', new FqsenResolver());
}
}