Skip to content

Commit 08c0b36

Browse files
committed
Improve test coverage
1 parent 13d9a6b commit 08c0b36

File tree

4 files changed

+177
-30
lines changed

4 files changed

+177
-30
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ psalm:
3030
.PHONY: test
3131
test:
3232
docker run -it --rm -v${CURDIR}:/github/workspace phpdoc/phpunit-ga
33-
docker run -it --rm -v${CURDIR}:/data -w /data php:7.2 -f ./tests/coverage-checker.php 89
33+
docker run -it --rm -v${CURDIR}:/data -w /data php:7.2 -f ./tests/coverage-checker.php 90
3434

3535
.PHONY: pre-commit-test
3636
pre-commit-test: test phpcs phpstan psalm

src/DocBlock/Tags/Example.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ final class Example implements Tag, Factory\StaticMethod
4848
public function __construct(string $filePath, bool $isURI, int $startingLine, int $lineCount, ?string $content)
4949
{
5050
Assert::notEmpty($filePath);
51-
Assert::greaterThanEq($startingLine, 0);
51+
Assert::greaterThanEq($startingLine, 1);
5252
Assert::greaterThanEq($lineCount, 0);
5353

5454
$this->filePath = $filePath;
@@ -63,7 +63,7 @@ public function __construct(string $filePath, bool $isURI, int $startingLine, in
6363

6464
public function getContent() : string
6565
{
66-
if ($this->content === null) {
66+
if ($this->content === null || $this->content === '') {
6767
$filePath = '"' . $this->filePath . '"';
6868
if ($this->isURI) {
6969
$filePath = $this->isUriRelative($this->filePath)
@@ -107,7 +107,7 @@ public static function create(string $body) : ?Tag
107107
// Starting line / Number of lines / Description
108108
if (preg_match('/^([1-9]\d*)(?:\s+((?1))\s*)?(.*)$/sux', $matches[3], $contentMatches)) {
109109
$startingLine = (int) $contentMatches[1];
110-
if (isset($contentMatches[2]) && $contentMatches[2] !== '') {
110+
if (isset($contentMatches[2])) {
111111
$lineCount = (int) $contentMatches[2];
112112
}
113113

tests/unit/DocBlock/Tags/AuthorTest.php

+31-5
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,40 @@ public function testStringRepresentationWithEmtpyEmail() : void
133133
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Author::<public>
134134
*
135135
* @covers ::create
136+
* @dataProvider authorTagProvider
136137
*/
137-
public function testFactoryMethod() : void
138+
public function testFactoryMethod(string $input, string $output, string $name, string $email) : void
138139
{
139-
$fixture = Author::create('Mike van Riel <[email protected]>');
140+
$fixture = Author::create($input);
140141

141-
$this->assertSame('Mike van Riel <[email protected]>', (string) $fixture);
142-
$this->assertSame('Mike van Riel', $fixture->getAuthorName());
143-
$this->assertSame('[email protected]', $fixture->getEmail());
142+
$this->assertSame($output, (string) $fixture);
143+
$this->assertSame($name, $fixture->getAuthorName());
144+
$this->assertSame($email, $fixture->getEmail());
145+
}
146+
147+
/** @return mixed[][] */
148+
public function authorTagProvider() : array
149+
{
150+
return [
151+
[
152+
'Mike van Riel <[email protected]>',
153+
'Mike van Riel <[email protected]>',
154+
'Mike van Riel',
155+
156+
],
157+
[
158+
'Mike van Riel < [email protected] >',
159+
'Mike van Riel <[email protected]>',
160+
'Mike van Riel',
161+
162+
],
163+
[
164+
'Mike van Riel',
165+
'Mike van Riel',
166+
'Mike van Riel',
167+
'',
168+
],
169+
];
144170
}
145171

146172
/**

tests/unit/DocBlock/Tags/ExampleTest.php

+142-21
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,18 @@
44

55
namespace DocBlock\Tags;
66

7-
use Mockery as m;
7+
use InvalidArgumentException;
88
use phpDocumentor\Reflection\DocBlock\Tags\Example;
99
use PHPUnit\Framework\TestCase;
1010

1111
/**
1212
* @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\Example
13+
* @covers ::<private>
1314
*/
1415
class ExampleTest extends TestCase
1516
{
1617
/**
17-
* Call Mockery::close after each test.
18-
*/
19-
public function tearDown() : void
20-
{
21-
m::close();
22-
}
23-
24-
/**
25-
* @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag
18+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag
2619
*
2720
* @covers ::create
2821
* @covers ::__construct
@@ -37,7 +30,7 @@ public function testExampleWithoutContent() : void
3730
}
3831

3932
/**
40-
* @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag
33+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag
4134
*
4235
* @covers ::create
4336
* @covers ::__construct
@@ -52,7 +45,7 @@ public function testWithDescription() : void
5245
}
5346

5447
/**
55-
* @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag
48+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag
5649
*
5750
* @covers ::create
5851
* @covers ::__construct
@@ -67,7 +60,7 @@ public function testStartlineIsParsed() : void
6760
}
6861

6962
/**
70-
* @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag
63+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag
7164
*
7265
* @covers ::create
7366
* @covers ::__construct
@@ -84,7 +77,7 @@ public function testAllowOmitingLineCount() : void
8477
}
8578

8679
/**
87-
* @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag
80+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag
8881
*
8982
* @covers ::create
9083
* @covers ::__construct
@@ -101,21 +94,149 @@ public function testLengthIsParsed() : void
10194
}
10295

10396
/**
104-
* @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag
97+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag
10598
*
99+
* @dataProvider tagContentProvider
106100
* @covers ::create
107101
* @covers ::__construct
108102
* @covers ::getFilePath
109103
* @covers ::getStartingLine
110104
* @covers ::getLineCount
111105
* @covers ::getDescription
106+
* @covers ::getContent
112107
*/
113-
public function testFullExample() : void
108+
public function testFactoryMethod(
109+
string $input,
110+
string $filePath,
111+
int $startLine,
112+
int $lineCount,
113+
?string $description,
114+
string $content
115+
) : void {
116+
$tag = Example::create($input);
117+
$this->assertSame($filePath, $tag->getFilePath());
118+
$this->assertSame($startLine, $tag->getStartingLine());
119+
$this->assertSame($lineCount, $tag->getLineCount());
120+
$this->assertSame($description, $tag->getDescription());
121+
$this->assertSame($content, $tag->getContent());
122+
}
123+
124+
/** @return mixed[][] */
125+
public function tagContentProvider() : array
114126
{
115-
$tag = Example::create('"example1.php" 10 5 test text');
116-
$this->assertEquals('example1.php', $tag->getFilePath());
117-
$this->assertEquals(10, $tag->getStartingLine());
118-
$this->assertEquals(5, $tag->getLineCount());
119-
$this->assertEquals('test text', $tag->getDescription());
127+
return [
128+
[
129+
'"example1.php" 10 5 test text ',
130+
'example1.php',
131+
10,
132+
5,
133+
'test text',
134+
'test text',
135+
],
136+
[
137+
'example1.php 10 5 test text',
138+
'example1.php',
139+
10,
140+
5,
141+
'test text',
142+
'test text',
143+
],
144+
[
145+
'example1.php 1 10 test text',
146+
'example1.php',
147+
1,
148+
10,
149+
'test text',
150+
'test text',
151+
],
152+
[
153+
'example1.php',
154+
'example1.php',
155+
1,
156+
0,
157+
null,
158+
'example1.php',
159+
],
160+
[
161+
'file://example1.php ',
162+
'file://example1.php',
163+
1,
164+
0,
165+
'',
166+
'file://example1.php',
167+
],
168+
[
169+
'/example1.php',
170+
'/example1.php',
171+
1,
172+
0,
173+
null,
174+
'/example1.php',
175+
],
176+
];
177+
}
178+
179+
/**
180+
* @dataProvider invalidExampleProvider
181+
* @covers ::__construct
182+
*/
183+
public function testValidatesArguments(
184+
string $filePath,
185+
bool $isUrl,
186+
int $startLine,
187+
int $lineCount,
188+
string $description
189+
) : void {
190+
$this->expectException(InvalidArgumentException::class);
191+
192+
new Example(
193+
$filePath,
194+
$isUrl,
195+
$startLine,
196+
$lineCount,
197+
$description
198+
);
199+
}
200+
201+
/** @return mixed[][] */
202+
public function invalidExampleProvider() : array
203+
{
204+
return [
205+
'invalid start' => [
206+
'/some/path',
207+
false,
208+
-1,
209+
0,
210+
'text',
211+
],
212+
'invalid start 2' => [
213+
'/some/path',
214+
false,
215+
-10,
216+
0,
217+
'text',
218+
],
219+
'invalid length' => [
220+
'/some/path',
221+
false,
222+
1,
223+
-1,
224+
'text',
225+
],
226+
'invalid length 2' => [
227+
'/some/path',
228+
false,
229+
1,
230+
-10,
231+
'text',
232+
],
233+
'empty filepath' => [
234+
'',
235+
false,
236+
1,
237+
0,
238+
'text',
239+
],
240+
];
120241
}
121242
}

0 commit comments

Comments
 (0)