CookieCacheContextTest.php

Same filename and directory in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Cache/Context/CookieCacheContextTest.php
  2. 10 core/tests/Drupal/Tests/Core/Cache/Context/CookieCacheContextTest.php
  3. 9 core/tests/Drupal/Tests/Core/Cache/Context/CookieCacheContextTest.php

Namespace

Drupal\Tests\Core\Cache\Context

File

core/tests/Drupal/Tests/Core/Cache/Context/CookieCacheContextTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\Core\Cache\Context;

use Drupal\Core\Cache\Context\CookiesCacheContext;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Tests Drupal\Core\Cache\Context\CookiesCacheContext.
 */
class CookieCacheContextTest extends UnitTestCase {
  
  /**
   * Tests get context.
   */
  public function testGetContext($cookies, $cookie_name, $context) : void {
    $request_stack = new RequestStack();
    $request = Request::create('/', 'GET');
    foreach ($cookies as $cookie => $value) {
      $request->cookies
        ->set($cookie, $value);
    }
    $request_stack->push($request);
    $cache_context = new CookiesCacheContext($request_stack);
    $this->assertSame($cache_context->getContext($cookie_name), $context);
  }
  
  /**
   * Provides a list of cookies and expected cache contexts.
   */
  public static function providerTestGetContext() : array {
    return [
      [
        [
          'foo' => 1,
          'bar' => 2,
          'baz' => 3,
        ],
        'foo',
        1,
      ],
      // Context is ordered by cookie name.
[
        [
          'foo' => 1,
          'bar' => 2,
          'baz' => 3,
        ],
        NULL,
        'bar=2&baz=3&foo=1',
      ],
    ];
  }

}

Classes

Title Deprecated Summary
CookieCacheContextTest Tests Drupal\Core\Cache\Context\CookiesCacheContext.

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