Skip to content

Commit

Permalink
Investigator and refactor Expectation
Browse files Browse the repository at this point in the history
  • Loading branch information
faissaloux committed Oct 21, 2024
1 parent e2de4b4 commit 8ca0807
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 32 deletions.
45 changes: 19 additions & 26 deletions src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}
}
6 changes: 4 additions & 2 deletions src/Inside.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected function getContentFrom(string $file): array
/**
* @return PestExpectation<string>
*/
protected function applyOnDirectory(int $depth, callable $callback): PestExpectation
protected function applyOnDirectory(int $depth, callable $callback, string $message): PestExpectation
{
$this->fetchFilesIfDirectory($depth);

Expand All @@ -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);
Expand Down
50 changes: 50 additions & 0 deletions src/Investigator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Faissaloux\PestInside;

trait Investigator
{
/**
* @param array<string> $array
*
* @return array<string>
*/
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<string> $array
*
* @return array<string>
*/
private function duplicatesIn(array $array): array
{
return array_diff_assoc($array, array_unique($array));
}

/**
* @param array<string> $array
*
* @return array<string>
*/
private function singleWordsIn(array $array): array
{
return array_filter($array, fn (string $word): bool => str_contains(trim($word), ' '));
}
}
1 change: 1 addition & 0 deletions tests/Fixtures/directory/subdirectory/notAllLowerCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
'lowercase',
'loWer',
'case',
'nOt',
];
4 changes: 2 additions & 2 deletions tests/toReturnLowercase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/toReturnSingleWords.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/toReturnUnique.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 8ca0807

Please sign in to comment.