Merge pull request #584 from fabio-ivona/datasets-scope

[2.x] Dataset scopes
This commit is contained in:
Nuno Maduro
2022-11-30 22:35:37 +00:00
committed by GitHub
17 changed files with 328 additions and 46 deletions

View File

@ -4,11 +4,13 @@ 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;
use RecursiveDirectoryIterator; use RecursiveDirectoryIterator;
use RecursiveIteratorIterator; use RecursiveIteratorIterator;
use SebastianBergmann\FileIterator\Facade as PhpUnitFileIterator;
/** /**
* @internal * @internal
@ -21,8 +23,6 @@ final class BootFiles
* @var array<int, string> * @var array<int, string>
*/ */
private const STRUCTURE = [ private const STRUCTURE = [
'Datasets',
'Datasets.php',
'Expectations', 'Expectations',
'Expectations.php', 'Expectations.php',
'Helpers', 'Helpers',
@ -56,6 +56,8 @@ final class BootFiles
$this->load($filename); $this->load($filename);
} }
} }
$this->bootDatasets($testsPath);
} }
/** /**
@ -73,4 +75,15 @@ final class BootFiles
include_once $filename; include_once $filename;
} }
private function bootDatasets(string $testsPath): void
{
$files = (new PhpUnitFileIterator)->getFilesAsArray($testsPath, '.php');
foreach ($files as $file) {
if (DatasetInfo::isADatasetsFile($file) || DatasetInfo::isInsideADatasetsDirectory($file)) {
$this->load($file);
}
}
}
} }

View File

@ -12,13 +12,13 @@ use Symfony\Component\Console\Exception\ExceptionInterface;
/** /**
* @internal * @internal
*/ */
final class DatasetAlreadyExist extends InvalidArgumentException implements ExceptionInterface, RenderlessEditor, RenderlessTrace final class DatasetAlreadyExists extends InvalidArgumentException implements ExceptionInterface, RenderlessEditor, RenderlessTrace
{ {
/** /**
* Creates a new Exception instance. * Creates a new Exception instance.
*/ */
public function __construct(string $name) public function __construct(string $name, string $scope)
{ {
parent::__construct(sprintf('A dataset with the name `%s` already exist.', $name)); parent::__construct(sprintf('A dataset with the name `%s` already exist in scope [%s].', $name, $scope));
} }
} }

View File

@ -9,6 +9,7 @@ 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\TestSuite; use Pest\TestSuite;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@ -60,7 +61,8 @@ if (! function_exists('dataset')) {
*/ */
function dataset(string $name, Closure|iterable $dataset): void function dataset(string $name, Closure|iterable $dataset): void
{ {
DatasetsRepository::set($name, $dataset); $scope = DatasetInfo::scope(Backtrace::datasetsFile());
DatasetsRepository::set($name, $dataset, $scope);
} }
} }

View File

@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Pest\Repositories; namespace Pest\Repositories;
use Closure; use Closure;
use Pest\Exceptions\DatasetAlreadyExist; use Pest\Exceptions\DatasetAlreadyExists;
use Pest\Exceptions\DatasetDoesNotExist; use Pest\Exceptions\DatasetDoesNotExist;
use Pest\Exceptions\ShouldNotHappen; use Pest\Exceptions\ShouldNotHappen;
use SebastianBergmann\Exporter\Exporter; use SebastianBergmann\Exporter\Exporter;
@ -17,6 +17,8 @@ use Traversable;
*/ */
final class DatasetsRepository final class DatasetsRepository
{ {
private const SEPARATOR = '>>';
/** /**
* Holds the datasets. * Holds the datasets.
* *
@ -36,13 +38,15 @@ final class DatasetsRepository
* *
* @param Closure|iterable<int|string, mixed> $data * @param Closure|iterable<int|string, mixed> $data
*/ */
public static function set(string $name, Closure|iterable $data): void public static function set(string $name, Closure|iterable $data, string $scope): void
{ {
if (array_key_exists($name, self::$datasets)) { $datasetKey = "$scope".self::SEPARATOR."$name";
throw new DatasetAlreadyExist($name);
if (array_key_exists("$datasetKey", self::$datasets)) {
throw new DatasetAlreadyExists($name, $scope);
} }
self::$datasets[$name] = $data; self::$datasets[$datasetKey] = $data;
} }
/** /**
@ -52,12 +56,12 @@ final class DatasetsRepository
*/ */
public static function with(string $filename, string $description, array $with): void public static function with(string $filename, string $description, array $with): void
{ {
self::$withs[$filename.'>>>'.$description] = $with; self::$withs["$filename".self::SEPARATOR."$description"] = $with;
} }
public static function has(string $filename, string $description): bool public static function has(string $filename, string $description): bool
{ {
return array_key_exists($filename.'>>>'.$description, self::$withs); return array_key_exists($filename.self::SEPARATOR.$description, self::$withs);
} }
/** /**
@ -67,9 +71,9 @@ final class DatasetsRepository
*/ */
public static function get(string $filename, string $description) public static function get(string $filename, string $description)
{ {
$dataset = self::$withs[$filename.'>>>'.$description]; $dataset = self::$withs[$filename.self::SEPARATOR.$description];
$dataset = self::resolve($description, $dataset); $dataset = self::resolve($dataset, $filename);
if ($dataset === null) { if ($dataset === null) {
throw ShouldNotHappen::fromMessage('Dataset [%s] not resolvable.'); throw ShouldNotHappen::fromMessage('Dataset [%s] not resolvable.');
@ -84,14 +88,14 @@ final class DatasetsRepository
* @param array<Closure|iterable<int|string, mixed>|string> $dataset * @param array<Closure|iterable<int|string, mixed>|string> $dataset
* @return array<string, mixed>|null * @return array<string, mixed>|null
*/ */
public static function resolve(string $description, array $dataset): array|null public static function resolve(array $dataset, string $currentTestFile): array|null
{ {
/* @phpstan-ignore-next-line */ /* @phpstan-ignore-next-line */
if (empty($dataset)) { if (empty($dataset)) {
return null; return null;
} }
$dataset = self::processDatasets($dataset); $dataset = self::processDatasets($dataset, $currentTestFile);
$datasetCombinations = self::getDatasetsCombinations($dataset); $datasetCombinations = self::getDatasetsCombinations($dataset);
@ -136,7 +140,7 @@ final class DatasetsRepository
* @param array<Closure|iterable<int|string, mixed>|string> $datasets * @param array<Closure|iterable<int|string, mixed>|string> $datasets
* @return array<array<mixed>> * @return array<array<mixed>>
*/ */
private static function processDatasets(array $datasets): array private static function processDatasets(array $datasets, string $currentTestFile): array
{ {
$processedDatasets = []; $processedDatasets = [];
@ -144,11 +148,7 @@ final class DatasetsRepository
$processedDataset = []; $processedDataset = [];
if (is_string($data)) { if (is_string($data)) {
if (! array_key_exists($data, self::$datasets)) { $datasets[$index] = self::getScopedDataset($data, $currentTestFile);
throw new DatasetDoesNotExist($data);
}
$datasets[$index] = self::$datasets[$data];
} }
if (is_callable($datasets[$index])) { if (is_callable($datasets[$index])) {
@ -174,6 +174,37 @@ final class DatasetsRepository
return $processedDatasets; return $processedDatasets;
} }
/**
* @return Closure|iterable<int|string, mixed>
*/
private static function getScopedDataset(string $name, string $currentTestFile)
{
$matchingDatasets = array_filter(self::$datasets, function (string $key) use ($name, $currentTestFile) {
[$datasetScope, $datasetName] = explode(self::SEPARATOR, $key);
if ($name !== $datasetName) {
return false;
}
if (! str_starts_with($currentTestFile, $datasetScope)) {
return false;
}
return true;
}, ARRAY_FILTER_USE_KEY);
$closestScopeDatasetKey = array_reduce(
array_keys($matchingDatasets),
fn ($keyA, $keyB) => $keyA !== null && strlen($keyA) > strlen($keyB) ? $keyA : $keyB
);
if ($closestScopeDatasetKey === null) {
throw new DatasetDoesNotExist($name);
}
return $matchingDatasets[$closestScopeDatasetKey];
}
/** /**
* @param array<array<mixed>> $combinations * @param array<array<mixed>> $combinations
* @return array<array<array<mixed>>> * @return array<array<array<mixed>>>

View File

@ -44,6 +44,30 @@ final class Backtrace
return $current[self::FILE]; return $current[self::FILE];
} }
/**
* Returns the current datasets file.
*/
public static function datasetsFile(): string
{
$current = null;
foreach (debug_backtrace(self::BACKTRACE_OPTIONS) as $trace) {
assert(array_key_exists(self::FILE, $trace));
if (Str::endsWith($trace['file'], 'Bootstrappers/BootFiles.php') || Str::endsWith($trace[self::FILE], 'overrides/Runner/TestSuiteLoader.php')) {
break;
}
$current = $trace;
}
if ($current === null) {
throw ShouldNotHappen::fromMessage('Dataset file not found.');
}
return $current[self::FILE];
}
/** /**
* Returns the filename that called the current function/method. * Returns the filename that called the current function/method.
*/ */

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

@ -29,7 +29,7 @@
✓ it does not append CoversNothing to other methods ✓ it does not append CoversNothing to other methods
✓ it throws exception if no class nor method has been found ✓ it throws exception if no class nor method has been found
PASS Tests\Features\Datasets PASS Tests\Features\DatasetsTests
✓ it throws exception if dataset does not exist ✓ it throws exception if dataset does not exist
✓ it throws exception if dataset already exist ✓ it throws exception if dataset already exist
✓ it sets closures ✓ it sets closures
@ -121,6 +121,7 @@
✓ it will not resolve a closure if it is type hinted as a callable with (Closure Object (...)) #2 ✓ it will not resolve a closure if it is type hinted as a callable with (Closure Object (...)) #2
✓ it can correctly resolve a bound dataset that returns an array with (Closure Object (...)) ✓ it can correctly resolve a bound dataset that returns an array with (Closure Object (...))
✓ it can correctly resolve a bound dataset that returns an array but wants to be spread with (Closure Object (...)) ✓ it can correctly resolve a bound dataset that returns an array but wants to be spread with (Closure Object (...))
↓ forbids to define tests in Datasets dirs and Datasets.php files
PASS Tests\Features\Depends PASS Tests\Features\Depends
✓ first ✓ first
@ -668,6 +669,47 @@
✓ get 'foo' → get 'bar' → expect true → toBeTrue ✓ get 'foo' → get 'bar' → expect true → toBeTrue
✓ get 'foo' → expect true → toBeTrue ✓ get 'foo' → expect true → toBeTrue
PASS Tests\Features\ScopedDatasets\Directory\NestedDirectory1\TestFileInNestedDirectoryWithDatasetsFile
✓ uses dataset with (1)
✓ uses dataset with (2)
✓ uses dataset with (3)
✓ uses dataset with (4)
✓ uses dataset with (5)
✓ uses dataset with ('ScopedDatasets/NestedDirector...ts.php')
✓ the right dataset is taken
PASS Tests\Features\ScopedDatasets\Directory\NestedDirectory2\TestFileInNestedDirectory
✓ uses dataset with (1)
✓ uses dataset with (2)
✓ uses dataset with (3)
✓ uses dataset with (4)
✓ uses dataset with (5)
✓ uses dataset with ('ScopedDatasets/Datasets/Scoped.php')
✓ the right dataset is taken
PASS Tests\Features\ScopedDatasets\Directory\TestFileWithLocallyDefinedDataset
✓ uses dataset with (1)
✓ uses dataset with (2)
✓ uses dataset with (3)
✓ uses dataset with (4)
✓ uses dataset with (5)
✓ uses dataset with ('ScopedDatasets/ScopedDatasets.php')
✓ the right dataset is taken
PASS Tests\Features\ScopedDatasets\Directory\TestFileWithScopedDataset
✓ uses dataset with (1)
✓ uses dataset with (2)
✓ uses dataset with (3)
✓ uses dataset with (4)
✓ uses dataset with (5)
✓ uses dataset with ('ScopedDatasets/Datasets/Scoped.php')
✓ the right dataset is taken
PASS Tests\Features\ScopedDatasets\TestFileOutOfScope
✓ uses dataset with (1)
✓ uses dataset with (2)
✓ the right dataset is taken
WARN Tests\Features\Skip WARN Tests\Features\Skip
✓ it do not skips ✓ it do not skips
- it skips with truthy → 1 - it skips with truthy → 1
@ -766,7 +808,7 @@
PASS Tests\Unit\Console\Help PASS Tests\Unit\Console\Help
✓ it outputs the help information when --help is used ✓ it outputs the help information when --help is used
PASS Tests\Unit\Datasets PASS Tests\Unit\DatasetsTests
✓ it show only the names of named datasets in their description ✓ it show only the names of named datasets in their description
✓ it show the actual dataset of non-named datasets in their description ✓ it show the actual dataset of non-named datasets in their description
✓ it show only the names of multiple named datasets in their description ✓ it show only the names of multiple named datasets in their description
@ -792,6 +834,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
@ -823,4 +885,4 @@
PASS Tests\Visual\Version PASS Tests\Visual\Version
✓ visual snapshot of help command output ✓ visual snapshot of help command output
Tests: 4 incomplete, 1 todo, 18 skipped, 567 passed (1465 assertions) Tests: 4 incomplete, 2 todos, 18 skipped, 616 passed (1526 assertions)

View File

@ -1,6 +1,6 @@
<?php <?php
use Pest\Exceptions\DatasetAlreadyExist; use Pest\Exceptions\DatasetAlreadyExists;
use Pest\Exceptions\DatasetDoesNotExist; use Pest\Exceptions\DatasetDoesNotExist;
use Pest\Plugin; use Pest\Plugin;
use Pest\Repositories\DatasetsRepository; use Pest\Repositories\DatasetsRepository;
@ -13,28 +13,28 @@ it('throws exception if dataset does not exist', function () {
$this->expectException(DatasetDoesNotExist::class); $this->expectException(DatasetDoesNotExist::class);
$this->expectExceptionMessage("A dataset with the name `first` does not exist. You can create it using `dataset('first', ['a', 'b']);`."); $this->expectExceptionMessage("A dataset with the name `first` does not exist. You can create it using `dataset('first', ['a', 'b']);`.");
DatasetsRepository::resolve('foo', ['first']); DatasetsRepository::resolve(['first'], __FILE__);
}); });
it('throws exception if dataset already exist', function () { it('throws exception if dataset already exist', function () {
DatasetsRepository::set('second', [[]]); DatasetsRepository::set('second', [[]], __DIR__);
$this->expectException(DatasetAlreadyExist::class); $this->expectException(DatasetAlreadyExists::class);
$this->expectExceptionMessage('A dataset with the name `second` already exist.'); $this->expectExceptionMessage('A dataset with the name `second` already exist in scope ['.__DIR__.'].');
DatasetsRepository::set('second', [[]]); DatasetsRepository::set('second', [[]], __DIR__);
}); });
it('sets closures', function () { it('sets closures', function () {
DatasetsRepository::set('foo', function () { DatasetsRepository::set('foo', function () {
yield [1]; yield [1];
}); }, __DIR__);
expect(DatasetsRepository::resolve('foo', ['foo']))->toBe(['(1)' => [1]]); expect(DatasetsRepository::resolve(['foo'], __FILE__))->toBe(['(1)' => [1]]);
}); });
it('sets arrays', function () { it('sets arrays', function () {
DatasetsRepository::set('bar', [[2]]); DatasetsRepository::set('bar', [[2]], __DIR__);
expect(DatasetsRepository::resolve('bar', ['bar']))->toBe(['(2)' => [2]]); expect(DatasetsRepository::resolve(['bar'], __FILE__))->toBe(['(2)' => [2]]);
}); });
it('gets bound to test case object', function ($value) { it('gets bound to test case object', function ($value) {
@ -323,3 +323,5 @@ it('can correctly resolve a bound dataset that returns an array but wants to be
return ['foo', 'bar', 'baz']; return ['foo', 'bar', 'baz'];
}, },
]); ]);
todo('forbids to define tests in Datasets dirs and Datasets.php files');

View File

@ -0,0 +1,5 @@
<?php
dataset('numbers.array', [
1, 2, 3, 4, 5, 'ScopedDatasets/Datasets/Scoped.php',
]);

View File

@ -0,0 +1,5 @@
<?php
dataset('numbers.array', [
1, 2, 3, 4, 5, 'ScopedDatasets/NestedDirectory1/Datasets.php',
]);

View File

@ -0,0 +1,12 @@
<?php
$state = new stdClass();
$state->text = '';
test('uses dataset', function ($value) use ($state) {
$state->text .= $value;
expect(true)->toBe(true);
})->with('numbers.array');
test('the right dataset is taken', function () use ($state) {
expect($state->text)->toBe('12345ScopedDatasets/NestedDirectory1/Datasets.php');
});

View File

@ -0,0 +1,12 @@
<?php
$state = new stdClass();
$state->text = '';
test('uses dataset', function ($value) use ($state) {
$state->text .= $value;
expect(true)->toBe(true);
})->with('numbers.array');
test('the right dataset is taken', function () use ($state) {
expect($state->text)->toBe('12345ScopedDatasets/Datasets/Scoped.php');
});

View File

@ -0,0 +1,16 @@
<?php
dataset('numbers.array', [
1, 2, 3, 4, 5, 'ScopedDatasets/ScopedDatasets.php',
]);
$state = new stdClass();
$state->text = '';
test('uses dataset', function ($value) use ($state) {
$state->text .= $value;
expect(true)->toBe(true);
})->with('numbers.array');
test('the right dataset is taken', function () use ($state) {
expect($state->text)->toBe('12345ScopedDatasets/ScopedDatasets.php');
});

View File

@ -0,0 +1,12 @@
<?php
$state = new stdClass();
$state->text = '';
test('uses dataset', function ($value) use ($state) {
$state->text .= $value;
expect(true)->toBe(true);
})->with('numbers.array');
test('the right dataset is taken', function () use ($state) {
expect($state->text)->toBe('12345ScopedDatasets/Datasets/Scoped.php');
});

View File

@ -0,0 +1,12 @@
<?php
$state = new stdClass();
$state->text = '';
test('uses dataset', function ($value) use ($state) {
$state->text .= $value;
expect(true)->toBe(true);
})->with('numbers.array');
test('the right dataset is taken', function () use ($state) {
expect($state->text)->toBe('12');
});

View File

@ -3,31 +3,31 @@
use Pest\Repositories\DatasetsRepository; use Pest\Repositories\DatasetsRepository;
it('show only the names of named datasets in their description', function () { it('show only the names of named datasets in their description', function () {
$descriptions = array_keys(DatasetsRepository::resolve('test description', [ $descriptions = array_keys(DatasetsRepository::resolve([
[ [
'one' => [1], 'one' => [1],
'two' => [[2]], 'two' => [[2]],
], ],
])); ], __FILE__));
expect($descriptions[0])->toBe('data set "one"') expect($descriptions[0])->toBe('data set "one"')
->and($descriptions[1])->toBe('data set "two"'); ->and($descriptions[1])->toBe('data set "two"');
}); });
it('show the actual dataset of non-named datasets in their description', function () { it('show the actual dataset of non-named datasets in their description', function () {
$descriptions = array_keys(DatasetsRepository::resolve('test description', [ $descriptions = array_keys(DatasetsRepository::resolve([
[ [
[1], [1],
[[2]], [[2]],
], ],
])); ], __FILE__));
expect($descriptions[0])->toBe('(1)'); expect($descriptions[0])->toBe('(1)');
expect($descriptions[1])->toBe('(array(2))'); expect($descriptions[1])->toBe('(array(2))');
}); });
it('show only the names of multiple named datasets in their description', function () { it('show only the names of multiple named datasets in their description', function () {
$descriptions = array_keys(DatasetsRepository::resolve('test description', [ $descriptions = array_keys(DatasetsRepository::resolve([
[ [
'one' => [1], 'one' => [1],
'two' => [[2]], 'two' => [[2]],
@ -36,7 +36,7 @@ it('show only the names of multiple named datasets in their description', functi
'three' => [3], 'three' => [3],
'four' => [[4]], 'four' => [[4]],
], ],
])); ], __FILE__));
expect($descriptions[0])->toBe('data set "one" / data set "three"'); expect($descriptions[0])->toBe('data set "one" / data set "three"');
expect($descriptions[1])->toBe('data set "one" / data set "four"'); expect($descriptions[1])->toBe('data set "one" / data set "four"');
@ -45,7 +45,7 @@ it('show only the names of multiple named datasets in their description', functi
}); });
it('show the actual dataset of multiple non-named datasets in their description', function () { it('show the actual dataset of multiple non-named datasets in their description', function () {
$descriptions = array_keys(DatasetsRepository::resolve('test description', [ $descriptions = array_keys(DatasetsRepository::resolve([
[ [
[1], [1],
[[2]], [[2]],
@ -54,7 +54,7 @@ it('show the actual dataset of multiple non-named datasets in their description'
[3], [3],
[[4]], [[4]],
], ],
])); ], __FILE__));
expect($descriptions[0])->toBe('(1) / (3)'); expect($descriptions[0])->toBe('(1) / (3)');
expect($descriptions[1])->toBe('(1) / (array(4))'); expect($descriptions[1])->toBe('(1) / (array(4))');
@ -63,7 +63,7 @@ it('show the actual dataset of multiple non-named datasets in their description'
}); });
it('show the correct description for mixed named and not-named datasets', function () { it('show the correct description for mixed named and not-named datasets', function () {
$descriptions = array_keys(DatasetsRepository::resolve('test description', [ $descriptions = array_keys(DatasetsRepository::resolve([
[ [
'one' => [1], 'one' => [1],
[[2]], [[2]],
@ -72,7 +72,7 @@ it('show the correct description for mixed named and not-named datasets', functi
[3], [3],
'four' => [[4]], 'four' => [[4]],
], ],
])); ], __FILE__));
expect($descriptions[0])->toBe('data set "one" / (3)'); expect($descriptions[0])->toBe('data set "one" / (3)');
expect($descriptions[1])->toBe('data set "one" / data set "four"'); expect($descriptions[1])->toBe('data set "one" / data set "four"');

View 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'],
]);