From 8ca0807f34b0225c2810acddc80495ca93c24d77 Mon Sep 17 00:00:00 2001 From: faissaloux Date: Tue, 22 Oct 2024 00:10:36 +0100 Subject: [PATCH] Investigator and refactor Expectation --- src/Expectation.php | 45 +++++++---------- src/Inside.php | 6 ++- src/Investigator.php | 50 +++++++++++++++++++ .../subdirectory/notAllLowerCase.php | 1 + tests/toReturnLowercase.php | 4 +- tests/toReturnSingleWords.php | 2 +- tests/toReturnUnique.php | 2 +- 7 files changed, 78 insertions(+), 32 deletions(-) create mode 100644 src/Investigator.php diff --git a/src/Expectation.php b/src/Expectation.php index f0c1373..a5cd5d6 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -4,43 +4,36 @@ namespace Faissaloux\PestInside; +use Faissaloux\PestInside\Investigator; + final class Expectation extends Inside { + use Investigator; + public function toReturnLowercase(int $depth = -1): void { - $this->applyOnDirectory($depth, function ($file, $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); - - foreach ($cleanContent as $key => $word) { - if ($word === '') { - continue; - } - - expect($word)->toBeLowercase("Not lowercase word detected: $content[$key] in $file"); - } - }); + $this->applyOnDirectory( + $depth, + fn (array $content): array => $this->lowercasesIn($content), + 'Not lowercase detected' + ); } public function toReturnUnique(int $depth = -1): void { - $this->applyOnDirectory($depth, function (string $file, array $content): void { - $duplicates = array_diff_assoc($content, array_unique($content)); - - expect($duplicates)->toBeEmpty('Duplicates found: '.implode(',', $duplicates)." in $file"); - }); + $this->applyOnDirectory( + $depth, + fn (array $content): array => $this->duplicatesIn($content), + 'Duplicates detected' + ); } public function toReturnSingleWords(int $depth = -1): void { - $this->applyOnDirectory($depth, function (string $file, array $content): void { - $notSingle = array_filter($content, function (string $word): bool { - return str_contains(trim($word), ' '); - }); - - expect($notSingle)->toBeEmpty('Not single words detected: '.implode(',', $notSingle)." in $file"); - }); + $this->applyOnDirectory( + $depth, + fn (array $content): array => $this->singleWordsIn($content), + 'Not single words detected' + ); } } diff --git a/src/Inside.php b/src/Inside.php index 7f7950f..25aa8fd 100644 --- a/src/Inside.php +++ b/src/Inside.php @@ -47,7 +47,7 @@ protected function getContentFrom(string $file): array /** * @return PestExpectation */ - protected function applyOnDirectory(int $depth, callable $callback): PestExpectation + protected function applyOnDirectory(int $depth, callable $callback, string $message): PestExpectation { $this->fetchFilesIfDirectory($depth); @@ -62,7 +62,9 @@ protected function applyOnDirectory(int $depth, callable $callback): PestExpecta $content = $this->getContentFrom($file); - $callback($file, $content); + $unwanted = $callback($content); + + expect($unwanted)->toBeEmpty("$message: ".implode(', ', $unwanted)." in $file"); } return new PestExpectation($this->value); diff --git a/src/Investigator.php b/src/Investigator.php new file mode 100644 index 0000000..cac8e4d --- /dev/null +++ b/src/Investigator.php @@ -0,0 +1,50 @@ + $array + * + * @return array + */ + private function lowercasesIn(array $array): array + { + $notLowerCase = []; + + foreach ($array as $word) { + if ($word === '') { + continue; + } + + if (! ctype_lower(preg_replace('/[^A-Za-z]/', '', $word))) { + array_push($notLowerCase, $word); + } + } + + return $notLowerCase; + } + + /** + * @param array $array + * + * @return array + */ + private function duplicatesIn(array $array): array + { + return array_diff_assoc($array, array_unique($array)); + } + + /** + * @param array $array + * + * @return array + */ + private function singleWordsIn(array $array): array + { + return array_filter($array, fn (string $word): bool => str_contains(trim($word), ' ')); + } +} diff --git a/tests/Fixtures/directory/subdirectory/notAllLowerCase.php b/tests/Fixtures/directory/subdirectory/notAllLowerCase.php index 43e7ec1..548a3c6 100644 --- a/tests/Fixtures/directory/subdirectory/notAllLowerCase.php +++ b/tests/Fixtures/directory/subdirectory/notAllLowerCase.php @@ -11,4 +11,5 @@ 'lowercase', 'loWer', 'case', + 'nOt', ]; diff --git a/tests/toReturnLowercase.php b/tests/toReturnLowercase.php index be7087b..e870cba 100644 --- a/tests/toReturnLowercase.php +++ b/tests/toReturnLowercase.php @@ -52,9 +52,9 @@ ->toReturnLowercase(); })->throws(ExpectationFailedException::class); -it('displays word detected', function (): void { +it('displays words detected', function (): void { expect('tests/Fixtures/directory')->toReturnLowercase(); -})->throws(ExpectationFailedException::class, 'Not lowercase word detected: loWer'); +})->throws(ExpectationFailedException::class, 'Not lowercase detected: loWer, nOt'); it('displays file where error detected', function (): void { expect('tests/Fixtures/directory')->toReturnLowercase(); diff --git a/tests/toReturnSingleWords.php b/tests/toReturnSingleWords.php index 74a3166..6610ec3 100644 --- a/tests/toReturnSingleWords.php +++ b/tests/toReturnSingleWords.php @@ -59,7 +59,7 @@ it('displays multiple not single words detected', function (): void { expect('tests/Fixtures/returnsDuplicates.php') ->toReturnSingleWords(); -})->throws(ExpectationFailedException::class, 'pest plugin,pest plugin inside'); +})->throws(ExpectationFailedException::class, 'pest plugin, pest plugin inside'); it('displays file where error detected', function (): void { expect('tests/Fixtures/directory')->toReturnSingleWords(); diff --git a/tests/toReturnUnique.php b/tests/toReturnUnique.php index e04b4e5..289f3d1 100644 --- a/tests/toReturnUnique.php +++ b/tests/toReturnUnique.php @@ -60,7 +60,7 @@ it('displays multiple found duplicates', function (): void { expect('tests/Fixtures/returnsMultipleDuplicates.php') ->toReturnUnique(); -})->throws(ExpectationFailedException::class, '1,duplicate'); +})->throws(ExpectationFailedException::class, '1, duplicate'); it('displays file where duplicate found', function (): void { expect('tests/Fixtures/returnsDuplicates.php')