diff --git a/README.md b/README.md index 4642842..b4c77d0 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,6 @@ 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: @@ -110,6 +109,17 @@ at tests/Fixtures/HasProfanityInComment.php:10 Duration: 0.06s ``` +By default, Profanify will scan all language files, which may cause some problems if a word in your language is fine but +is listed as profane in another language. To combat this, you can specify a default language, which means only that file will be +checked against when the test runs: + +```php +expect('App') + ->toHaveNoProfanity(language: 'en'); +``` + +The example above means that only profanity in `Config/profanities/en.php` file will be picked up. + ## Languages Profanify currently supports the following languages: diff --git a/src/Expectations/Profanity.php b/src/Expectations/Profanity.php index 02ffcaa..96d940f 100644 --- a/src/Expectations/Profanity.php +++ b/src/Expectations/Profanity.php @@ -7,9 +7,9 @@ use Pest\Arch\Support\FileLineFinder; use PHPUnit\Architecture\Elements\ObjectDescription; -expect()->extend('toHaveNoProfanity', fn (array $excluding = [], array $including = []): ArchExpectation => Targeted::make( +expect()->extend('toHaveNoProfanity', fn (array $excluding = [], array $including = [], $language = null): ArchExpectation => Targeted::make( $this, - function (ObjectDescription $object) use (&$foundWords, $excluding, $including): bool { + function (ObjectDescription $object) use (&$foundWords, $excluding, $including, $language): bool { $words = []; $profanitiesDir = __DIR__.'/../Config/profanities'; @@ -19,11 +19,19 @@ function (ObjectDescription $object) use (&$foundWords, $excluding, $including): } $profanitiesFiles = array_diff($profanitiesFiles, ['.', '..']); - foreach ($profanitiesFiles as $profanitiesFile) { - $words = array_merge( - $words, - include "$profanitiesDir/$profanitiesFile" - ); + + if ($language) { + $specificLanguage = "$profanitiesDir/$language.php"; + if (file_exists($specificLanguage)) { + $words = include $specificLanguage; + } + } else { + foreach ($profanitiesFiles as $profanitiesFile) { + $words = array_merge( + $words, + include "$profanitiesDir/$profanitiesFile" + ); + } } $toleratedWords = include __DIR__.'/../Config/tolerated.php'; diff --git a/tests/Fixtures/HasDifferentLanguageProfanity.php b/tests/Fixtures/HasDifferentLanguageProfanity.php new file mode 100644 index 0000000..34b241c --- /dev/null +++ b/tests/Fixtures/HasDifferentLanguageProfanity.php @@ -0,0 +1,15 @@ +toHaveNoProfanity(); }); + +it('passes if a language is specified and a file contains profanity in another language', function () { + expect('Tests\Fixtures\HasDifferentLanguageProfanity') + ->toHaveNoProfanity(language: 'en'); +}); diff --git a/tests/toHaveProfanity.php b/tests/toHaveProfanity.php index 44474ab..80693bc 100644 --- a/tests/toHaveProfanity.php +++ b/tests/toHaveProfanity.php @@ -28,3 +28,8 @@ expect('Tests\Fixtures\HasUncoveredProfanity') ->toHaveNoProfanity(including: ['dagnabbit']); })->throws(ArchExpectationFailedException::class); + +it('fails if file contains profanity when a specific language has been set', function () { + expect('Tests\Fixtures\HasProfanityInComment') + ->toHaveNoProfanity(language: 'en'); +})->throws(ArchExpectationFailedException::class);