You are here

public function LinkCollectionNormalizerTest::testNormalize in Drupal 10

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()

Tests the link collection normalizer.

File

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

Class

LinkCollectionNormalizerTest
@coversDefaultClass \Drupal\jsonapi\Normalizer\LinkCollectionNormalizer @group jsonapi

Namespace

Drupal\Tests\jsonapi\Kernel\Normalizer

Code

public function testNormalize() {
  $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));
}