NamespacedAttributeBagTest.php 5.3 KB

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