diff --git a/README.md b/README.md index 137ba38..08a55a3 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ use PHPUnit\Framework\TestCase; /** * @group integration - * @testedClass none + * @coversNothing */ final class SuiteComplianceTest extends TestCaseBase { @@ -191,54 +191,6 @@ yield 'Every test has same namespace as tested class' => [ ]; ``` -### Every test has same namespace as tested class - -Asserts that all test share same namespace with class they're testing. -Consider src namespace `Ns` and test namespace `Ns/Tests` then for test `Ns/Tests/UnitTest` must exist class `Ns/Unit`. - -You can use `@testedClass` annotation to link test with tested class - -```php -namespace Ns; - -final class Unit {} -``` - -:x: -```php -namespace Ns\Tests; - -final class NonexistentUnitTest extends TestCase {} -``` - -```php -namespace Ns\Tests\Sub; - -final class UnitTest extends TestCase {} -``` - -:heavy_check_mark: -```php -namespace Ns\Tests; - -final class UnitTest extends TestCase {} -``` - -```php -namespace Ns\Tests\Sub; - -/** @testedClass \Ns\Unit */ -final class UnitTest extends TestCase {} -``` - -Configured in test provider as - -```php -yield 'Every test has same namespace as tested class' => [ - new EveryTestHasSameNamespaceAsTestedClass($testFiles), -]; -``` - ### Every test inherits from testcase base class Consider you have a base for all tests and want each of them extend it. diff --git a/src/TestCheck/EveryTestHasSameNamespaceAsTestedClass.php b/src/TestCheck/EveryTestHasSameNamespaceAsTestedClass.php deleted file mode 100644 index 365a375..0000000 --- a/src/TestCheck/EveryTestHasSameNamespaceAsTestedClass.php +++ /dev/null @@ -1,98 +0,0 @@ -.+?)(?:\n| \*/)~'; - - private string $testsNamespaceSuffix; - - /** @param iterable $filePathNames */ - public function __construct(private iterable $filePathNames, string $testsNamespaceSuffix = 'Tests') - { - $this->testsNamespaceSuffix = '\\' . $testsNamespaceSuffix . '\\'; - } - - public function run(TestCase $testCaseContext): void - { - $testCaseContext::assertTrue(true); - - foreach ($this->filePathNames as $file) { - $classReflection = new ReflectionClass(ClassExtractor::get($file)); - - $docComment = $classReflection->getDocComment(); - if ($docComment === false) { - $docComment = ''; - } - - preg_match(self::PATTERN, $docComment, $targetClassMatches); - - if ($targetClassMatches !== [] && $targetClassMatches['targetClass'] === 'none') { - continue; - } - - $className = $classReflection->getName(); - $classNameWithoutSuffix = substr($className, 0, -4); - $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; - } - - if (class_exists($classNameWithoutSuffix)) { - continue; - } - - if ($targetClassMatches === []) { - $testCaseContext::fail( - sprintf( - 'Test "%s" is in the wrong namespace, ' . - 'has name different from tested class or is missing @testedClass annotation', - $classReflection->getName(), - ), - ); - } - - /** @psalm-var class-string $targetClass */ - $targetClass = $targetClassMatches['targetClass']; - if (class_exists($targetClass)) { - continue; - } - - $testCaseContext::fail( - sprintf( - 'Test %s is pointing to an non-existing class "%s"', - $classReflection->getName(), - $targetClassMatches['targetClass'], - ), - ); - } - } -} diff --git a/tests/TestCheck/EveryTestHasSameNamespaceAsTestedClassTest.php b/tests/TestCheck/EveryTestHasSameNamespaceAsTestedClassTest.php deleted file mode 100644 index 5e4c8ae..0000000 --- a/tests/TestCheck/EveryTestHasSameNamespaceAsTestedClassTest.php +++ /dev/null @@ -1,58 +0,0 @@ -run($this); - } - - /** @return Generator> */ - public static 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> */ - public static 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', - ]; - } -} diff --git a/tests/TestCheck/Fixtures/EveryTestHasSameNamespaceAsTestedClass/SameNamespace.php b/tests/TestCheck/Fixtures/EveryTestHasSameNamespaceAsTestedClass/SameNamespace.php deleted file mode 100644 index c45cb5c..0000000 --- a/tests/TestCheck/Fixtures/EveryTestHasSameNamespaceAsTestedClass/SameNamespace.php +++ /dev/null @@ -1,9 +0,0 @@ -