菜谱项目

NamespacedAttributeBagTest.php 5.3KB

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