You are here

protected function LinkProviderTest::getLink in JSON:API Hypermedia 8

Requests a document from which to assert & extract an expected link.

Parameters

string $link_location: The path to the links object in the document given as a dot (.) separated list of JSON property names.

string $link_key: The key of the expected link.

string[]|true $expected_on_document_types: The types of documents on which to assert and extract the link. TRUE if the link is expected on all document types.

Return value

array|null The normalized link or NULL if the link was not found.

1 call to LinkProviderTest::getLink()
LinkProviderTest::testLinkProviders in tests/src/Functional/LinkProviderTest.php
Tests that link provider plugins properly affect the JSON:API output.

File

tests/src/Functional/LinkProviderTest.php, line 164

Class

LinkProviderTest
Class LinkProviderTest.

Namespace

Drupal\Tests\jsonapi_hypermedia\Functional

Code

protected function getLink($link_location, $link_key, $expected_on_document_types) {
  assert($expected_on_document_types === TRUE || is_array($expected_on_document_types));
  $path = array_map(function ($key) {
    return is_numeric($key) ? (int) $key : $key;
  }, array_merge(explode('.', $link_location), [
    $link_key,
  ]));
  $link = NULL;
  foreach ($this->uris as $document_type => $uri) {
    $is_error_type = $document_type === 'error';
    $document = $this
      ->getJsonapiDocument($uri, $is_error_type ? 404 : 200);
    $exists = NULL;
    $current_link = NestedArray::getValue($document, $path, $exists);
    $expected_everywhere = $expected_on_document_types === TRUE;
    $expected_on_current_type = !$expected_everywhere && in_array($document_type, $expected_on_document_types);
    $expected_on_success = !$expected_everywhere && in_array('success', $expected_on_document_types);
    if ($expected_everywhere || $expected_on_current_type || $expected_on_success && !$is_error_type) {
      $this
        ->assertTrue($exists, "Expected link under `{$link_location}` on the {$document_type} document at {$uri}");
    }
    else {
      $this
        ->assertFalse($exists, "Unexpected link under `{$link_location}` on the {$document_type} document at {$uri}");
    }
    $link = $current_link ?? $link;
  }
  return $link;
}