diff --git a/src/Box.php b/src/Box.php index 4c51b934b..c1e9acc57 100644 --- a/src/Box.php +++ b/src/Box.php @@ -124,6 +124,7 @@ public function startBuffering(): void } /** + * TODO: replace callable by Closure. * @param callable(SymbolsRegistry, string, string[]): void $dumpAutoload */ public function endBuffering(?callable $dumpAutoload): void diff --git a/src/Test/CommandTestCase.php b/src/Test/CommandTestCase.php index 33483611e..9aec12636 100644 --- a/src/Test/CommandTestCase.php +++ b/src/Test/CommandTestCase.php @@ -14,6 +14,7 @@ namespace KevinGH\Box\Test; +use Closure; use Fidry\Console\Bridge\Command\SymfonyCommand; use Fidry\Console\Command\Command; use Fidry\Console\Test\CommandTester; @@ -61,12 +62,12 @@ protected function tearDown(): void abstract protected function getCommand(): Command; /** - * @param callable(string):string $extraNormalizers + * @param Closure(string):string $extraNormalizers */ public function assertSameOutput( string $expectedOutput, int $expectedStatusCode, - callable ...$extraNormalizers, + Closure ...$extraNormalizers, ): void { OutputAssertions::assertSameOutput( $expectedOutput, diff --git a/tests/Configuration/ConfigurationFileTest.php b/tests/Configuration/ConfigurationFileTest.php index 77d77c71b..36a407ce0 100644 --- a/tests/Configuration/ConfigurationFileTest.php +++ b/tests/Configuration/ConfigurationFileTest.php @@ -14,6 +14,7 @@ namespace KevinGH\Box\Configuration; +use Closure; use Fidry\FileSystem\FS; use InvalidArgumentException; use KevinGH\Box\Json\JsonValidationException; @@ -139,7 +140,7 @@ public function test_the_files_can_be_configured(): void } #[DataProvider('configWithMainScriptProvider')] - public function test_the_main_script_file_is_always_ignored(callable $setUp, array $config, array $expectedFiles, array $expectedBinFiles): void + public function test_the_main_script_file_is_always_ignored(Closure $setUp, array $config, array $expectedFiles, array $expectedBinFiles): void { $setUp(); @@ -153,7 +154,7 @@ public function test_the_main_script_file_is_always_ignored(callable $setUp, arr } #[DataProvider('configWithGeneratedArtefactProvider')] - public function test_the_generated_artefact_is_always_ignored(callable $setUp, array $config, array $expectedFiles, array $expectedBinFiles): void + public function test_the_generated_artefact_is_always_ignored(Closure $setUp, array $config, array $expectedFiles, array $expectedBinFiles): void { $setUp(); @@ -1552,7 +1553,7 @@ public function test_files_are_autodiscovered_by_default(): void #[DataProvider('filesAutodiscoveryConfigProvider')] public function test_files_are_autodiscovered_unless_directory_or_finder_config_is_provided( - callable $setUp, + Closure $setUp, array $config, bool $expectedFilesAutodiscovery, ): void { diff --git a/tests/Configuration/ConfigurationTest.php b/tests/Configuration/ConfigurationTest.php index 256d916d1..86630a64f 100644 --- a/tests/Configuration/ConfigurationTest.php +++ b/tests/Configuration/ConfigurationTest.php @@ -14,6 +14,7 @@ namespace KevinGH\Box\Configuration; +use Closure; use DateTimeImmutable; use DateTimeInterface; use Fidry\FileSystem\FS; @@ -316,7 +317,7 @@ public function test_the_base_path_value_is_normalized(): void #[DataProvider('composerArtifactsProvider')] public function test_it_attempts_to_get_and_decode_the_json_and_lock_files( - callable $setup, + Closure $setup, ?string $expectedJsonPath, ?array $expectedJsonContents, ?string $expectedLockPath, diff --git a/tests/Console/ApplicationTest.php b/tests/Console/ApplicationTest.php index 0b37d35cd..bcef7c536 100644 --- a/tests/Console/ApplicationTest.php +++ b/tests/Console/ApplicationTest.php @@ -52,7 +52,7 @@ public function test_it_can_display_the_version_when_no_specific_version_is_give $expected, ExitCode::SUCCESS, $appTester, - DisplayNormalizer::createReplaceBoxVersionNormalizer(), + DisplayNormalizer::normalizeBoxVersion(...), ); } @@ -154,7 +154,7 @@ public function test_get_helper_menu(): void $expected, ExitCode::SUCCESS, $appTester, - DisplayNormalizer::createReplaceBoxVersionNormalizer(), + DisplayNormalizer::normalizeBoxVersion(...), ); } diff --git a/tests/Console/Command/CompileCommandTest.php b/tests/Console/Command/CompileCommandTest.php index f6e35c5bf..67376982f 100644 --- a/tests/Console/Command/CompileCommandTest.php +++ b/tests/Console/Command/CompileCommandTest.php @@ -14,6 +14,7 @@ namespace KevinGH\Box\Console\Command; +use Closure; use DirectoryIterator; use Fidry\Console\Bridge\Command\SymfonyCommand; use Fidry\Console\Command\Command; @@ -913,7 +914,7 @@ public function test_it_can_build_a_phar_file_in_verbose_mode(): void $this->assertSameOutput( $expected, ExitCode::SUCCESS, - self::createComposerPathNormalizer(), + self::normalizeComposerPath(...), ); } @@ -1050,7 +1051,7 @@ public function test_it_can_build_a_phar_file_in_very_verbose_mode(): void $this->assertSameOutput( $expected, ExitCode::SUCCESS, - self::createComposerPathNormalizer(), + self::normalizeComposerPath(...), ); } @@ -2997,79 +2998,75 @@ public static function aliasConfigProvider(): iterable yield [false]; } - private function createCompilerDisplayNormalizer(): callable + private function normalizeCompilerDisplay(string $output): string { - $tmp = $this->tmp; + $output = str_replace($this->tmp, '/path/to/tmp', $output); - return static function (string $output) use ($tmp): string { - $output = str_replace($tmp, '/path/to/tmp', $output); + $output = preg_replace( + '/Loading the configuration file[\s\n]+.*[\s\n\/]+.*box\.json[comment\<\>\n\s\/]*"\./', + 'Loading the configuration file "/path/to/box.json.dist".', + $output, + ); - $output = preg_replace( - '/Loading the configuration file[\s\n]+.*[\s\n\/]+.*box\.json[comment\<\>\n\s\/]*"\./', - 'Loading the configuration file "/path/to/box.json.dist".', - $output, - ); + $output = preg_replace( + '/You can inspect the generated PHAR( | *\n *\/\/ *)with( | *\n *\/\/ *)the( | *\n *\/\/ *)"info"( | *\n *\/\/ *)command/', + 'You can inspect the generated PHAR with the "info" command', + $output, + ); - $output = preg_replace( - '/You can inspect the generated PHAR( | *\n *\/\/ *)with( | *\n *\/\/ *)the( | *\n *\/\/ *)"info"( | *\n *\/\/ *)command/', - 'You can inspect the generated PHAR with the "info" command', - $output, - ); + $output = preg_replace( + '/\/\/ PHAR: (\d+ files?) \(\d+\.\d{2}K?B\)/', + '// PHAR: $1 (100B)', + $output, + ); - $output = preg_replace( - '/\/\/ PHAR: (\d+ files?) \(\d+\.\d{2}K?B\)/', - '// PHAR: $1 (100B)', - $output, - ); + $output = preg_replace( + '/\/\/ Memory usage: \d+\.\d{2}MB \(peak: \d+\.\d{2}MB\), time: .*(sec|s|ms)/', + '// Memory usage: 5.00MB (peak: 10.00MB), time: 0.00s', + $output, + ); - $output = preg_replace( - '/\/\/ Memory usage: \d+\.\d{2}MB \(peak: \d+\.\d{2}MB\), time: .*(sec|s|ms)/', - '// Memory usage: 5.00MB (peak: 10.00MB), time: 0.00s', - $output, - ); + $output = str_replace( + 'Xdebug', + 'xdebug', + $output, + ); - $output = str_replace( - 'Xdebug', - 'xdebug', - $output, - ); + $output = preg_replace( + '/\[debug\] Increased the maximum number of open file descriptors from \([^\)]+\) to \([^\)]+\)'.PHP_EOL.'/', + '', + $output, + ); - $output = preg_replace( - '/\[debug\] Increased the maximum number of open file descriptors from \([^\)]+\) to \([^\)]+\)'.PHP_EOL.'/', - '', - $output, - ); + $output = str_replace( + '[debug] Restored the maximum number of open file descriptors'.PHP_EOL, + '', + $output, + ); - $output = str_replace( - '[debug] Restored the maximum number of open file descriptors'.PHP_EOL, + if (extension_loaded('xdebug')) { + $output = preg_replace( + '/'.PHP_EOL.'You are running composer with xdebug enabled. This has a major impact on runtime performance. See https:\/[^\s]+'.PHP_EOL.'/', '', $output, ); + } - if (extension_loaded('xdebug')) { - $output = preg_replace( - '/'.PHP_EOL.'You are running composer with xdebug enabled. This has a major impact on runtime performance. See https:\/[^\s]+'.PHP_EOL.'/', - '', - $output, - ); - } - - return $output; - }; + return $output; } - private static function createComposerPathNormalizer(): callable + private static function normalizeComposerPath(string $output): string { - return static fn (string $output): string => preg_replace( + return preg_replace( '/(\/.*?composer(?:\.phar)?)/', '/usr/local/bin/composer', $output, ); } - private static function createComposerVersionNormalizer(): callable + private static function normalizeComposerVersion(string $output): string { - return static fn (string $output): string => preg_replace( + return preg_replace( '/> Version detected: ([\d.]+) \(Box requires \^2\.2\.0\)/', '> Version detected: 2.2.22 (Box requires ^2.2.0)', $output, @@ -3120,21 +3117,21 @@ private static function normalizeStub(string $pharStub): string } /** - * @param callable(string):string $extraNormalizers + * @param Closure(string):string $extraNormalizers */ private function assertSameOutput( string $expectedOutput, int $expectedStatusCode, - callable ...$extraNormalizers, + Closure ...$extraNormalizers, ): void { OutputAssertions::assertSameOutput( $expectedOutput, $expectedStatusCode, $this->commandTester, - BoxDisplayNormalizer::createReplaceBoxVersionNormalizer(), - $this->createCompilerDisplayNormalizer(), - self::createComposerPathNormalizer(), - self::createComposerVersionNormalizer(), + BoxDisplayNormalizer::normalizeBoxVersion(...), + $this->normalizeCompilerDisplay(...), + self::normalizeComposerPath(...), + self::normalizeComposerVersion(...), ...$extraNormalizers, ); } diff --git a/tests/Console/Command/ProcessCommandTest.php b/tests/Console/Command/ProcessCommandTest.php index a74f57861..1376a5291 100644 --- a/tests/Console/Command/ProcessCommandTest.php +++ b/tests/Console/Command/ProcessCommandTest.php @@ -14,6 +14,7 @@ namespace KevinGH\Box\Console\Command; +use Closure; use Fidry\Console\Command\Command; use Fidry\Console\ExitCode; use Fidry\FileSystem\FS; @@ -263,13 +264,13 @@ public function test_it_processes_a_file_and_displays_only_the_processed_content public function assertSameOutput( string $expectedOutput, int $expectedStatusCode, - callable ...$extraNormalizers, + Closure ...$extraNormalizers, ): void { parent::assertSameOutput( $expectedOutput, $expectedStatusCode, - DisplayNormalizer::createVarDumperObjectReferenceNormalizer(), - DisplayNormalizer::createLoadingFilePathOutputNormalizer(), + DisplayNormalizer::normalizeVarDumperObjectReference(...), + DisplayNormalizer::normalizeLoadingFilePathOutput(...), ...$extraNormalizers, ); } diff --git a/tests/Console/Command/ValidateCommandTest.php b/tests/Console/Command/ValidateCommandTest.php index 04651b17a..3ea6f894c 100644 --- a/tests/Console/Command/ValidateCommandTest.php +++ b/tests/Console/Command/ValidateCommandTest.php @@ -324,7 +324,7 @@ public function test_an_invalid_json_file_is_invalid(): void $this->assertSameOutput( $expected, ExitCode::FAILURE, - DisplayNormalizer::createLoadingFilePathOutputNormalizer(), + DisplayNormalizer::normalizeLoadingFilePathOutput(...), ); } @@ -384,7 +384,7 @@ public function test_an_incorrect_config_file_is_invalid(): void $this->assertSameOutput( $expected, ExitCode::FAILURE, - DisplayNormalizer::createLoadingFilePathOutputNormalizer(), + DisplayNormalizer::normalizeLoadingFilePathOutput(...), ); } diff --git a/tests/Console/DisplayNormalizer.php b/tests/Console/DisplayNormalizer.php index 88cfbe0b5..4dd455726 100644 --- a/tests/Console/DisplayNormalizer.php +++ b/tests/Console/DisplayNormalizer.php @@ -34,36 +34,27 @@ public static function removeTrailingSpaces(string $display): string return implode("\n", $lines); } - /** - * @return callable(string):string - */ - public static function createLoadingFilePathOutputNormalizer(): callable + public static function normalizeLoadingFilePathOutput(string $output): string { - return static fn ($output) => preg_replace( + return preg_replace( '/\s\/\/ Loading the configuration file([\s\S]*)box\.json[comment\<\>\n\s\/]*"\./', ' // Loading the configuration file "box.json".', $output, ); } - /** - * @return callable(string):string - */ - public static function createVarDumperObjectReferenceNormalizer(): callable + public static function normalizeVarDumperObjectReference(string $output): string { - return static fn ($output) => preg_replace( + return preg_replace( '/ \{#\d+/', ' {#140', $output, ); } - /** - * @return callable(string):string - */ - public static function createReplaceBoxVersionNormalizer(): callable + public static function normalizeBoxVersion(string $output): string { - return static fn (string $output): string => preg_replace( + return preg_replace( '/Box version .+@[a-z\d]{7}/', 'Box version x.x-dev@151e40a', $output, diff --git a/tests/Console/DisplayNormalizerTest.php b/tests/Console/DisplayNormalizerTest.php index a98060259..3f8ff3dc4 100644 --- a/tests/Console/DisplayNormalizerTest.php +++ b/tests/Console/DisplayNormalizerTest.php @@ -91,7 +91,7 @@ public static function test_it_can_normalize_an_object_reference_from_the_var_du ): void { $expected ??= $value; - $actual = DisplayNormalizer::createVarDumperObjectReferenceNormalizer()($value); + $actual = DisplayNormalizer::normalizeVarDumperObjectReference($value); self::assertSame($expected, $actual); }