make test directory configurable

This commit is contained in:
Brian Faust
2021-05-03 18:28:20 +03:00
parent d2babb1331
commit 567af55a19
11 changed files with 43 additions and 15 deletions

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Pest\Actions;
use Pest\Support\Str;
use function Pest\testDirectory;
use PHPUnit\TextUI\Configuration\Configuration;
use PHPUnit\Util\FileLoader;
use RecursiveDirectoryIterator;
@ -33,7 +34,7 @@ final class LoadStructure
*/
public static function in(string $rootPath): void
{
$testsPath = $rootPath . DIRECTORY_SEPARATOR . 'tests';
$testsPath = $rootPath . DIRECTORY_SEPARATOR . testDirectory();
$load = function ($filename): bool {
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\Str;
use Pest\Exceptions\InvalidConsoleArgument;
use function Pest\testDirectory;
/**
* @internal
@ -36,7 +37,7 @@ final class PestDatasetCommand extends Command
/** @var string $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 */
$target = base_path($relativePath);

View File

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

View File

@ -8,6 +8,7 @@ use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Pest\Exceptions\InvalidConsoleArgument;
use Pest\Support\Str;
use function Pest\testDirectory;
/**
* @internal
@ -38,7 +39,7 @@ final class PestTestCommand extends Command
$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,
ucfirst($name)
);

View File

@ -8,3 +8,8 @@ function version(): string
{
return '1.0.4';
}
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
{
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;
/**
* Holds the test path.
*
* @var string
*/
public $testPath;
/**
* Holds an instance of the test suite.
*
@ -76,7 +83,7 @@ final class TestSuite
/**
* 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->beforeEach = new BeforeEachRepository();
@ -85,15 +92,16 @@ final class TestSuite
$this->afterAll = new AfterAllRepository();
$this->rootPath = (string) realpath($rootPath);
$this->testPath = $testPath;
}
/**
* 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)) {
self::$instance = new TestSuite($rootPath);
if (is_string($rootPath) && is_string($testPath)) {
self::$instance = new TestSuite($rootPath, $testPath);
foreach (Plugin::$callables as $callable) {
$callable();