Skip to content

Commit

Permalink
Closes #4493
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Oct 27, 2020
1 parent ff7705d commit f714ce7
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 24 deletions.
20 changes: 8 additions & 12 deletions .psalm/baseline.xml
Expand Up @@ -1207,7 +1207,7 @@
<LessSpecificReturnStatement occurrences="1">
<code>$class-&gt;newInstance($outputStream)</code>
</LessSpecificReturnStatement>
<MissingThrowsDocblock occurrences="22">
<MissingThrowsDocblock occurrences="21">
<code>argument</code>
<code>argument</code>
<code>atLeastVersion</code>
Expand All @@ -1224,7 +1224,6 @@
<code>iniSettings</code>
<code>mapToLegacyArray</code>
<code>printerClass</code>
<code>run</code>
<code>stop</code>
<code>testSuiteLoaderClass</code>
<code>unrecognizedOrderBy</code>
Expand Down Expand Up @@ -1355,6 +1354,13 @@
<code>$tooFewColumnsRequested</code>
</UnusedVariable>
</file>
<file src="src/TextUI/TestSuiteMapper.php">
<UnusedMethodCall occurrences="3">
<code>$configuration</code>
<code>$testSuiteConfiguration-&gt;directories()</code>
<code>$testSuiteConfiguration-&gt;files()</code>
</UnusedMethodCall>
</file>
<file src="src/TextUI/XmlConfiguration/CodeCoverage/CodeCoverage.php">
<PossiblyUnusedMethod occurrences="8">
<code>clover</code>
Expand Down Expand Up @@ -1724,16 +1730,6 @@
<code>$position</code>
</PropertyNotSetInConstructor>
</file>
<file src="src/TextUI/XmlConfiguration/TestSuite/TestSuiteMapper.php">
<MissingThrowsDocblock occurrences="1">
<code>new TestSuiteObject</code>
</MissingThrowsDocblock>
<UnusedMethodCall occurrences="3">
<code>$configuration</code>
<code>$testSuiteConfiguration-&gt;directories()</code>
<code>$testSuiteConfiguration-&gt;files()</code>
</UnusedMethodCall>
</file>
<file src="src/Util/Annotation/DocBlock.php">
<MissingThrowsDocblock occurrences="1">
<code>throw new SkippedTestError;</code>
Expand Down
1 change: 1 addition & 0 deletions ChangeLog-9.5.md
Expand Up @@ -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
16 changes: 12 additions & 4 deletions src/TextUI/Command.php
Expand Up @@ -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']);
Expand Down
29 changes: 29 additions & 0 deletions src/TextUI/Exception/TestDirectoryNotFoundException.php
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* 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
)
);
}
}
29 changes: 29 additions & 0 deletions src/TextUI/Exception/TestFileNotFoundException.php
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* 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
)
);
}
}
28 changes: 20 additions & 8 deletions src/TextUI/TestSuiteMapper.php
Expand Up @@ -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;
Expand All @@ -25,6 +27,8 @@ final class TestSuiteMapper
{
/**
* @throws RuntimeException
* @throws TestDirectoryNotFoundException
* @throws TestFileNotFoundException
*/
public function map(TestSuiteCollection $configuration, string $filter): TestSuiteObject
{
Expand All @@ -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;
}
Expand Down
9 changes: 9 additions & 0 deletions tests/end-to-end/test-directory-does-not-exist/phpunit.xml
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd">
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
</phpunit>
@@ -0,0 +1,14 @@
--TEST--
An error is emitted when a configured test directory does not exist
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][1] = '--configuration';
$_SERVER['argv'][2] = __DIR__ . '/phpunit.xml';

require __DIR__ . '/../../bootstrap.php';

PHPUnit\TextUI\Command::main();
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Test directory "%s/test-directory-does-not-exist/tests" not found

0 comments on commit f714ce7

Please sign in to comment.