PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockFileSessionStorageTest.php

https://bitbucket.org/pyneff/symfony2
PHP | 117 lines | 82 code | 16 blank | 19 comment | 1 complexity | 7e4854d0c0491f6f4643b4c3526ef3ae MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0, BSD-3-Clause, BSD-2-Clause
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Tests\Session\Storage;
  11. use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
  12. use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
  13. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
  14. /**
  15. * Test class for MockFileSessionStorage.
  16. *
  17. * @author Drak <drak@zikula.org>
  18. */
  19. class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase
  20. {
  21. /**
  22. * @var string
  23. */
  24. private $sessionDir;
  25. /**
  26. * @var FileMockSessionStorage
  27. */
  28. protected $storage;
  29. protected function setUp()
  30. {
  31. $this->sessionDir = sys_get_temp_dir().'/sf2test';
  32. $this->storage = $this->getStorage();
  33. }
  34. protected function tearDown()
  35. {
  36. $this->sessionDir = null;
  37. $this->storage = null;
  38. array_map('unlink', glob($this->sessionDir.'/*.session'));
  39. if (is_dir($this->sessionDir)) {
  40. rmdir($this->sessionDir);
  41. }
  42. }
  43. public function testStart()
  44. {
  45. $this->assertEquals('', $this->storage->getId());
  46. $this->assertTrue($this->storage->start());
  47. $id = $this->storage->getId();
  48. $this->assertNotEquals('', $this->storage->getId());
  49. $this->assertTrue($this->storage->start());
  50. $this->assertEquals($id, $this->storage->getId());
  51. }
  52. public function testRegenerate()
  53. {
  54. $this->storage->start();
  55. $this->storage->getBag('attributes')->set('regenerate', 1234);
  56. $this->storage->regenerate();
  57. $this->assertEquals(1234, $this->storage->getBag('attributes')->get('regenerate'));
  58. $this->storage->regenerate(true);
  59. $this->assertEquals(1234, $this->storage->getBag('attributes')->get('regenerate'));
  60. }
  61. public function testGetId()
  62. {
  63. $this->assertEquals('', $this->storage->getId());
  64. $this->storage->start();
  65. $this->assertNotEquals('', $this->storage->getId());
  66. }
  67. public function testSave()
  68. {
  69. $this->storage->start();
  70. $id = $this->storage->getId();
  71. $this->assertNotEquals('108', $this->storage->getBag('attributes')->get('new'));
  72. $this->assertFalse($this->storage->getBag('flashes')->has('newkey'));
  73. $this->storage->getBag('attributes')->set('new', '108');
  74. $this->storage->getBag('flashes')->set('newkey', 'test');
  75. $this->storage->save();
  76. $storage = $this->getStorage();
  77. $storage->setId($id);
  78. $storage->start();
  79. $this->assertEquals('108', $storage->getBag('attributes')->get('new'));
  80. $this->assertTrue($storage->getBag('flashes')->has('newkey'));
  81. $this->assertEquals(array('test'), $storage->getBag('flashes')->peek('newkey'));
  82. }
  83. public function testMultipleInstances()
  84. {
  85. $storage1 = $this->getStorage();
  86. $storage1->start();
  87. $storage1->getBag('attributes')->set('foo', 'bar');
  88. $storage1->save();
  89. $storage2 = $this->getStorage();
  90. $storage2->setId($storage1->getId());
  91. $storage2->start();
  92. $this->assertEquals('bar', $storage2->getBag('attributes')->get('foo'), 'values persist between instances');
  93. }
  94. private function getStorage()
  95. {
  96. $storage = new MockFileSessionStorage($this->sessionDir);
  97. $storage->registerBag(new FlashBag());
  98. $storage->registerBag(new AttributeBag());
  99. return $storage;
  100. }
  101. }