Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ Make sure a file or directory files returns only string values.
expect('file.php')->toReturnStrings();
```

### forbidEmpty
Make sure a file or directory files does not return any empty value.
```php
expect('file.php')->forbidEmpty();
```

----

### Success
Expand Down
5 changes: 4 additions & 1 deletion src/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
use Pest\Expectation as PestExpectation;

$expectations = get_class_methods(Expectation::class);
$expectations = array_filter($expectations, fn ($function): bool => str_starts_with($function, 'toReturn') || str_starts_with($function, 'toBe'));
$expectations = array_filter($expectations, fn ($function): bool => str_starts_with($function, 'toReturn')
|| str_starts_with($function, 'toBe')
|| str_starts_with($function, 'forbid')
);

foreach ($expectations as $expectation) {
expect()->extend(
Expand Down
9 changes: 9 additions & 0 deletions src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,13 @@ public function toReturnStrings(int $depth = -1): void
'Not string detected'
);
}

public function forbidEmpty(int $depth = -1): void
{
$this->applyOnDirectory(
$depth,
fn (Content $content): bool => $this->emptyIn($content),
'Empty value detected'
);
}
}
6 changes: 5 additions & 1 deletion src/Inside.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ protected function applyOnDirectory(int $depth, callable $callback, string $mess

$unwanted = $callback($content);

expect($unwanted)->toBeEmpty("$message: ".implode(', ', $unwanted)." in $file");
if (is_array($unwanted)) {
expect($unwanted)->toBeEmpty("$message: ".implode(', ', $unwanted)." in $file");
} else {
expect($unwanted)->toBeTrue($message);
}
}

return new PestExpectation($this->value);
Expand Down
20 changes: 20 additions & 0 deletions src/Investigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,24 @@ private function notStringsIn(Content|array $content): array

return $unwanted;
}

/**
* @param Content|array<int|string, string|array<string, string>> $content
*/
private function emptyIn(Content|array $content): bool
{
foreach ($content as $word) {
if (is_array($word)) {
$this->emptyIn($word);

continue;
}

if ($word == '') {
return false;
}
}

return true;
}
}
1 change: 0 additions & 1 deletion tests/Fixtures/returnsMultipleDuplicates.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

return [
'',
'f@issa!oux',
1,
'pest',
Expand Down
1 change: 0 additions & 1 deletion tests/Fixtures/text/returnsMultipleDuplicates.stub
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

f@issa!oux
1
pest
Expand Down
25 changes: 25 additions & 0 deletions tests/forbidEmpty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use PHPUnit\Framework\ExpectationFailedException;

it('passes', function (string $file): void {
expect($file)->forbidEmpty();
})->with([
'tests/Fixtures/returnsMultipleDuplicates.php',
'tests/Fixtures/text/returnsMultipleDuplicates.stub',
]);

it('fails', function (string $file): void {
expect($file)->forbidEmpty();
})->with([
'tests/Fixtures/returnsDuplicates.php',
'tests/Fixtures/text/returnsDuplicates.stub',
])->throws(ExpectationFailedException::class, 'Empty value detected');

it('passes when directory is empty', function (): void {
expect('tests/Fixtures/empty')->forbidEmpty();
});

it('fails when directory does not exist', function (): void {
expect('tests/Fixtures/notExist')->forbidEmpty();
})->throws(ExpectationFailedException::class);
Loading