diff --git a/tests/unit/Converter/Proxy/CacheTest.php b/tests/unit/Converter/Proxy/CacheTest.php index 4483e9c3..fb2a3484 100644 --- a/tests/unit/Converter/Proxy/CacheTest.php +++ b/tests/unit/Converter/Proxy/CacheTest.php @@ -6,6 +6,7 @@ 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; @@ -13,26 +14,43 @@ 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) + ); } /**