Skip to content

Commit b4a76a0

Browse files
authored
Merge pull request #21 from faissaloux/forbidEmpty
`forbidEmpty()`
2 parents 5fdcd00 + f00bd07 commit b4a76a0

File tree

8 files changed

+69
-4
lines changed

8 files changed

+69
-4
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ Make sure a file or directory files returns only string values.
5555
expect('file.php')->toReturnStrings();
5656
```
5757

58+
### forbidEmpty
59+
Make sure a file or directory files does not return any empty value.
60+
```php
61+
expect('file.php')->forbidEmpty();
62+
```
63+
5864
----
5965

6066
### Success

src/Autoload.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
use Pest\Expectation as PestExpectation;
77

88
$expectations = get_class_methods(Expectation::class);
9-
$expectations = array_filter($expectations, fn ($function): bool => str_starts_with($function, 'toReturn') || str_starts_with($function, 'toBe'));
9+
$expectations = array_filter($expectations, fn ($function): bool => str_starts_with($function, 'toReturn')
10+
|| str_starts_with($function, 'toBe')
11+
|| str_starts_with($function, 'forbid')
12+
);
1013

1114
foreach ($expectations as $expectation) {
1215
expect()->extend(

src/Expectation.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,13 @@ public function toReturnStrings(int $depth = -1): void
6363
'Not string detected'
6464
);
6565
}
66+
67+
public function forbidEmpty(int $depth = -1): void
68+
{
69+
$this->applyOnDirectory(
70+
$depth,
71+
fn (Content $content): bool => $this->emptyIn($content),
72+
'Empty value detected'
73+
);
74+
}
6675
}

src/Inside.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ protected function applyOnDirectory(int $depth, callable $callback, string $mess
5252

5353
$unwanted = $callback($content);
5454

55-
expect($unwanted)->toBeEmpty("$message: ".implode(', ', $unwanted)." in $file");
55+
if (is_array($unwanted)) {
56+
expect($unwanted)->toBeEmpty("$message: ".implode(', ', $unwanted)." in $file");
57+
} else {
58+
expect($unwanted)->toBeTrue($message);
59+
}
5660
}
5761

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

src/Investigator.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,24 @@ private function notStringsIn(Content|array $content): array
176176

177177
return $unwanted;
178178
}
179+
180+
/**
181+
* @param Content|array<int|string, string|array<string, string>> $content
182+
*/
183+
private function emptyIn(Content|array $content): bool
184+
{
185+
foreach ($content as $word) {
186+
if (is_array($word)) {
187+
$this->emptyIn($word);
188+
189+
continue;
190+
}
191+
192+
if ($word == '') {
193+
return false;
194+
}
195+
}
196+
197+
return true;
198+
}
179199
}

tests/Fixtures/returnsMultipleDuplicates.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
declare(strict_types=1);
44

55
return [
6-
'',
76
'f@issa!oux',
87
1,
98
'pest',

tests/Fixtures/text/returnsMultipleDuplicates.stub

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
f@issa!oux
32
1
43
pest

tests/forbidEmpty.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use PHPUnit\Framework\ExpectationFailedException;
4+
5+
it('passes', function (string $file): void {
6+
expect($file)->forbidEmpty();
7+
})->with([
8+
'tests/Fixtures/returnsMultipleDuplicates.php',
9+
'tests/Fixtures/text/returnsMultipleDuplicates.stub',
10+
]);
11+
12+
it('fails', function (string $file): void {
13+
expect($file)->forbidEmpty();
14+
})->with([
15+
'tests/Fixtures/returnsDuplicates.php',
16+
'tests/Fixtures/text/returnsDuplicates.stub',
17+
])->throws(ExpectationFailedException::class, 'Empty value detected');
18+
19+
it('passes when directory is empty', function (): void {
20+
expect('tests/Fixtures/empty')->forbidEmpty();
21+
});
22+
23+
it('fails when directory does not exist', function (): void {
24+
expect('tests/Fixtures/notExist')->forbidEmpty();
25+
})->throws(ExpectationFailedException::class);

0 commit comments

Comments
 (0)