function GetDocumentFromResponseTrait::getDocumentFromResponse

Same name and namespace in other branches
  1. 11.x core/modules/jsonapi/tests/src/Traits/GetDocumentFromResponseTrait.php \Drupal\Tests\jsonapi\Traits\GetDocumentFromResponseTrait::getDocumentFromResponse()

Retrieve document from response, with basic validation.

Parameters

\Psr\Http\Message\ResponseInterface $response: Response to extract JSON:API document from.

bool $validate: Determines whether the data is validated or not. Defaults to TRUE.

Return value

?array JSON:API document extracted from the response, or NULL.

Throws

\PHPUnit\Framework\AssertionFailedError Thrown when the document does not pass basic validation against the spec.

8 calls to GetDocumentFromResponseTrait::getDocumentFromResponse()
CommentExtrasTest::testPostIndividualSkipCommentApproval in core/modules/jsonapi/tests/src/Functional/CommentExtrasTest.php
Tests POSTing a comment with and without 'skip comment approval'.
JsonApiFilterRegressionTest::testEmptyRelationshipFilteringFromIssue3025372 in core/modules/jsonapi/tests/src/Functional/JsonApiFilterRegressionTest.php
Ensure filtering for entities with empty entity reference fields works.
JsonApiFilterRegressionTest::testFilterByIdFromIssue3015759 in core/modules/jsonapi/tests/src/Functional/JsonApiFilterRegressionTest.php
Ensures that filtering by a sequential internal ID named 'id' is possible.
JsonApiFilterRegressionTest::testFilteringEntitiesByEntityReferenceTargetId in core/modules/jsonapi/tests/src/Functional/JsonApiFilterRegressionTest.php
Tests that collections can be filtered by an entity reference target_id.
JsonApiPatchRegressionTest::testNonTranslatableEntityUpdatesFromIssue3043168 in core/modules/jsonapi/tests/src/Functional/JsonApiPatchRegressionTest.php
Ensure non-translatable entities can be PATCHed with an alternate language.

... See full list

File

core/modules/jsonapi/tests/src/Traits/GetDocumentFromResponseTrait.php, line 30

Class

GetDocumentFromResponseTrait
Test trait for retrieving the JSON:API document from a response.

Namespace

Drupal\Tests\jsonapi\Traits

Code

protected function getDocumentFromResponse(ResponseInterface $response, bool $validate = TRUE) : ?array {
  assert($this instanceof TestCase);
  $document = Json::decode((string) $response->getBody());
  if (isset($document['data']) && isset($document['errors'])) {
    $this->fail('Document contains both data and errors members; only one is allowed.');
  }
  if ($validate === TRUE && !isset($document['data'])) {
    if (isset($document['errors'])) {
      $errors = [];
      foreach ($document['errors'] as $error) {
        $errors[] = $error['title'] . ' (' . $error['status'] . '): ' . $error['detail'];
      }
      $this->fail('Missing expected data member in document. Error(s): ' . PHP_EOL . '  ' . implode('  ' . PHP_EOL, $errors));
    }
    $this->fail('Missing both data and errors members in document; either is required. Response body: ' . PHP_EOL . '  ' . $response->getBody());
  }
  return $document;
}

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