From ab3ebf467c9b672febd6298f3c4ee70d33784681 Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Sat, 13 Jun 2015 18:41:45 +0200 Subject: [PATCH] Add assertions to DocBlock --- src/DocBlock.php | 9 ++++++ tests/unit/DocBlockTest.php | 55 +++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/DocBlock.php b/src/DocBlock.php index f59cd76..0b7565c 100644 --- a/src/DocBlock.php +++ b/src/DocBlock.php @@ -14,6 +14,7 @@ namespace phpDocumentor\Reflection; use phpDocumentor\Reflection\DocBlock\Tag; use phpDocumentor\Reflection\Types\Context; +use Webmozart\Assert\Assert; final class DocBlock { @@ -57,6 +58,10 @@ final class DocBlock $isTemplateEnd = false ) { + Assert::string($summary); + Assert::boolean($isTemplateStart); + Assert::boolean($isTemplateEnd); + $this->summary = $summary; $this->description = $description ?: new DocBlock\Description(''); foreach ($tags as $tag) { @@ -164,6 +169,8 @@ final class DocBlock */ public function getTagsByName($name) { + Assert::string($name); + $result = array(); /** @var Tag $tag */ @@ -187,6 +194,8 @@ final class DocBlock */ public function hasTag($name) { + Assert::string($name); + /** @var Tag $tag */ foreach ($this->getTags() as $tag) { if ($tag->getName() == $name) { diff --git a/tests/unit/DocBlockTest.php b/tests/unit/DocBlockTest.php index ffd7ea5..403f52b 100644 --- a/tests/unit/DocBlockTest.php +++ b/tests/unit/DocBlockTest.php @@ -18,6 +18,7 @@ use phpDocumentor\Reflection\Types\Context; /** * @coversDefaultClass phpDocumentor\Reflection\DocBlock * @covers :: + * @uses \Webmozart\Assert\Assert */ class DocBlockTest extends \PHPUnit_Framework_TestCase { @@ -36,6 +37,36 @@ class DocBlockTest extends \PHPUnit_Framework_TestCase $this->assertSame($summary, $fixture->getSummary()); } + /** + * @covers ::__construct + * + * @expectedException \InvalidArgumentException + */ + public function testExceptionIsThrownIfSummaryIsNotAString() + { + new DocBlock([]); + } + + /** + * @covers ::__construct + * + * @expectedException \InvalidArgumentException + */ + public function testExceptionIsThrownIfTemplateStartIsNotABoolean() + { + new DocBlock('', null, [], null, null, ['is not boolean']); + } + + /** + * @covers ::__construct + * + * @expectedException \InvalidArgumentException + */ + public function testExceptionIsThrownIfTemplateEndIsNotABoolean() + { + new DocBlock('', null, [], null, null, false, ['is not boolean']); + } + /** * @covers ::__construct * @covers ::getDescription @@ -94,6 +125,18 @@ class DocBlockTest extends \PHPUnit_Framework_TestCase $this->assertSame([], $fixture->getTagsByName('Ebcd')); } + /** + * @covers ::__construct + * @covers ::getTagsByName + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @expectedException \InvalidArgumentException + */ + public function testExceptionIsThrownIfNameForTagsIsNotString() + { + $fixture = new DocBlock(); + $fixture->getTagsByName([]); + } + /** * @covers ::__construct * @covers ::hasTag @@ -119,6 +162,18 @@ class DocBlockTest extends \PHPUnit_Framework_TestCase $this->assertFalse($fixture->hasTag('Ebcd')); } + /** + * @covers ::__construct + * @covers ::hasTag + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @expectedException \InvalidArgumentException + */ + public function testExceptionIsThrownIfNameForCheckingTagsIsNotString() + { + $fixture = new DocBlock(); + $fixture->hasTag([]); + } + /** * @covers ::__construct * @covers ::getContext