mirror of
https://github.com/danog/Valinor.git
synced 2024-11-30 04:39:05 +01:00
69ad3f4777
The cache implementation that was previously injected in the mapper builder must now be manually injected. This gives better control on when the cache should be enabled, especially depending on which environment the application is running. The library provides a cache implementation out of the box, which saves cache entries into the file system. It is also possible to use any PSR-16 compliant implementation, as long as it is capable of caching the entries handled by the library. ```php $cache = new \CuyZ\Valinor\Cache\FileSystemCache('path/to/cache-dir'); (new \CuyZ\Valinor\MapperBuilder()) ->withCache($cache) ->mapper() ->map(SomeClass::class, [/* … */]); ```
45 lines
1.4 KiB
PHP
45 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace CuyZ\Valinor\Tests\Integration\Mapping;
|
|
|
|
use CuyZ\Valinor\Mapper\MappingError;
|
|
use CuyZ\Valinor\Mapper\Tree\Node;
|
|
use CuyZ\Valinor\MapperBuilder;
|
|
use CuyZ\Valinor\Tests\Fake\Mapper\Tree\Message\FakeErrorMessage;
|
|
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
|
|
use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject;
|
|
|
|
final class VisitorMappingTest extends IntegrationTest
|
|
{
|
|
public function test_visitors_are_called_during_mapping(): void
|
|
{
|
|
$visits = [];
|
|
$error = new FakeErrorMessage();
|
|
|
|
try {
|
|
(new MapperBuilder())
|
|
->visit(function (Node $node) use (&$visits): void {
|
|
if ($node->isRoot()) {
|
|
$visits[] = '#1';
|
|
}
|
|
})
|
|
->visit(function (Node $node) use (&$visits): void {
|
|
if ($node->isRoot()) {
|
|
$visits[] = '#2';
|
|
}
|
|
})
|
|
->visit(function () use ($error): void {
|
|
throw $error;
|
|
})
|
|
->mapper()
|
|
->map(SimpleObject::class, ['value' => 'foo']);
|
|
} catch (MappingError $exception) {
|
|
self::assertSame('some error message', (string)$exception->node()->messages()[0]);
|
|
}
|
|
|
|
self::assertSame(['#1', '#2'], $visits);
|
|
}
|
|
}
|