mirror of
https://github.com/danog/Valinor.git
synced 2024-12-02 09:37:46 +01:00
2d70efbfbb
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.
40 lines
1.1 KiB
PHP
40 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace CuyZ\Valinor\Tests\Integration\Cache;
|
|
|
|
use CuyZ\Valinor\Cache\FileSystemCache;
|
|
use CuyZ\Valinor\Cache\FileWatchingCache;
|
|
use CuyZ\Valinor\MapperBuilder;
|
|
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
|
|
use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject;
|
|
use org\bovigo\vfs\vfsStream;
|
|
|
|
use function strtoupper;
|
|
|
|
final class CacheInjectionTest extends IntegrationTest
|
|
{
|
|
public function test_cache_entries_are_written_during_mapping(): void
|
|
{
|
|
$files = vfsStream::setup('cache-dir');
|
|
|
|
$cache = new FileSystemCache($files->url());
|
|
$cache = new FileWatchingCache($cache);
|
|
|
|
self::assertFalse($files->hasChildren());
|
|
|
|
$object = (new MapperBuilder())
|
|
->withCache($cache)
|
|
// The cache should be able to cache function definitions…
|
|
->alter(fn (string $value): string => strtoupper($value))
|
|
->mapper()
|
|
// …as well as class definitions.
|
|
->map(SimpleObject::class, 'foo');
|
|
|
|
self::assertSame('FOO', $object->value);
|
|
|
|
self::assertTrue($files->hasChildren());
|
|
}
|
|
}
|