You are here

protected function ResourceTestBase::assertSameDocument in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::assertSameDocument()
  2. 10 core/modules/jsonapi/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.

3 calls to ResourceTestBase::assertSameDocument()
ResourceTestBase::assertResourceResponse in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Asserts that a resource response has the given status code and body.
ResourceTestBase::doTestRelationshipMutation in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Performs one round of relationship POST, PATCH and DELETE route testing.
TermTest::testGetIndividualTermWithParent in core/modules/jsonapi/tests/src/Functional/TermTest.php
Tests GETting a term with a parent term other than the default <root> (0).

File

core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php, line 760

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']);
  }
  if (isset($actual_document['meta']['omitted']) && isset($expected_document['meta']['omitted'])) {
    $actual_omitted =& $actual_document['meta']['omitted'];
    $expected_omitted =& $expected_document['meta']['omitted'];
    static::sortOmittedLinks($actual_omitted);
    static::sortOmittedLinks($expected_omitted);
    static::resetOmittedLinkKeys($actual_omitted);
    static::resetOmittedLinkKeys($expected_omitted);
  }
  $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.");
  }
}