mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
feat: not.toHaveSuspiciousCharacters
This commit is contained in:
@ -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(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user