SessionHelperTest.php in Examples for Developers 8        
                          
                  
                        
  
  
  
  
  
File
  stream_wrapper_example/tests/src/Unit/SessionHelperTest.php
  
    View source  
  <?php
namespace Drupal\Tests\stream_wrapper_example\Unit;
use Drupal\stream_wrapper_example\SessionHelper;
use Drupal\Tests\stream_wrapper_example\Traits\MockSessionTrait;
use Drupal\Tests\UnitTestCase;
class SessionHelperTest extends UnitTestCase {
  use MockSessionTrait;
  
  protected function setUp() {
    parent::setUp();
    
    $this
      ->createSessionMock();
    
    $helper = new SessionHelper($this->requestStack);
  }
  
  public function testWrapper() {
    
    $helper = new SessionHelper($this->requestStack);
    $root = $helper
      ->getPath('');
    $this
      ->assertInternalType('array', $root);
    $this
      ->assertEmpty($root);
    
    $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.");
    
    $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.");
    
    $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.");
    
    $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.");
    
    $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.");
    
    $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.");
  }
}