You are here

public function ResourceResponseValidatorTest::testDoValidateResponse 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::testDoValidateResponse()

@covers ::doValidateResponse

File

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

Class

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

Namespace

Drupal\Tests\jsonapi\Unit\EventSubscriber

Code

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

  // Capture the default assert settings.
  $zend_assertions_default = ini_get('zend.assertions');
  $assert_active_default = assert_options(ASSERT_ACTIVE);

  // The validator *should* be called when asserts are active.
  $validator = $this
    ->prophesize(Validator::class);
  $validator
    ->check(Argument::any(), Argument::any())
    ->shouldBeCalled('Validation should be run when asserts are active.');
  $validator
    ->isValid()
    ->willReturn(TRUE);
  $this->subscriber
    ->setValidator($validator
    ->reveal());

  // Ensure asset is active.
  ini_set('zend.assertions', 1);
  assert_options(ASSERT_ACTIVE, 1);
  $this->subscriber
    ->doValidateResponse($response, $request);

  // The validator should *not* be called when asserts are inactive.
  $validator = $this
    ->prophesize(Validator::class);
  $validator
    ->check(Argument::any(), Argument::any())
    ->shouldNotBeCalled('Validation should not be run when asserts are not active.');
  $this->subscriber
    ->setValidator($validator
    ->reveal());

  // Ensure asset is inactive.
  ini_set('zend.assertions', 0);
  assert_options(ASSERT_ACTIVE, 0);
  $this->subscriber
    ->doValidateResponse($response, $request);

  // Reset the original assert values.
  ini_set('zend.assertions', $zend_assertions_default);
  assert_options(ASSERT_ACTIVE, $assert_active_default);
}