/vendor/symfony/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php

https://github.com/israelnoguera/parejas · PHP · 232 lines · 151 code · 67 blank · 14 comment · 0 complexity · 5977c227e5c15e8d4c452bcf6a7ad196 MD5 · raw file

  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\Tests\Component\HttpFoundation;
  11. use Symfony\Component\HttpFoundation\Session;
  12. use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
  13. /**
  14. * SessionTest
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. * @author Robert Schönthal <seroscho@googlemail.com>
  18. */
  19. class SessionTest extends \PHPUnit_Framework_TestCase
  20. {
  21. protected $storage;
  22. protected $session;
  23. public function setUp()
  24. {
  25. $this->storage = new ArraySessionStorage();
  26. $this->session = $this->getSession();
  27. }
  28. protected function tearDown()
  29. {
  30. $this->storage = null;
  31. $this->session = null;
  32. }
  33. public function testFlash()
  34. {
  35. $this->session->clearFlashes();
  36. $this->assertSame(array(), $this->session->getFlashes());
  37. $this->assertFalse($this->session->hasFlash('foo'));
  38. $this->session->setFlash('foo', 'bar');
  39. $this->assertTrue($this->session->hasFlash('foo'));
  40. $this->assertSame('bar', $this->session->getFlash('foo'));
  41. $this->session->removeFlash('foo');
  42. $this->assertFalse($this->session->hasFlash('foo'));
  43. $flashes = array('foo' => 'bar', 'bar' => 'foo');
  44. $this->session->setFlashes($flashes);
  45. $this->assertSame($flashes, $this->session->getFlashes());
  46. }
  47. public function testFlashesAreFlushedWhenNeeded()
  48. {
  49. $this->session->setFlash('foo', 'bar');
  50. $this->session->save();
  51. $this->session = $this->getSession();
  52. $this->assertTrue($this->session->hasFlash('foo'));
  53. $this->session->save();
  54. $this->session = $this->getSession();
  55. $this->assertFalse($this->session->hasFlash('foo'));
  56. }
  57. public function testAll()
  58. {
  59. $this->assertFalse($this->session->has('foo'));
  60. $this->assertNull($this->session->get('foo'));
  61. $this->session->set('foo', 'bar');
  62. $this->assertTrue($this->session->has('foo'));
  63. $this->assertSame('bar', $this->session->get('foo'));
  64. $this->session = $this->getSession();
  65. $this->session->remove('foo');
  66. $this->session->set('foo', 'bar');
  67. $this->session->remove('foo');
  68. $this->assertFalse($this->session->has('foo'));
  69. $attrs = array('foo' => 'bar', 'bar' => 'foo');
  70. $this->session = $this->getSession();
  71. $this->session->replace($attrs);
  72. $this->assertSame($attrs, $this->session->all());
  73. $this->session->clear();
  74. $this->assertSame(array(), $this->session->all());
  75. }
  76. public function testMigrateAndInvalidate()
  77. {
  78. $this->session->set('foo', 'bar');
  79. $this->session->setFlash('foo', 'bar');
  80. $this->assertSame('bar', $this->session->get('foo'));
  81. $this->assertSame('bar', $this->session->getFlash('foo'));
  82. $this->session->migrate();
  83. $this->assertSame('bar', $this->session->get('foo'));
  84. $this->assertSame('bar', $this->session->getFlash('foo'));
  85. $this->session = $this->getSession();
  86. $this->session->invalidate();
  87. $this->assertSame(array(), $this->session->all());
  88. $this->assertSame(array(), $this->session->getFlashes());
  89. }
  90. public function testSerialize()
  91. {
  92. $this->session = new Session($this->storage);
  93. $compare = serialize($this->storage);
  94. $this->assertSame($compare, $this->session->serialize());
  95. $this->session->unserialize($compare);
  96. $_storage = new \ReflectionProperty(get_class($this->session), 'storage');
  97. $_storage->setAccessible(true);
  98. $this->assertEquals($_storage->getValue($this->session), $this->storage, 'storage match');
  99. }
  100. public function testSave()
  101. {
  102. $this->storage = new ArraySessionStorage();
  103. $this->session = new Session($this->storage);
  104. $this->session->set('foo', 'bar');
  105. $this->session->save();
  106. $compare = array('_symfony2' => array('attributes' => array('foo' => 'bar'), 'flashes' => array()));
  107. $r = new \ReflectionObject($this->storage);
  108. $p = $r->getProperty('data');
  109. $p->setAccessible(true);
  110. $this->assertSame($p->getValue($this->storage), $compare);
  111. }
  112. public function testGetId()
  113. {
  114. $this->assertNull($this->session->getId());
  115. }
  116. public function testStart()
  117. {
  118. $this->session->start();
  119. $this->assertSame(array(), $this->session->getFlashes());
  120. $this->assertSame(array(), $this->session->all());
  121. }
  122. public function testSavedOnDestruct()
  123. {
  124. $this->session->set('foo', 'bar');
  125. $this->session->__destruct();
  126. $expected = array(
  127. 'attributes'=>array('foo'=>'bar'),
  128. 'flashes'=>array(),
  129. );
  130. $saved = $this->storage->read('_symfony2');
  131. $this->assertSame($expected, $saved);
  132. }
  133. public function testSavedOnDestructAfterManualSave()
  134. {
  135. $this->session->set('foo', 'nothing');
  136. $this->session->save();
  137. $this->session->set('foo', 'bar');
  138. $this->session->__destruct();
  139. $expected = array(
  140. 'attributes'=>array('foo'=>'bar'),
  141. 'flashes'=>array(),
  142. );
  143. $saved = $this->storage->read('_symfony2');
  144. $this->assertSame($expected, $saved);
  145. }
  146. public function testStorageRegenerate()
  147. {
  148. $this->storage->write('foo', 'bar');
  149. $this->assertTrue($this->storage->regenerate());
  150. $this->assertEquals('bar', $this->storage->read('foo'));
  151. $this->assertTrue($this->storage->regenerate(true));
  152. $this->assertNull($this->storage->read('foo'));
  153. }
  154. public function testStorageRemove()
  155. {
  156. $this->storage->write('foo', 'bar');
  157. $this->assertEquals('bar', $this->storage->read('foo'));
  158. $this->storage->remove('foo');
  159. $this->assertNull($this->storage->read('foo'));
  160. }
  161. protected function getSession()
  162. {
  163. return new Session($this->storage);
  164. }
  165. }