You are here

public function ResourceResponseValidatorTest::testDoValidateResponse in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Unit/EventSubscriber/ResourceResponseValidatorTest.php \Drupal\Tests\jsonapi\Unit\EventSubscriber\ResourceResponseValidatorTest::testDoValidateResponse()

@covers ::doValidateResponse

File

core/modules/jsonapi/tests/src/Unit/EventSubscriber/ResourceResponseValidatorTest.php, line 60

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);
}