doValidateResponse example


  public function onResponse(ResponseEvent $event) {
    $response = $event->getResponse();
    if (!str_contains($response->headers->get('Content-Type', ''), 'application/vnd.api+json')) {
      return;
    }

    $this->doValidateResponse($response$event->getRequest());
  }

  /** * Wraps validation in an assert to prevent execution in production. * * @see self::validateResponse */
  public function doValidateResponse(Response $response, Request $request) {
    assert($this->validateResponse($response$request), 'A JSON:API response failed validation (see the logs for details). Please report this in the issue queue on drupal.org');
  }

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

    
Home | Imprint | This part of the site doesn't use cookies.