CacheableHttpException example

use Drupal\Tests\UnitTestCase;

/** * @group Http */
class CacheableExceptionTest extends UnitTestCase {

  /** * @covers \Drupal\Core\Http\Exception\CacheableHttpException */
  public function testCacheableHttpException() {
    $exception = new CacheableHttpException((new CacheableMetadata())->setCacheContexts(['route']), 500, 'test message', NULL, ['X-Drupal-Exception' => 'Test'], 123);
    $this->assertSame(['route']$exception->getCacheContexts());
    $this->assertSame(500, $exception->getStatusCode());
    $this->assertSame('test message', $exception->getMessage());
    $this->assertSame(['X-Drupal-Exception' => 'Test']$exception->getHeaders());
    $this->assertSame(123, $exception->getCode());
  }

  /** * @dataProvider providerTestExceptions */
  public function testExceptions($status_code$class$argument = NULL, $expected_headers = []) {
    


    $has_version_param = $request->query->has(static::RESOURCE_VERSION_QUERY_PARAMETER);

    // If the resource type is not versionable, then nothing needs to be     // enhanced.     if (!$resource_type->isVersionable()) {
      // If the query parameter was provided but the resource type is not       // versionable, provide a helpful error.       if ($has_version_param) {
        $cacheability = (new CacheableMetadata())->addCacheContexts(['url.path', static::CACHE_CONTEXT]);
        throw new CacheableHttpException($cacheability, 501, 'Resource versioning is not yet supported for this resource type.');
      }
      return $defaults;
    }

    // Since the resource type is versionable, responses must always vary by the     // requested version, without regard for whether a version query parameter     // was provided or not.     if (isset($defaults['entity'])) {
      assert($defaults['entity'] instanceof EntityInterface);
      $defaults['entity']->addCacheContexts([static::CACHE_CONTEXT]);
    }

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