From a3107961655d3d1bb3e8ba44568f3549a053999c Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Tue, 29 Apr 2025 11:38:33 +0100 Subject: [PATCH] Fixes filtering tests --- overrides/TextUI/TestSuiteFilterProcessor.php | 9 +++++ src/Bootstrappers/BootExcludeList.php | 2 +- src/Bootstrappers/BootFiles.php | 2 +- src/Bootstrappers/BootOverrides.php | 2 +- src/Bootstrappers/BootSubscribers.php | 2 +- src/Concerns/Testable.php | 5 +-- src/Console/Help.php | 2 +- src/Console/Thanks.php | 2 +- src/Kernel.php | 2 +- src/Logging/Converter.php | 2 +- src/Plugins/Cache.php | 2 +- src/Plugins/Configuration.php | 2 +- src/Plugins/Coverage.php | 15 ++----- src/Plugins/Environment.php | 4 +- src/Plugins/Init.php | 4 +- src/Plugins/Only.php | 2 +- src/Plugins/Parallel.php | 6 +-- src/Plugins/Parallel/Handlers/Parallel.php | 2 +- .../Parallel/Paratest/CleanConsoleOutput.php | 1 + .../Parallel/Paratest/WrapperRunner.php | 13 ++++++- .../Parallel/Support/CompactPrinter.php | 2 +- src/Plugins/Verbose.php | 2 +- src/Repositories/DatasetsRepository.php | 2 +- src/Result.php | 6 +-- .../EnsureTestCaseIsInitiatedFilter.php | 39 +++++++++++++++++++ src/Support/Backtrace.php | 7 +--- src/Support/DatasetInfo.php | 4 +- src/Support/ExceptionTrace.php | 2 +- src/Support/Exporter.php | 2 +- src/Support/HigherOrderMessage.php | 2 +- src/Support/Str.php | 7 +--- 31 files changed, 98 insertions(+), 58 deletions(-) create mode 100644 src/Runner/Filter/EnsureTestCaseIsInitiatedFilter.php diff --git a/overrides/TextUI/TestSuiteFilterProcessor.php b/overrides/TextUI/TestSuiteFilterProcessor.php index 536ab208..e13d5c98 100644 --- a/overrides/TextUI/TestSuiteFilterProcessor.php +++ b/overrides/TextUI/TestSuiteFilterProcessor.php @@ -45,6 +45,7 @@ declare(strict_types=1); namespace PHPUnit\TextUI; use Pest\Plugins\Only; +use Pest\Runner\Filter\EnsureTestCaseIsInitiatedFilter; use PHPUnit\Event; use PHPUnit\Framework\TestSuite; use PHPUnit\Runner\Filter\Factory; @@ -66,6 +67,12 @@ final readonly class TestSuiteFilterProcessor { $factory = new Factory; + // @phpstan-ignore-next-line + (fn () => $this->filters[] = [ + 'className' => EnsureTestCaseIsInitiatedFilter::class, + 'argument' => '', + ])->call($factory); + if (! $configuration->hasFilter() && ! $configuration->hasGroups() && ! $configuration->hasExcludeGroups() && @@ -73,6 +80,8 @@ final readonly class TestSuiteFilterProcessor ! $configuration->hasTestsCovering() && ! $configuration->hasTestsUsing() && ! Only::isEnabled()) { + $suite->injectFilter($factory); + return; } diff --git a/src/Bootstrappers/BootExcludeList.php b/src/Bootstrappers/BootExcludeList.php index abd1552c..69d9dce1 100644 --- a/src/Bootstrappers/BootExcludeList.php +++ b/src/Bootstrappers/BootExcludeList.php @@ -17,7 +17,7 @@ final class BootExcludeList implements Bootstrapper * * @var array */ - private const EXCLUDE_LIST = [ + private const array EXCLUDE_LIST = [ 'bin', 'overrides', 'resources', diff --git a/src/Bootstrappers/BootFiles.php b/src/Bootstrappers/BootFiles.php index 2017a796..ea7e60fa 100644 --- a/src/Bootstrappers/BootFiles.php +++ b/src/Bootstrappers/BootFiles.php @@ -24,7 +24,7 @@ final class BootFiles implements Bootstrapper * * @var array */ - private const STRUCTURE = [ + private const array STRUCTURE = [ 'Expectations', 'Expectations.php', 'Helpers', diff --git a/src/Bootstrappers/BootOverrides.php b/src/Bootstrappers/BootOverrides.php index efbcf7a3..ae2fa070 100644 --- a/src/Bootstrappers/BootOverrides.php +++ b/src/Bootstrappers/BootOverrides.php @@ -17,7 +17,7 @@ final class BootOverrides implements Bootstrapper * * @var array */ - public const FILES = [ + public const array FILES = [ '53c246e5f416a39817ac81124cdd64ea8403038d01d7a202e1ffa486fbdf3fa7' => 'Runner/Filter/NameFilterIterator.php', '77ffb7647b583bd82e37962c6fbdc4b04d3344d8a2c1ed103e625ed1ff7cb5c2' => 'Runner/ResultCache/DefaultResultCache.php', 'd0e81317889ad88c707db4b08a94cadee4c9010d05ff0a759f04e71af5efed89' => 'Runner/TestSuiteLoader.php', diff --git a/src/Bootstrappers/BootSubscribers.php b/src/Bootstrappers/BootSubscribers.php index 57f98e33..7877b237 100644 --- a/src/Bootstrappers/BootSubscribers.php +++ b/src/Bootstrappers/BootSubscribers.php @@ -20,7 +20,7 @@ final readonly class BootSubscribers implements Bootstrapper * * @var array> */ - private const SUBSCRIBERS = [ + private const array SUBSCRIBERS = [ Subscribers\EnsureConfigurationIsAvailable::class, Subscribers\EnsureIgnorableTestCasesAreIgnored::class, Subscribers\EnsureKernelDumpIsFlushed::class, diff --git a/src/Concerns/Testable.php b/src/Concerns/Testable.php index 7199ad5b..9b8dc5f9 100644 --- a/src/Concerns/Testable.php +++ b/src/Concerns/Testable.php @@ -219,9 +219,6 @@ trait Testable { TestSuite::getInstance()->test = $this; - // Initialize test case properties - $this->__initializeTestCase(); - $method = TestSuite::getInstance()->tests->get(self::$__filename)->getMethod($this->name()); $method->setUp($this); @@ -270,7 +267,7 @@ trait Testable /** * Initialize test case properties from TestSuite. */ - private function __initializeTestCase(): void + public function __initializeTestCase(): void { // Return if the test case has already been initialized if (isset($this->__test)) { diff --git a/src/Console/Help.php b/src/Console/Help.php index 3d09d5f5..50823d59 100644 --- a/src/Console/Help.php +++ b/src/Console/Help.php @@ -16,7 +16,7 @@ final readonly class Help * * @var array */ - private const HELP_MESSAGES = [ + private const array HELP_MESSAGES = [ 'Pest Options:', ' --init Initialise a standard Pest configuration', ' --coverage Enable coverage and output to standard output', diff --git a/src/Console/Thanks.php b/src/Console/Thanks.php index 7e68f871..fc9f558a 100644 --- a/src/Console/Thanks.php +++ b/src/Console/Thanks.php @@ -22,7 +22,7 @@ final readonly class Thanks * * @var array */ - private const FUNDING_MESSAGES = [ + private const array FUNDING_MESSAGES = [ 'Star' => 'https://github.com/pestphp/pest', 'YouTube' => 'https://youtube.com/@nunomaduro', 'TikTok' => 'https://tiktok.com/@nunomaduro', diff --git a/src/Kernel.php b/src/Kernel.php index fc82574c..92716913 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -34,7 +34,7 @@ final readonly class Kernel * * @var array */ - private const BOOTSTRAPPERS = [ + private const array BOOTSTRAPPERS = [ Bootstrappers\BootOverrides::class, Bootstrappers\BootSubscribers::class, Bootstrappers\BootFiles::class, diff --git a/src/Logging/Converter.php b/src/Logging/Converter.php index b4560e22..4805a946 100644 --- a/src/Logging/Converter.php +++ b/src/Logging/Converter.php @@ -31,7 +31,7 @@ final readonly class Converter /** * The prefix for the test suite name. */ - private const PREFIX = 'P\\'; + private const string PREFIX = 'P\\'; /** * The state generator. diff --git a/src/Plugins/Cache.php b/src/Plugins/Cache.php index ea3abb78..3ae0433b 100644 --- a/src/Plugins/Cache.php +++ b/src/Plugins/Cache.php @@ -21,7 +21,7 @@ final class Cache implements HandlesArguments /** * The temporary folder. */ - private const TEMPORARY_FOLDER = __DIR__ + private const string TEMPORARY_FOLDER = __DIR__ .DIRECTORY_SEPARATOR .'..' .DIRECTORY_SEPARATOR diff --git a/src/Plugins/Configuration.php b/src/Plugins/Configuration.php index acae8fb4..27d07ab8 100644 --- a/src/Plugins/Configuration.php +++ b/src/Plugins/Configuration.php @@ -21,7 +21,7 @@ final class Configuration implements HandlesArguments, Terminable /** * The base PHPUnit file. */ - public const BASE_PHPUNIT_FILE = __DIR__ + public const string BASE_PHPUNIT_FILE = __DIR__ .DIRECTORY_SEPARATOR .'..' .DIRECTORY_SEPARATOR diff --git a/src/Plugins/Coverage.php b/src/Plugins/Coverage.php index a5061d25..712f5de5 100644 --- a/src/Plugins/Coverage.php +++ b/src/Plugins/Coverage.php @@ -17,20 +17,11 @@ use Symfony\Component\Console\Output\OutputInterface; */ final class Coverage implements AddsOutput, HandlesArguments { - /** - * @var string - */ - private const COVERAGE_OPTION = 'coverage'; + private const string COVERAGE_OPTION = 'coverage'; - /** - * @var string - */ - private const MIN_OPTION = 'min'; + private const string MIN_OPTION = 'min'; - /** - * @var string - */ - private const EXACTLY_OPTION = 'exactly'; + private const string EXACTLY_OPTION = 'exactly'; /** * Whether it should show the coverage or not. diff --git a/src/Plugins/Environment.php b/src/Plugins/Environment.php index 8ff10b4c..7edbbbd3 100644 --- a/src/Plugins/Environment.php +++ b/src/Plugins/Environment.php @@ -14,12 +14,12 @@ final class Environment implements HandlesArguments /** * The continuous integration environment. */ - public const CI = 'ci'; + public const string CI = 'ci'; /** * The local environment. */ - public const LOCAL = 'local'; + public const string LOCAL = 'local'; /** * The current environment. diff --git a/src/Plugins/Init.php b/src/Plugins/Init.php index eb87b086..c31dd759 100644 --- a/src/Plugins/Init.php +++ b/src/Plugins/Init.php @@ -20,12 +20,12 @@ final readonly class Init implements HandlesArguments /** * The option the triggers the init job. */ - private const INIT_OPTION = '--init'; + private const string INIT_OPTION = '--init'; /** * The files that will be created. */ - private const STUBS = [ + private const array STUBS = [ 'phpunit.xml.stub' => 'phpunit.xml', 'Pest.php.stub' => 'tests/Pest.php', 'TestCase.php.stub' => 'tests/TestCase.php', diff --git a/src/Plugins/Only.php b/src/Plugins/Only.php index 0d958173..7c2809f1 100644 --- a/src/Plugins/Only.php +++ b/src/Plugins/Only.php @@ -15,7 +15,7 @@ final class Only implements Terminable /** * The temporary folder. */ - private const TEMPORARY_FOLDER = __DIR__ + private const string TEMPORARY_FOLDER = __DIR__ .DIRECTORY_SEPARATOR .'..' .DIRECTORY_SEPARATOR diff --git a/src/Plugins/Parallel.php b/src/Plugins/Parallel.php index 1632a050..94902823 100644 --- a/src/Plugins/Parallel.php +++ b/src/Plugins/Parallel.php @@ -23,9 +23,9 @@ final class Parallel implements HandlesArguments { use HandleArguments; - private const GLOBAL_PREFIX = 'PEST_PARALLEL_GLOBAL_'; + private const string GLOBAL_PREFIX = 'PEST_PARALLEL_GLOBAL_'; - private const HANDLERS = [ + private const array HANDLERS = [ Parallel\Handlers\Parallel::class, Parallel\Handlers\Pest::class, Parallel\Handlers\Laravel::class, @@ -34,7 +34,7 @@ final class Parallel implements HandlesArguments /** * @var string[] */ - private const UNSUPPORTED_ARGUMENTS = ['--todo', '--todos', '--retry', '--notes', '--issue', '--pr', '--pull-request']; + private const array UNSUPPORTED_ARGUMENTS = ['--todo', '--todos', '--retry', '--notes', '--issue', '--pr', '--pull-request']; /** * Whether the given command line arguments indicate that the test suite should be run in parallel. diff --git a/src/Plugins/Parallel/Handlers/Parallel.php b/src/Plugins/Parallel/Handlers/Parallel.php index 76a59af6..d99139b2 100644 --- a/src/Plugins/Parallel/Handlers/Parallel.php +++ b/src/Plugins/Parallel/Handlers/Parallel.php @@ -18,7 +18,7 @@ final class Parallel implements HandlesArguments /** * The list of arguments to remove. */ - private const ARGS_TO_REMOVE = [ + private const array ARGS_TO_REMOVE = [ '--parallel', '-p', '--no-output', diff --git a/src/Plugins/Parallel/Paratest/CleanConsoleOutput.php b/src/Plugins/Parallel/Paratest/CleanConsoleOutput.php index d2801ced..cf5272b1 100644 --- a/src/Plugins/Parallel/Paratest/CleanConsoleOutput.php +++ b/src/Plugins/Parallel/Paratest/CleanConsoleOutput.php @@ -11,6 +11,7 @@ final class CleanConsoleOutput extends ConsoleOutput /** * {@inheritdoc} */ + #[\Override] protected function doWrite(string $message, bool $newline): void // @pest-arch-ignore-line { if ($this->isOpeningHeadline($message)) { diff --git a/src/Plugins/Parallel/Paratest/WrapperRunner.php b/src/Plugins/Parallel/Paratest/WrapperRunner.php index 48c88e7b..877a7373 100644 --- a/src/Plugins/Parallel/Paratest/WrapperRunner.php +++ b/src/Plugins/Parallel/Paratest/WrapperRunner.php @@ -50,7 +50,7 @@ final class WrapperRunner implements RunnerInterface /** * The time to sleep between cycles. */ - private const CYCLE_SLEEP = 10000; + private const int CYCLE_SLEEP = 10000; /** * The result printer. @@ -313,7 +313,6 @@ final class WrapperRunner implements RunnerInterface $testResult = unserialize($contents); assert($testResult instanceof TestResult); - $testResultSum = new TestResult( (int) $testResultSum->hasTests() + (int) $testResult->hasTests(), $testResultSum->numberOfTestsRun() + $testResult->numberOfTestsRun(), @@ -328,15 +327,25 @@ final class WrapperRunner implements RunnerInterface array_merge_recursive($testResultSum->testTriggeredPhpunitErrorEvents(), $testResult->testTriggeredPhpunitErrorEvents()), array_merge_recursive($testResultSum->testTriggeredPhpunitNoticeEvents(), $testResult->testTriggeredPhpunitNoticeEvents()), array_merge_recursive($testResultSum->testTriggeredPhpunitWarningEvents(), $testResult->testTriggeredPhpunitWarningEvents()), + // @phpstan-ignore-next-line array_merge_recursive($testResultSum->testRunnerTriggeredDeprecationEvents(), $testResult->testRunnerTriggeredDeprecationEvents()), + // @phpstan-ignore-next-line array_merge_recursive($testResultSum->testRunnerTriggeredNoticeEvents(), $testResult->testRunnerTriggeredNoticeEvents()), + // @phpstan-ignore-next-line array_merge_recursive($testResultSum->testRunnerTriggeredWarningEvents(), $testResult->testRunnerTriggeredWarningEvents()), + // @phpstan-ignore-next-line array_merge_recursive($testResultSum->errors(), $testResult->errors()), + // @phpstan-ignore-next-line array_merge_recursive($testResultSum->deprecations(), $testResult->deprecations()), + // @phpstan-ignore-next-line array_merge_recursive($testResultSum->notices(), $testResult->notices()), + // @phpstan-ignore-next-line array_merge_recursive($testResultSum->warnings(), $testResult->warnings()), + // @phpstan-ignore-next-line array_merge_recursive($testResultSum->phpDeprecations(), $testResult->phpDeprecations()), + // @phpstan-ignore-next-line array_merge_recursive($testResultSum->phpNotices(), $testResult->phpNotices()), + // @phpstan-ignore-next-line array_merge_recursive($testResultSum->phpWarnings(), $testResult->phpWarnings()), $testResultSum->numberOfIssuesIgnoredByBaseline() + $testResult->numberOfIssuesIgnoredByBaseline(), ); diff --git a/src/Plugins/Parallel/Support/CompactPrinter.php b/src/Plugins/Parallel/Support/CompactPrinter.php index bc2e1c3f..aa2da210 100644 --- a/src/Plugins/Parallel/Support/CompactPrinter.php +++ b/src/Plugins/Parallel/Support/CompactPrinter.php @@ -34,7 +34,7 @@ final class CompactPrinter /** * @var array> */ - private const LOOKUP_TABLE = [ + private const array LOOKUP_TABLE = [ '.' => ['gray', '.'], 'S' => ['yellow', 's'], 'T' => ['cyan', 't'], diff --git a/src/Plugins/Verbose.php b/src/Plugins/Verbose.php index e37938a3..9cec77de 100644 --- a/src/Plugins/Verbose.php +++ b/src/Plugins/Verbose.php @@ -16,7 +16,7 @@ final class Verbose implements HandlesArguments /** * The list of verbosity levels. */ - private const VERBOSITY_LEVELS = ['v', 'vv', 'vvv', 'q']; + private const array VERBOSITY_LEVELS = ['v', 'vv', 'vvv', 'q']; /** * {@inheritDoc} diff --git a/src/Repositories/DatasetsRepository.php b/src/Repositories/DatasetsRepository.php index 7d318a2d..3deee5bd 100644 --- a/src/Repositories/DatasetsRepository.php +++ b/src/Repositories/DatasetsRepository.php @@ -19,7 +19,7 @@ use function sprintf; */ final class DatasetsRepository { - private const SEPARATOR = '>>'; + private const string SEPARATOR = '>>'; /** * Holds the datasets. diff --git a/src/Result.php b/src/Result.php index 98e9e8b6..22e1e895 100644 --- a/src/Result.php +++ b/src/Result.php @@ -13,11 +13,11 @@ use PHPUnit\TextUI\Configuration\Configuration; */ final class Result { - private const SUCCESS_EXIT = 0; + private const int SUCCESS_EXIT = 0; - private const FAILURE_EXIT = 1; + private const int FAILURE_EXIT = 1; - private const EXCEPTION_EXIT = 2; + private const int EXCEPTION_EXIT = 2; /** * If the exit code is different from 0. diff --git a/src/Runner/Filter/EnsureTestCaseIsInitiatedFilter.php b/src/Runner/Filter/EnsureTestCaseIsInitiatedFilter.php new file mode 100644 index 00000000..614b38a4 --- /dev/null +++ b/src/Runner/Filter/EnsureTestCaseIsInitiatedFilter.php @@ -0,0 +1,39 @@ + $iterator + */ + public function __construct(RecursiveIterator $iterator) + { + parent::__construct($iterator); + } + + /** + * {@inheritdoc} + */ + public function accept(): bool + { + $test = $this->getInnerIterator()->current(); + + if ($test instanceof HasPrintableTestCaseName) { + /** @phpstan-ignore-next-line */ + $test->__initializeTestCase(); + } + + return true; + } +} diff --git a/src/Support/Backtrace.php b/src/Support/Backtrace.php index 03001976..652eb442 100644 --- a/src/Support/Backtrace.php +++ b/src/Support/Backtrace.php @@ -11,12 +11,9 @@ use Pest\Exceptions\ShouldNotHappen; */ final class Backtrace { - /** - * @var string - */ - private const FILE = 'file'; + private const string FILE = 'file'; - private const BACKTRACE_OPTIONS = DEBUG_BACKTRACE_IGNORE_ARGS; + private const int BACKTRACE_OPTIONS = DEBUG_BACKTRACE_IGNORE_ARGS; /** * Returns the current test file. diff --git a/src/Support/DatasetInfo.php b/src/Support/DatasetInfo.php index 46f39c41..c67f317c 100644 --- a/src/Support/DatasetInfo.php +++ b/src/Support/DatasetInfo.php @@ -11,9 +11,9 @@ use function Pest\testDirectory; */ final class DatasetInfo { - public const DATASETS_DIR_NAME = 'Datasets'; + public const string DATASETS_DIR_NAME = 'Datasets'; - public const DATASETS_FILE_NAME = 'Datasets.php'; + public const string DATASETS_FILE_NAME = 'Datasets.php'; public static function isInsideADatasetsDirectory(string $file): bool { diff --git a/src/Support/ExceptionTrace.php b/src/Support/ExceptionTrace.php index 9af6aa5b..9d4132e2 100644 --- a/src/Support/ExceptionTrace.php +++ b/src/Support/ExceptionTrace.php @@ -13,7 +13,7 @@ use Throwable; */ final class ExceptionTrace { - private const UNDEFINED_METHOD = 'Call to undefined method P\\'; + private const string UNDEFINED_METHOD = 'Call to undefined method P\\'; /** * Ensures the given closure reports the good execution context. diff --git a/src/Support/Exporter.php b/src/Support/Exporter.php index 169f4891..44367c08 100644 --- a/src/Support/Exporter.php +++ b/src/Support/Exporter.php @@ -15,7 +15,7 @@ final readonly class Exporter /** * The maximum number of items in an array to export. */ - private const MAX_ARRAY_ITEMS = 3; + private const int MAX_ARRAY_ITEMS = 3; /** * Creates a new Exporter instance. diff --git a/src/Support/HigherOrderMessage.php b/src/Support/HigherOrderMessage.php index 89c3e1f1..ce948244 100644 --- a/src/Support/HigherOrderMessage.php +++ b/src/Support/HigherOrderMessage.php @@ -13,7 +13,7 @@ use Throwable; */ final class HigherOrderMessage { - public const UNDEFINED_METHOD = 'Method %s does not exist'; + public const string UNDEFINED_METHOD = 'Method %s does not exist'; /** * An optional condition that will determine if the message will be executed. diff --git a/src/Support/Str.php b/src/Support/Str.php index 0e654bc8..67cff796 100644 --- a/src/Support/Str.php +++ b/src/Support/Str.php @@ -13,12 +13,9 @@ final class Str * Pool of alpha-numeric characters for generating (unsafe) random strings * from. */ - private const POOL = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + private const string POOL = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; - /** - * @var string - */ - private const PREFIX = '__pest_evaluable_'; + private const string PREFIX = '__pest_evaluable_'; /** * Create a (unsecure & non-cryptographically safe) random alpha-numeric