Merge pull request #283 from faustbrian/test-directory-config

make test directory configurable
This commit is contained in:
Nuno Maduro
2021-05-05 11:07:10 +01:00
committed by GitHub
11 changed files with 43 additions and 15 deletions

View File

@ -28,11 +28,12 @@ use Symfony\Component\Console\Output\OutputInterface;
(new Provider())->register(); (new Provider())->register();
// get $rootPath based on $autoloadPath // get $rootPath based on $autoloadPath
$rootPath = dirname($autoloadPath, 2); $rootPath = dirname($autoloadPath, 2);
$argv = new ArgvInput();
$testSuite = TestSuite::getInstance($rootPath); $testSuite = TestSuite::getInstance($rootPath, $argv->getParameterOption('--test-directory', 'tests'));
$isDecorated = (new ArgvInput())->getParameterOption('--colors', 'always') !== 'never'; $isDecorated = $argv->getParameterOption('--colors', 'always') !== 'never';
$output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, $isDecorated); $output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, $isDecorated);
$container = Container::getInstance(); $container = Container::getInstance();
@ -41,5 +42,14 @@ use Symfony\Component\Console\Output\OutputInterface;
ValidatesEnvironment::in($testSuite); ValidatesEnvironment::in($testSuite);
// lets remove any arguments that PHPUnit does not understand
if ($argv->hasParameterOption('--test-directory')) {
foreach ($_SERVER['argv'] as $key => $value) {
if (strpos($value, '--test-directory') !== false) {
unset($_SERVER['argv'][$key]);
}
}
}
exit($container->get(Command::class)->run($_SERVER['argv'])); exit($container->get(Command::class)->run($_SERVER['argv']));
})(); })();

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Pest\Actions; namespace Pest\Actions;
use Pest\Support\Str; use Pest\Support\Str;
use function Pest\testDirectory;
use PHPUnit\TextUI\Configuration\Configuration; use PHPUnit\TextUI\Configuration\Configuration;
use PHPUnit\Util\FileLoader; use PHPUnit\Util\FileLoader;
use RecursiveDirectoryIterator; use RecursiveDirectoryIterator;
@ -33,7 +34,7 @@ final class LoadStructure
*/ */
public static function in(string $rootPath): void public static function in(string $rootPath): void
{ {
$testsPath = $rootPath . DIRECTORY_SEPARATOR . 'tests'; $testsPath = $rootPath . DIRECTORY_SEPARATOR . testDirectory();
$load = function ($filename): bool { $load = function ($filename): bool {
return file_exists($filename) && (bool) FileLoader::checkAndLoad($filename); return file_exists($filename) && (bool) FileLoader::checkAndLoad($filename);

View File

@ -8,6 +8,7 @@ use Illuminate\Console\Command;
use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\File;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Pest\Exceptions\InvalidConsoleArgument; use Pest\Exceptions\InvalidConsoleArgument;
use function Pest\testDirectory;
/** /**
* @internal * @internal
@ -36,7 +37,7 @@ final class PestDatasetCommand extends Command
/** @var string $name */ /** @var string $name */
$name = $this->argument('name'); $name = $this->argument('name');
$relativePath = sprintf('tests/Datasets/%s.php', ucfirst($name)); $relativePath = sprintf(testDirectory('Datasets/%s.php'), ucfirst($name));
/* @phpstan-ignore-next-line */ /* @phpstan-ignore-next-line */
$target = base_path($relativePath); $target = base_path($relativePath);

View File

@ -8,6 +8,7 @@ use Illuminate\Console\Command;
use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\File;
use Pest\Console\Thanks; use Pest\Console\Thanks;
use Pest\Exceptions\InvalidConsoleArgument; use Pest\Exceptions\InvalidConsoleArgument;
use function Pest\testDirectory;
/** /**
* @internal * @internal
@ -34,7 +35,7 @@ final class PestInstallCommand extends Command
public function handle(): void public function handle(): void
{ {
/* @phpstan-ignore-next-line */ /* @phpstan-ignore-next-line */
$pest = base_path('tests/Pest.php'); $pest = base_path(testDirectory('Pest.php'));
$stubs = 'stubs/Laravel'; $stubs = 'stubs/Laravel';
if (File::exists($pest)) { if (File::exists($pest)) {

View File

@ -8,6 +8,7 @@ use Illuminate\Console\Command;
use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\File;
use Pest\Exceptions\InvalidConsoleArgument; use Pest\Exceptions\InvalidConsoleArgument;
use Pest\Support\Str; use Pest\Support\Str;
use function Pest\testDirectory;
/** /**
* @internal * @internal
@ -38,7 +39,7 @@ final class PestTestCommand extends Command
$type = ((bool) $this->option('unit')) ? 'Unit' : (((bool) $this->option('dusk')) ? 'Browser' : 'Feature'); $type = ((bool) $this->option('unit')) ? 'Unit' : (((bool) $this->option('dusk')) ? 'Browser' : 'Feature');
$relativePath = sprintf('tests/%s/%s.php', $relativePath = sprintf(testDirectory('%s/%s.php'),
$type, $type,
ucfirst($name) ucfirst($name)
); );

View File

@ -8,3 +8,8 @@ function version(): string
{ {
return '1.1.0'; return '1.1.0';
} }
function testDirectory(string $file = ''): string
{
return TestSuite::getInstance()->testPath . '/' . $file;
}

View File

@ -22,7 +22,7 @@ final class Plugin
public static function uses(string ...$traits): void public static function uses(string ...$traits): void
{ {
self::$callables[] = function () use ($traits): void { self::$callables[] = function () use ($traits): void {
uses(...$traits)->in(TestSuite::getInstance()->rootPath . DIRECTORY_SEPARATOR . 'tests'); uses(...$traits)->in(TestSuite::getInstance()->rootPath . DIRECTORY_SEPARATOR . testDirectory());
}; };
} }
} }

View File

@ -66,6 +66,13 @@ final class TestSuite
*/ */
public $rootPath; public $rootPath;
/**
* Holds the test path.
*
* @var string
*/
public $testPath;
/** /**
* Holds an instance of the test suite. * Holds an instance of the test suite.
* *
@ -76,7 +83,7 @@ final class TestSuite
/** /**
* Creates a new instance of the test suite. * Creates a new instance of the test suite.
*/ */
public function __construct(string $rootPath) public function __construct(string $rootPath, string $testPath)
{ {
$this->beforeAll = new BeforeAllRepository(); $this->beforeAll = new BeforeAllRepository();
$this->beforeEach = new BeforeEachRepository(); $this->beforeEach = new BeforeEachRepository();
@ -85,15 +92,16 @@ final class TestSuite
$this->afterAll = new AfterAllRepository(); $this->afterAll = new AfterAllRepository();
$this->rootPath = (string) realpath($rootPath); $this->rootPath = (string) realpath($rootPath);
$this->testPath = $testPath;
} }
/** /**
* Returns the current instance of the test suite. * Returns the current instance of the test suite.
*/ */
public static function getInstance(string $rootPath = null): TestSuite public static function getInstance(string $rootPath = null, string $testPath = null): TestSuite
{ {
if (is_string($rootPath)) { if (is_string($rootPath) && is_string($testPath)) {
self::$instance = new TestSuite($rootPath); self::$instance = new TestSuite($rootPath, $testPath);
foreach (Plugin::$callables as $callable) { foreach (Plugin::$callables as $callable) {
$callable(); $callable();

View File

@ -18,7 +18,7 @@ test('default php unit tests', function () {
$testSuite->addTest($phpUnitTestCase); $testSuite->addTest($phpUnitTestCase);
expect($testSuite->tests())->toHaveCount(1); expect($testSuite->tests())->toHaveCount(1);
AddsTests::to($testSuite, new \Pest\TestSuite(getcwd())); AddsTests::to($testSuite, new \Pest\TestSuite(getcwd(), 'tests'));
expect($testSuite->tests())->toHaveCount(1); expect($testSuite->tests())->toHaveCount(1);
}); });
@ -27,6 +27,6 @@ it('removes warnings', function () {
$warningTestCase = new WarningTestCase('No tests found in class "Pest\TestCase".'); $warningTestCase = new WarningTestCase('No tests found in class "Pest\TestCase".');
$testSuite->addTest($warningTestCase); $testSuite->addTest($warningTestCase);
AddsTests::to($testSuite, new \Pest\TestSuite(getcwd())); AddsTests::to($testSuite, new \Pest\TestSuite(getcwd(), 'tests'));
expect($testSuite->tests())->toHaveCount(0); expect($testSuite->tests())->toHaveCount(0);
}); });

View File

@ -38,6 +38,7 @@ it('creates an instance and resolves also sub parameters', function () {
it('can resolve builtin value types', function () { it('can resolve builtin value types', function () {
$this->container->add('rootPath', getcwd()); $this->container->add('rootPath', getcwd());
$this->container->add('testPath', 'tests');
$instance = $this->container->get(TestSuite::class); $instance = $this->container->get(TestSuite::class);
expect($instance)->toBeInstanceOf(TestSuite::class); expect($instance)->toBeInstanceOf(TestSuite::class);

View File

@ -4,7 +4,7 @@ use Pest\Exceptions\TestAlreadyExist;
use Pest\TestSuite; use Pest\TestSuite;
it('does not allow to add the same test description twice', function () { it('does not allow to add the same test description twice', function () {
$testSuite = new TestSuite(getcwd()); $testSuite = new TestSuite(getcwd(), 'tests');
$test = function () {}; $test = function () {};
$testSuite->tests->set(new \Pest\Factories\TestCaseFactory(__FILE__, 'foo', $test)); $testSuite->tests->set(new \Pest\Factories\TestCaseFactory(__FILE__, 'foo', $test));
$this->expectException(TestAlreadyExist::class); $this->expectException(TestAlreadyExist::class);