Skip to content

Commit

Permalink
Test-cover EveryTestHasSameNamespaceAsTestedClass
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Oct 26, 2021
1 parent 283222f commit cee7377
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/infection.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ jobs:
uses: "ramsey/composer-install@v1"

- name: Run Infection
run: vendor/bin/roave-infection-static-analysis-plugin --min-msi=59 --min-covered-msi=93 --log-verbosity=none -s
run: vendor/bin/roave-infection-static-analysis-plugin --min-msi=59 --min-covered-msi=90 --log-verbosity=none -s
env:
INFECTION_BADGE_API_KEY: ${{ secrets.INFECTION_BADGE_API_KEY }}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
},
"autoload-dev": {
"psr-4": {
"Cdn77\\TestUtils\\Tests\\": "tests/"
"Cdn77\\TestUtils\\Tests\\": "tests/",
"Cdn77\\TestUtils\\Tests\\Tests\\TestCheck\\Fixtures\\EveryTestHasSameNamespaceAsTestedClass\\": "tests/TestCheck/Fixtures/EveryTestHasSameNamespaceAsTestedClass/tests"
}
},
"require": {
Expand Down
28 changes: 21 additions & 7 deletions src/TestCheck/EveryTestHasSameNamespaceAsTestedClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,25 @@
use function Safe\preg_match;
use function Safe\sprintf;
use function Safe\substr;
use function str_replace;
use function strlen;
use function strpos;
use function substr_replace;
use function trait_exists;

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

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

private string $testsNamespaceSuffix;

/** @param iterable<string> $filePathNames */
public function __construct(iterable $filePathNames)
public function __construct(iterable $filePathNames, string $testsNamespaceSuffix = 'Tests')
{
$this->filePathNames = $filePathNames;
$this->testsNamespaceSuffix = '\\' . $testsNamespaceSuffix . '\\';
}

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

$docComment = $classReflection->getDocComment();
if ($docComment === false) {
$testCaseContext::fail(
sprintf('Test "%s" is missing phpdoc. See other tests for examples', $classReflection->getName())
);
$docComment = '';
}

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

$className = $classReflection->getName();
$classNameWithoutSuffix = substr($className, 0, -4);
$testedClassName = str_replace('\Tests\\', '\\', $classNameWithoutSuffix);
$pos = strpos($classNameWithoutSuffix, $this->testsNamespaceSuffix);
if ($pos === false) {
$testedClassName = $classNameWithoutSuffix;
} else {
$testedClassName = substr_replace(
$classNameWithoutSuffix,
'\\',
$pos,
strlen($this->testsNamespaceSuffix)
);
}

if (class_exists($testedClassName) || trait_exists($testedClassName)) {
continue;
}
Expand Down
58 changes: 58 additions & 0 deletions tests/TestCheck/EveryTestHasSameNamespaceAsTestedClassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Cdn77\TestUtils\Tests\TestCheck;

use Cdn77\TestUtils\TestCheck\EveryTestHasSameNamespaceAsTestedClass;
use Cdn77\TestUtils\Tests\BaseTestCase;
use Generator;
use PHPUnit\Framework\AssertionFailedError;

final class EveryTestHasSameNamespaceAsTestedClassTest extends BaseTestCase
{
/** @dataProvider providerSuccess */
public function testSuccess(string $filePath) : void
{
$check = new EveryTestHasSameNamespaceAsTestedClass(
[__DIR__ . '/Fixtures/EveryTestHasSameNamespaceAsTestedClass/tests/' . $filePath],
'Tests'
);
$check->run($this);
}

/** @return Generator<array-key, list<string>> */
public function providerSuccess() : Generator
{
yield ['SameNamespaceTest.php'];
yield ['SameNamespaceLinkedTest.php'];
yield ['NoLinkTest.php'];
}

/** @dataProvider providerFail */
public function testFail(string $filePath, string $error) : void
{
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage($error);

$check = new EveryTestHasSameNamespaceAsTestedClass(
[__DIR__ . '/Fixtures/EveryTestHasSameNamespaceAsTestedClass/tests/' . $filePath],
'Tests'
);
$check->run($this);
}

/** @return Generator<array-key, list<string>> */
public function providerFail() : Generator
{
yield [
'MissingAnnotationsTest.php',
'is in the wrong namespace, has name different from tested class or is missing @testedClass annotation',
];

yield [
'NonexistentLinkTest.php',
'is pointing to an non-existing class',
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Cdn77\TestUtils\Tests\TestCheck\Fixtures\EveryTestHasSameNamespaceAsTestedClass;

final class SameNamespace
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Cdn77\TestUtils\Tests\Tests\TestCheck\Fixtures\EveryTestHasSameNamespaceAsTestedClass;

final class MissingAnnotationsTest
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Cdn77\TestUtils\Tests\Tests\TestCheck\Fixtures\EveryTestHasSameNamespaceAsTestedClass;

/** @testedClass none */
final class NoLinkTest
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Cdn77\TestUtils\Tests\Tests\TestCheck\Fixtures\EveryTestHasSameNamespaceAsTestedClass;

/** @testedClass Cdn77\TestUtils\Tests\TestCheck\Fixtures\EveryTestHasSameNamespaceAsTestedClass\Noexists */
final class NonexistentLinkTest
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Cdn77\TestUtils\Tests\Tests\TestCheck\Fixtures\EveryTestHasSameNamespaceAsTestedClass;

/** @testedClass Cdn77\TestUtils\Tests\TestCheck\Fixtures\EveryTestHasSameNamespaceAsTestedClass\SameNamespace */
final class SameNamespaceLinkedTest
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Cdn77\TestUtils\Tests\Tests\TestCheck\Fixtures\EveryTestHasSameNamespaceAsTestedClass;

final class SameNamespaceTest
{
}

0 comments on commit cee7377

Please sign in to comment.