-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ffdb46b
commit e2de4b4
Showing
2 changed files
with
80 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Faissaloux\PestInside; | ||
|
||
use Pest\Expectation as PestExpectation; | ||
|
||
class Inside | ||
{ | ||
/** | ||
* @var array<string> | ||
*/ | ||
protected array $files; | ||
|
||
public function __construct(protected string $value) | ||
{ | ||
$this->files = [$value]; | ||
} | ||
|
||
protected function fetchFilesIfDirectory(int $depth): void | ||
{ | ||
if (is_dir($this->files[0])) { | ||
$this->files = getFilesIn($this->files[0], $depth); | ||
} | ||
} | ||
|
||
protected function checkFileExistence(string $file): void | ||
{ | ||
if (! file_exists($file)) { | ||
expect(true)->toBeFalse("$file not found!"); | ||
} | ||
} | ||
|
||
/** | ||
* @return array<string> | ||
*/ | ||
protected function getContentFrom(string $file): array | ||
{ | ||
$content = include $file; | ||
|
||
expect($content)->toBeArray(); | ||
|
||
return $content; | ||
} | ||
|
||
/** | ||
* @return PestExpectation<string> | ||
*/ | ||
protected function applyOnDirectory(int $depth, callable $callback): PestExpectation | ||
{ | ||
$this->fetchFilesIfDirectory($depth); | ||
|
||
if ($this->files === []) { | ||
expect(true)->toBeTrue(); | ||
|
||
return new PestExpectation($this->value); | ||
} | ||
|
||
foreach ($this->files as $file) { | ||
$this->checkFileExistence($file); | ||
|
||
$content = $this->getContentFrom($file); | ||
|
||
$callback($file, $content); | ||
} | ||
|
||
return new PestExpectation($this->value); | ||
} | ||
} |