Skip to content

Commit

Permalink
add test coverage for handling per class replacement value on custom …
Browse files Browse the repository at this point in the history
…regex classes.
  • Loading branch information
yordadev committed Sep 22, 2023
1 parent 308330c commit dbb6c0c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/Repositories/RegexRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public function __construct(
) {
}

public static function checkAndSanitize(string $regex, string $replace, string $content, int &$hits = 0):
string
public static function checkAndSanitize(string $regex, string $replace, string $content, int &$hits = 0): string
{
return preg_replace("~$regex~i", $replace, $content, -1, $hits);
}
Expand Down
70 changes: 70 additions & 0 deletions tests/Unit/Services/ScrubberServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace YorCreative\Scrubber\Tests\Unit\Services;

use GuzzleHttp\Psr7\Response;
use Illuminate\Support\Str;
use YorCreative\Scrubber\Interfaces\RegexCollectionInterface;
use YorCreative\Scrubber\Repositories\RegexRepository;
use YorCreative\Scrubber\Services\ScrubberService;
use YorCreative\Scrubber\Tests\TestCase;
Expand Down Expand Up @@ -65,4 +67,72 @@ public function test_it_can_get_regex_repository()
{
$this->assertInstanceOf(RegexRepository::class, ScrubberService::getRegexRepository());
}

public function test_it_can_handle_get_replacement_value_on_custom_class()
{
$withReplacement = new class() implements RegexCollectionInterface {

public function isSecret(): bool
{
return false;
}

public function getPattern(): string
{
return 'something_with';
}

public function getTestableString(): string
{
return 'something_with';
}

public function getReplacementValue(): string
{
return 'not_something';
}
};

$withoutReplacement = new class() implements RegexCollectionInterface {

public function isSecret(): bool
{
return false;
}

public function getPattern(): string
{
return 'without_something';
}

public function getTestableString(): string
{
return 'without_something';
}
};

$regexCollection = collect([
'with_replacement' => $withReplacement,
'without_replacement' => $withoutReplacement
]);

$regexRepository = new RegexRepository($regexCollection);
$this->app->instance(RegexRepository::class, $regexRepository);

$content = 'something_with';
ScrubberService::autoSanitize($content);
$this->assertEquals($withReplacement->getReplacementValue(), $content);


$content = 'without_something';
ScrubberService::autoSanitize($content);

$defaultReplacementValue = config('scrubber.redaction');
$this->assertEquals($defaultReplacementValue, $content);

$this->assertNotEquals(
$withReplacement->getReplacementValue(),
$defaultReplacementValue
);
}
}

0 comments on commit dbb6c0c

Please sign in to comment.