class SessionHelperTest
Same name in other branches
- 4.0.x modules/stream_wrapper_example/tests/src/Unit/SessionHelperTest.php \Drupal\Tests\stream_wrapper_example\Unit\SessionHelperTest
PHPUnit test for the SessionHelper session manipulation class.
The SessionHelper class is a utility used to manipulate an associative array stored in the session object as if it were a file system. This greatly simplifies the code in our stream wrapper class, since SessionHelper handles things like interacting with the session object, and also deals with translating path strings into nested arrays.
The test class covers the equivalent of adding directories and files, reading and writing data nodes (our "files"), and clearing of arrays and data nodes (file deletion for purposes of the stream wrapper class).
@group stream_wrapper_example @group examples
@coversDefaultClass \Drupal\stream_wrapper_example\SessionHelper
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait
- class \Drupal\Tests\stream_wrapper_example\Unit\SessionHelperTest extends \Drupal\Tests\UnitTestCase uses \Drupal\Tests\stream_wrapper_example\Traits\MockSessionTrait
Expanded class hierarchy of SessionHelperTest
Related topics
File
-
modules/
stream_wrapper_example/ tests/ src/ Unit/ SessionHelperTest.php, line 28
Namespace
Drupal\Tests\stream_wrapper_example\UnitView source
class SessionHelperTest extends UnitTestCase {
use MockSessionTrait;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
// Mock the session service.
$this->createSessionMock();
// Set up the example.
$helper = new SessionHelper($this->requestStack);
}
/**
* Run our wrapper through the paces.
*/
public function testWrapper() {
// Check out root.
$helper = new SessionHelper($this->requestStack);
$root = $helper->getPath('');
$this->assertIsArray($root);
$this->assertEmpty($root);
// Add a top level file.
$helper = new SessionHelper($this->requestStack);
$helper->setPath('drupal.txt', "Stuff");
$text = $helper->getPath('drupal.txt');
$this->assertEquals($text, "Stuff", "File at base of hierarchy can be read.");
// Add a "directory".
$helper = new SessionHelper($this->requestStack);
$dir = [
'file.txt' => 'More stuff',
];
$helper->setPath('directory1', $dir);
$fetched_dir = $helper->getPath('directory1');
$this->assertEquals($fetched_dir['file.txt'], "More stuff", "File inside of directory can be read.");
// Check file existance.
$helper = new SessionHelper($this->requestStack);
$this->assertTrue($helper->checkPath('drupal.txt'), "File at root still exists.");
$this->assertFalse($helper->checkPath('file.txt'), "Non-existant file at root does not exist.");
$this->assertTrue($helper->checkPath('directory1'), "Directory at root still exists.");
$this->assertTrue($helper->checkPath('directory1/file.txt'), "File in directory at root still exists.");
// Two deep.
$helper = new SessionHelper($this->requestStack);
$helper->setPath('directory1/directory2', []);
$helper->setPath('directory1/directory2/junk.txt', "Store some junk");
$text = $helper->getPath('directory1/directory2/junk.txt');
$this->assertEquals($text, "Store some junk", "File inside of nested directory can be read.");
// Clear references.
$helper = new SessionHelper($this->requestStack);
$before = $helper->checkPath('directory1/directory2/junk.txt');
$this->assertTrue($before, "File 2 deep exists.");
$helper->clearPath('directory1/directory2/junk.txt');
$after = $helper->checkPath('directory1/directory2/junk.txt');
$this->assertFalse($after, "File 2 deep should be gone.");
// Clean up test.
$helper = new SessionHelper($this->requestStack);
$store = $helper->getPath('');
$this->assertNotEmpty($store, "Before cleanup store is not empty.");
$helper->cleanUpStore();
$store = $helper->getPath('');
$this->assertEmpty($store, "After cleanup store is empty.");
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
MockSessionTrait::$requestStack | protected | property | A representation of the HTTP request. | |||
MockSessionTrait::$sessionStore | protected | property | We'll use this array to back our mock session. | |||
MockSessionTrait::createSessionMock | protected | function | Create a mock session object. | |||
MockSessionTrait::getSessionHelper | public | function | Get a session helper. | |||
MockSessionTrait::getSessionStore | public | function | Helper for mocks. | |||
MockSessionTrait::resetSessionStore | public | function | Helper for our mocks. | |||
MockSessionTrait::setSessionStore | public | function | Helper for our mocks. | |||
PhpUnitWarnings::$deprecationWarnings | private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |||
PhpUnitWarnings::addWarning | public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |||
SessionHelperTest::setUp | protected | function | Overrides UnitTestCase::setUp | |||
SessionHelperTest::testWrapper | public | function | Run our wrapper through the paces. | |||
UnitTestCase::$randomGenerator | protected | property | The random generator. | |||
UnitTestCase::$root | protected | property | The app root. | 1 | ||
UnitTestCase::assertArrayEquals | Deprecated | protected | function | Asserts if two arrays are equal by sorting them first. | ||
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::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | |||
UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |||
UnitTestCase::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |||
UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |||
UnitTestCase::randomMachineName | public | function | Generates a unique random string containing letters and numbers. | |||
UnitTestCase::setUpBeforeClass | public static | function |