Improve test coverage

This commit is contained in:
Jaapio
2020-09-02 22:46:14 +02:00
parent 13d9a6bb0b
commit 08c0b366d7
4 changed files with 177 additions and 30 deletions
+1 -1
View File
@@ -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
+3 -3
View File
@@ -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];
}
+31 -5
View File
@@ -133,14 +133,40 @@ class AuthorTest extends TestCase
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Author::<public>
*
* @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 <[email protected]>');
$fixture = Author::create($input);
$this->assertSame('Mike van Riel <[email protected]>', (string) $fixture);
$this->assertSame('Mike van Riel', $fixture->getAuthorName());
$this->assertSame('[email protected]', $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 <[email protected]>',
'Mike van Riel <[email protected]>',
'Mike van Riel',
'[email protected]',
],
[
'Mike van Riel < [email protected] >',
'Mike van Riel <[email protected]>',
'Mike van Riel',
'[email protected]',
],
[
'Mike van Riel',
'Mike van Riel',
'Mike van Riel',
'',
],
];
}
/**
+142 -21
View File
@@ -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 ::<private>
*/
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',
],
];
}
}