diff --git a/.psalm/baseline.xml b/.psalm/baseline.xml index 1dc4b14bfd0..370318a9847 100644 --- a/.psalm/baseline.xml +++ b/.psalm/baseline.xml @@ -1207,7 +1207,7 @@ $class->newInstance($outputStream) - + argument argument atLeastVersion @@ -1224,7 +1224,6 @@ iniSettings mapToLegacyArray printerClass - run stop testSuiteLoaderClass unrecognizedOrderBy @@ -1355,6 +1354,13 @@ $tooFewColumnsRequested + + + $configuration + $testSuiteConfiguration->directories() + $testSuiteConfiguration->files() + + clover @@ -1724,16 +1730,6 @@ $position - - - new TestSuiteObject - - - $configuration - $testSuiteConfiguration->directories() - $testSuiteConfiguration->files() - - throw new SkippedTestError; diff --git a/ChangeLog-9.5.md b/ChangeLog-9.5.md index 579db03ce07..d2ac37ab48a 100644 --- a/ChangeLog-9.5.md +++ b/ChangeLog-9.5.md @@ -9,5 +9,6 @@ All notable changes of the PHPUnit 9.5 release series are documented in this fil * [#4490](https://github.com/sebastianbergmann/phpunit/issues/4490): Emit Error instead of Warning when test case class cannot be instantiated * [#4491](https://github.com/sebastianbergmann/phpunit/issues/4491): Emit Error instead of Warning when data provider does not work correctly * [#4492](https://github.com/sebastianbergmann/phpunit/issues/4492): Emit Error instead of Warning when test double configuration is invalid +* [#4493](https://github.com/sebastianbergmann/phpunit/issues/4493): Emit error when (configured) test directory does not exist [9.5.0]: https://github.com/sebastianbergmann/phpunit/compare/9.4...master diff --git a/src/TextUI/Command.php b/src/TextUI/Command.php index 0a32e9c7648..d4240cab072 100644 --- a/src/TextUI/Command.php +++ b/src/TextUI/Command.php @@ -382,10 +382,18 @@ protected function handleArguments(array $argv): void } if (!isset($this->arguments['test'])) { - $this->arguments['test'] = (new TestSuiteMapper)->map( - $this->arguments['configurationObject']->testSuite(), - $this->arguments['testsuite'] ?? '' - ); + try { + $this->arguments['test'] = (new TestSuiteMapper)->map( + $this->arguments['configurationObject']->testSuite(), + $this->arguments['testsuite'] ?? '' + ); + } catch (Exception $e) { + $this->printVersionString(); + + print $e->getMessage() . PHP_EOL; + + exit(TestRunner::EXCEPTION_EXIT); + } } } elseif (isset($this->arguments['bootstrap'])) { $this->handleBootstrap($this->arguments['bootstrap']); diff --git a/src/TextUI/Exception/TestDirectoryNotFoundException.php b/src/TextUI/Exception/TestDirectoryNotFoundException.php new file mode 100644 index 00000000000..770ad874293 --- /dev/null +++ b/src/TextUI/Exception/TestDirectoryNotFoundException.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TextUI; + +use function sprintf; +use RuntimeException; + +/** + * @internal This interface is not covered by the backward compatibility promise for PHPUnit + */ +final class TestDirectoryNotFoundException extends RuntimeException implements Exception +{ + public function __construct(string $path) + { + parent::__construct( + sprintf( + 'Test directory "%s" not found', + $path + ) + ); + } +} diff --git a/src/TextUI/Exception/TestFileNotFoundException.php b/src/TextUI/Exception/TestFileNotFoundException.php new file mode 100644 index 00000000000..7ffd2c78c06 --- /dev/null +++ b/src/TextUI/Exception/TestFileNotFoundException.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TextUI; + +use function sprintf; +use RuntimeException; + +/** + * @internal This interface is not covered by the backward compatibility promise for PHPUnit + */ +final class TestFileNotFoundException extends RuntimeException implements Exception +{ + public function __construct(string $path) + { + parent::__construct( + sprintf( + 'Test file "%s" not found', + $path + ) + ); + } +} diff --git a/src/TextUI/TestSuiteMapper.php b/src/TextUI/TestSuiteMapper.php index 2ea2c0ab1e5..6fa5c0b423f 100644 --- a/src/TextUI/TestSuiteMapper.php +++ b/src/TextUI/TestSuiteMapper.php @@ -12,6 +12,8 @@ use const PHP_VERSION; use function explode; use function in_array; +use function is_file; +use function strpos; use function version_compare; use PHPUnit\Framework\Exception as FrameworkException; use PHPUnit\Framework\TestSuite as TestSuiteObject; @@ -25,6 +27,8 @@ final class TestSuiteMapper { /** * @throws RuntimeException + * @throws TestDirectoryNotFoundException + * @throws TestFileNotFoundException */ public function map(TestSuiteCollection $configuration, string $filter): TestSuiteObject { @@ -51,19 +55,27 @@ public function map(TestSuiteCollection $configuration, string $filter): TestSui $exclude[] = $file->path(); } - $testSuite->addTestFiles( - (new Facade)->getFilesAsArray( - $directory->path(), - $directory->suffix(), - $directory->prefix(), - $exclude - ) + $files = (new Facade)->getFilesAsArray( + $directory->path(), + $directory->suffix(), + $directory->prefix(), + $exclude ); - $testSuiteEmpty = false; + if (!empty($files)) { + $testSuite->addTestFiles($files); + + $testSuiteEmpty = false; + } elseif (strpos($directory->path(), '*') === false) { + throw new TestDirectoryNotFoundException($directory->path()); + } } foreach ($testSuiteConfiguration->files() as $file) { + if (!is_file($file->path())) { + throw new TestFileNotFoundException($file->path()); + } + if (!version_compare(PHP_VERSION, $file->phpVersion(), $file->phpVersionOperator()->asString())) { continue; } diff --git a/tests/end-to-end/test-directory-does-not-exist/phpunit.xml b/tests/end-to-end/test-directory-does-not-exist/phpunit.xml new file mode 100644 index 00000000000..1cb2fee9012 --- /dev/null +++ b/tests/end-to-end/test-directory-does-not-exist/phpunit.xml @@ -0,0 +1,9 @@ + + + + + tests + + + diff --git a/tests/end-to-end/test-directory-does-not-exist/test-directory-does-not-exist.phpt b/tests/end-to-end/test-directory-does-not-exist/test-directory-does-not-exist.phpt new file mode 100644 index 00000000000..eb2a9f9a714 --- /dev/null +++ b/tests/end-to-end/test-directory-does-not-exist/test-directory-does-not-exist.phpt @@ -0,0 +1,14 @@ +--TEST-- +An error is emitted when a configured test directory does not exist +--FILE-- +