-
-
Notifications
You must be signed in to change notification settings - Fork 962
Expand file tree
/
Copy pathDocumentNormalizerTest.php
More file actions
83 lines (65 loc) · 2.62 KB
/
DocumentNormalizerTest.php
File metadata and controls
83 lines (65 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Elasticsearch\Tests\Serializer;
use ApiPlatform\Elasticsearch\Serializer\DocumentNormalizer;
use ApiPlatform\Elasticsearch\Tests\Fixtures\Foo;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerAwareInterface;
final class DocumentNormalizerTest extends TestCase
{
public function testConstruct(): void
{
$normalizer = new DocumentNormalizer();
self::assertInstanceOf(NormalizerInterface::class, $normalizer);
self::assertInstanceOf(SerializerAwareInterface::class, $normalizer);
}
public function testSupportsNormalization(): void
{
$normalizer = new DocumentNormalizer();
self::assertTrue($normalizer->supportsNormalization(new Foo(), DocumentNormalizer::FORMAT));
self::assertFalse($normalizer->supportsNormalization(new Foo(), 'json'));
self::assertFalse($normalizer->supportsNormalization('not an object', DocumentNormalizer::FORMAT));
}
public function testNormalize(): void
{
$normalizer = new DocumentNormalizer();
$foo = new Foo();
$foo->setName('Test');
$foo->setBar('Value');
$result = $normalizer->normalize($foo, DocumentNormalizer::FORMAT);
self::assertIsArray($result);
self::assertSame('Test', $result['name']);
self::assertSame('Value', $result['bar']);
}
public function testNormalizeWithId(): void
{
$normalizer = new DocumentNormalizer();
// Use anonymous class with id to test _id/_source wrapping
$object = new class {
public int $id = 1;
public string $name = 'Test';
};
$result = $normalizer->normalize($object, DocumentNormalizer::FORMAT);
self::assertIsArray($result);
self::assertArrayHasKey('_id', $result);
self::assertArrayHasKey('_source', $result);
self::assertSame('1', $result['_id']);
self::assertSame(1, $result['_source']['id']);
self::assertSame('Test', $result['_source']['name']);
}
public function testGetSupportedTypes(): void
{
$normalizer = new DocumentNormalizer();
self::assertSame(['object' => true], $normalizer->getSupportedTypes(DocumentNormalizer::FORMAT));
self::assertSame([], $normalizer->getSupportedTypes('json'));
}
}