From 08c0b366d7dd79f0158d0963f59de08530bf3b61 Mon Sep 17 00:00:00 2001 From: Jaapio Date: Wed, 2 Sep 2020 22:46:14 +0200 Subject: [PATCH] Improve test coverage --- Makefile | 2 +- src/DocBlock/Tags/Example.php | 6 +- tests/unit/DocBlock/Tags/AuthorTest.php | 36 ++++- tests/unit/DocBlock/Tags/ExampleTest.php | 163 ++++++++++++++++++++--- 4 files changed, 177 insertions(+), 30 deletions(-) diff --git a/Makefile b/Makefile index 5aff03e..ca5f31b 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ psalm: .PHONY: test test: docker run -it --rm -v${CURDIR}:/github/workspace phpdoc/phpunit-ga - docker run -it --rm -v${CURDIR}:/data -w /data php:7.2 -f ./tests/coverage-checker.php 89 + docker run -it --rm -v${CURDIR}:/data -w /data php:7.2 -f ./tests/coverage-checker.php 90 .PHONY: pre-commit-test pre-commit-test: test phpcs phpstan psalm diff --git a/src/DocBlock/Tags/Example.php b/src/DocBlock/Tags/Example.php index 8ccb4fd..3673ffb 100644 --- a/src/DocBlock/Tags/Example.php +++ b/src/DocBlock/Tags/Example.php @@ -48,7 +48,7 @@ final class Example implements Tag, Factory\StaticMethod public function __construct(string $filePath, bool $isURI, int $startingLine, int $lineCount, ?string $content) { Assert::notEmpty($filePath); - Assert::greaterThanEq($startingLine, 0); + Assert::greaterThanEq($startingLine, 1); Assert::greaterThanEq($lineCount, 0); $this->filePath = $filePath; @@ -63,7 +63,7 @@ final class Example implements Tag, Factory\StaticMethod public function getContent() : string { - if ($this->content === null) { + if ($this->content === null || $this->content === '') { $filePath = '"' . $this->filePath . '"'; if ($this->isURI) { $filePath = $this->isUriRelative($this->filePath) @@ -107,7 +107,7 @@ final class Example implements Tag, Factory\StaticMethod // Starting line / Number of lines / Description if (preg_match('/^([1-9]\d*)(?:\s+((?1))\s*)?(.*)$/sux', $matches[3], $contentMatches)) { $startingLine = (int) $contentMatches[1]; - if (isset($contentMatches[2]) && $contentMatches[2] !== '') { + if (isset($contentMatches[2])) { $lineCount = (int) $contentMatches[2]; } diff --git a/tests/unit/DocBlock/Tags/AuthorTest.php b/tests/unit/DocBlock/Tags/AuthorTest.php index 59e4cda..cc06727 100644 --- a/tests/unit/DocBlock/Tags/AuthorTest.php +++ b/tests/unit/DocBlock/Tags/AuthorTest.php @@ -133,14 +133,40 @@ class AuthorTest extends TestCase * @uses \phpDocumentor\Reflection\DocBlock\Tags\Author:: * * @covers ::create + * @dataProvider authorTagProvider */ - public function testFactoryMethod() : void + public function testFactoryMethod(string $input, string $output, string $name, string $email) : void { - $fixture = Author::create('Mike van Riel '); + $fixture = Author::create($input); - $this->assertSame('Mike van Riel ', (string) $fixture); - $this->assertSame('Mike van Riel', $fixture->getAuthorName()); - $this->assertSame('mike@phpdoc.org', $fixture->getEmail()); + $this->assertSame($output, (string) $fixture); + $this->assertSame($name, $fixture->getAuthorName()); + $this->assertSame($email, $fixture->getEmail()); + } + + /** @return mixed[][] */ + public function authorTagProvider() : array + { + return [ + [ + 'Mike van Riel ', + 'Mike van Riel ', + 'Mike van Riel', + 'mike@phpdoc.org', + ], + [ + 'Mike van Riel < mike@phpdoc.org >', + 'Mike van Riel ', + 'Mike van Riel', + 'mike@phpdoc.org', + ], + [ + 'Mike van Riel', + 'Mike van Riel', + 'Mike van Riel', + '', + ], + ]; } /** diff --git a/tests/unit/DocBlock/Tags/ExampleTest.php b/tests/unit/DocBlock/Tags/ExampleTest.php index 77809d4..1d6d159 100644 --- a/tests/unit/DocBlock/Tags/ExampleTest.php +++ b/tests/unit/DocBlock/Tags/ExampleTest.php @@ -4,25 +4,18 @@ declare(strict_types=1); namespace DocBlock\Tags; -use Mockery as m; +use InvalidArgumentException; use phpDocumentor\Reflection\DocBlock\Tags\Example; use PHPUnit\Framework\TestCase; /** * @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\Example + * @covers :: */ class ExampleTest extends TestCase { /** - * Call Mockery::close after each test. - */ - public function tearDown() : void - { - m::close(); - } - - /** - * @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag + * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag * * @covers ::create * @covers ::__construct @@ -37,7 +30,7 @@ class ExampleTest extends TestCase } /** - * @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag + * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag * * @covers ::create * @covers ::__construct @@ -52,7 +45,7 @@ class ExampleTest extends TestCase } /** - * @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag + * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag * * @covers ::create * @covers ::__construct @@ -67,7 +60,7 @@ class ExampleTest extends TestCase } /** - * @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag + * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag * * @covers ::create * @covers ::__construct @@ -84,7 +77,7 @@ class ExampleTest extends TestCase } /** - * @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag + * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag * * @covers ::create * @covers ::__construct @@ -101,21 +94,149 @@ class ExampleTest extends TestCase } /** - * @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag + * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag * + * @dataProvider tagContentProvider * @covers ::create * @covers ::__construct * @covers ::getFilePath * @covers ::getStartingLine * @covers ::getLineCount * @covers ::getDescription + * @covers ::getContent */ - public function testFullExample() : void + public function testFactoryMethod( + string $input, + string $filePath, + int $startLine, + int $lineCount, + ?string $description, + string $content + ) : void { + $tag = Example::create($input); + $this->assertSame($filePath, $tag->getFilePath()); + $this->assertSame($startLine, $tag->getStartingLine()); + $this->assertSame($lineCount, $tag->getLineCount()); + $this->assertSame($description, $tag->getDescription()); + $this->assertSame($content, $tag->getContent()); + } + + /** @return mixed[][] */ + public function tagContentProvider() : array { - $tag = Example::create('"example1.php" 10 5 test text'); - $this->assertEquals('example1.php', $tag->getFilePath()); - $this->assertEquals(10, $tag->getStartingLine()); - $this->assertEquals(5, $tag->getLineCount()); - $this->assertEquals('test text', $tag->getDescription()); + return [ + [ + '"example1.php" 10 5 test text ', + 'example1.php', + 10, + 5, + 'test text', + 'test text', + ], + [ + 'example1.php 10 5 test text', + 'example1.php', + 10, + 5, + 'test text', + 'test text', + ], + [ + 'example1.php 1 10 test text', + 'example1.php', + 1, + 10, + 'test text', + 'test text', + ], + [ + 'example1.php', + 'example1.php', + 1, + 0, + null, + 'example1.php', + ], + [ + 'file://example1.php ', + 'file://example1.php', + 1, + 0, + '', + 'file://example1.php', + ], + [ + '/example1.php', + '/example1.php', + 1, + 0, + null, + '/example1.php', + ], + ]; + } + + /** + * @dataProvider invalidExampleProvider + * @covers ::__construct + */ + public function testValidatesArguments( + string $filePath, + bool $isUrl, + int $startLine, + int $lineCount, + string $description + ) : void { + $this->expectException(InvalidArgumentException::class); + + new Example( + $filePath, + $isUrl, + $startLine, + $lineCount, + $description + ); + } + + /** @return mixed[][] */ + public function invalidExampleProvider() : array + { + return [ + 'invalid start' => [ + '/some/path', + false, + -1, + 0, + 'text', + ], + 'invalid start 2' => [ + '/some/path', + false, + -10, + 0, + 'text', + ], + 'invalid length' => [ + '/some/path', + false, + 1, + -1, + 'text', + ], + 'invalid length 2' => [ + '/some/path', + false, + 1, + -10, + 'text', + ], + 'empty filepath' => [ + '', + false, + 1, + 0, + 'text', + ], + ]; } }