PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php

https://github.com/maoueh/symfony
PHP | 256 lines | 175 code | 35 blank | 46 comment | 0 complexity | 44c20ae89431d73ff3e21f6d85870a27 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\Component\HttpFoundation\Tests\Session;
  11. use Symfony\Component\HttpFoundation\Session\Session;
  12. use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
  13. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
  14. use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
  15. /**
  16. * SessionTest
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. * @author Robert Schönthal <seroscho@googlemail.com>
  20. * @author Drak <drak@zikula.org>
  21. */
  22. class SessionTest extends \PHPUnit_Framework_TestCase
  23. {
  24. /**
  25. * @var \Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface
  26. */
  27. protected $storage;
  28. /**
  29. * @var \Symfony\Component\HttpFoundation\Session\SessionInterface
  30. */
  31. protected $session;
  32. protected function setUp()
  33. {
  34. $this->storage = new MockArraySessionStorage();
  35. $this->session = new Session($this->storage, new AttributeBag(), new FlashBag());
  36. }
  37. protected function tearDown()
  38. {
  39. $this->storage = null;
  40. $this->session = null;
  41. }
  42. public function testStart()
  43. {
  44. $this->assertEquals('', $this->session->getId());
  45. $this->assertTrue($this->session->start());
  46. $this->assertNotEquals('', $this->session->getId());
  47. }
  48. public function testGet()
  49. {
  50. // tests defaults
  51. $this->assertNull($this->session->get('foo'));
  52. $this->assertEquals(1, $this->session->get('foo', 1));
  53. }
  54. /**
  55. * @dataProvider setProvider
  56. */
  57. public function testSet($key, $value)
  58. {
  59. $this->session->set($key, $value);
  60. $this->assertEquals($value, $this->session->get($key));
  61. }
  62. /**
  63. * @dataProvider setProvider
  64. */
  65. public function testHas($key, $value)
  66. {
  67. $this->session->set($key, $value);
  68. $this->assertTrue($this->session->has($key));
  69. $this->assertFalse($this->session->has($key.'non_value'));
  70. }
  71. public function testReplace()
  72. {
  73. $this->session->replace(array('happiness' => 'be good', 'symfony' => 'awesome'));
  74. $this->assertEquals(array('happiness' => 'be good', 'symfony' => 'awesome'), $this->session->all());
  75. $this->session->replace(array());
  76. $this->assertEquals(array(), $this->session->all());
  77. }
  78. /**
  79. * @dataProvider setProvider
  80. */
  81. public function testAll($key, $value, $result)
  82. {
  83. $this->session->set($key, $value);
  84. $this->assertEquals($result, $this->session->all());
  85. }
  86. /**
  87. * @dataProvider setProvider
  88. */
  89. public function testClear($key, $value)
  90. {
  91. $this->session->set('hi', 'fabien');
  92. $this->session->set($key, $value);
  93. $this->session->clear();
  94. $this->assertEquals(array(), $this->session->all());
  95. }
  96. public function setProvider()
  97. {
  98. return array(
  99. array('foo', 'bar', array('foo' => 'bar')),
  100. array('foo.bar', 'too much beer', array('foo.bar' => 'too much beer')),
  101. array('great', 'symfony2 is great', array('great' => 'symfony2 is great')),
  102. );
  103. }
  104. /**
  105. * @dataProvider setProvider
  106. */
  107. public function testRemove($key, $value)
  108. {
  109. $this->session->set('hi.world', 'have a nice day');
  110. $this->session->set($key, $value);
  111. $this->session->remove($key);
  112. $this->assertEquals(array('hi.world' => 'have a nice day'), $this->session->all());
  113. }
  114. public function testInvalidate()
  115. {
  116. $this->session->set('invalidate', 123);
  117. $this->session->invalidate();
  118. $this->assertEquals(array(), $this->session->all());
  119. }
  120. public function testMigrate()
  121. {
  122. $this->session->set('migrate', 321);
  123. $this->session->migrate();
  124. $this->assertEquals(321, $this->session->get('migrate'));
  125. }
  126. public function testMigrateDestroy()
  127. {
  128. $this->session->set('migrate', 333);
  129. $this->session->migrate(true);
  130. $this->assertEquals(333, $this->session->get('migrate'));
  131. }
  132. public function testSave()
  133. {
  134. $this->session->save();
  135. }
  136. public function testGetId()
  137. {
  138. $this->assertEquals('', $this->session->getId());
  139. $this->session->start();
  140. $this->assertNotEquals('', $this->session->getId());
  141. }
  142. public function testGetFlashBag()
  143. {
  144. $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface', $this->session->getFlashBag());
  145. }
  146. // deprecated since 2.1, will be removed from 2.3
  147. public function testGetSetFlashes()
  148. {
  149. $array = array('notice' => 'hello', 'error' => 'none');
  150. $this->assertEquals(array(), $this->session->getFlashes());
  151. $this->session->setFlashes($array);
  152. $this->assertEquals($array, $this->session->getFlashes());
  153. $this->assertEquals(array(), $this->session->getFlashes());
  154. $this->session->getFlashBag()->add('notice', 'foo');
  155. // test that BC works by only retrieving the first added.
  156. $this->session->getFlashBag()->add('notice', 'foo2');
  157. $this->assertEquals(array('notice' => 'foo'), $this->session->getFlashes());
  158. }
  159. public function testGetSetFlash()
  160. {
  161. $this->assertNull($this->session->getFlash('notice'));
  162. $this->assertEquals('default', $this->session->getFlash('notice', 'default'));
  163. $this->session->getFlashBag()->add('notice', 'foo');
  164. $this->session->getFlashBag()->add('notice', 'foo2');
  165. // test that BC works by only retrieving the first added.
  166. $this->assertEquals('foo', $this->session->getFlash('notice'));
  167. $this->assertNull($this->session->getFlash('notice'));
  168. }
  169. public function testHasFlash()
  170. {
  171. $this->assertFalse($this->session->hasFlash('notice'));
  172. $this->session->setFlash('notice', 'foo');
  173. $this->assertTrue($this->session->hasFlash('notice'));
  174. }
  175. public function testRemoveFlash()
  176. {
  177. $this->session->setFlash('notice', 'foo');
  178. $this->session->setFlash('error', 'bar');
  179. $this->assertTrue($this->session->hasFlash('notice'));
  180. $this->session->removeFlash('error');
  181. $this->assertTrue($this->session->hasFlash('notice'));
  182. $this->assertFalse($this->session->hasFlash('error'));
  183. }
  184. public function testClearFlashes()
  185. {
  186. $this->assertFalse($this->session->hasFlash('notice'));
  187. $this->assertFalse($this->session->hasFlash('error'));
  188. $this->session->setFlash('notice', 'foo');
  189. $this->session->setFlash('error', 'bar');
  190. $this->assertTrue($this->session->hasFlash('notice'));
  191. $this->assertTrue($this->session->hasFlash('error'));
  192. $this->session->clearFlashes();
  193. $this->assertFalse($this->session->hasFlash('notice'));
  194. $this->assertFalse($this->session->hasFlash('error'));
  195. }
  196. /**
  197. * @covers Symfony\Component\HttpFoundation\Session\Session::getIterator
  198. */
  199. public function testGetIterator()
  200. {
  201. $attributes = array('hello' => 'world', 'symfony2' => 'rocks');
  202. foreach ($attributes as $key => $val) {
  203. $this->session->set($key, $val);
  204. }
  205. $i = 0;
  206. foreach ($this->session as $key => $val) {
  207. $this->assertEquals($attributes[$key], $val);
  208. $i++;
  209. }
  210. $this->assertEquals(count($attributes), $i);
  211. }
  212. /**
  213. * @covers Symfony\Component\HttpFoundation\Session\Session::count
  214. */
  215. public function testGetCount()
  216. {
  217. $this->session->set('hello', 'world');
  218. $this->session->set('symfony2', 'rocks');
  219. $this->assertEquals(2, count($this->session));
  220. }
  221. }