feat: not.toHaveSuspiciousCharacters

This commit is contained in:
Nuno Maduro
2025-07-26 07:47:00 -06:00
parent 66e59efec6
commit 4c88590b89
5 changed files with 30 additions and 11 deletions

View File

@ -4,6 +4,9 @@ declare(strict_types=1);
namespace Pest\ArchPresets;
use Pest\Arch\Contracts\ArchExpectation;
use Pest\Expectation;
/**
* @internal
*/
@ -89,5 +92,9 @@ final class Php extends AbstractPreset
'xdebug_var_dump',
'trap',
])->not->toBeUsed();
$this->eachUserNamespace(
fn (Expectation $namespace): ArchExpectation => $namespace->not->toHaveSuspiciousCharacters(),
);
}
}

View File

@ -897,16 +897,9 @@ final class Expectation
/**
* Asserts that the source code of the given expectation target does not include suspicious characters.
*/
public function toNotIncludeSuspiciousCharacters(): ArchExpectation
public function toHaveSuspiciousCharacters(): ArchExpectation
{
$checker = new Spoofchecker();
return Targeted::make(
$this,
fn (ObjectDescription $object) => ! $checker->isSuspicious(file_get_contents($object->path)),
'to not include suspicious characters',
FileLineFinder::where(fn (string $line) => $checker->isSuspicious($line)),
);
throw InvalidExpectation::fromMethods(['toHaveSuspiciousCharacters']);
}
/**

View File

@ -24,6 +24,7 @@ use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\ExpectationFailedException;
use ReflectionMethod;
use ReflectionProperty;
use Spoofchecker;
use stdClass;
/**
@ -278,6 +279,24 @@ final readonly class OppositeExpectation
);
}
/**
* Asserts that the given expectation target does not have suspicious characters.
*/
public function toHaveSuspiciousCharacters(): ArchExpectation
{
$checker = new Spoofchecker;
/** @var Expectation<array<int, string>|string> $original */
$original = $this->original;
return Targeted::make(
$original,
fn (ObjectDescription $object): bool => ! $checker->isSuspicious((string) file_get_contents($object->path)),
'to not include suspicious characters',
FileLineFinder::where(fn (string $line): bool => $checker->isSuspicious($line)),
);
}
/**
* Asserts that the given expectation target does not have the given methods.
*

View File

@ -1785,4 +1785,4 @@
✓ pass with dataset with ('my-datas-set-value')
✓ within describe → pass with dataset with ('my-datas-set-value')
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 39 todos, 35 skipped, 1189 passed (2815 assertions)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 39 todos, 35 skipped, 1189 passed (2816 assertions)

View File

@ -16,7 +16,7 @@ $run = function () {
test('parallel', function () use ($run) {
expect($run('--exclude-group=integration'))
->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 39 todos, 26 skipped, 1179 passed (2791 assertions)')
->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 39 todos, 26 skipped, 1179 passed (2792 assertions)')
->toContain('Parallel: 3 processes');
})->skipOnWindows();