Skip to content

Commit cee7377

Browse files
committed
Test-cover EveryTestHasSameNamespaceAsTestedClass
1 parent 283222f commit cee7377

File tree

10 files changed

+139
-9
lines changed

10 files changed

+139
-9
lines changed

.github/workflows/infection.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ jobs:
2727
uses: "ramsey/composer-install@v1"
2828

2929
- name: Run Infection
30-
run: vendor/bin/roave-infection-static-analysis-plugin --min-msi=59 --min-covered-msi=93 --log-verbosity=none -s
30+
run: vendor/bin/roave-infection-static-analysis-plugin --min-msi=59 --min-covered-msi=90 --log-verbosity=none -s
3131
env:
3232
INFECTION_BADGE_API_KEY: ${{ secrets.INFECTION_BADGE_API_KEY }}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
},
1111
"autoload-dev": {
1212
"psr-4": {
13-
"Cdn77\\TestUtils\\Tests\\": "tests/"
13+
"Cdn77\\TestUtils\\Tests\\": "tests/",
14+
"Cdn77\\TestUtils\\Tests\\Tests\\TestCheck\\Fixtures\\EveryTestHasSameNamespaceAsTestedClass\\": "tests/TestCheck/Fixtures/EveryTestHasSameNamespaceAsTestedClass/tests"
1415
}
1516
},
1617
"require": {

src/TestCheck/EveryTestHasSameNamespaceAsTestedClass.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,25 @@
1212
use function Safe\preg_match;
1313
use function Safe\sprintf;
1414
use function Safe\substr;
15-
use function str_replace;
15+
use function strlen;
16+
use function strpos;
17+
use function substr_replace;
1618
use function trait_exists;
1719

1820
final class EveryTestHasSameNamespaceAsTestedClass implements TestCheck
1921
{
20-
private const PATTERN = '~\* @testedClass (?<targetClass>.+)\n~';
22+
private const PATTERN = '~\* @testedClass (?<targetClass>.+?)(?:\n| \*/)~';
2123

2224
/** @var iterable<string> $filePathNames */
2325
private iterable $filePathNames;
2426

27+
private string $testsNamespaceSuffix;
28+
2529
/** @param iterable<string> $filePathNames */
26-
public function __construct(iterable $filePathNames)
30+
public function __construct(iterable $filePathNames, string $testsNamespaceSuffix = 'Tests')
2731
{
2832
$this->filePathNames = $filePathNames;
33+
$this->testsNamespaceSuffix = '\\' . $testsNamespaceSuffix . '\\';
2934
}
3035

3136
public function run(TestCase $testCaseContext) : void
@@ -37,9 +42,7 @@ public function run(TestCase $testCaseContext) : void
3742

3843
$docComment = $classReflection->getDocComment();
3944
if ($docComment === false) {
40-
$testCaseContext::fail(
41-
sprintf('Test "%s" is missing phpdoc. See other tests for examples', $classReflection->getName())
42-
);
45+
$docComment = '';
4346
}
4447

4548
preg_match(self::PATTERN, $docComment, $targetClassMatches);
@@ -50,7 +53,18 @@ public function run(TestCase $testCaseContext) : void
5053

5154
$className = $classReflection->getName();
5255
$classNameWithoutSuffix = substr($className, 0, -4);
53-
$testedClassName = str_replace('\Tests\\', '\\', $classNameWithoutSuffix);
56+
$pos = strpos($classNameWithoutSuffix, $this->testsNamespaceSuffix);
57+
if ($pos === false) {
58+
$testedClassName = $classNameWithoutSuffix;
59+
} else {
60+
$testedClassName = substr_replace(
61+
$classNameWithoutSuffix,
62+
'\\',
63+
$pos,
64+
strlen($this->testsNamespaceSuffix)
65+
);
66+
}
67+
5468
if (class_exists($testedClassName) || trait_exists($testedClassName)) {
5569
continue;
5670
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cdn77\TestUtils\Tests\TestCheck;
6+
7+
use Cdn77\TestUtils\TestCheck\EveryTestHasSameNamespaceAsTestedClass;
8+
use Cdn77\TestUtils\Tests\BaseTestCase;
9+
use Generator;
10+
use PHPUnit\Framework\AssertionFailedError;
11+
12+
final class EveryTestHasSameNamespaceAsTestedClassTest extends BaseTestCase
13+
{
14+
/** @dataProvider providerSuccess */
15+
public function testSuccess(string $filePath) : void
16+
{
17+
$check = new EveryTestHasSameNamespaceAsTestedClass(
18+
[__DIR__ . '/Fixtures/EveryTestHasSameNamespaceAsTestedClass/tests/' . $filePath],
19+
'Tests'
20+
);
21+
$check->run($this);
22+
}
23+
24+
/** @return Generator<array-key, list<string>> */
25+
public function providerSuccess() : Generator
26+
{
27+
yield ['SameNamespaceTest.php'];
28+
yield ['SameNamespaceLinkedTest.php'];
29+
yield ['NoLinkTest.php'];
30+
}
31+
32+
/** @dataProvider providerFail */
33+
public function testFail(string $filePath, string $error) : void
34+
{
35+
$this->expectException(AssertionFailedError::class);
36+
$this->expectExceptionMessage($error);
37+
38+
$check = new EveryTestHasSameNamespaceAsTestedClass(
39+
[__DIR__ . '/Fixtures/EveryTestHasSameNamespaceAsTestedClass/tests/' . $filePath],
40+
'Tests'
41+
);
42+
$check->run($this);
43+
}
44+
45+
/** @return Generator<array-key, list<string>> */
46+
public function providerFail() : Generator
47+
{
48+
yield [
49+
'MissingAnnotationsTest.php',
50+
'is in the wrong namespace, has name different from tested class or is missing @testedClass annotation',
51+
];
52+
53+
yield [
54+
'NonexistentLinkTest.php',
55+
'is pointing to an non-existing class',
56+
];
57+
}
58+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cdn77\TestUtils\Tests\TestCheck\Fixtures\EveryTestHasSameNamespaceAsTestedClass;
6+
7+
final class SameNamespace
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cdn77\TestUtils\Tests\Tests\TestCheck\Fixtures\EveryTestHasSameNamespaceAsTestedClass;
6+
7+
final class MissingAnnotationsTest
8+
{
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cdn77\TestUtils\Tests\Tests\TestCheck\Fixtures\EveryTestHasSameNamespaceAsTestedClass;
6+
7+
/** @testedClass none */
8+
final class NoLinkTest
9+
{
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cdn77\TestUtils\Tests\Tests\TestCheck\Fixtures\EveryTestHasSameNamespaceAsTestedClass;
6+
7+
/** @testedClass Cdn77\TestUtils\Tests\TestCheck\Fixtures\EveryTestHasSameNamespaceAsTestedClass\Noexists */
8+
final class NonexistentLinkTest
9+
{
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cdn77\TestUtils\Tests\Tests\TestCheck\Fixtures\EveryTestHasSameNamespaceAsTestedClass;
6+
7+
/** @testedClass Cdn77\TestUtils\Tests\TestCheck\Fixtures\EveryTestHasSameNamespaceAsTestedClass\SameNamespace */
8+
final class SameNamespaceLinkedTest
9+
{
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cdn77\TestUtils\Tests\Tests\TestCheck\Fixtures\EveryTestHasSameNamespaceAsTestedClass;
6+
7+
final class SameNamespaceTest
8+
{
9+
}

0 commit comments

Comments
 (0)