Skip to content

Commit

Permalink
Allow filtering fixtures (#6)
Browse files Browse the repository at this point in the history
* Allow filtering fixtures

* Filtering docs
  • Loading branch information
blueo authored Nov 23, 2021
1 parent c210053 commit b1dcee2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,21 @@ vendor/bin/sake dev/tasks/load-fixtures directory=app/src/fixtures purgeOnly=tru
```

### Quality of life functionality:

#### Filtering

You can selectively run fixtures using the filter param:

```bash
vendor/bin/sake dev/tasks/load-fixtures directory=app/src/fixtures filter=/PageFixtureTwo/
```

This will run any fixture matching the filter pattern *and any [dependencies](#dependant-fixtures)*. Note that you may filter out [ordered fixtures](#ordered-fixtures) and these won't be automatically resolved like dependencies.

The filter pattern must be a valid pattern for [preg_match](https://www.php.net/manual/en/function.preg-match.php) including a delimiter. The pattern is matched against the fully qualified class name (eg `App\My\Fixture`).

#### Creating assets

You can create assets really easily like so:
```php
\AdrHumphreys\Fixtures\ReferenceManager::findOrMakeAsset('my-asset-id', 'file/path.jpg');
Expand Down
38 changes: 31 additions & 7 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ class Loader
*/
private $orderFixturesByDependencies = false;

/*
/**
* Find fixtures classes in a given directory and load them.
*
* @param string $directory - directory to search for fixtures
* @param string|null $pattern - optional pattern to match classname - follows preg_match syntax
*/
public function loadFromDirectory(string $directory): void
public function loadFromDirectory(string $directory, ?string $pattern = null): void
{
if (!is_dir($directory)) {
throw new InvalidArgumentException(sprintf('"%s" does not exist', $dir));
throw new InvalidArgumentException(sprintf('"%s" does not exist', $directory));
}

$iterator = new RecursiveIteratorIterator(
Expand Down Expand Up @@ -79,12 +82,31 @@ public function loadFromDirectory(string $directory): void
$reflClass = new ReflectionClass($className);
$sourceFile = $reflClass->getFileName();

if (!in_array($sourceFile, $includedFiles)
if (
!in_array($sourceFile, $includedFiles)
|| $reflClass->isAbstract()
|| !is_subclass_of($className, FixtureInterface::class)) {
|| !is_subclass_of($className, FixtureInterface::class)
) {
continue;
}

// apply filter
if ($pattern !== null) {
$match = preg_match($pattern, $className);

if ($match !== 1) {
continue;
}

Logger::green(
sprintf(
'class "%s" matches filter: "%s"',
$className,
$pattern
)
);
}

$this->addFixture($className);
}
}
Expand All @@ -102,8 +124,10 @@ public function addFixture(string $fixtureClass): void

$fixture = new $fixtureClass();

if ($fixture instanceof OrderedFixtureInterface
&& $fixture instanceof DependentFixtureInterface) {
if (
$fixture instanceof OrderedFixtureInterface
&& $fixture instanceof DependentFixtureInterface
) {
throw new InvalidArgumentException(sprintf(
'Class "%s" can\'t implement "%s" and "%s" at the same time.',
$fixtureClass,
Expand Down
6 changes: 4 additions & 2 deletions src/tasks/LoadFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public function run($request): void

$onlyPurge = $request->getVar('onlyPurge') === 'true';

if($append && $onlyPurge) {
$filter = $request->getVar('filter') ?? null;

if ($append && $onlyPurge) {
Logger::log("You've asked for nothing...");

return;
Expand All @@ -55,7 +57,7 @@ public function run($request): void
}

$loader = new Loader();
$loader->loadFromDirectory($directory);
$loader->loadFromDirectory($directory, $filter);
$purger = new Purger();
$executor = new Executor($purger);
$executor->execute($loader->getFixtures(), $append, $onlyPurge);
Expand Down

0 comments on commit b1dcee2

Please sign in to comment.