class WriteSafeSessionHandlerTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/Tests/Core/Session/WriteSafeSessionHandlerTest.php \Drupal\Tests\Core\Session\WriteSafeSessionHandlerTest
- 10 core/tests/Drupal/Tests/Core/Session/WriteSafeSessionHandlerTest.php \Drupal\Tests\Core\Session\WriteSafeSessionHandlerTest
- 9 core/tests/Drupal/Tests/Core/Session/WriteSafeSessionHandlerTest.php \Drupal\Tests\Core\Session\WriteSafeSessionHandlerTest
- 8.9.x core/tests/Drupal/Tests/Core/Session/WriteSafeSessionHandlerTest.php \Drupal\Tests\Core\Session\WriteSafeSessionHandlerTest
Tests \Drupal\Core\Session\WriteSafeSessionHandler.
Attributes
#[CoversClass(WriteSafeSessionHandler::class)]
#[Group('Session')]
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\DrupalTestCaseTrait, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\Core\Session\WriteSafeSessionHandlerTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of WriteSafeSessionHandlerTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Session/ WriteSafeSessionHandlerTest.php, line 16
Namespace
Drupal\Tests\Core\SessionView source
class WriteSafeSessionHandlerTest extends UnitTestCase {
/**
* The wrapped session handler.
*
* @var \SessionHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $wrappedSessionHandler;
/**
* The write safe session handler.
*
* @var \Drupal\Core\Session\WriteSafeSessionHandler
*/
protected $sessionHandler;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->wrappedSessionHandler = $this->createMock('SessionHandlerInterface');
$this->sessionHandler = new WriteSafeSessionHandler($this->wrappedSessionHandler);
}
/**
* Tests creating a WriteSafeSessionHandler with default arguments.
*
* @legacy-covers ::__construct
* @legacy-covers ::isSessionWritable
* @legacy-covers ::write
*/
public function testConstructWriteSafeSessionHandlerDefaultArgs() : void {
$session_id = 'some-id';
$session_data = 'serialized-session-data';
$this->assertTrue($this->sessionHandler
->isSessionWritable());
// Writing should be enabled, return value passed to the caller by default.
$this->wrappedSessionHandler
->expects($this->exactly(2))
->method('write')
->with($session_id, $session_data)
->willReturnOnConsecutiveCalls(TRUE, FALSE);
$result = $this->sessionHandler
->write($session_id, $session_data);
$this->assertTrue($result);
$result = $this->sessionHandler
->write($session_id, $session_data);
$this->assertFalse($result);
}
/**
* Tests creating a WriteSafeSessionHandler with session writing disabled.
*
* @legacy-covers ::__construct
* @legacy-covers ::isSessionWritable
* @legacy-covers ::write
*/
public function testConstructWriteSafeSessionHandlerDisableWriting() : void {
$session_id = 'some-id';
$session_data = 'serialized-session-data';
// Disable writing upon construction.
$this->sessionHandler = new WriteSafeSessionHandler($this->wrappedSessionHandler, FALSE);
$this->assertFalse($this->sessionHandler
->isSessionWritable());
$result = $this->sessionHandler
->write($session_id, $session_data);
$this->assertTrue($result);
}
/**
* Tests using setSessionWritable to enable/disable session writing.
*
* @legacy-covers ::setSessionWritable
* @legacy-covers ::write
*/
public function testSetSessionWritable() : void {
$session_id = 'some-id';
$session_data = 'serialized-session-data';
$this->assertTrue($this->sessionHandler
->isSessionWritable());
// Disable writing after construction.
$this->sessionHandler
->setSessionWritable(FALSE);
$this->assertFalse($this->sessionHandler
->isSessionWritable());
$this->sessionHandler = new WriteSafeSessionHandler($this->wrappedSessionHandler, FALSE);
$this->assertFalse($this->sessionHandler
->isSessionWritable());
$result = $this->sessionHandler
->write($session_id, $session_data);
$this->assertTrue($result);
// Enable writing again.
$this->sessionHandler
->setSessionWritable(TRUE);
$this->assertTrue($this->sessionHandler
->isSessionWritable());
// Writing should be enabled, return value passed to the caller by default.
$this->wrappedSessionHandler
->expects($this->exactly(2))
->method('write')
->with($session_id, $session_data)
->willReturnOnConsecutiveCalls(TRUE, FALSE);
$result = $this->sessionHandler
->write($session_id, $session_data);
$this->assertTrue($result);
$result = $this->sessionHandler
->write($session_id, $session_data);
$this->assertFalse($result);
}
/**
* Tests that other invocations are passed unmodified to the wrapped handler.
*
* @legacy-covers ::setSessionWritable
* @legacy-covers ::open
* @legacy-covers ::read
* @legacy-covers ::close
* @legacy-covers ::destroy
* @legacy-covers ::gc
*/
public function testOtherMethods($method, $expected_result, $args) : void {
$invocation = $this->wrappedSessionHandler
->expects($this->exactly(2))
->method($method)
->willReturn($expected_result);
// Set the parameter matcher.
call_user_func_array([
$invocation,
'with',
], $args);
// Test with writable session.
$this->assertTrue($this->sessionHandler
->isSessionWritable());
$actual_result = call_user_func_array([
$this->sessionHandler,
$method,
], $args);
$this->assertSame($expected_result, $actual_result);
// Test with non-writable session.
$this->sessionHandler
->setSessionWritable(FALSE);
$this->assertFalse($this->sessionHandler
->isSessionWritable());
$actual_result = call_user_func_array([
$this->sessionHandler,
$method,
], $args);
$this->assertSame($expected_result, $actual_result);
}
/**
* Provides test data for the other methods test.
*
* @return array
* Test data.
*/
public static function providerTestOtherMethods() : array {
return [
[
'open',
TRUE,
[
'/some/path',
'some-session-id',
],
],
[
'read',
'some-session-data',
[
'a-session-id',
],
],
[
'close',
TRUE,
[],
],
[
'destroy',
TRUE,
[
'old-session-id',
],
],
[
'gc',
0,
[
42,
],
],
];
}
}
Members
| Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title |
|---|---|---|---|---|---|
| DrupalTestCaseTrait::checkErrorHandlerOnTearDown | public | function | Checks the test error handler after test execution. | ||
| ExpectDeprecationTrait::expectDeprecation | Deprecated | public | function | Adds an expected deprecation. | |
| ExpectDeprecationTrait::regularExpressionForFormatDescription | private | function | |||
| RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | ||
| RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | ||
| RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | ||
| RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | ||
| UnitTestCase::$root | protected | property | The app root. | ||
| UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | ||
| UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | ||
| UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | ||
| UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | ||
| UnitTestCase::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | ||
| UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. | ||
| WriteSafeSessionHandlerTest::$sessionHandler | protected | property | The write safe session handler. | ||
| WriteSafeSessionHandlerTest::$wrappedSessionHandler | protected | property | The wrapped session handler. | ||
| WriteSafeSessionHandlerTest::providerTestOtherMethods | public static | function | Provides test data for the other methods test. | ||
| WriteSafeSessionHandlerTest::setUp | protected | function | Overrides UnitTestCase::setUp | ||
| WriteSafeSessionHandlerTest::testConstructWriteSafeSessionHandlerDefaultArgs | public | function | Tests creating a WriteSafeSessionHandler with default arguments. | ||
| WriteSafeSessionHandlerTest::testConstructWriteSafeSessionHandlerDisableWriting | public | function | Tests creating a WriteSafeSessionHandler with session writing disabled. | ||
| WriteSafeSessionHandlerTest::testOtherMethods | public | function | Tests that other invocations are passed unmodified to the wrapped handler. | ||
| WriteSafeSessionHandlerTest::testSetSessionWritable | public | function | Tests using setSessionWritable to enable/disable session writing. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.