Skip to content

Commit

Permalink
Refactor unit test of cache converter
Browse files Browse the repository at this point in the history
  • Loading branch information
guvra committed Mar 13, 2024
1 parent 2eb53a1 commit 8ed8b49
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions tests/unit/Converter/Proxy/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,51 @@

use Smile\GdprDump\Converter\Parameters\ValidationException;
use Smile\GdprDump\Converter\Proxy\Cache;
use Smile\GdprDump\Converter\Randomizer\RandomizeText;
use Smile\GdprDump\Tests\Framework\Mock\Converter\ConverterMock;
use Smile\GdprDump\Tests\Unit\Converter\TestCase;
use stdClass;

class CacheTest extends TestCase
{
/**
* Test the converter.
* Assert that values are properly cached.
*/
public function testConverter(): void
public function testValueIsCached(): void
{
$converter1 = $this->createConverter(Cache::class, [
'converter' => $this->createConverter(ConverterMock::class, ['prefix' => '1_']),
$parameters = [
'converter' => $this->createConverter(RandomizeText::class),
'cache_key' => 'key1',
]);
$converter2 = $this->createConverter(Cache::class, [
'converter' => $this->createConverter(ConverterMock::class, ['prefix' => '2_']),
'cache_key' => 'key2',
]);
];

$converter = $this->createConverter(Cache::class, $parameters);
$value = 'text to randomize';
$convertedValue = $converter->convert($value);

// The converter must always return the same result for a given value
$this->assertSame($convertedValue, $converter->convert($value));
$this->assertNotSame($convertedValue, $converter->convert('another text to randomize'));

// Value generated must not be the same when using another cache key
$converter->setParameters(array_merge($parameters, ['cache_key' => 'key2']));
$this->assertNotSame($convertedValue, $converter->convert($value));
}

$value = 'textToAnonymize';
$value1 = $converter1->convert($value);
$value2 = $converter1->convert($value);
$this->assertSame($value1, $value2);
/**
* Assert that that the cache storage is shared between converter instances.
*/
public function testCacheIsSharedBetweenInstances(): void
{
$value = 'text to randomize';
$parameters = [
'converter' => $this->createConverter(RandomizeText::class),
'cache_key' => 'key1',
];

$value3 = $converter2->convert($value);
$this->assertNotSame($value3, $value2);
$this->assertSame(
$this->createConverter(Cache::class, $parameters)->convert($value),
$this->createConverter(Cache::class, $parameters)->convert($value)
);
}

/**
Expand Down

0 comments on commit 8ed8b49

Please sign in to comment.