From 70eadb02dfe47ce6c434d97976798dceff9b7644 Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Sun, 14 Jun 2015 08:50:52 +0200 Subject: [PATCH] Add tests for covers tag --- src/DocBlock/Tags/Covers.php | 23 ++-- tests/unit/DocBlock/Tags/CoversTest.php | 154 ++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 9 deletions(-) create mode 100644 tests/unit/DocBlock/Tags/CoversTest.php diff --git a/src/DocBlock/Tags/Covers.php b/src/DocBlock/Tags/Covers.php index c2ce19c..b498160 100644 --- a/src/DocBlock/Tags/Covers.php +++ b/src/DocBlock/Tags/Covers.php @@ -1,11 +1,11 @@ - * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com) + * @copyright 2010-2015 Mike van Riel * @license http://www.opensource.org/licenses/mit-license.php MIT * @link http://phpdoc.org */ @@ -15,17 +15,19 @@ namespace phpDocumentor\Reflection\DocBlock\Tags; use phpDocumentor\Reflection\DocBlock\DescriptionFactory; use phpDocumentor\Reflection\Fqsen; use phpDocumentor\Reflection\DocBlock\Description; -use phpDocumentor\Reflection\DocBlock\Context; +use phpDocumentor\Reflection\Types\Context; use phpDocumentor\Reflection\FqsenResolver; -use phpDocumentor\Reflection\TypeResolver; +use Webmozart\Assert\Assert; /** * Reflection class for a @covers tag in a Docblock. */ -class Covers extends BaseTag +final class Covers extends BaseTag { + protected $name = 'covers'; + /** @var Fqsen */ - protected $refers = null; + private $refers = null; /** * Initializes this tag. @@ -33,7 +35,7 @@ class Covers extends BaseTag * @param Fqsen $refers * @param Description $description */ - public function __construct(Fqsen $refers, Description $description) + public function __construct(Fqsen $refers, Description $description = null) { $this->refers = $refers; $this->description = $description; @@ -49,6 +51,9 @@ class Covers extends BaseTag Context $context = null ) { + Assert::string($body); + Assert::notEmpty($body); + $parts = preg_split('/\s+/Su', $body, 2); return new static( diff --git a/tests/unit/DocBlock/Tags/CoversTest.php b/tests/unit/DocBlock/Tags/CoversTest.php new file mode 100644 index 0000000..e3a89eb --- /dev/null +++ b/tests/unit/DocBlock/Tags/CoversTest.php @@ -0,0 +1,154 @@ + + * @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\Covers + * @covers :: + */ +class CoversTest extends \PHPUnit_Framework_TestCase +{ + /** + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Covers::__construct + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName + */ + public function testIfCorrectTagNameIsReturned() + { + $fixture = new Covers(new Fqsen('\DateTime'), new Description('Description')); + + $this->assertSame('covers', $fixture->getName()); + } + + /** + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Covers::__construct + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Covers::__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 Covers(new Fqsen('\DateTime'), new Description('Description')); + + $this->assertSame('\DateTime Description', $fixture->render()); + } + + /** + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Covers::__construct + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render + */ + public function testIfTagCanBeRenderedUsingSpecificFormatter() + { + $fixture = new Covers(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 Covers($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 Covers(new Fqsen('\DateTime'), $expected); + + $this->assertSame($expected, $fixture->getDescription()); + } + + /** + * @covers ::__construct + * @covers ::__toString + * @uses \phpDocumentor\Reflection\DocBlock\Description + */ + public function testStringRepresentationIsReturned() + { + $fixture = new Covers(new Fqsen('\DateTime'), new Description('Description')); + + $this->assertSame('\DateTime Description', (string)$fixture); + } + + /** + * @covers ::create + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Covers:: + * @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 = Covers::create('DateTime My Description', $descriptionFactory, $resolver, $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(Covers::create([])); + } + + /** + * @covers ::create + * @expectedException \InvalidArgumentException + */ + public function testFactoryMethodFailsIfBodyIsNotEmpty() + { + $this->assertNull(Covers::create('')); + } +}