From e7b85586eee1f359d1a8792144eb7129b281083f Mon Sep 17 00:00:00 2001 From: faissaloux Date: Tue, 15 Oct 2024 18:50:27 +0100 Subject: [PATCH] toReturnSingleWords --- src/Expectation.php | 30 ++++++++- .../directory/subdirectory/notAllUnique.php | 1 + tests/Fixtures/directory1/notAllUnique.php | 1 + tests/Fixtures/returnsDuplicates.php | 2 + tests/toReturnSingleWords.php | 66 +++++++++++++++++++ 5 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 tests/toReturnSingleWords.php diff --git a/src/Expectation.php b/src/Expectation.php index 5f7b322..340b2ab 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -99,7 +99,35 @@ public function toReturnUnique(int $depth = -1): PestExpectation $duplicates = array_diff_assoc($content, array_unique($content)); - expect($duplicates)->toBeEmpty('Duplicates found:'.implode(',', $duplicates)." in $file"); + expect($duplicates)->toBeEmpty('Duplicates found: '.implode(',', $duplicates)." in $file"); + } + + return new PestExpectation($this->value); + } + + /** + * @return PestExpectation + */ + public function toReturnSingleWords(int $depth = -1): 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); + + $notSingle = array_filter($content, function (string $word): bool { + return str_contains(trim($word), ' '); + }); + + expect($notSingle)->toBeEmpty('Not single words detected: '.implode(',', $notSingle)." in $file"); } return new PestExpectation($this->value); diff --git a/tests/Fixtures/directory/subdirectory/notAllUnique.php b/tests/Fixtures/directory/subdirectory/notAllUnique.php index 91d7c4c..4e6993f 100644 --- a/tests/Fixtures/directory/subdirectory/notAllUnique.php +++ b/tests/Fixtures/directory/subdirectory/notAllUnique.php @@ -6,6 +6,7 @@ '', 'f@issa!oux', 'pest', + 'plugin inside', 'plugin', 'inside', 'duplicate', diff --git a/tests/Fixtures/directory1/notAllUnique.php b/tests/Fixtures/directory1/notAllUnique.php index 91d7c4c..4e6993f 100644 --- a/tests/Fixtures/directory1/notAllUnique.php +++ b/tests/Fixtures/directory1/notAllUnique.php @@ -6,6 +6,7 @@ '', 'f@issa!oux', 'pest', + 'plugin inside', 'plugin', 'inside', 'duplicate', diff --git a/tests/Fixtures/returnsDuplicates.php b/tests/Fixtures/returnsDuplicates.php index 91d7c4c..c512405 100644 --- a/tests/Fixtures/returnsDuplicates.php +++ b/tests/Fixtures/returnsDuplicates.php @@ -5,6 +5,8 @@ return [ '', 'f@issa!oux', + 'pest plugin', + 'pest plugin inside', 'pest', 'plugin', 'inside', diff --git a/tests/toReturnSingleWords.php b/tests/toReturnSingleWords.php new file mode 100644 index 0000000..74a3166 --- /dev/null +++ b/tests/toReturnSingleWords.php @@ -0,0 +1,66 @@ +toReturnSingleWords(); +}); + +it('passes with not', function (): void { + expect('tests/Fixtures/returnsUnique.php') + ->not->toReturnSingleWords(); +}); + +it('passes when directory is empty', function (): void { + expect('tests/Fixtures/empty') + ->toReturnSingleWords(); +}); + +it('passes when all directory files content are single words', function (): void { + expect('tests/Fixtures/directory') + ->toReturnSingleWords(depth: 0); +}); + +it('fails', function (): void { + expect('tests/Fixtures/returnsUnique.php') + ->toReturnSingleWords(); +})->throws(ExpectationFailedException::class); + +it('fails with not', function (): void { + expect('tests/Fixtures/returnsMultipleDuplicates.php') + ->not->toReturnSingleWords(); +})->throws(ExpectationFailedException::class); + +it('fails when file does not exist', function (): void { + expect('tests/Fixtures/notExist.php') + ->toReturnSingleWords(); +})->throws(ExpectationFailedException::class, 'tests/Fixtures/notExist.php not found'); + +it('fails when directory does not exist', function (): void { + expect('tests/Fixtures/notExist') + ->toReturnSingleWords(); +})->throws(ExpectationFailedException::class); + +it('fails when not all directory files content are single words', function (): void { + expect('tests/Fixtures/directory1') + ->toReturnSingleWords(); +})->throws(ExpectationFailedException::class); + +it('fails when not all subdirectories files content are single words', function (): void { + expect('tests/Fixtures/directory') + ->toReturnSingleWords(); +})->throws(ExpectationFailedException::class); + +it('displays word detected', function (): void { + expect('tests/Fixtures/directory')->toReturnSingleWords(); +})->throws(ExpectationFailedException::class, 'Not single words detected: plugin inside'); + +it('displays multiple not single words detected', function (): void { + expect('tests/Fixtures/returnsDuplicates.php') + ->toReturnSingleWords(); +})->throws(ExpectationFailedException::class, 'pest plugin,pest plugin inside'); + +it('displays file where error detected', function (): void { + expect('tests/Fixtures/directory')->toReturnSingleWords(); +})->throws(ExpectationFailedException::class, 'notAllUnique.php');