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