Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

specify language #22

Merged
merged 2 commits into from
Oct 6, 2024
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
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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:

Expand Down
22 changes: 15 additions & 7 deletions src/Expectations/Profanity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down
15 changes: 15 additions & 0 deletions tests/Fixtures/HasDifferentLanguageProfanity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures;

class HasDifferentLanguageProfanity
{
/**
* just a merda comment
*/
public function __construct(
//
) {}
}
5 changes: 5 additions & 0 deletions tests/toHaveNoProfanity.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@
expect('Tests\Fixtures\HasCapitalisedToleratedProfanity')
->toHaveNoProfanity();
});

it('passes if a language is specified and a file contains profanity in another language', function () {
expect('Tests\Fixtures\HasDifferentLanguageProfanity')
->toHaveNoProfanity(language: 'en');
});
5 changes: 5 additions & 0 deletions tests/toHaveProfanity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Loading