diff --git a/README.md b/README.md index 7008531..e124f2a 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,14 @@ expect('App') In the test above, the test would pass even if the word `69` was included in one of the tested files. +Or, you may want to test for profanity not included in the list. To do this, pass an `including` argument to the `toHaveNoProfanity` method. This argument should be an array of strings you also want to consider as profanity. For example: + +```php +expect('App') + ->toHaveNoProfanity(including: ['dagnabbit']); +``` + + If a test does fail because of Profanity, then the output will show the offending file and line. IDE's such as PHPStorm, will allow you to click the file and be taken straight to the line that contains profanity: diff --git a/src/Expectations/Profanity.php b/src/Expectations/Profanity.php index 813c7f5..83f4131 100644 --- a/src/Expectations/Profanity.php +++ b/src/Expectations/Profanity.php @@ -7,12 +7,14 @@ use Pest\Arch\Support\FileLineFinder; use PHPUnit\Architecture\Elements\ObjectDescription; -expect()->extend('toHaveNoProfanity', fn (array $excluding = []): ArchExpectation => Targeted::make( +expect()->extend('toHaveNoProfanity', fn (array $excluding = [], array $including = []): ArchExpectation => Targeted::make( $this, - function (ObjectDescription $object) use (&$foundWords, $excluding): bool { + function (ObjectDescription $object) use (&$foundWords, $excluding, $including): bool { $words = include __DIR__.'/../Config/words.php'; $toleratedWords = include __DIR__.'/../Config/tolerated.php'; + $words = array_merge($words, $including); + $words = array_diff($words, $excluding); $fileContents = (string) file_get_contents($object->path); diff --git a/tests/Fixtures/HasUncoveredProfanity.php b/tests/Fixtures/HasUncoveredProfanity.php new file mode 100644 index 0000000..1f37d72 --- /dev/null +++ b/tests/Fixtures/HasUncoveredProfanity.php @@ -0,0 +1,15 @@ +toHaveNoProfanity(); })->throws(ArchExpectationFailedException::class); + +it('fails if file contains profanity manually included', function () { + expect('Tests\Fixtures\HasUncoveredProfanity') + ->toHaveNoProfanity(including: ['dagnabbit']); +})->throws(ArchExpectationFailedException::class);