function LinkCollectionNormalizerTest::testNormalize

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/tests/src/Kernel/Normalizer/LinkCollectionNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\LinkCollectionNormalizerTest::testNormalize()
  2. 11.x core/modules/jsonapi/tests/src/Kernel/Normalizer/LinkCollectionNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\LinkCollectionNormalizerTest::testNormalize()

Tests the link collection normalizer.

File

core/modules/jsonapi/tests/src/Kernel/Normalizer/LinkCollectionNormalizerTest.php, line 80

Class

LinkCollectionNormalizerTest
@coversDefaultClass \Drupal\jsonapi\Normalizer\LinkCollectionNormalizer[[api-linebreak]] @group jsonapi

Namespace

Drupal\Tests\jsonapi\Kernel\Normalizer

Code

public function testNormalize() : void {
  $link_context = new ResourceObject(new CacheableMetadata(), new ResourceType('n/a', 'n/a', 'n/a'), 'n/a', NULL, [], new LinkCollection([]));
  $link_collection = (new LinkCollection([]))->withLink('related', new Link(new CacheableMetadata(), Url::fromUri('http://example.com/post/42'), 'related', [
    'title' => 'Most viewed',
  ]))
    ->withLink('related', new Link(new CacheableMetadata(), Url::fromUri('http://example.com/post/42'), 'related', [
    'title' => 'Top rated',
  ]))
    ->withContext($link_context);
  // Create the SUT.
  $normalized = $this->getNormalizer()
    ->normalize($link_collection)
    ->getNormalization();
  $this->assertIsArray($normalized);
  foreach (array_keys($normalized) as $key) {
    $this->assertStringStartsWith('related', $key);
  }
  $this->assertSame([
    [
      'href' => 'http://example.com/post/42',
      'meta' => [
        'title' => 'Most viewed',
      ],
    ],
    [
      'href' => 'http://example.com/post/42',
      'meta' => [
        'title' => 'Top rated',
      ],
    ],
  ], array_values($normalized));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.