public function JsonapiHypermediaLinkCollectionNormalizerTest::testNormalize in JSON:API Hypermedia 8
Tests link collection normalization.
File
- tests/
src/ Kernel/ Normalizer/ JsonapiHypermediaLinkCollectionNormalizerTest.php, line 53
Class
- JsonapiHypermediaLinkCollectionNormalizerTest
- Test the link collection normalizer that replaces the core normalizer.
Namespace
Drupal\Tests\jsonapi_hypermedia\Kernel\NormalizerCode
public function testNormalize() {
// A single link with a key that matches its link relation type.
$this
->assertSame([
'self' => [
'href' => 'https://jsonapi.org',
],
], $this
->getNormalization($this
->getTestLinkCollection([
'self' => new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org'), 'self'),
])));
// A single link with a key that's different from its link relation type.
$this
->assertSame([
'self' => [
'href' => 'https://jsonapi.org',
'meta' => [
'linkParams' => [
'rel' => [
'describedby',
],
],
],
],
], $this
->getNormalization($this
->getTestLinkCollection([
'self' => new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org'), 'describedby'),
])));
// Two links with a matching keys and matching link relation types.
$link_collection = $this
->getTestLinkCollection([
'self' => new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org'), 'self'),
]);
$link_collection = $link_collection
->withLink('self', new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org'), 'self'));
$this
->assertSame([
'self' => [
'href' => 'https://jsonapi.org',
],
], $this
->getNormalization($link_collection));
// Two links with a matching keys but different link relation types.
$link_collection = $this
->getTestLinkCollection([
'self' => new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org'), 'self'),
]);
$link_collection = $link_collection
->withLink('self', new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org'), 'describedby'));
$this
->assertSame([
'self' => [
'href' => 'https://jsonapi.org',
'meta' => [
'linkParams' => [
'rel' => [
'self',
'describedby',
],
],
],
],
], $this
->getNormalization($link_collection));
// Two links with a matching keys and matching link relation types and
// target attributes.
$link_collection = $this
->getTestLinkCollection([
'self' => new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org'), 'self', [
'foo' => 'bar',
]),
]);
$link_collection = $link_collection
->withLink('self', new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org'), 'self', [
'foo' => 'bar',
]));
$actual = $this
->getNormalization($link_collection);
$this
->assertCount(1, $actual, var_export($actual, TRUE));
$this
->assertSame([
'self' => [
'href' => 'https://jsonapi.org',
'meta' => [
'linkParams' => [
'foo' => 'bar',
],
],
],
], $actual);
// Two links with a matching keys and matching link relation types, but
// different target attributes.
$link_collection = $this
->getTestLinkCollection([
'self' => new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org'), 'self', [
'foo' => 'bar',
]),
]);
$link_collection = $link_collection
->withLink('self', new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org'), 'self', [
'foo' => 'baz',
]));
$actual = $this
->getNormalization($link_collection);
$this
->assertCount(2, $actual, var_export($actual, TRUE));
$normalized_keys = array_keys($actual);
$this
->assert(array_reduce($normalized_keys, function ($bool, $key) {
return $bool ? strpos($key, 'self--') === 0 : FALSE;
}, TRUE), var_export($actual, TRUE));
$this
->assertSame([
'href' => 'https://jsonapi.org',
'meta' => [
'linkParams' => [
'foo' => 'bar',
],
],
], $actual[$normalized_keys[0]]);
$this
->assertSame([
'href' => 'https://jsonapi.org',
'meta' => [
'linkParams' => [
'foo' => 'baz',
],
],
], $actual[$normalized_keys[1]]);
// Two links with different keys and link relation types that match their
// keys.
$this
->assertSame([
'related' => [
'href' => 'https://jsonapi.org',
],
'self' => [
'href' => 'https://jsonapi.org',
],
], $this
->getNormalization($this
->getTestLinkCollection([
'self' => new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org'), 'self'),
'related' => new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org'), 'related'),
])));
// Two links with different keys and link relation types that match their
// sibling's keys.
$this
->assertSame([
'related' => [
'href' => 'https://jsonapi.org',
'meta' => [
'linkParams' => [
'rel' => [
'self',
],
],
],
],
'self' => [
'href' => 'https://jsonapi.org',
'meta' => [
'linkParams' => [
'rel' => [
'related',
],
],
],
],
], $this
->getNormalization($this
->getTestLinkCollection([
'self' => new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org'), 'related'),
'related' => new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org'), 'self'),
])));
}