diff --git a/bin/phpunit-check-annotation b/bin/phpunit-check-annotation index 32f7b84..48c9b13 100755 --- a/bin/phpunit-check-annotation +++ b/bin/phpunit-check-annotation @@ -23,7 +23,7 @@ if (!$autoloadFileFound) { try { $pathToTest = $argv[1]; - $errors = (new \EdmondsCommerce\PHPQA\PHPUnit\CheckForLargeAndMediumAnnotations())->main($pathToTest); + $errors = (new \EdmondsCommerce\PHPQA\PHPUnit\CheckAnnotations())->main($pathToTest); } catch (\RuntimeException $e) { echo "\n".$e->getMessage()."\n"; exit(1); diff --git a/qaConfig/phpunit.xml b/qaConfig/phpunit.xml index 4c1487b..105422b 100644 --- a/qaConfig/phpunit.xml +++ b/qaConfig/phpunit.xml @@ -9,7 +9,6 @@ timeoutForMediumTests="5" timeoutForSmallTests="1" timeoutForLargeTests="300" - failOnRisky="true" > diff --git a/src/Markdown/LinksChecker.php b/src/Markdown/LinksChecker.php index 12b5b67..085fd9a 100644 --- a/src/Markdown/LinksChecker.php +++ b/src/Markdown/LinksChecker.php @@ -162,24 +162,49 @@ public static function main(string $projectRootDirectory = null): int private static function validateHttpLink(array $link, array &$errors, int &$return) { - list(, $anchor, $link) = $link; - $context = stream_context_create(['http' => ['method' => 'HEAD']]); + list(, $anchor, $href) = $link; + static $checked = []; + $hashPos = strpos($href, '#'); + if ($hashPos > 0) { + $href = substr($href, 0, $hashPos); + } + if (isset($checked[$href])) { + return; + } + $checked[$href] = true; + #$start = microtime(true); + #fwrite(STDERR, "\n".'Validating link: '.$href); + $context = stream_context_create([ + 'http' => [ + 'method' => 'HEAD', + 'protocol_version' => 1.1, + 'header' => [ + 'Connection: close', + ], + ], + ]); $result = null; try { - $headers=get_headers($link, 0, $context); + $headers = get_headers($href, 0, $context); foreach ($headers as $header) { if (false !== strpos($header, ' 200 ')) { + #$time = round(microtime(true) - $start, 2); + #fwrite(STDERR, "\n".'OK ('.$time.' seconds): '.$href); + return; } } } catch (\Throwable $e) { } + $errors[] = \sprintf( "\nBad link for \"%s\" to \"%s\"\nresult: %s\n", $anchor, - $link, + $href, var_export($result, true) ); $return = 1; + #$time = round(microtime(true) - $start, 2); + #fwrite(STDERR, "\n".'Failed ('.$time.' seconds): '.$href); } } diff --git a/src/PHPUnit/CheckForLargeAndMediumAnnotations.php b/src/PHPUnit/CheckAnnotations.php similarity index 90% rename from src/PHPUnit/CheckForLargeAndMediumAnnotations.php rename to src/PHPUnit/CheckAnnotations.php index 3100061..064e594 100644 --- a/src/PHPUnit/CheckForLargeAndMediumAnnotations.php +++ b/src/PHPUnit/CheckAnnotations.php @@ -11,7 +11,7 @@ * * @package EdmondsCommerce\PHPQA\PHPUnit */ -class CheckForLargeAndMediumAnnotations +class CheckAnnotations { /** * @var string @@ -22,6 +22,11 @@ class CheckForLargeAndMediumAnnotations */ private $mediumPath; + /** + * @var string + */ + private $smallPath; + /** * @var array */ @@ -43,8 +48,10 @@ public function main(string $pathToTestsDirectory): array } $this->largePath = $pathToTestsDirectory.'/Large'; $this->mediumPath = $pathToTestsDirectory.'/Medium'; + $this->smallPath = $pathToTestsDirectory.'/Small'; $this->checkLarge(); $this->checkMedium(); + $this->checkSmall(); return $this->errors; } @@ -65,6 +72,14 @@ private function checkMedium(): void $this->checkDirectory($this->mediumPath, 'medium'); } + private function checkSmall(): void + { + if (!is_dir($this->smallPath)) { + return; + } + $this->checkDirectory($this->smallPath, 'small'); + } + private function checkDirectory(string $path, string $annotation) { foreach ($this->yieldTestFilesInPath($path) as $fileInfo) { diff --git a/src/PHPUnit/RerunCommandGenerator.php b/src/PHPUnit/RerunCommandGenerator.php index 7316546..f25b5fe 100644 --- a/src/PHPUnit/RerunCommandGenerator.php +++ b/src/PHPUnit/RerunCommandGenerator.php @@ -61,7 +61,7 @@ public function main(string $junitLogPath = null): string } - protected function load(string $contents) + private function load(string $contents) { libxml_use_internal_errors(true); @@ -80,7 +80,7 @@ protected function load(string $contents) * @throws \Exception * @SuppressWarnings(PHPMD.StaticAccess) */ - protected function getDefaultFilePath(): string + private function getDefaultFilePath(): string { return Helper::getProjectRootDirectory().'/var/qa/phpunit.junit.log.xml'; } diff --git a/src/Psr4Validator.php b/src/Psr4Validator.php index 5db8d60..7846d55 100644 --- a/src/Psr4Validator.php +++ b/src/Psr4Validator.php @@ -13,11 +13,11 @@ class Psr4Validator * @var array */ protected $decodedComposerJson; - private $parseErrors = []; - private $psr4Errors = []; - private $ignoreRegexPatterns; - private $ignoredFiles = []; - private $missingPaths = []; + private $parseErrors = []; + private $psr4Errors = []; + private $ignoreRegexPatterns; + private $ignoredFiles = []; + private $missingPaths = []; /** * Psr4Validator constructor. @@ -108,7 +108,7 @@ private function getActualNamespace(\SplFileInfo $fileInfo): string { $contents = \file_get_contents($fileInfo->getPathname()); \preg_match('%namespace\s+?([^;]+)%', $contents, $matches); - if (empty($matches) || !isset($matches[1])) { + if (empty($matches)) { $this->parseErrors[] = $fileInfo->getRealPath(); return ''; @@ -142,7 +142,7 @@ public function __construct(\RecursiveIteratorIterator $iterator) } } - public function compare($item1, $item2) + protected function compare($item1, $item2) { return strcmp($item2->getRealpath(), $item1->getRealpath()); } diff --git a/tests/Small/HelperTest.php b/tests/Small/HelperTest.php index 312d396..81d2e75 100644 --- a/tests/Small/HelperTest.php +++ b/tests/Small/HelperTest.php @@ -17,7 +17,7 @@ class HelperTest extends TestCase /** * @throws \Exception * @covers ::getComposerJsonDecoded() - * + * @small */ public function testItCanGetComposerJsonDecode() { @@ -28,7 +28,7 @@ public function testItCanGetComposerJsonDecode() /** * @throws \Exception * @covers ::getComposerJsonDecoded() - * + * @small */ public function testItWillThrowExceptionForInvalidComposerJson() { @@ -39,7 +39,7 @@ public function testItWillThrowExceptionForInvalidComposerJson() /** * @throws \Exception * @covers ::getProjectRootDirectory() - * + * @small */ public function testGetProjectRoot() { diff --git a/tests/Small/Markdown/LinksCheckerTest.php b/tests/Small/Markdown/LinksCheckerTest.php index 2fb911a..b368ff2 100644 --- a/tests/Small/Markdown/LinksCheckerTest.php +++ b/tests/Small/Markdown/LinksCheckerTest.php @@ -18,7 +18,7 @@ class LinksCheckerTest extends TestCase * @throws \Exception * @SuppressWarnings(PHPMD.StaticAccess) * @covers \EdmondsCommerce\PHPQA\Markdown\LinksChecker - * + * @small */ public function testInvalidProject() { @@ -42,7 +42,7 @@ public function testInvalidProject() * @throws \Exception * @SuppressWarnings(PHPMD.StaticAccess) * @covers \EdmondsCommerce\PHPQA\Markdown\LinksChecker - * + * @small */ public function testMainNoReadmeFile() { @@ -53,7 +53,7 @@ public function testMainNoReadmeFile() /** * @throws \Exception * @covers \EdmondsCommerce\PHPQA\Markdown\LinksChecker - * + * @small */ public function testValidNoDocsFolder() { @@ -65,7 +65,7 @@ public function testValidNoDocsFolder() /** * @covers \EdmondsCommerce\PHPQA\Markdown\LinksChecker - * + * @small */ public function testItHandlesNonFileLinks() { diff --git a/tests/Small/PHPUnit/CheckForLargeAndMediumAnnotationsTest.php b/tests/Small/PHPUnit/CheckAnnotationsTest.php similarity index 59% rename from tests/Small/PHPUnit/CheckForLargeAndMediumAnnotationsTest.php rename to tests/Small/PHPUnit/CheckAnnotationsTest.php index 2b92e6f..7006955 100644 --- a/tests/Small/PHPUnit/CheckForLargeAndMediumAnnotationsTest.php +++ b/tests/Small/PHPUnit/CheckAnnotationsTest.php @@ -2,32 +2,43 @@ namespace EdmondsCommerce\PHPQA\Tests\Small\PHPUnit; -use EdmondsCommerce\PHPQA\PHPUnit\CheckForLargeAndMediumAnnotations; +use EdmondsCommerce\PHPQA\PHPUnit\CheckAnnotations; use PHPUnit\Framework\TestCase; /** * Class CheckForLargeAndMediumAnnotationsTest * - * @coversDefaultClass \EdmondsCommerce\PHPQA\PHPUnit\CheckForLargeAndMediumAnnotations + * @coversDefaultClass \EdmondsCommerce\PHPQA\PHPUnit\CheckAnnotations * * @package EdmondsCommerce\PHPQA\Tests\Large\PHPUnit */ -class CheckForLargeAndMediumAnnotationsTest extends TestCase +class CheckAnnotationsTest extends TestCase { /** - * @var CheckForLargeAndMediumAnnotations + * @var CheckAnnotations */ private $checker; public function setup(): void { - $this->checker = new CheckForLargeAndMediumAnnotations(); + $this->checker = new CheckAnnotations(); } /** * @test - * @covers \EdmondsCommerce\PHPQA\PHPUnit\CheckForLargeAndMediumAnnotations - * @large + * @covers ::main() + * @small + */ + public function itThrowAnExceptionIfTestsPathIsInvalid() + { + $this->expectException(\InvalidArgumentException::class); + $this->checker->main('/invalid/path'); + } + + /** + * @test + * @covers \EdmondsCommerce\PHPQA\PHPUnit\CheckAnnotations + * @small */ public function itReturnsNoErrorsIfItsAllGood(): void { @@ -39,8 +50,25 @@ public function itReturnsNoErrorsIfItsAllGood(): void /** * @test - * @covers \EdmondsCommerce\PHPQA\PHPUnit\CheckForLargeAndMediumAnnotations - * @large + * @covers \EdmondsCommerce\PHPQA\PHPUnit\CheckAnnotations + * @small + */ + public function itFindsMissingSmallAnnotations(): void + { + $pathToTestsDirectory = __DIR__.'/../../assets/phpunitAnnotations/projectMissingSmall/tests'; + $expected = [ + 'SomethingTest.php' => [ + 'Failed finding @small for method: itDoesSomething', + ], + ]; + $actual = $this->checker->main($pathToTestsDirectory); + $this->assertSame($expected, $actual); + } + + /** + * @test + * @covers \EdmondsCommerce\PHPQA\PHPUnit\CheckAnnotations + * @small */ public function itFindsMissingMediumAnnotations(): void { @@ -56,8 +84,8 @@ public function itFindsMissingMediumAnnotations(): void /** * @test - * @covers \EdmondsCommerce\PHPQA\PHPUnit\CheckForLargeAndMediumAnnotations - * @large + * @covers \EdmondsCommerce\PHPQA\PHPUnit\CheckAnnotations + * @small */ public function itFindsMissingLargeAnnotations(): void { @@ -73,8 +101,8 @@ public function itFindsMissingLargeAnnotations(): void /** * @test - * @covers \EdmondsCommerce\PHPQA\PHPUnit\CheckForLargeAndMediumAnnotations - * @large + * @covers \EdmondsCommerce\PHPQA\PHPUnit\CheckAnnotations + * @small */ public function itReturnsNoErrorsIfNotApplicableToProject(): void { diff --git a/tests/Small/PHPUnit/RerunCommandGeneratorTest.php b/tests/Small/PHPUnit/RerunCommandGeneratorTest.php index e368e91..1bd06f1 100644 --- a/tests/Small/PHPUnit/RerunCommandGeneratorTest.php +++ b/tests/Small/PHPUnit/RerunCommandGeneratorTest.php @@ -15,7 +15,7 @@ class RerunCommandGeneratorTest extends TestCase { /** * @covers \EdmondsCommerce\PHPQA\PHPUnit\RerunCommandGenerator - * + * @small */ public function testCanParseFailuresAndErrors() { @@ -27,7 +27,7 @@ public function testCanParseFailuresAndErrors() /** * @covers \EdmondsCommerce\PHPQA\PHPUnit\RerunCommandGenerator - * + * @small */ public function testWillReturnNoFilterIfLogPathDoesNotExist() { @@ -38,7 +38,7 @@ public function testWillReturnNoFilterIfLogPathDoesNotExist() /** * @covers \EdmondsCommerce\PHPQA\PHPUnit\RerunCommandGenerator - * + * @small */ public function testWillReturnNoFilterIfLogIsEmpty() { @@ -49,7 +49,7 @@ public function testWillReturnNoFilterIfLogIsEmpty() /** * @covers \EdmondsCommerce\PHPQA\PHPUnit\RerunCommandGenerator - * + * @small */ public function testWillReturnNoFilterIfNoFailures() { @@ -60,7 +60,7 @@ public function testWillReturnNoFilterIfNoFailures() /** * @covers \EdmondsCommerce\PHPQA\PHPUnit\RerunCommandGenerator - * + * @small */ public function testItWillThrowExceptionIfXmlInvalid() { diff --git a/tests/Small/Psr4ValidatorTest.php b/tests/Small/Psr4ValidatorTest.php index 1957558..c4063db 100644 --- a/tests/Small/Psr4ValidatorTest.php +++ b/tests/Small/Psr4ValidatorTest.php @@ -18,7 +18,7 @@ class Psr4ValidatorTest extends TestCase /** * @throws \Exception * @covers \EdmondsCommerce\PHPQA\Psr4Validator - * + * @small */ public function testItFindsNoErrorsOnAValidProject(): void { @@ -36,7 +36,7 @@ public function testItFindsNoErrorsOnAValidProject(): void /** * @throws \Exception * @covers \EdmondsCommerce\PHPQA\Psr4Validator - * + * @small */ public function testItCanHandleOddComposerConfigs() { @@ -54,7 +54,7 @@ public function testItCanHandleOddComposerConfigs() /** * @throws \Exception * @covers \EdmondsCommerce\PHPQA\Psr4Validator - * + * @small */ public function testItFindsErrorsAndThrowsAnExceptionOnAnInvalidProject() { diff --git a/tests/assets/phpunitAnnotations/projectAllGood/tests/Small/SomethingTest.php b/tests/assets/phpunitAnnotations/projectAllGood/tests/Small/SomethingTest.php new file mode 100644 index 0000000..ad36ac7 --- /dev/null +++ b/tests/assets/phpunitAnnotations/projectAllGood/tests/Small/SomethingTest.php @@ -0,0 +1,15 @@ +