AttributeBagTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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\Attribute;
  11. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
  12. /**
  13. * Tests AttributeBag.
  14. *
  15. * @author Drak <drak@zikula.org>
  16. */
  17. class AttributeBagTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @var array
  21. */
  22. private $array;
  23. /**
  24. * @var AttributeBag
  25. */
  26. private $bag;
  27. protected function setUp()
  28. {
  29. $this->array = array(
  30. 'hello' => 'world',
  31. 'always' => 'be happy',
  32. 'user.login' => 'drak',
  33. 'csrf.token' => array(
  34. 'a' => '1234',
  35. 'b' => '4321',
  36. ),
  37. 'category' => array(
  38. 'fishing' => array(
  39. 'first' => 'cod',
  40. 'second' => 'sole',
  41. ),
  42. ),
  43. );
  44. $this->bag = new AttributeBag('_sf2');
  45. $this->bag->initialize($this->array);
  46. }
  47. protected function tearDown()
  48. {
  49. $this->bag = null;
  50. $this->array = array();
  51. }
  52. public function testInitialize()
  53. {
  54. $bag = new AttributeBag();
  55. $bag->initialize($this->array);
  56. $this->assertEquals($this->array, $bag->all());
  57. $array = array('should' => 'change');
  58. $bag->initialize($array);
  59. $this->assertEquals($array, $bag->all());
  60. }
  61. public function testGetStorageKey()
  62. {
  63. $this->assertEquals('_sf2', $this->bag->getStorageKey());
  64. $attributeBag = new AttributeBag('test');
  65. $this->assertEquals('test', $attributeBag->getStorageKey());
  66. }
  67. public function testGetSetName()
  68. {
  69. $this->assertEquals('attributes', $this->bag->getName());
  70. $this->bag->setName('foo');
  71. $this->assertEquals('foo', $this->bag->getName());
  72. }
  73. /**
  74. * @dataProvider attributesProvider
  75. */
  76. public function testHas($key, $value, $exists)
  77. {
  78. $this->assertEquals($exists, $this->bag->has($key));
  79. }
  80. /**
  81. * @dataProvider attributesProvider
  82. */
  83. public function testGet($key, $value, $expected)
  84. {
  85. $this->assertEquals($value, $this->bag->get($key));
  86. }
  87. public function testGetDefaults()
  88. {
  89. $this->assertNull($this->bag->get('user2.login'));
  90. $this->assertEquals('default', $this->bag->get('user2.login', 'default'));
  91. }
  92. /**
  93. * @dataProvider attributesProvider
  94. */
  95. public function testSet($key, $value, $expected)
  96. {
  97. $this->bag->set($key, $value);
  98. $this->assertEquals($value, $this->bag->get($key));
  99. }
  100. public function testAll()
  101. {
  102. $this->assertEquals($this->array, $this->bag->all());
  103. $this->bag->set('hello', 'fabien');
  104. $array = $this->array;
  105. $array['hello'] = 'fabien';
  106. $this->assertEquals($array, $this->bag->all());
  107. }
  108. public function testReplace()
  109. {
  110. $array = array();
  111. $array['name'] = 'jack';
  112. $array['foo.bar'] = 'beep';
  113. $this->bag->replace($array);
  114. $this->assertEquals($array, $this->bag->all());
  115. $this->assertNull($this->bag->get('hello'));
  116. $this->assertNull($this->bag->get('always'));
  117. $this->assertNull($this->bag->get('user.login'));
  118. }
  119. public function testRemove()
  120. {
  121. $this->assertEquals('world', $this->bag->get('hello'));
  122. $this->bag->remove('hello');
  123. $this->assertNull($this->bag->get('hello'));
  124. $this->assertEquals('be happy', $this->bag->get('always'));
  125. $this->bag->remove('always');
  126. $this->assertNull($this->bag->get('always'));
  127. $this->assertEquals('drak', $this->bag->get('user.login'));
  128. $this->bag->remove('user.login');
  129. $this->assertNull($this->bag->get('user.login'));
  130. }
  131. public function testClear()
  132. {
  133. $this->bag->clear();
  134. $this->assertEquals(array(), $this->bag->all());
  135. }
  136. public function attributesProvider()
  137. {
  138. return array(
  139. array('hello', 'world', true),
  140. array('always', 'be happy', true),
  141. array('user.login', 'drak', true),
  142. array('csrf.token', array('a' => '1234', 'b' => '4321'), true),
  143. array('category', array('fishing' => array('first' => 'cod', 'second' => 'sole')), true),
  144. array('user2.login', null, false),
  145. array('never', null, false),
  146. array('bye', null, false),
  147. array('bye/for/now', null, false),
  148. );
  149. }
  150. public function testGetIterator()
  151. {
  152. $i = 0;
  153. foreach ($this->bag as $key => $val) {
  154. $this->assertEquals($this->array[$key], $val);
  155. ++$i;
  156. }
  157. $this->assertEquals(count($this->array), $i);
  158. }
  159. public function testCount()
  160. {
  161. $this->assertEquals(count($this->array), count($this->bag));
  162. }
  163. }