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

Add found words to output #26

Merged
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
8 changes: 5 additions & 3 deletions src/Expectations/Profanity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

use JonPurvis\Profanify\Expectations\TargetedProfanity;
use Pest\Arch\Contracts\ArchExpectation;
use Pest\Arch\Expectations\Targeted;
use Pest\Arch\Support\FileLineFinder;
use PHPUnit\Architecture\Elements\ObjectDescription;

expect()->extend('toHaveNoProfanity', fn (array $excluding = [], array $including = [], $language = null): ArchExpectation => Targeted::make(
expect()->extend('toHaveNoProfanity', fn (array $excluding = [], array $including = [], $language = null): ArchExpectation => TargetedProfanity::make(
$this,
function (ObjectDescription $object) use (&$foundWords, $excluding, $including, $language): bool {

Expand Down Expand Up @@ -50,7 +50,9 @@ function (ObjectDescription $object) use (&$foundWords, $excluding, $including,

return $foundWords === [];
},
'to not use profanity',
function ($path) use (&$foundWords): string {
return "to have no profanity, but found '".implode(', ', array_values($foundWords ?? []))."'";
},
FileLineFinder::where(function (string $line) use (&$foundWords): bool {
return str_contains(strtolower($line), strtolower((string) array_values($foundWords ?? [])[0]));
})
Expand Down
56 changes: 56 additions & 0 deletions src/Expectations/TargetedProfanity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace JonPurvis\Profanify\Expectations;

use Pest\Arch\Blueprint;
use Pest\Arch\Collections\Dependencies;
use Pest\Arch\Exceptions\ArchExpectationFailedException;
use Pest\Arch\Options\LayerOptions;
use Pest\Arch\SingleArchExpectation;
use Pest\Arch\ValueObjects\Targets;
use Pest\Arch\ValueObjects\Violation;
use Pest\Expectation;

/**
* @internal
*/
final class TargetedProfanity
{
/**
* Creates an "TargetedProfanity" expectation.
*/
public static function make(
Expectation $expectation,
callable $callback,
callable $what,
callable $line,
): SingleArchExpectation {
assert(is_string($expectation->value) || is_array($expectation->value));
/** @var Expectation<array<int, string>|string> $expectation */
$blueprint = Blueprint::make(
Targets::fromExpectation($expectation),
Dependencies::fromExpectationInput([]),
);

return SingleArchExpectation::fromExpectation(
$expectation,
static function (LayerOptions $options) use ($callback, $blueprint, $what, $line): void {
$blueprint->targeted(
$callback,
$options,
static fn (Violation $violation) => throw new ArchExpectationFailedException(
$violation,
sprintf(
"Expecting '%s' %s.",
$violation->path,
$what($violation->path),
),
),
$line,
);
},
);
}
}
Loading