Valinor/tests/Integration/Mapping/Attribute/IdentifierAttributeMappingTest.php
Romain Canon 69ad3f4777 feat: allow injecting a cache implementation that is used by the mapper
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, [/* … */]);
```
2022-05-23 20:28:02 +02:00

98 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
namespace CuyZ\Valinor\Tests\Integration\Mapping\Attribute;
use CuyZ\Valinor\Attribute\Identifier;
use CuyZ\Valinor\Mapper\MappingError;
use CuyZ\Valinor\MapperBuilder;
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
use function get_class;
final class IdentifierAttributeMappingTest extends IntegrationTest
{
/**
* @requires PHP >= 8
*/
public function test_identifier_attribute_is_mapped_properly(): void
{
$class = new class () {
/** @var \CuyZ\Valinor\Tests\Integration\Mapping\Attribute\IdentifierAttribute[] */
public array $identifierAttribute;
/** @var \CuyZ\Valinor\Tests\Integration\Mapping\Attribute\IdentifierAttributeWithConstructor[] */
public array $identifierAttributeWithConstructor;
};
$source = [
'identifierAttribute' => [
'foo' => [],
'bar' => [],
'baz' => [],
],
'identifierAttributeWithConstructor' => [
'foo' => [],
'bar' => [],
'baz' => [],
],
];
try {
$result = (new MapperBuilder())->mapper()->map(get_class($class), $source);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertSame('foo', $result->identifierAttribute['foo']->identifier);
self::assertSame('bar', $result->identifierAttribute['bar']->identifier);
self::assertSame('baz', $result->identifierAttribute['baz']->identifier);
self::assertSame('foo', $result->identifierAttributeWithConstructor['foo']->identifier);
self::assertSame('bar', $result->identifierAttributeWithConstructor['bar']->identifier);
self::assertSame('baz', $result->identifierAttributeWithConstructor['baz']->identifier);
}
public function test_identifier_doctrine_annotation_is_mapped_properly(): void
{
$class = new class () {
/** @var \CuyZ\Valinor\Tests\Integration\Mapping\Attribute\IdentifierDoctrineAnnotation[] */
public array $identifierDoctrineAnnotation;
};
$source = ['identifierDoctrineAnnotation' => [
'foo' => [],
'bar' => [],
'baz' => [],
]];
$result = (new MapperBuilder())->enableLegacyDoctrineAnnotations()->mapper()->map(get_class($class), $source);
self::assertSame('foo', $result->identifierDoctrineAnnotation['foo']->identifier);
self::assertSame('bar', $result->identifierDoctrineAnnotation['bar']->identifier);
self::assertSame('baz', $result->identifierDoctrineAnnotation['baz']->identifier);
}
}
final class IdentifierDoctrineAnnotation
{
/** @Identifier */
public string $identifier;
}
class IdentifierAttribute
{
#[Identifier]
public string $identifier;
}
class IdentifierAttributeWithConstructor extends IdentifierAttribute
{
public function __construct(
#[Identifier]
string $identifier
) {
$this->identifier = $identifier;
}
}