You are here

protected function ResourceTestBase::assertSameDocument in JSON:API 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::assertSameDocument()

Asserts that an expected document matches the response body.

Parameters

array $expected_document: The expected JSON API document.

array $actual_document: The actual response document to assert.

6 calls to ResourceTestBase::assertSameDocument()
ResourceTestBase::assertResourceResponse in tests/src/Functional/ResourceTestBase.php
Asserts that a resource response has the given status code and body.
ResourceTestBase::doTestIncluded in tests/src/Functional/ResourceTestBase.php
Tests included resources.
ResourceTestBase::doTestRelated in tests/src/Functional/ResourceTestBase.php
Performs one round of related route testing.
ResourceTestBase::doTestRelationshipGet in tests/src/Functional/ResourceTestBase.php
Performs one round of relationship route testing.
ResourceTestBase::doTestRelationshipPost in tests/src/Functional/ResourceTestBase.php
Performs one round of relationship POST, PATCH and DELETE route testing.

... See full list

File

tests/src/Functional/ResourceTestBase.php, line 709

Class

ResourceTestBase
Subclass this for every JSON API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function assertSameDocument(array $expected_document, array $actual_document) {
  static::recursiveKsort($expected_document);
  static::recursiveKsort($actual_document);
  if (!empty($expected_document['included'])) {
    static::sortResourceCollection($expected_document['included']);
    static::sortResourceCollection($actual_document['included']);
  }

  // @todo: remove in https://www.drupal.org/project/jsonapi/issues/2853066.
  if (isset($actual_document['errors']) && isset($expected_document['errors'])) {
    $actual_errors =& $actual_document['errors'];
    static::sortErrors($actual_errors);
    $expected_errors =& $expected_document['errors'];
    static::sortErrors($expected_errors);
  }
  if (isset($actual_document['meta']['errors']) && isset($expected_document['meta']['errors'])) {
    $actual_errors =& $actual_document['meta']['errors'];
    static::sortErrors($actual_errors);
    $expected_errors =& $expected_document['meta']['errors'];
    static::sortErrors($expected_errors);
  }

  // @todo remove this in https://www.drupal.org/project/jsonapi/issues/2943176
  $strip_error_identifiers = function (&$document) {
    if (isset($document['errors'])) {
      foreach ($document['errors'] as &$error) {
        unset($error['id']);
      }
    }
    if (isset($document['meta']['errors'])) {
      foreach ($document['meta']['errors'] as &$error) {
        unset($error['id']);
      }
    }
  };
  $strip_error_identifiers($expected_document);
  $strip_error_identifiers($actual_document);
  $expected_keys = array_keys($expected_document);
  $actual_keys = array_keys($actual_document);
  $missing_member_names = array_diff($expected_keys, $actual_keys);
  $extra_member_names = array_diff($actual_keys, $expected_keys);
  if (!empty($missing_member_names) || !empty($extra_member_names)) {
    $message_format = "The document members did not match the expected values. Missing: [ %s ]. Unexpected: [ %s ]";
    $message = sprintf($message_format, implode(', ', $missing_member_names), implode(', ', $extra_member_names));
    $this
      ->assertSame($expected_document, $actual_document, $message);
  }
  foreach ($expected_document as $member_name => $expected_member) {
    $actual_member = $actual_document[$member_name];
    $this
      ->assertSame($expected_member, $actual_member, "The '{$member_name}' member was not as expected.");
  }
}