diff --git a/src/DocBlock/Tags/Author.php b/src/DocBlock/Tags/Author.php index 3805708..02482d6 100644 --- a/src/DocBlock/Tags/Author.php +++ b/src/DocBlock/Tags/Author.php @@ -12,7 +12,7 @@ namespace phpDocumentor\Reflection\DocBlock\Tags; -use phpDocumentor\Reflection\DocBlock\Description; +use Webmozart\Assert\Assert; /** * Reflection class for an {@}author tag in a Docblock. @@ -36,10 +36,9 @@ final class Author extends BaseTag */ public function __construct($authorName, $authorEmail) { - if (!is_string($authorName)) { - throw new \InvalidArgumentException('The author tag does not have a valid name'); - } - if (!is_string($authorEmail) || ($authorEmail && !filter_var($authorEmail, FILTER_VALIDATE_EMAIL))) { + Assert::string($authorName); + Assert::string($authorEmail); + if ($authorEmail && !filter_var($authorEmail, FILTER_VALIDATE_EMAIL)) { throw new \InvalidArgumentException('The author tag does not have a valid e-mail address'); } @@ -78,10 +77,16 @@ final class Author extends BaseTag } /** - * {@inheritdoc} + * Attempts to create a new Author object based on †he tag body. + * + * @param string $body + * + * @return static */ public static function create($body) { + Assert::string($body); + $splitTagContent = preg_match('/^([^\<]*)(?:\<([^\>]*)\>)?$/u', $body, $matches); if (!$splitTagContent) { return null; diff --git a/tests/unit/DocBlock/Tags/AuthorTest.php b/tests/unit/DocBlock/Tags/AuthorTest.php new file mode 100644 index 0000000..f86c8c5 --- /dev/null +++ b/tests/unit/DocBlock/Tags/AuthorTest.php @@ -0,0 +1,111 @@ + + * @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\Tags\Other; + +/** + * @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\Author + * @covers :: + */ +class AuthorTest extends \PHPUnit_Framework_TestCase +{ + /** + * @covers ::__construct + * @covers ::getAuthorName + */ + public function testHasTheAuthorName() + { + $expected = 'Mike van Riel'; + + $fixture = new Author($expected, 'mike@phpdoc.org'); + + $this->assertSame($expected, $fixture->getAuthorName()); + } + + /** + * @covers ::__construct + * @covers ::getAuthorName + * @expectedException \InvalidArgumentException + */ + public function testInitializationFailsIfAuthorNameIsNotAString() + { + new Author([], 'mike@phpdoc.org'); + } + + /** + * @covers ::__construct + * @covers ::getEmail + */ + public function testHasTheAuthorMailAddress() + { + $expected = 'mike@phpdoc.org'; + + $fixture = new Author('Mike van Riel', $expected); + + $this->assertSame($expected, $fixture->getEmail()); + } + + /** + * @covers ::__construct + * @expectedException \InvalidArgumentException + */ + public function testInitializationFailsIfEmailIsNotAString() + { + new Author('Mike van Riel', []); + } + + /** + * @covers ::__construct + * @expectedException \InvalidArgumentException + */ + public function testInitializationFailsIfEmailIsNotValid() + { + new Author('Mike van Riel', 'mike'); + } + + /** + * @covers ::__construct + * @covers ::__toString + */ + public function testStringRepresentationIsReturned() + { + $fixture = new Author('Mike van Riel', 'mike@phpdoc.org'); + + $this->assertSame('Mike van Riel', (string)$fixture); + } + + /** + * @covers ::create + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Author:: + */ + public function testFactoryMethod() + { + $fixture = Author::create('Mike van Riel '); + + $this->assertSame('Mike van Riel', (string)$fixture); + $this->assertSame('Mike van Riel', $fixture->getAuthorName()); + $this->assertSame('mike@phpdoc.org', $fixture->getEmail()); + } + + /** + * @covers ::create + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Author:: + */ + public function testFactoryMethodReturnsNullIfItCouldNotReadBody() + { + $this->assertNull(Author::create('dfgr<')); + } +}