Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

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