Skip to content

Commit

Permalink
Merge pull request #5 from alexmacarthur/master
Browse files Browse the repository at this point in the history
Add means for covering more profanity.
  • Loading branch information
JonPurvis authored Aug 6, 2024
2 parents dc1b998 + a4c4fd4 commit 2f9c6b8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

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

declare(strict_types=1);

namespace Tests\Fixtures;

class HasUncoveredProfanity
{
/**
* oh dagnabbit
*/
public function __construct(
//
) {}
}
5 changes: 5 additions & 0 deletions tests/toHaveProfanity.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@
expect('Tests\Fixtures\HasProfanityInProperty')
->toHaveNoProfanity();
})->throws(ArchExpectationFailedException::class);

it('fails if file contains profanity manually included', function () {
expect('Tests\Fixtures\HasUncoveredProfanity')
->toHaveNoProfanity(including: ['dagnabbit']);
})->throws(ArchExpectationFailedException::class);

0 comments on commit 2f9c6b8

Please sign in to comment.