From 7e2abc2a4b2ab82b7ccd4a5d6f34a62bc969d9ee Mon Sep 17 00:00:00 2001 From: faissaloux Date: Tue, 15 Oct 2024 13:11:59 +0100 Subject: [PATCH] group expectation in Expectation class --- src/Autoload.php | 19 ++++++++++++-- src/Expectation.php | 63 ++++++++++++++++++++++++++------------------- 2 files changed, 53 insertions(+), 29 deletions(-) diff --git a/src/Autoload.php b/src/Autoload.php index 9052778..34d67aa 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -2,6 +2,21 @@ declare(strict_types=1); -namespace Faissaloux\PestInside; +use Faissaloux\PestInside\Expectation; +use Pest\Expectation as PestExpectation; -require_once __DIR__.'/Expectation.php'; +$expectations = get_class_methods(Expectation::class); +$expectations = array_filter($expectations, fn ($function): bool => str_starts_with($function, 'toReturn')); + +foreach ($expectations as $expectation) { + expect()->extend( + $expectation, + function (int $depth = -1) use ($expectation): PestExpectation { + if (is_callable($callback = [new Expectation($this->value), $expectation])) { + call_user_func($callback, ...func_get_args()); + } + + return $this; + } + ); +} diff --git a/src/Expectation.php b/src/Expectation.php index 29d2fac..0d31b93 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -2,20 +2,28 @@ declare(strict_types=1); +namespace Faissaloux\PestInside; + use Pest\Expectation as PestExpectation; -expect()->extend( - 'toReturnUnique', - function (int $depth = -1): PestExpectation { - $files = [$this->value]; +final class Expectation +{ + public function __construct(private string $value) {} + /** + * @return PestExpectation + */ + public function toReturnLowercase(int $depth = -1): PestExpectation + { + $files = [$this->value]; + if (is_dir($files[0])) { $files = getFilesIn($files[0], $depth); if ($files === []) { expect(true)->toBeTrue(); - return $this; + return new PestExpectation($this->value); } } @@ -28,27 +36,36 @@ function (int $depth = -1): PestExpectation { expect($content)->toBeArray(); - $duplicates = array_diff_assoc($content, array_unique($content)); + // Clean up content from numerics and special characters. + $cleanContent = array_map(function (string $word): string { + return (string) preg_replace('/[^A-Za-z]/', '', $word); + }, $content); - expect($duplicates)->toBeEmpty('Duplicates found:'.implode(',', $duplicates)." in $file"); + foreach ($cleanContent as $key => $word) { + if ($word === '') { + continue; + } + + expect($word)->toBeLowercase("Not lowercase word detected: $content[$key] in $file"); + } } - return $this; + return new PestExpectation($this->value); } -); -expect()->extend( - 'toReturnLowercase', - function (int $depth = -1): PestExpectation { + /** + * @return PestExpectation + */ + public function toReturnUnique(int $depth = -1): PestExpectation { $files = [$this->value]; - + if (is_dir($files[0])) { $files = getFilesIn($files[0], $depth); if ($files === []) { expect(true)->toBeTrue(); - return $this; + return new PestExpectation($this->value); } } @@ -61,20 +78,12 @@ function (int $depth = -1): PestExpectation { expect($content)->toBeArray(); - // Clean up content from numerics and special characters. - $cleanContent = array_map(function (string $word): string { - return (string) preg_replace('/[^A-Za-z]/', '', $word); - }, $content); - - foreach ($cleanContent as $key => $word) { - if ($word === '') { - continue; - } + $duplicates = array_diff_assoc($content, array_unique($content)); - expect($word)->toBeLowercase("Not lowercase word detected: $content[$key] in $file"); - } + expect($duplicates)->toBeEmpty('Duplicates found:'.implode(',', $duplicates)." in $file"); } - return $this; + return new PestExpectation($this->value); } -); + +}