diff --git a/src/Linters/NoTestPrefixInTests.php b/src/Linters/NoTestPrefixInTests.php new file mode 100644 index 00000000..0a3e5cfb --- /dev/null +++ b/src/Linters/NoTestPrefixInTests.php @@ -0,0 +1,43 @@ +extends; + } + + return $extends + && $extends->toString() === 'TestCase' + && $node instanceof Node\Stmt\ClassMethod + && $node->isPublic() + && str_starts_with($node->name->toString(), 'test'); + }); + + $traverser->addVisitor($visitor); + + $traverser->traverse($parser->parse($this->code)); + + return $visitor->getFoundNodes(); + } +} diff --git a/src/Presets/TightenPreset.php b/src/Presets/TightenPreset.php index a1f22514..e91cbbb8 100644 --- a/src/Presets/TightenPreset.php +++ b/src/Presets/TightenPreset.php @@ -24,6 +24,7 @@ public function getLinters(): array Linters\NoLeadingSlashesOnRoutePaths::class, Linters\NoSpaceAfterBladeDirectives::class, Linters\NoStringInterpolationWithoutBraces::class, + Linters\NoTestPrefixInTests::class, Linters\NoUnusedImports::class, Linters\OneLineBetweenClassVisibilityChanges::class, Linters\QualifiedNamesOnlyForClassName::class, diff --git a/tests/Linting/Linters/NoTestPrefixInTestsTest.php b/tests/Linting/Linters/NoTestPrefixInTestsTest.php new file mode 100644 index 00000000..22243a8a --- /dev/null +++ b/tests/Linting/Linters/NoTestPrefixInTestsTest.php @@ -0,0 +1,110 @@ +lint( + new NoTestPrefixInTests($file) + ); + + $this->assertEmpty($lints); + } + /** @test */ + public function does_not_trigger_on_private_method() + { + $file = <<lint( + new NoTestPrefixInTests($file) + ); + + $this->assertEmpty($lints); + } + + /** @test */ + public function catches_snake_case_test_method_with_test_prefix() + { + $file = <<lint( + new NoTestPrefixInTests($file) + ); + + $this->assertEquals(7, $lints[0]->getNode()->getLine()); + } + + /** @test */ + public function catches_camel_case_test_method_with_test_prefix() + { + $file = <<lint( + new NoTestPrefixInTests($file) + ); + + $this->assertEquals(7, $lints[0]->getNode()->getLine()); + } +}