Valinor/tests/Unit/Cache/Compiled/MixedValueCacheCompilerTest.php
Romain Canon 2d70efbfbb feat: extract file watching feature in own cache implementation
When the application runs in a development environment, the cache
implementation should be decorated with `FileWatchingCache` to prevent
invalid cache entries states, which can result in the library not
behaving as expected (missing property value, callable with outdated
signature, …).

```php
$cache = new \CuyZ\Valinor\Cache\FileSystemCache('path/to/cache-dir');

if ($isApplicationInDevelopmentEnvironment) {
    $cache = new \CuyZ\Valinor\Cache\FileWatchingCache($cache);
}

(new \CuyZ\Valinor\MapperBuilder())
    ->withCache($cache)
    ->mapper()
    ->map(SomeClass::class, [/* … */]);
```

This behavior now forces to explicitly inject `FileWatchingCache`, when
it was done automatically before; but because it shouldn't be used in
a production environment, it will increase overall performance.
2022-05-23 20:28:02 +02:00

46 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace CuyZ\Valinor\Tests\Unit\Cache\Compiled;
use CuyZ\Valinor\Cache\Compiled\MixedValueCacheCompiler;
use PHPUnit\Framework\TestCase;
use stdClass;
final class MixedValueCacheCompilerTest extends TestCase
{
private MixedValueCacheCompiler $compiler;
protected function setUp(): void
{
parent::setUp();
$this->compiler = new MixedValueCacheCompiler();
}
/**
* @dataProvider values_are_compiled_correctly_data_provider
*
* @param mixed $value
*/
public function test_values_are_compiled_correctly($value): void
{
$compiledValue = eval('return ' . $this->compiler->compile($value) . ';');
self::assertEquals($value, $compiledValue);
}
public function values_are_compiled_correctly_data_provider(): iterable
{
yield 'Float' => [1337.42];
yield 'Int' => [404];
yield 'String' => ['Schwifty!'];
yield 'True' => [true];
yield 'False' => [false];
yield 'Array of scalar' => [[1337.42, 404, 'Schwifty!', true, false]];
yield 'Object' => [new stdClass()];
yield 'Array with object' => [[new stdClass()]];
}
}