You are here

public function ResourceResponseValidatorTest::testValidateResponseSchemata in JSON:API 8

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/EventSubscriber/ResourceResponseValidatorTest.php \Drupal\Tests\jsonapi\Unit\EventSubscriber\ResourceResponseValidatorTest::testValidateResponseSchemata()

@covers ::onResponse

File

tests/src/Unit/EventSubscriber/ResourceResponseValidatorTest.php, line 105

Class

ResourceResponseValidatorTest
@coversDefaultClass \Drupal\jsonapi\EventSubscriber\ResourceResponseValidator @group jsonapi

Namespace

Drupal\Tests\jsonapi\Unit\EventSubscriber

Code

public function testValidateResponseSchemata() {
  $request = $this
    ->createRequest('jsonapi.node--article.individual', new ResourceType('node', 'article', NULL));
  $response = $this
    ->createResponse('{"data":null}');

  // The validator should be called *once* if schemata is *not* installed.
  $validator = $this
    ->prophesize(Validator::class);
  $validator
    ->check(Argument::any(), Argument::any())
    ->shouldBeCalledTimes(1);
  $validator
    ->isValid()
    ->willReturn(TRUE);
  $this->subscriber
    ->setValidator($validator
    ->reveal());

  // Run validations.
  $this->subscriber
    ->doValidateResponse($response, $request);

  // The validator should be called *twice* if schemata is installed.
  $validator = $this
    ->prophesize(Validator::class);
  $validator
    ->check(Argument::any(), Argument::any())
    ->shouldBeCalledTimes(2);
  $validator
    ->isValid()
    ->willReturn(TRUE);
  $this->subscriber
    ->setValidator($validator
    ->reveal());

  // Make the schemata factory available.
  $schema_factory = $this
    ->prophesize(SchemaFactory::class);
  $schema_factory
    ->create('node', 'article')
    ->willReturn('{}');
  $this->subscriber
    ->setSchemaFactory($schema_factory
    ->reveal());

  // Run validations.
  $this->subscriber
    ->doValidateResponse($response, $request);

  // The validator resource specific schema should *not* be validated on
  // 'related' routes.
  $request = $this
    ->createRequest('jsonapi.node--article.related', new ResourceType('node', 'article', NULL));

  // Since only the generic schema should be validated, the validator should
  // only be called once.
  $validator = $this
    ->prophesize(Validator::class);
  $validator
    ->check(Argument::any(), Argument::any())
    ->shouldBeCalledTimes(1);
  $validator
    ->isValid()
    ->willReturn(TRUE);
  $this->subscriber
    ->setValidator($validator
    ->reveal());

  // Run validations.
  $this->subscriber
    ->doValidateResponse($response, $request);

  // The validator resource specific schema should *not* be validated on
  // 'relationship' routes.
  $request = $this
    ->createRequest('jsonapi.node--article.relationship', new ResourceType('node', 'article', NULL));

  // Since only the generic schema should be validated, the validator should
  // only be called once.
  $validator = $this
    ->prophesize(Validator::class);
  $validator
    ->check(Argument::any(), Argument::any())
    ->shouldBeCalledTimes(1);
  $validator
    ->isValid()
    ->willReturn(TRUE);
  $this->subscriber
    ->setValidator($validator
    ->reveal());

  // Run validations.
  $this->subscriber
    ->doValidateResponse($response, $request);
}