Open Monograph Press  3.3.0
HeaderBagTest.php
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 
14 use PHPUnit\Framework\TestCase;
16 
17 class HeaderBagTest extends TestCase
18 {
19  public function testConstructor()
20  {
21  $bag = new HeaderBag(array('foo' => 'bar'));
22  $this->assertTrue($bag->has('foo'));
23  }
24 
25  public function testToStringNull()
26  {
27  $bag = new HeaderBag();
28  $this->assertEquals('', $bag->__toString());
29  }
30 
31  public function testToStringNotNull()
32  {
33  $bag = new HeaderBag(array('foo' => 'bar'));
34  $this->assertEquals("Foo: bar\r\n", $bag->__toString());
35  }
36 
37  public function testKeys()
38  {
39  $bag = new HeaderBag(array('foo' => 'bar'));
40  $keys = $bag->keys();
41  $this->assertEquals('foo', $keys[0]);
42  }
43 
44  public function testGetDate()
45  {
46  $bag = new HeaderBag(array('foo' => 'Tue, 4 Sep 2012 20:00:00 +0200'));
47  $headerDate = $bag->getDate('foo');
48  $this->assertInstanceOf('DateTime', $headerDate);
49  }
50 
54  public function testGetDateException()
55  {
56  $bag = new HeaderBag(array('foo' => 'Tue'));
57  $headerDate = $bag->getDate('foo');
58  }
59 
60  public function testGetCacheControlHeader()
61  {
62  $bag = new HeaderBag();
63  $bag->addCacheControlDirective('public', '#a');
64  $this->assertTrue($bag->hasCacheControlDirective('public'));
65  $this->assertEquals('#a', $bag->getCacheControlDirective('public'));
66  }
67 
68  public function testAll()
69  {
70  $bag = new HeaderBag(array('foo' => 'bar'));
71  $this->assertEquals(array('foo' => array('bar')), $bag->all(), '->all() gets all the input');
72 
73  $bag = new HeaderBag(array('FOO' => 'BAR'));
74  $this->assertEquals(array('foo' => array('BAR')), $bag->all(), '->all() gets all the input key are lower case');
75  }
76 
77  public function testReplace()
78  {
79  $bag = new HeaderBag(array('foo' => 'bar'));
80 
81  $bag->replace(array('NOPE' => 'BAR'));
82  $this->assertEquals(array('nope' => array('BAR')), $bag->all(), '->replace() replaces the input with the argument');
83  $this->assertFalse($bag->has('foo'), '->replace() overrides previously set the input');
84  }
85 
86  public function testGet()
87  {
88  $bag = new HeaderBag(array('foo' => 'bar', 'fuzz' => 'bizz'));
89  $this->assertEquals('bar', $bag->get('foo'), '->get return current value');
90  $this->assertEquals('bar', $bag->get('FoO'), '->get key in case insensitive');
91  $this->assertEquals(array('bar'), $bag->get('foo', 'nope', false), '->get return the value as array');
92 
93  // defaults
94  $this->assertNull($bag->get('none'), '->get unknown values returns null');
95  $this->assertEquals('default', $bag->get('none', 'default'), '->get unknown values returns default');
96  $this->assertEquals(array('default'), $bag->get('none', 'default', false), '->get unknown values returns default as array');
97 
98  $bag->set('foo', 'bor', false);
99  $this->assertEquals('bar', $bag->get('foo'), '->get return first value');
100  $this->assertEquals(array('bar', 'bor'), $bag->get('foo', 'nope', false), '->get return all values as array');
101  }
102 
103  public function testSetAssociativeArray()
104  {
105  $bag = new HeaderBag();
106  $bag->set('foo', array('bad-assoc-index' => 'value'));
107  $this->assertSame('value', $bag->get('foo'));
108  $this->assertEquals(array('value'), $bag->get('foo', 'nope', false), 'assoc indices of multi-valued headers are ignored');
109  }
110 
111  public function testContains()
112  {
113  $bag = new HeaderBag(array('foo' => 'bar', 'fuzz' => 'bizz'));
114  $this->assertTrue($bag->contains('foo', 'bar'), '->contains first value');
115  $this->assertTrue($bag->contains('fuzz', 'bizz'), '->contains second value');
116  $this->assertFalse($bag->contains('nope', 'nope'), '->contains unknown value');
117  $this->assertFalse($bag->contains('foo', 'nope'), '->contains unknown value');
118 
119  // Multiple values
120  $bag->set('foo', 'bor', false);
121  $this->assertTrue($bag->contains('foo', 'bar'), '->contains first value');
122  $this->assertTrue($bag->contains('foo', 'bor'), '->contains second value');
123  $this->assertFalse($bag->contains('foo', 'nope'), '->contains unknown value');
124  }
125 
127  {
128  $bag = new HeaderBag();
129  $bag->addCacheControlDirective('public');
130 
131  $this->assertTrue($bag->hasCacheControlDirective('public'));
132  $this->assertTrue($bag->getCacheControlDirective('public'));
133  $this->assertEquals('public', $bag->get('cache-control'));
134 
135  $bag->addCacheControlDirective('max-age', 10);
136  $this->assertTrue($bag->hasCacheControlDirective('max-age'));
137  $this->assertEquals(10, $bag->getCacheControlDirective('max-age'));
138  $this->assertEquals('max-age=10, public', $bag->get('cache-control'));
139 
140  $bag->removeCacheControlDirective('max-age');
141  $this->assertFalse($bag->hasCacheControlDirective('max-age'));
142  }
143 
145  {
146  $bag = new HeaderBag(array('cache-control' => 'public, max-age=10'));
147  $this->assertTrue($bag->hasCacheControlDirective('public'));
148  $this->assertTrue($bag->getCacheControlDirective('public'));
149 
150  $this->assertTrue($bag->hasCacheControlDirective('max-age'));
151  $this->assertEquals(10, $bag->getCacheControlDirective('max-age'));
152 
153  $bag->addCacheControlDirective('s-maxage', 100);
154  $this->assertEquals('max-age=10, public, s-maxage=100', $bag->get('cache-control'));
155  }
156 
158  {
159  $bag = new HeaderBag(array('cache-control' => 'max-age="0"'));
160  $this->assertTrue($bag->hasCacheControlDirective('max-age'));
161  $this->assertEquals(0, $bag->getCacheControlDirective('max-age'));
162  }
163 
165  {
166  $bag = new HeaderBag(array('cache-control' => 'private, max-age=100'));
167  $bag->replace(array('cache-control' => 'public, max-age=10'));
168  $this->assertTrue($bag->hasCacheControlDirective('public'));
169  $this->assertTrue($bag->getCacheControlDirective('public'));
170 
171  $this->assertTrue($bag->hasCacheControlDirective('max-age'));
172  $this->assertEquals(10, $bag->getCacheControlDirective('max-age'));
173  }
174 
175  public function testCacheControlClone()
176  {
177  $headers = array('foo' => 'bar');
178  $bag1 = new HeaderBag($headers);
179  $bag2 = new HeaderBag($bag1->all());
180 
181  $this->assertEquals($bag1->all(), $bag2->all());
182  }
183 
184  public function testGetIterator()
185  {
186  $headers = array('foo' => 'bar', 'hello' => 'world', 'third' => 'charm');
187  $headerBag = new HeaderBag($headers);
188 
189  $i = 0;
190  foreach ($headerBag as $key => $val) {
191  ++$i;
192  $this->assertEquals(array($headers[$key]), $val);
193  }
194 
195  $this->assertEquals(count($headers), $i);
196  }
197 
198  public function testCount()
199  {
200  $headers = array('foo' => 'bar', 'HELLO' => 'WORLD');
201  $headerBag = new HeaderBag($headers);
202 
203  $this->assertEquals(count($headers), count($headerBag));
204  }
205 }
Symfony\Component\HttpFoundation\Tests\HeaderBagTest\testCacheControlDirectiveAccessors
testCacheControlDirectiveAccessors()
Definition: HeaderBagTest.php:126
Symfony\Component\HttpFoundation\Tests\HeaderBagTest\testCacheControlDirectiveParsing
testCacheControlDirectiveParsing()
Definition: HeaderBagTest.php:144
Symfony\Component\HttpFoundation\Tests\HeaderBagTest\testCacheControlDirectiveOverrideWithReplace
testCacheControlDirectiveOverrideWithReplace()
Definition: HeaderBagTest.php:164
Symfony\Component\HttpFoundation\Tests\HeaderBagTest\testGet
testGet()
Definition: HeaderBagTest.php:86
Symfony\Component\HttpFoundation\Tests\HeaderBagTest\testCount
testCount()
Definition: HeaderBagTest.php:198
Symfony\Component\HttpFoundation\Tests\HeaderBagTest\testKeys
testKeys()
Definition: HeaderBagTest.php:37
Symfony\Component\HttpFoundation\Tests\HeaderBagTest\testToStringNotNull
testToStringNotNull()
Definition: HeaderBagTest.php:31
Symfony\Component\HttpFoundation\Tests\HeaderBagTest\testGetIterator
testGetIterator()
Definition: HeaderBagTest.php:184
Symfony\Component\HttpFoundation\Tests\HeaderBagTest\testAll
testAll()
Definition: HeaderBagTest.php:68
Symfony\Component\HttpFoundation\Tests\HeaderBagTest\testToStringNull
testToStringNull()
Definition: HeaderBagTest.php:25
Symfony\Component\HttpFoundation\Tests\HeaderBagTest\testConstructor
testConstructor()
Definition: HeaderBagTest.php:19
Symfony\Component\HttpFoundation\Tests\HeaderBagTest\testContains
testContains()
Definition: HeaderBagTest.php:111
Symfony\Component\HttpFoundation\Tests\HeaderBagTest\testReplace
testReplace()
Definition: HeaderBagTest.php:77
Symfony\Component\HttpFoundation\Tests\HeaderBagTest\testCacheControlDirectiveParsingQuotedZero
testCacheControlDirectiveParsingQuotedZero()
Definition: HeaderBagTest.php:157
Symfony\Component\HttpFoundation\HeaderBag
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:19
Symfony\Component\HttpFoundation\Tests
Definition: AcceptHeaderItemTest.php:12
Symfony\Component\HttpFoundation\Tests\HeaderBagTest\testCacheControlClone
testCacheControlClone()
Definition: HeaderBagTest.php:175
Symfony\Component\HttpFoundation\Tests\HeaderBagTest\testGetDateException
testGetDateException()
Definition: HeaderBagTest.php:54
Symfony\Component\HttpFoundation\Tests\HeaderBagTest\testGetCacheControlHeader
testGetCacheControlHeader()
Definition: HeaderBagTest.php:60
Symfony\Component\HttpFoundation\Tests\HeaderBagTest\testSetAssociativeArray
testSetAssociativeArray()
Definition: HeaderBagTest.php:103
Symfony\Component\HttpFoundation\Tests\HeaderBagTest\testGetDate
testGetDate()
Definition: HeaderBagTest.php:44
Symfony\Component\HttpFoundation\Tests\HeaderBagTest
Definition: HeaderBagTest.php:17