You are here

public function JsonApiDocumentTopLevelNormalizerTest::testNormalizeException in JSON:API 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testNormalizeException()

@covers ::normalize

File

tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php, line 380

Class

JsonApiDocumentTopLevelNormalizerTest
@coversDefaultClass \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer @group jsonapi @group legacy

Namespace

Drupal\Tests\jsonapi\Kernel\Normalizer

Code

public function testNormalizeException() {
  list($request, $resource_type) = $this
    ->generateProphecies('node', 'article', 'id');
  $document_wrapper = $this
    ->prophesize(JsonApiDocumentTopLevel::class);
  $document_wrapper
    ->getData()
    ->willReturn($this->node);
  $request->query = new ParameterBag([
    'fields' => [
      'node--article' => 'title,type,uid',
      'user--user' => 'name',
    ],
    'include' => 'uid',
  ]);
  $normalized = $this->container
    ->get('jsonapi.serializer_do_not_use_removal_imminent')
    ->serialize(new BadRequestHttpException('Lorem'), 'api_json', [
    'request' => $request,
    'resource_type' => $resource_type,
    'data_wrapper' => 'errors',
  ]);
  $normalized = Json::decode($normalized);
  $this
    ->assertNotEmpty($normalized['errors']);
  $this
    ->assertArrayNotHasKey('data', $normalized);
  $this
    ->assertEquals(400, $normalized['errors'][0]['status']);
  $this
    ->assertEquals('Lorem', $normalized['errors'][0]['detail']);
  $this
    ->assertEquals([
    'info' => 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1',
  ], $normalized['errors'][0]['links']);
}