Yaffs site version 1.1
[yaffs-website] / vendor / symfony / http-foundation / Tests / 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
12 namespace Symfony\Component\HttpFoundation\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\HeaderBag;
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
51     /**
52      * @expectedException \RuntimeException
53      */
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
126     public function testCacheControlDirectiveAccessors()
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
144     public function testCacheControlDirectiveParsing()
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
157     public function testCacheControlDirectiveParsingQuotedZero()
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
164     public function testCacheControlDirectiveOverrideWithReplace()
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 testGetIterator()
176     {
177         $headers = array('foo' => 'bar', 'hello' => 'world', 'third' => 'charm');
178         $headerBag = new HeaderBag($headers);
179
180         $i = 0;
181         foreach ($headerBag as $key => $val) {
182             ++$i;
183             $this->assertEquals(array($headers[$key]), $val);
184         }
185
186         $this->assertEquals(count($headers), $i);
187     }
188
189     public function testCount()
190     {
191         $headers = array('foo' => 'bar', 'HELLO' => 'WORLD');
192         $headerBag = new HeaderBag($headers);
193
194         $this->assertEquals(count($headers), count($headerBag));
195     }
196 }