Skip to content

Commit 91ca739

Browse files
authored
Merge pull request #9 from faissaloux/toReturnSingleWords
`toReturnSingleWords()`
2 parents b524f51 + 3fafa8f commit 91ca739

File tree

6 files changed

+105
-1
lines changed

6 files changed

+105
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ Make sure a file or directory files returns an array with unique values.
1818
expect('file.php')->toReturnUnique();
1919
```
2020

21+
### toReturnSingleWords
22+
Make sure a file or directory files returns an array with single words.
23+
```php
24+
expect('file.php')->toReturnSingleWords();
25+
```
26+
2127
----
2228

2329
### Success

src/Expectation.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,35 @@ public function toReturnUnique(int $depth = -1): PestExpectation
9999

100100
$duplicates = array_diff_assoc($content, array_unique($content));
101101

102-
expect($duplicates)->toBeEmpty('Duplicates found:'.implode(',', $duplicates)." in $file");
102+
expect($duplicates)->toBeEmpty('Duplicates found: '.implode(',', $duplicates)." in $file");
103+
}
104+
105+
return new PestExpectation($this->value);
106+
}
107+
108+
/**
109+
* @return PestExpectation<string>
110+
*/
111+
public function toReturnSingleWords(int $depth = -1): PestExpectation
112+
{
113+
$this->fetchFilesIfDirectory($depth);
114+
115+
if ($this->files === []) {
116+
expect(true)->toBeTrue();
117+
118+
return new PestExpectation($this->value);
119+
}
120+
121+
foreach ($this->files as $file) {
122+
$this->checkFileExistence($file);
123+
124+
$content = $this->getContentFrom($file);
125+
126+
$notSingle = array_filter($content, function (string $word): bool {
127+
return str_contains(trim($word), ' ');
128+
});
129+
130+
expect($notSingle)->toBeEmpty('Not single words detected: '.implode(',', $notSingle)." in $file");
103131
}
104132

105133
return new PestExpectation($this->value);

tests/Fixtures/directory/subdirectory/notAllUnique.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
'',
77
'f@issa!oux',
88
'pest',
9+
'plugin inside',
910
'plugin',
1011
'inside',
1112
'duplicate',

tests/Fixtures/directory1/notAllUnique.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
'',
77
'f@issa!oux',
88
'pest',
9+
'plugin inside',
910
'plugin',
1011
'inside',
1112
'duplicate',

tests/Fixtures/returnsDuplicates.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
return [
66
'',
77
'f@issa!oux',
8+
'pest plugin',
9+
'pest plugin inside',
810
'pest',
911
'plugin',
1012
'inside',

tests/toReturnSingleWords.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
use PHPUnit\Framework\ExpectationFailedException;
4+
5+
it('passes', function (): void {
6+
expect('tests/Fixtures/returnsMultipleDuplicates.php')
7+
->toReturnSingleWords();
8+
});
9+
10+
it('passes with not', function (): void {
11+
expect('tests/Fixtures/returnsUnique.php')
12+
->not->toReturnSingleWords();
13+
});
14+
15+
it('passes when directory is empty', function (): void {
16+
expect('tests/Fixtures/empty')
17+
->toReturnSingleWords();
18+
});
19+
20+
it('passes when all directory files content are single words', function (): void {
21+
expect('tests/Fixtures/directory')
22+
->toReturnSingleWords(depth: 0);
23+
});
24+
25+
it('fails', function (): void {
26+
expect('tests/Fixtures/returnsUnique.php')
27+
->toReturnSingleWords();
28+
})->throws(ExpectationFailedException::class);
29+
30+
it('fails with not', function (): void {
31+
expect('tests/Fixtures/returnsMultipleDuplicates.php')
32+
->not->toReturnSingleWords();
33+
})->throws(ExpectationFailedException::class);
34+
35+
it('fails when file does not exist', function (): void {
36+
expect('tests/Fixtures/notExist.php')
37+
->toReturnSingleWords();
38+
})->throws(ExpectationFailedException::class, 'tests/Fixtures/notExist.php not found');
39+
40+
it('fails when directory does not exist', function (): void {
41+
expect('tests/Fixtures/notExist')
42+
->toReturnSingleWords();
43+
})->throws(ExpectationFailedException::class);
44+
45+
it('fails when not all directory files content are single words', function (): void {
46+
expect('tests/Fixtures/directory1')
47+
->toReturnSingleWords();
48+
})->throws(ExpectationFailedException::class);
49+
50+
it('fails when not all subdirectories files content are single words', function (): void {
51+
expect('tests/Fixtures/directory')
52+
->toReturnSingleWords();
53+
})->throws(ExpectationFailedException::class);
54+
55+
it('displays word detected', function (): void {
56+
expect('tests/Fixtures/directory')->toReturnSingleWords();
57+
})->throws(ExpectationFailedException::class, 'Not single words detected: plugin inside');
58+
59+
it('displays multiple not single words detected', function (): void {
60+
expect('tests/Fixtures/returnsDuplicates.php')
61+
->toReturnSingleWords();
62+
})->throws(ExpectationFailedException::class, 'pest plugin,pest plugin inside');
63+
64+
it('displays file where error detected', function (): void {
65+
expect('tests/Fixtures/directory')->toReturnSingleWords();
66+
})->throws(ExpectationFailedException::class, 'notAllUnique.php');

0 commit comments

Comments
 (0)