From 4872720b73e2054a2615c72869eeb0bbe1b3fe3e Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Mon, 13 Jan 2025 14:49:57 +0100 Subject: [PATCH 1/3] Refactor for readability --- src/Metadata/Api/Groups.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Metadata/Api/Groups.php b/src/Metadata/Api/Groups.php index 65e6ae3b3f8..6b6826b5f6d 100644 --- a/src/Metadata/Api/Groups.php +++ b/src/Metadata/Api/Groups.php @@ -104,7 +104,9 @@ public function groups(string $className, string $methodName, bool $includeVirtu } } - return self::$groupCache[$key] = array_unique($groups); + self::$groupCache[$key] = array_unique($groups); + + return self::$groupCache[$key]; } /** From e42dd587585c658d5c9cdfc02dbb79af24a62009 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Mon, 13 Jan 2025 14:50:34 +0100 Subject: [PATCH 2/3] Fix handling of --covers / --uses CLI options with function targets --- src/Metadata/Api/Groups.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Metadata/Api/Groups.php b/src/Metadata/Api/Groups.php index 6b6826b5f6d..e5615abd204 100644 --- a/src/Metadata/Api/Groups.php +++ b/src/Metadata/Api/Groups.php @@ -13,6 +13,7 @@ use function array_key_exists; use function array_unique; use function assert; +use function ltrim; use function strtolower; use function trim; use PHPUnit\Framework\TestSize\TestSize; @@ -69,7 +70,7 @@ public function groups(string $className, string $methodName, bool $includeVirtu /** @phpstan-ignore booleanOr.alwaysTrue */ assert($metadata instanceof CoversClass || $metadata instanceof CoversFunction); - $groups[] = '__phpunit_covers_' . $this->canonicalizeName($metadata->asStringForCodeUnitMapper()); + $groups[] = '__phpunit_covers_' . $this->canonicalizeName(ltrim($metadata->asStringForCodeUnitMapper(), ':')); continue; } @@ -86,7 +87,7 @@ public function groups(string $className, string $methodName, bool $includeVirtu /** @phpstan-ignore booleanOr.alwaysTrue */ assert($metadata instanceof UsesClass || $metadata instanceof UsesFunction); - $groups[] = '__phpunit_uses_' . $this->canonicalizeName($metadata->asStringForCodeUnitMapper()); + $groups[] = '__phpunit_uses_' . $this->canonicalizeName(ltrim($metadata->asStringForCodeUnitMapper(), ':')); continue; } From 3b620e5df5a79f0bb42ce454b42d3922707c2bf1 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Mon, 13 Jan 2025 14:50:44 +0100 Subject: [PATCH 3/3] Add tests --- .../attribute-based-filtering/phpunit.xml | 2 +- .../src/autoload.php | 12 +++++++ .../attribute-based-filtering/src/f.php | 15 ++++++++ .../{CoversTest.php => CoversClassTest.php} | 2 +- .../tests/CoversFunctionTest.php | 22 ++++++++++++ .../tests/UsesFunctionTest.php | 22 ++++++++++++ ...{covers-csv.phpt => covers-class-csv.phpt} | 14 ++++---- .../group/{covers.phpt => covers-class.phpt} | 14 ++++---- .../end-to-end/cli/group/covers-function.phpt | 34 +++++++++++++++++++ .../cli/group/requires-php-extension.phpt | 2 +- .../{uses-csv.phpt => uses-class-csv.phpt} | 2 +- .../cli/group/{uses.phpt => uses-class.phpt} | 2 +- tests/end-to-end/cli/group/uses-function.phpt | 34 +++++++++++++++++++ 13 files changed, 158 insertions(+), 19 deletions(-) create mode 100644 tests/end-to-end/_files/attribute-based-filtering/src/autoload.php create mode 100644 tests/end-to-end/_files/attribute-based-filtering/src/f.php rename tests/end-to-end/_files/attribute-based-filtering/tests/{CoversTest.php => CoversClassTest.php} (91%) create mode 100644 tests/end-to-end/_files/attribute-based-filtering/tests/CoversFunctionTest.php create mode 100644 tests/end-to-end/_files/attribute-based-filtering/tests/UsesFunctionTest.php rename tests/end-to-end/cli/group/{covers-csv.phpt => covers-class-csv.phpt} (85%) rename tests/end-to-end/cli/group/{covers.phpt => covers-class.phpt} (83%) create mode 100644 tests/end-to-end/cli/group/covers-function.phpt rename tests/end-to-end/cli/group/{uses-csv.phpt => uses-class-csv.phpt} (98%) rename tests/end-to-end/cli/group/{uses.phpt => uses-class.phpt} (98%) create mode 100644 tests/end-to-end/cli/group/uses-function.phpt diff --git a/tests/end-to-end/_files/attribute-based-filtering/phpunit.xml b/tests/end-to-end/_files/attribute-based-filtering/phpunit.xml index 7f0012487d6..0025e227829 100644 --- a/tests/end-to-end/_files/attribute-based-filtering/phpunit.xml +++ b/tests/end-to-end/_files/attribute-based-filtering/phpunit.xml @@ -1,7 +1,7 @@ + bootstrap="src/autoload.php"> tests diff --git a/tests/end-to-end/_files/attribute-based-filtering/src/autoload.php b/tests/end-to-end/_files/attribute-based-filtering/src/autoload.php new file mode 100644 index 00000000000..72277ab24b3 --- /dev/null +++ b/tests/end-to-end/_files/attribute-based-filtering/src/autoload.php @@ -0,0 +1,12 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +require __DIR__ . '/f.php'; + +require __DIR__ . '/Foo.php'; diff --git a/tests/end-to-end/_files/attribute-based-filtering/src/f.php b/tests/end-to-end/_files/attribute-based-filtering/src/f.php new file mode 100644 index 00000000000..0c98490d30e --- /dev/null +++ b/tests/end-to-end/_files/attribute-based-filtering/src/f.php @@ -0,0 +1,15 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture\AttributeBasedFiltering; + +function f(): bool +{ + return true; +} diff --git a/tests/end-to-end/_files/attribute-based-filtering/tests/CoversTest.php b/tests/end-to-end/_files/attribute-based-filtering/tests/CoversClassTest.php similarity index 91% rename from tests/end-to-end/_files/attribute-based-filtering/tests/CoversTest.php rename to tests/end-to-end/_files/attribute-based-filtering/tests/CoversClassTest.php index e5595432ebd..6a5ecf417b5 100644 --- a/tests/end-to-end/_files/attribute-based-filtering/tests/CoversTest.php +++ b/tests/end-to-end/_files/attribute-based-filtering/tests/CoversClassTest.php @@ -13,7 +13,7 @@ use PHPUnit\Framework\TestCase; #[CoversClass(Foo::class)] -final class CoversTest extends TestCase +final class CoversClassTest extends TestCase { public function testOne(): void { diff --git a/tests/end-to-end/_files/attribute-based-filtering/tests/CoversFunctionTest.php b/tests/end-to-end/_files/attribute-based-filtering/tests/CoversFunctionTest.php new file mode 100644 index 00000000000..db81b6e2853 --- /dev/null +++ b/tests/end-to-end/_files/attribute-based-filtering/tests/CoversFunctionTest.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture\AttributeBasedFiltering; + +use PHPUnit\Framework\Attributes\CoversFunction; +use PHPUnit\Framework\TestCase; + +#[CoversFunction('PHPUnit\TestFixture\AttributeBasedFiltering\f')] +final class CoversFunctionTest extends TestCase +{ + public function testOne(): void + { + $this->assertTrue(true); + } +} diff --git a/tests/end-to-end/_files/attribute-based-filtering/tests/UsesFunctionTest.php b/tests/end-to-end/_files/attribute-based-filtering/tests/UsesFunctionTest.php new file mode 100644 index 00000000000..aad32abfea7 --- /dev/null +++ b/tests/end-to-end/_files/attribute-based-filtering/tests/UsesFunctionTest.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture\AttributeBasedFiltering; + +use PHPUnit\Framework\Attributes\UsesFunction; +use PHPUnit\Framework\TestCase; + +#[UsesFunction('PHPUnit\TestFixture\AttributeBasedFiltering\f')] +final class UsesFunctionTest extends TestCase +{ + public function testOne(): void + { + $this->assertTrue(true); + } +} diff --git a/tests/end-to-end/cli/group/covers-csv.phpt b/tests/end-to-end/cli/group/covers-class-csv.phpt similarity index 85% rename from tests/end-to-end/cli/group/covers-csv.phpt rename to tests/end-to-end/cli/group/covers-class-csv.phpt index 4ff5a6b21f4..ebaaeea1e6c 100644 --- a/tests/end-to-end/cli/group/covers-csv.phpt +++ b/tests/end-to-end/cli/group/covers-class-csv.phpt @@ -17,18 +17,18 @@ PHPUnit Started (PHPUnit %s using %s) Test Runner Triggered Warning (Using comma-separated values with --covers is deprecated and will no longer work in PHPUnit 12. You can use --covers multiple times instead.) Test Runner Configured Event Facade Sealed -Test Suite Loaded (3 tests) +Test Suite Loaded (5 tests) Test Runner Started Test Suite Sorted Test Suite Filtered (1 test) Test Runner Execution Started (1 test) Test Suite Started (CLI Arguments, 1 test) -Test Suite Started (PHPUnit\TestFixture\AttributeBasedFiltering\CoversTest, 1 test) -Test Preparation Started (PHPUnit\TestFixture\AttributeBasedFiltering\CoversTest::testOne) -Test Prepared (PHPUnit\TestFixture\AttributeBasedFiltering\CoversTest::testOne) -Test Passed (PHPUnit\TestFixture\AttributeBasedFiltering\CoversTest::testOne) -Test Finished (PHPUnit\TestFixture\AttributeBasedFiltering\CoversTest::testOne) -Test Suite Finished (PHPUnit\TestFixture\AttributeBasedFiltering\CoversTest, 1 test) +Test Suite Started (PHPUnit\TestFixture\AttributeBasedFiltering\CoversClassTest, 1 test) +Test Preparation Started (PHPUnit\TestFixture\AttributeBasedFiltering\CoversClassTest::testOne) +Test Prepared (PHPUnit\TestFixture\AttributeBasedFiltering\CoversClassTest::testOne) +Test Passed (PHPUnit\TestFixture\AttributeBasedFiltering\CoversClassTest::testOne) +Test Finished (PHPUnit\TestFixture\AttributeBasedFiltering\CoversClassTest::testOne) +Test Suite Finished (PHPUnit\TestFixture\AttributeBasedFiltering\CoversClassTest, 1 test) Test Suite Finished (CLI Arguments, 1 test) Test Runner Execution Finished Test Runner Finished diff --git a/tests/end-to-end/cli/group/covers.phpt b/tests/end-to-end/cli/group/covers-class.phpt similarity index 83% rename from tests/end-to-end/cli/group/covers.phpt rename to tests/end-to-end/cli/group/covers-class.phpt index 404040369ca..a573705ec4a 100644 --- a/tests/end-to-end/cli/group/covers.phpt +++ b/tests/end-to-end/cli/group/covers-class.phpt @@ -16,18 +16,18 @@ require_once __DIR__ . '/../../../bootstrap.php'; PHPUnit Started (PHPUnit %s using %s) Test Runner Configured Event Facade Sealed -Test Suite Loaded (3 tests) +Test Suite Loaded (5 tests) Test Runner Started Test Suite Sorted Test Suite Filtered (1 test) Test Runner Execution Started (1 test) Test Suite Started (CLI Arguments, 1 test) -Test Suite Started (PHPUnit\TestFixture\AttributeBasedFiltering\CoversTest, 1 test) -Test Preparation Started (PHPUnit\TestFixture\AttributeBasedFiltering\CoversTest::testOne) -Test Prepared (PHPUnit\TestFixture\AttributeBasedFiltering\CoversTest::testOne) -Test Passed (PHPUnit\TestFixture\AttributeBasedFiltering\CoversTest::testOne) -Test Finished (PHPUnit\TestFixture\AttributeBasedFiltering\CoversTest::testOne) -Test Suite Finished (PHPUnit\TestFixture\AttributeBasedFiltering\CoversTest, 1 test) +Test Suite Started (PHPUnit\TestFixture\AttributeBasedFiltering\CoversClassTest, 1 test) +Test Preparation Started (PHPUnit\TestFixture\AttributeBasedFiltering\CoversClassTest::testOne) +Test Prepared (PHPUnit\TestFixture\AttributeBasedFiltering\CoversClassTest::testOne) +Test Passed (PHPUnit\TestFixture\AttributeBasedFiltering\CoversClassTest::testOne) +Test Finished (PHPUnit\TestFixture\AttributeBasedFiltering\CoversClassTest::testOne) +Test Suite Finished (PHPUnit\TestFixture\AttributeBasedFiltering\CoversClassTest, 1 test) Test Suite Finished (CLI Arguments, 1 test) Test Runner Execution Finished Test Runner Finished diff --git a/tests/end-to-end/cli/group/covers-function.phpt b/tests/end-to-end/cli/group/covers-function.phpt new file mode 100644 index 00000000000..44b894c98b2 --- /dev/null +++ b/tests/end-to-end/cli/group/covers-function.phpt @@ -0,0 +1,34 @@ +--TEST-- +phpunit --covers PHPUnit\TestFixture\AttributeBasedFiltering\f +--FILE-- +run($_SERVER['argv']); +--EXPECTF-- +PHPUnit Started (PHPUnit %s using %s) +Test Runner Configured +Event Facade Sealed +Test Suite Loaded (5 tests) +Test Runner Started +Test Suite Sorted +Test Suite Filtered (1 test) +Test Runner Execution Started (1 test) +Test Suite Started (CLI Arguments, 1 test) +Test Suite Started (PHPUnit\TestFixture\AttributeBasedFiltering\CoversFunctionTest, 1 test) +Test Preparation Started (PHPUnit\TestFixture\AttributeBasedFiltering\CoversFunctionTest::testOne) +Test Prepared (PHPUnit\TestFixture\AttributeBasedFiltering\CoversFunctionTest::testOne) +Test Passed (PHPUnit\TestFixture\AttributeBasedFiltering\CoversFunctionTest::testOne) +Test Finished (PHPUnit\TestFixture\AttributeBasedFiltering\CoversFunctionTest::testOne) +Test Suite Finished (PHPUnit\TestFixture\AttributeBasedFiltering\CoversFunctionTest, 1 test) +Test Suite Finished (CLI Arguments, 1 test) +Test Runner Execution Finished +Test Runner Finished +PHPUnit Finished (Shell Exit Code: 0) diff --git a/tests/end-to-end/cli/group/requires-php-extension.phpt b/tests/end-to-end/cli/group/requires-php-extension.phpt index 4f120f50264..4d2e66f33c2 100644 --- a/tests/end-to-end/cli/group/requires-php-extension.phpt +++ b/tests/end-to-end/cli/group/requires-php-extension.phpt @@ -16,7 +16,7 @@ require_once __DIR__ . '/../../../bootstrap.php'; PHPUnit Started (PHPUnit %s using %s) Test Runner Configured Event Facade Sealed -Test Suite Loaded (3 tests) +Test Suite Loaded (5 tests) Test Runner Started Test Suite Sorted Test Suite Filtered (1 test) diff --git a/tests/end-to-end/cli/group/uses-csv.phpt b/tests/end-to-end/cli/group/uses-class-csv.phpt similarity index 98% rename from tests/end-to-end/cli/group/uses-csv.phpt rename to tests/end-to-end/cli/group/uses-class-csv.phpt index ce67c008c45..8527c270d3f 100644 --- a/tests/end-to-end/cli/group/uses-csv.phpt +++ b/tests/end-to-end/cli/group/uses-class-csv.phpt @@ -17,7 +17,7 @@ PHPUnit Started (PHPUnit %s using %s) Test Runner Triggered Warning (Using comma-separated values with --uses is deprecated and will no longer work in PHPUnit 12. You can use --uses multiple times instead.) Test Runner Configured Event Facade Sealed -Test Suite Loaded (3 tests) +Test Suite Loaded (5 tests) Test Runner Started Test Suite Sorted Test Suite Filtered (1 test) diff --git a/tests/end-to-end/cli/group/uses.phpt b/tests/end-to-end/cli/group/uses-class.phpt similarity index 98% rename from tests/end-to-end/cli/group/uses.phpt rename to tests/end-to-end/cli/group/uses-class.phpt index 83b4a57761a..a2631cdf3b0 100644 --- a/tests/end-to-end/cli/group/uses.phpt +++ b/tests/end-to-end/cli/group/uses-class.phpt @@ -16,7 +16,7 @@ require_once __DIR__ . '/../../../bootstrap.php'; PHPUnit Started (PHPUnit %s using %s) Test Runner Configured Event Facade Sealed -Test Suite Loaded (3 tests) +Test Suite Loaded (5 tests) Test Runner Started Test Suite Sorted Test Suite Filtered (1 test) diff --git a/tests/end-to-end/cli/group/uses-function.phpt b/tests/end-to-end/cli/group/uses-function.phpt new file mode 100644 index 00000000000..2b71c3956bb --- /dev/null +++ b/tests/end-to-end/cli/group/uses-function.phpt @@ -0,0 +1,34 @@ +--TEST-- +phpunit --covers PHPUnit\TestFixture\AttributeBasedFiltering\f +--FILE-- +run($_SERVER['argv']); +--EXPECTF-- +PHPUnit Started (PHPUnit %s using %s) +Test Runner Configured +Event Facade Sealed +Test Suite Loaded (5 tests) +Test Runner Started +Test Suite Sorted +Test Suite Filtered (1 test) +Test Runner Execution Started (1 test) +Test Suite Started (CLI Arguments, 1 test) +Test Suite Started (PHPUnit\TestFixture\AttributeBasedFiltering\UsesFunctionTest, 1 test) +Test Preparation Started (PHPUnit\TestFixture\AttributeBasedFiltering\UsesFunctionTest::testOne) +Test Prepared (PHPUnit\TestFixture\AttributeBasedFiltering\UsesFunctionTest::testOne) +Test Passed (PHPUnit\TestFixture\AttributeBasedFiltering\UsesFunctionTest::testOne) +Test Finished (PHPUnit\TestFixture\AttributeBasedFiltering\UsesFunctionTest::testOne) +Test Suite Finished (PHPUnit\TestFixture\AttributeBasedFiltering\UsesFunctionTest, 1 test) +Test Suite Finished (CLI Arguments, 1 test) +Test Runner Execution Finished +Test Runner Finished +PHPUnit Finished (Shell Exit Code: 0)