Add DatasetInfo support class

This commit is contained in:
Fabio Ivona
2022-09-22 10:33:15 +02:00
parent cbee6e76b0
commit 80854d5f87
6 changed files with 101 additions and 44 deletions

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Pest\Bootstrappers;
use Pest\Support\DatasetInfo;
use Pest\Support\Str;
use function Pest\testDirectory;
use Pest\TestSuite;
@ -79,20 +80,9 @@ final class BootFiles
{
$files = (new PhpUnitFileIterator)->getFilesAsArray($testsPath, '.php');
foreach ($files as $fullPath) {
$filename = Str::afterLast($fullPath, DIRECTORY_SEPARATOR);
if ($filename === 'Datasets.php') {
$this->load($fullPath);
continue;
}
$directoryFullPath = Str::beforeLast($fullPath, DIRECTORY_SEPARATOR);
$directory = Str::afterLast($directoryFullPath, DIRECTORY_SEPARATOR);
if ($directory === 'Datasets') {
$this->load($fullPath);
foreach ($files as $file) {
if (DatasetInfo::isADatasetsFile($file) || DatasetInfo::isInsideADatasetsDirectory($file)) {
$this->load($file);
}
}
}

View File

@ -9,8 +9,8 @@ use Pest\PendingCalls\TestCall;
use Pest\PendingCalls\UsesCall;
use Pest\Repositories\DatasetsRepository;
use Pest\Support\Backtrace;
use Pest\Support\DatasetInfo;
use Pest\Support\HigherOrderTapProxy;
use Pest\Support\Str;
use Pest\TestSuite;
use PHPUnit\Framework\TestCase;
@ -61,16 +61,7 @@ if (! function_exists('dataset')) {
*/
function dataset(string $name, Closure|iterable $dataset): void
{
$file = Backtrace::datasetsFile();
$filename = Str::afterLast($file, DIRECTORY_SEPARATOR);
$scope = Str::beforeLast($file, DIRECTORY_SEPARATOR);
if (Str::afterLast($scope, DIRECTORY_SEPARATOR) === 'Datasets') {
$scope = Str::beforeLast($scope, DIRECTORY_SEPARATOR);
} elseif ($filename !== 'Datasets.php') {
$scope = $file;
}
$scope = DatasetInfo::scope(Backtrace::datasetsFile());
DatasetsRepository::set($name, $dataset, $scope);
}
}

View File

@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace Pest\Support;
/**
* @internal
*/
final class DatasetInfo
{
public const DATASETS_DIR_NAME = 'Datasets';
public const DATASETS_FILE_NAME = 'Datasets.php';
public static function isInsideADatasetsDirectory(string $file): bool
{
return basename(dirname($file)) === self::DATASETS_DIR_NAME;
}
public static function isADatasetsFile(string $file): bool
{
return basename($file) === self::DATASETS_FILE_NAME;
}
public static function scope(string $file): string
{
if (self::isInsideADatasetsDirectory($file)) {
return dirname($file, 2);
}
if (self::isADatasetsFile($file)) {
return dirname($file);
}
return $file;
}
}

View File

@ -59,24 +59,6 @@ final class Str
return (string) preg_replace('/[^A-Z_a-z0-9\\\\]/', '', $code);
}
/**
* Return the remainder of a string after the last occurrence of a given value.
*/
public static function afterLast(string $subject, string $search): string
{
if ($search === '') {
return $subject;
}
$position = strrpos($subject, $search);
if ($position === false) {
return $subject;
}
return substr($subject, $position + strlen($search));
}
/**
* Get the portion of a string before the last occurrence of a given value.
*/