From ede4ea964eacbdab42197e14e158f79f79636be5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Sun, 20 Nov 2022 12:49:51 +0100 Subject: [PATCH] Move Makefile test to an auto-review namespace (#777) --- tests/AutoReview/E2ECollector.php | 95 ++++++++++++++++++++++ tests/AutoReview/E2ECollectorTest.php | 45 ++++++++++ tests/{ => AutoReview}/MakefileE2ETest.php | 33 ++++++-- 3 files changed, 164 insertions(+), 9 deletions(-) create mode 100644 tests/AutoReview/E2ECollector.php create mode 100644 tests/AutoReview/E2ECollectorTest.php rename tests/{ => AutoReview}/MakefileE2ETest.php (68%) diff --git a/tests/AutoReview/E2ECollector.php b/tests/AutoReview/E2ECollector.php new file mode 100644 index 000000000..10e3f2fdc --- /dev/null +++ b/tests/AutoReview/E2ECollector.php @@ -0,0 +1,95 @@ +, + * Pádraic Brady + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Humbug\PhpScoper\AutoReview; + +use Humbug\PhpScoper\NotInstantiable; +use SplFileInfo; +use Symfony\Component\Finder\Finder; +use function array_filter; +use function array_map; +use function in_array; +use function iterator_to_array; +use function sort; +use function substr; +use const SORT_STRING; + +final class E2ECollector +{ + use NotInstantiable; + + private const E2E_DIR = __DIR__.'/../../fixtures'; + + private const NON_E2E_TESTS = [ + 'set000', + 'set001', + 'set002', + 'set003', + 'set006', + 'set007', + 'set008', + 'set009', + 'set010', + 'set012', + ]; + + /** + * @return list + */ + public static function getE2ENames(): array + { + static $names; + + if (!isset($names)) { + $names = self::findE2ENames(); + } + + return $names; + } + + /** + * @return list + */ + private static function findE2ENames(): array + { + $finder = Finder::create() + ->directories() + ->in(self::E2E_DIR) + ->depth(0); + + $names = array_filter( + array_map( + self::extractName(...), + iterator_to_array($finder, false), + ), + ); + + sort($names, SORT_STRING); + + return $names; + } + + private static function extractName(SplFileInfo $fileInfo): ?string + { + $filename = $fileInfo->getFilename(); + + if (in_array($filename, self::NON_E2E_TESTS, true)) { + return null; + } + + $setNumber = substr($filename, 3, 3); + + return 'e2e_'.$setNumber; + } +} diff --git a/tests/AutoReview/E2ECollectorTest.php b/tests/AutoReview/E2ECollectorTest.php new file mode 100644 index 000000000..a2010721c --- /dev/null +++ b/tests/AutoReview/E2ECollectorTest.php @@ -0,0 +1,45 @@ +, + * Pádraic Brady + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Humbug\PhpScoper\AutoReview; + +use PHPUnit\Framework\TestCase; +use function count; +use function in_array; + +/** + * @covers \Humbug\PhpScoper\AutoReview\E2ECollector + * + * @internal + */ +class E2ECollectorTest extends TestCase +{ + public function test_it_collects_the_e2e_test_names(): void + { + $names = E2ECollector::getE2ENames(); + + self::assertGreaterThan(0, count($names)); + + foreach ($names as $name) { + self::assertMatchesRegularExpression('/^e2e_\d{3}$/', $name); + } + } + + public function test_it_ignores_non_e2e_tests(): void + { + $names = E2ECollector::getE2ENames(); + + self::assertFalse(in_array('e2e_000', $names, true)); + } +} diff --git a/tests/MakefileE2ETest.php b/tests/AutoReview/MakefileE2ETest.php similarity index 68% rename from tests/MakefileE2ETest.php rename to tests/AutoReview/MakefileE2ETest.php index 463f6db15..0ab5898db 100644 --- a/tests/MakefileE2ETest.php +++ b/tests/AutoReview/MakefileE2ETest.php @@ -12,7 +12,7 @@ * file that was distributed with this source code. */ -namespace Humbug\PhpScoper; +namespace Humbug\PhpScoper\AutoReview; use PHPUnit\Framework\TestCase; use function array_filter; @@ -31,20 +31,35 @@ */ class MakefileE2ETest extends TestCase { - public function test_the_e2e_test_executes_all_the_e2e_sub_rules(): void + private const MAKEFILE_PATH = __DIR__.'/../../Makefile'; + + private static string $makeFileContents; + + public static function setUpBeforeClass(): void { - $contents = file_get_contents(__DIR__.'/../Makefile'); + self::$makeFileContents = file_get_contents(self::MAKEFILE_PATH); + } - $mainE2ERule = $this->retrieveE2ERule($contents); - $e2eSubRules = $this->retrieveSubE2ERules($contents); + public function test_the_e2e_test_executes_all_the_e2e_sub_rules(): void + { + $mainE2ERule = self::retrieveE2ERule(self::$makeFileContents); + $e2eSubRules = self::retrieveSubE2ERules(self::$makeFileContents); self::assertSame($e2eSubRules, $mainE2ERule); } + public function test_it_lists_all_e2e_tests(): void + { + $expected = E2ECollector::getE2ENames(); + $actual = self::retrieveE2ERule(self::$makeFileContents); + + self::assertEqualsCanonicalizing($expected, $actual); + } + /** - * @return string[] + * @return list */ - private function retrieveE2ERule(string $makefileContents): array + private static function retrieveE2ERule(string $makefileContents): array { if (1 !== preg_match( '/e2e:(?[\p{L}\d_ ]+)/u', @@ -68,9 +83,9 @@ private function retrieveE2ERule(string $makefileContents): array } /** - * @return string[] + * @return list */ - private function retrieveSubE2ERules(string $makefileContents): array + private static function retrieveSubE2ERules(string $makefileContents): array { if (1 !== preg_match_all( '/(?e2e_\d+):/u',