NamespacedAttributeBagTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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\NamespacedAttributeBag;
  12. /**
  13. * Tests NamespacedAttributeBag.
  14. *
  15. * @author Drak <drak@zikula.org>
  16. */
  17. class NamespacedAttributeBagTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @var array
  21. */
  22. private $array;
  23. /**
  24. * @var NamespacedAttributeBag
  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 NamespacedAttributeBag('_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 NamespacedAttributeBag();
  55. $bag->initialize($this->array);
  56. $this->assertEquals($this->array, $this->bag->all());
  57. $array = array('should' => 'not stick');
  58. $bag->initialize($array);
  59. // should have remained the same
  60. $this->assertEquals($this->array, $this->bag->all());
  61. }
  62. public function testGetStorageKey()
  63. {
  64. $this->assertEquals('_sf2', $this->bag->getStorageKey());
  65. $attributeBag = new NamespacedAttributeBag('test');
  66. $this->assertEquals('test', $attributeBag->getStorageKey());
  67. }
  68. /**
  69. * @dataProvider attributesProvider
  70. */
  71. public function testHas($key, $value, $exists)
  72. {
  73. $this->assertEquals($exists, $this->bag->has($key));
  74. }
  75. /**
  76. * @dataProvider attributesProvider
  77. */
  78. public function testGet($key, $value, $expected)
  79. {
  80. $this->assertEquals($value, $this->bag->get($key));
  81. }
  82. public function testGetDefaults()
  83. {
  84. $this->assertNull($this->bag->get('user2.login'));
  85. $this->assertEquals('default', $this->bag->get('user2.login', 'default'));
  86. }
  87. /**
  88. * @dataProvider attributesProvider
  89. */
  90. public function testSet($key, $value, $expected)
  91. {
  92. $this->bag->set($key, $value);
  93. $this->assertEquals($value, $this->bag->get($key));
  94. }
  95. public function testAll()
  96. {
  97. $this->assertEquals($this->array, $this->bag->all());
  98. $this->bag->set('hello', 'fabien');
  99. $array = $this->array;
  100. $array['hello'] = 'fabien';
  101. $this->assertEquals($array, $this->bag->all());
  102. }
  103. public function testReplace()
  104. {
  105. $array = array();
  106. $array['name'] = 'jack';
  107. $array['foo.bar'] = 'beep';
  108. $this->bag->replace($array);
  109. $this->assertEquals($array, $this->bag->all());
  110. $this->assertNull($this->bag->get('hello'));
  111. $this->assertNull($this->bag->get('always'));
  112. $this->assertNull($this->bag->get('user.login'));
  113. }
  114. public function testRemove()
  115. {
  116. $this->assertEquals('world', $this->bag->get('hello'));
  117. $this->bag->remove('hello');
  118. $this->assertNull($this->bag->get('hello'));
  119. $this->assertEquals('be happy', $this->bag->get('always'));
  120. $this->bag->remove('always');
  121. $this->assertNull($this->bag->get('always'));
  122. $this->assertEquals('drak', $this->bag->get('user.login'));
  123. $this->bag->remove('user.login');
  124. $this->assertNull($this->bag->get('user.login'));
  125. }
  126. public function testRemoveExistingNamespacedAttribute()
  127. {
  128. $this->assertSame('cod', $this->bag->remove('category/fishing/first'));
  129. }
  130. public function testRemoveNonexistingNamespacedAttribute()
  131. {
  132. $this->assertNull($this->bag->remove('foo/bar/baz'));
  133. }
  134. public function testClear()
  135. {
  136. $this->bag->clear();
  137. $this->assertEquals(array(), $this->bag->all());
  138. }
  139. public function attributesProvider()
  140. {
  141. return array(
  142. array('hello', 'world', true),
  143. array('always', 'be happy', true),
  144. array('user.login', 'drak', true),
  145. array('csrf.token', array('a' => '1234', 'b' => '4321'), true),
  146. array('csrf.token/a', '1234', true),
  147. array('csrf.token/b', '4321', true),
  148. array('category', array('fishing' => array('first' => 'cod', 'second' => 'sole')), true),
  149. array('category/fishing', array('first' => 'cod', 'second' => 'sole'), true),
  150. array('category/fishing/missing/first', null, false),
  151. array('category/fishing/first', 'cod', true),
  152. array('category/fishing/second', 'sole', true),
  153. array('category/fishing/missing/second', null, false),
  154. array('user2.login', null, false),
  155. array('never', null, false),
  156. array('bye', null, false),
  157. array('bye/for/now', null, false),
  158. );
  159. }
  160. }