class ExceptionStatusCodeCacheContextTest

Same name and namespace in other branches
  1. main core/tests/Drupal/Tests/Core/Cache/Context/ExceptionStatusCodeCacheContextTest.php \Drupal\Tests\Core\Cache\Context\ExceptionStatusCodeCacheContextTest

Tests Drupal\Core\Cache\Context\CookiesCacheContext.

Attributes

#[CoversClass(ExceptionStatusCodeCacheContext::class)] #[Group('Cache')]

Hierarchy

Expanded class hierarchy of ExceptionStatusCodeCacheContextTest

File

core/tests/Drupal/Tests/Core/Cache/Context/ExceptionStatusCodeCacheContextTest.php, line 22

Namespace

Drupal\Tests\Core\Cache\Context
View source
class ExceptionStatusCodeCacheContextTest extends UnitTestCase {
  
  /**
   * Tests get context.
   */
  public function testGetContext(?\Exception $exception, string $result) : void {
    $request_stack = new RequestStack();
    $request = Request::create('/', 'GET');
    if (isset($exception)) {
      $request->attributes
        ->set('exception', $exception);
    }
    $request_stack->push($request);
    $cache_context = new ExceptionStatusCodeCacheContext($request_stack);
    $this->assertSame($cache_context->getContext(), $result);
  }
  
  /**
   * Provides a list of cookies and expected cache contexts.
   */
  public static function providerTestGetContext() : array {
    return [
      [
        new NotFoundHttpException(),
        '404',
      ],
      [
        new AccessDeniedHttpException(),
        '403',
      ],
      [
        new BadRequestHttpException(),
        '400',
      ],
      [
        new MethodNotAllowedHttpException([
          'POST',
        ]),
        '405',
      ],
      [
        NULL,
        '0',
      ],
    ];
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.