mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 15:57:21 +01:00
Add DatasetInfo support class
This commit is contained in:
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pest\Bootstrappers;
|
namespace Pest\Bootstrappers;
|
||||||
|
|
||||||
|
use Pest\Support\DatasetInfo;
|
||||||
use Pest\Support\Str;
|
use Pest\Support\Str;
|
||||||
use function Pest\testDirectory;
|
use function Pest\testDirectory;
|
||||||
use Pest\TestSuite;
|
use Pest\TestSuite;
|
||||||
@ -79,20 +80,9 @@ final class BootFiles
|
|||||||
{
|
{
|
||||||
$files = (new PhpUnitFileIterator)->getFilesAsArray($testsPath, '.php');
|
$files = (new PhpUnitFileIterator)->getFilesAsArray($testsPath, '.php');
|
||||||
|
|
||||||
foreach ($files as $fullPath) {
|
foreach ($files as $file) {
|
||||||
$filename = Str::afterLast($fullPath, DIRECTORY_SEPARATOR);
|
if (DatasetInfo::isADatasetsFile($file) || DatasetInfo::isInsideADatasetsDirectory($file)) {
|
||||||
|
$this->load($file);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,8 +9,8 @@ use Pest\PendingCalls\TestCall;
|
|||||||
use Pest\PendingCalls\UsesCall;
|
use Pest\PendingCalls\UsesCall;
|
||||||
use Pest\Repositories\DatasetsRepository;
|
use Pest\Repositories\DatasetsRepository;
|
||||||
use Pest\Support\Backtrace;
|
use Pest\Support\Backtrace;
|
||||||
|
use Pest\Support\DatasetInfo;
|
||||||
use Pest\Support\HigherOrderTapProxy;
|
use Pest\Support\HigherOrderTapProxy;
|
||||||
use Pest\Support\Str;
|
|
||||||
use Pest\TestSuite;
|
use Pest\TestSuite;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
@ -61,16 +61,7 @@ if (! function_exists('dataset')) {
|
|||||||
*/
|
*/
|
||||||
function dataset(string $name, Closure|iterable $dataset): void
|
function dataset(string $name, Closure|iterable $dataset): void
|
||||||
{
|
{
|
||||||
$file = Backtrace::datasetsFile();
|
$scope = DatasetInfo::scope(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;
|
|
||||||
}
|
|
||||||
|
|
||||||
DatasetsRepository::set($name, $dataset, $scope);
|
DatasetsRepository::set($name, $dataset, $scope);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
38
src/Support/DatasetInfo.php
Normal file
38
src/Support/DatasetInfo.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -59,24 +59,6 @@ final class Str
|
|||||||
return (string) preg_replace('/[^A-Z_a-z0-9\\\\]/', '', $code);
|
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.
|
* Get the portion of a string before the last occurrence of a given value.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -829,6 +829,26 @@
|
|||||||
✓ it can resolve builtin value types
|
✓ it can resolve builtin value types
|
||||||
✓ it cannot resolve a parameter without type
|
✓ it cannot resolve a parameter without type
|
||||||
|
|
||||||
|
PASS Tests\Unit\Support\DatasetInfo
|
||||||
|
✓ it can check if dataset is defined inside a Datasets directory with ('/var/www/project/tests/Datase...rs.php', true)
|
||||||
|
✓ it can check if dataset is defined inside a Datasets directory with ('/var/www/project/tests/Datasets.php', false)
|
||||||
|
✓ it can check if dataset is defined inside a Datasets directory with ('/var/www/project/tests/Featur...rs.php', true)
|
||||||
|
✓ it can check if dataset is defined inside a Datasets directory with ('/var/www/project/tests/Featur...rs.php', false)
|
||||||
|
✓ it can check if dataset is defined inside a Datasets directory with ('/var/www/project/tests/Featur...ts.php', false)
|
||||||
|
✓ it can check if dataset is defined inside a Datasets.php file with ('/var/www/project/tests/Datase...rs.php', false)
|
||||||
|
✓ it can check if dataset is defined inside a Datasets.php file with ('/var/www/project/tests/Datasets.php', true)
|
||||||
|
✓ it can check if dataset is defined inside a Datasets.php file with ('/var/www/project/tests/Featur...rs.php', false) #1
|
||||||
|
✓ it can check if dataset is defined inside a Datasets.php file with ('/var/www/project/tests/Featur...rs.php', false) #2
|
||||||
|
✓ it can check if dataset is defined inside a Datasets.php file with ('/var/www/project/tests/Featur...ts.php', true)
|
||||||
|
✓ it computes the dataset scope with ('/var/www/project/tests/Datase...rs.php', '/var/www/project/tests')
|
||||||
|
✓ it computes the dataset scope with ('/var/www/project/tests/Datasets.php', '/var/www/project/tests')
|
||||||
|
✓ it computes the dataset scope with ('/var/www/project/tests/Featur...rs.php', '/var/www/project/tests/Features')
|
||||||
|
✓ it computes the dataset scope with ('/var/www/project/tests/Featur...rs.php', '/var/www/project/tests/Featur...rs.php') #1
|
||||||
|
✓ it computes the dataset scope with ('/var/www/project/tests/Featur...ts.php', '/var/www/project/tests/Features')
|
||||||
|
✓ it computes the dataset scope with ('/var/www/project/tests/Featur...rs.php', '/var/www/project/tests/Featur...ollers')
|
||||||
|
✓ it computes the dataset scope with ('/var/www/project/tests/Featur...rs.php', '/var/www/project/tests/Featur...rs.php') #2
|
||||||
|
✓ it computes the dataset scope with ('/var/www/project/tests/Featur...ts.php', '/var/www/project/tests/Featur...ollers')
|
||||||
|
|
||||||
PASS Tests\Unit\Support\Reflection
|
PASS Tests\Unit\Support\Reflection
|
||||||
✓ it gets file name from closure
|
✓ it gets file name from closure
|
||||||
✓ it gets property values
|
✓ it gets property values
|
||||||
@ -860,4 +880,4 @@
|
|||||||
PASS Tests\Visual\Version
|
PASS Tests\Visual\Version
|
||||||
✓ visual snapshot of help command output
|
✓ visual snapshot of help command output
|
||||||
|
|
||||||
Tests: 4 incomplete, 2 todos, 18 skipped, 593 passed (1503 assertions)
|
Tests: 4 incomplete, 2 todos, 18 skipped, 611 passed (1521 assertions)
|
||||||
36
tests/Unit/Support/DatasetInfo.php
Normal file
36
tests/Unit/Support/DatasetInfo.php
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Pest\Support\DatasetInfo;
|
||||||
|
|
||||||
|
it('can check if dataset is defined inside a Datasets directory', function (string $file, bool $inside) {
|
||||||
|
expect(DatasetInfo::isInsideADatasetsDirectory($file))->toBe($inside);
|
||||||
|
})->with([
|
||||||
|
['file' => '/var/www/project/tests/Datasets/Numbers.php', 'inside' => true],
|
||||||
|
['file' => '/var/www/project/tests/Datasets.php', 'inside' => false],
|
||||||
|
['file' => '/var/www/project/tests/Features/Datasets/Numbers.php', 'inside' => true],
|
||||||
|
['file' => '/var/www/project/tests/Features/Numbers.php', 'inside' => false],
|
||||||
|
['file' => '/var/www/project/tests/Features/Datasets.php', 'inside' => false],
|
||||||
|
]);
|
||||||
|
|
||||||
|
it('can check if dataset is defined inside a Datasets.php file', function (string $path, bool $inside) {
|
||||||
|
expect(DatasetInfo::isADatasetsFile($path))->toBe($inside);
|
||||||
|
})->with([
|
||||||
|
['file' => '/var/www/project/tests/Datasets/Numbers.php', 'inside' => false],
|
||||||
|
['file' => '/var/www/project/tests/Datasets.php', 'inside' => true],
|
||||||
|
['file' => '/var/www/project/tests/Features/Datasets/Numbers.php', 'inside' => false],
|
||||||
|
['file' => '/var/www/project/tests/Features/Numbers.php', 'inside' => false],
|
||||||
|
['file' => '/var/www/project/tests/Features/Datasets.php', 'inside' => true],
|
||||||
|
]);
|
||||||
|
|
||||||
|
it('computes the dataset scope', function (string $file, string $scope) {
|
||||||
|
expect(DatasetInfo::scope($file))->toBe($scope);
|
||||||
|
})->with([
|
||||||
|
['file' => '/var/www/project/tests/Datasets/Numbers.php', 'scope' => '/var/www/project/tests'],
|
||||||
|
['file' => '/var/www/project/tests/Datasets.php', 'scope' => '/var/www/project/tests'],
|
||||||
|
['file' => '/var/www/project/tests/Features/Datasets/Numbers.php', 'scope' => '/var/www/project/tests/Features'],
|
||||||
|
['file' => '/var/www/project/tests/Features/Numbers.php', 'scope' => '/var/www/project/tests/Features/Numbers.php'],
|
||||||
|
['file' => '/var/www/project/tests/Features/Datasets.php', 'scope' => '/var/www/project/tests/Features'],
|
||||||
|
['file' => '/var/www/project/tests/Features/Controllers/Datasets/Numbers.php', 'scope' => '/var/www/project/tests/Features/Controllers'],
|
||||||
|
['file' => '/var/www/project/tests/Features/Controllers/Numbers.php', 'scope' => '/var/www/project/tests/Features/Controllers/Numbers.php'],
|
||||||
|
['file' => '/var/www/project/tests/Features/Controllers/Datasets.php', 'scope' => '/var/www/project/tests/Features/Controllers'],
|
||||||
|
]);
|
||||||
Reference in New Issue
Block a user