class SessionCacheContextTest in Drupal 9
Same name and namespace in other branches
- 8 core/tests/Drupal/Tests/Core/Cache/Context/SessionCacheContextTest.php \Drupal\Tests\Core\Cache\Context\SessionCacheContextTest
@coversDefaultClass \Drupal\Core\Cache\Context\SessionCacheContext @group Cache
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, PhpUnitCompatibilityTrait, PhpUnitWarnings
- class \Drupal\Tests\Core\Cache\Context\SessionCacheContextTest
Expanded class hierarchy of SessionCacheContextTest
File
- core/
tests/ Drupal/ Tests/ Core/ Cache/ Context/ SessionCacheContextTest.php, line 14
Namespace
Drupal\Tests\Core\Cache\ContextView source
class SessionCacheContextTest extends UnitTestCase {
/**
* The request.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* The session object.
*
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $session;
public function setUp() : void {
$this->request = new Request();
$this->requestStack = new RequestStack();
$this->requestStack
->push($this->request);
$this->session = $this
->getMockBuilder('\\Symfony\\Component\\HttpFoundation\\Session\\SessionInterface')
->getMock();
}
/**
* @covers ::getContext
*/
public function testSameContextForSameSession() {
$this->request
->setSession($this->session);
$cache_context = new SessionCacheContext($this->requestStack);
$session_id = 'aSebeZ52bbM6SvADurQP89SFnEpxY6j8';
$this->session
->expects($this
->exactly(2))
->method('getId')
->will($this
->returnValue($session_id));
$context1 = $cache_context
->getContext();
$context2 = $cache_context
->getContext();
$this
->assertSame($context1, $context2);
$this
->assertStringNotContainsString($session_id, $context1, 'Session ID not contained in cache context');
}
/**
* @covers ::getContext
*/
public function testDifferentContextForDifferentSession() {
$this->request
->setSession($this->session);
$cache_context = new SessionCacheContext($this->requestStack);
$session1_id = 'pjH_8aSoofyCDQiuVYXJcbfyr-CPtkUY';
$session2_id = 'aSebeZ52bbM6SvADurQP89SFnEpxY6j8';
$this->session
->expects($this
->exactly(2))
->method('getId')
->willReturnOnConsecutiveCalls($session1_id, $session2_id);
$context1 = $cache_context
->getContext();
$context2 = $cache_context
->getContext();
$this
->assertNotEquals($context1, $context2);
$this
->assertStringNotContainsString($session1_id, $context1, 'Session ID not contained in cache context');
$this
->assertStringNotContainsString($session2_id, $context2, 'Session ID not contained in cache context');
}
/**
* @covers ::getContext
*/
public function testContextWithoutSessionInRequest() {
$cache_context = new SessionCacheContext($this->requestStack);
$this
->assertSame('none', $cache_context
->getContext());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpUnitWarnings:: |
private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |
PhpUnitWarnings:: |
public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |
SessionCacheContextTest:: |
protected | property | The request. | |
SessionCacheContextTest:: |
protected | property | The request stack. | |
SessionCacheContextTest:: |
protected | property | The session object. | |
SessionCacheContextTest:: |
public | function |
Overrides UnitTestCase:: |
|
SessionCacheContextTest:: |
public | function | @covers ::getContext | |
SessionCacheContextTest:: |
public | function | @covers ::getContext | |
SessionCacheContextTest:: |
public | function | @covers ::getContext | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
public static | function |