Skip to content

Commit

Permalink
toReturnSingleWords
Browse files Browse the repository at this point in the history
  • Loading branch information
faissaloux committed Oct 15, 2024
1 parent b524f51 commit e7b8558
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>
*/
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);
Expand Down
1 change: 1 addition & 0 deletions tests/Fixtures/directory/subdirectory/notAllUnique.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'',
'f@issa!oux',
'pest',
'plugin inside',
'plugin',
'inside',
'duplicate',
Expand Down
1 change: 1 addition & 0 deletions tests/Fixtures/directory1/notAllUnique.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'',
'f@issa!oux',
'pest',
'plugin inside',
'plugin',
'inside',
'duplicate',
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixtures/returnsDuplicates.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
return [
'',
'f@issa!oux',
'pest plugin',
'pest plugin inside',
'pest',
'plugin',
'inside',
Expand Down
66 changes: 66 additions & 0 deletions tests/toReturnSingleWords.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

use PHPUnit\Framework\ExpectationFailedException;

it('passes', function (): void {
expect('tests/Fixtures/returnsMultipleDuplicates.php')
->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');

0 comments on commit e7b8558

Please sign in to comment.