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\Session;
13
 
14
use Symfony\Component\HttpFoundation\Session\Session;
15
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
16
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
17
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
18
 
19
/**
20
 * SessionTest.
21
 *
22
 * @author Fabien Potencier <fabien@symfony.com>
23
 * @author Robert Schönthal <seroscho@googlemail.com>
24
 * @author Drak <drak@zikula.org>
25
 */
26
class SessionTest extends \PHPUnit_Framework_TestCase
27
{
28
    /**
29
     * @var \Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface
30
     */
31
    protected $storage;
32
 
33
    /**
34
     * @var \Symfony\Component\HttpFoundation\Session\SessionInterface
35
     */
36
    protected $session;
37
 
38
    protected function setUp()
39
    {
40
        $this->storage = new MockArraySessionStorage();
41
        $this->session = new Session($this->storage, new AttributeBag(), new FlashBag());
42
    }
43
 
44
    protected function tearDown()
45
    {
46
        $this->storage = null;
47
        $this->session = null;
48
    }
49
 
50
    public function testStart()
51
    {
52
        $this->assertEquals('', $this->session->getId());
53
        $this->assertTrue($this->session->start());
54
        $this->assertNotEquals('', $this->session->getId());
55
    }
56
 
57
    public function testIsStarted()
58
    {
59
        $this->assertFalse($this->session->isStarted());
60
        $this->session->start();
61
        $this->assertTrue($this->session->isStarted());
62
    }
63
 
64
    public function testSetId()
65
    {
66
        $this->assertEquals('', $this->session->getId());
67
        $this->session->setId('0123456789abcdef');
68
        $this->session->start();
69
        $this->assertEquals('0123456789abcdef', $this->session->getId());
70
    }
71
 
72
    public function testSetName()
73
    {
74
        $this->assertEquals('MOCKSESSID', $this->session->getName());
75
        $this->session->setName('session.test.com');
76
        $this->session->start();
77
        $this->assertEquals('session.test.com', $this->session->getName());
78
    }
79
 
80
    public function testGet()
81
    {
82
        // tests defaults
83
        $this->assertNull($this->session->get('foo'));
84
        $this->assertEquals(1, $this->session->get('foo', 1));
85
    }
86
 
87
    /**
88
     * @dataProvider setProvider
89
     */
90
    public function testSet($key, $value)
91
    {
92
        $this->session->set($key, $value);
93
        $this->assertEquals($value, $this->session->get($key));
94
    }
95
 
96
    /**
97
     * @dataProvider setProvider
98
     */
99
    public function testHas($key, $value)
100
    {
101
        $this->session->set($key, $value);
102
        $this->assertTrue($this->session->has($key));
103
        $this->assertFalse($this->session->has($key.'non_value'));
104
    }
105
 
106
    public function testReplace()
107
    {
108
        $this->session->replace(array('happiness' => 'be good', 'symfony' => 'awesome'));
109
        $this->assertEquals(array('happiness' => 'be good', 'symfony' => 'awesome'), $this->session->all());
110
        $this->session->replace(array());
111
        $this->assertEquals(array(), $this->session->all());
112
    }
113
 
114
    /**
115
     * @dataProvider setProvider
116
     */
117
    public function testAll($key, $value, $result)
118
    {
119
        $this->session->set($key, $value);
120
        $this->assertEquals($result, $this->session->all());
121
    }
122
 
123
    /**
124
     * @dataProvider setProvider
125
     */
126
    public function testClear($key, $value)
127
    {
128
        $this->session->set('hi', 'fabien');
129
        $this->session->set($key, $value);
130
        $this->session->clear();
131
        $this->assertEquals(array(), $this->session->all());
132
    }
133
 
134
    public function setProvider()
135
    {
136
        return array(
137
            array('foo', 'bar', array('foo' => 'bar')),
138
            array('foo.bar', 'too much beer', array('foo.bar' => 'too much beer')),
139
            array('great', 'symfony is great', array('great' => 'symfony is great')),
140
        );
141
    }
142
 
143
    /**
144
     * @dataProvider setProvider
145
     */
146
    public function testRemove($key, $value)
147
    {
148
        $this->session->set('hi.world', 'have a nice day');
149
        $this->session->set($key, $value);
150
        $this->session->remove($key);
151
        $this->assertEquals(array('hi.world' => 'have a nice day'), $this->session->all());
152
    }
153
 
154
    public function testInvalidate()
155
    {
156
        $this->session->set('invalidate', 123);
157
        $this->session->invalidate();
158
        $this->assertEquals(array(), $this->session->all());
159
    }
160
 
161
    public function testMigrate()
162
    {
163
        $this->session->set('migrate', 321);
164
        $this->session->migrate();
165
        $this->assertEquals(321, $this->session->get('migrate'));
166
    }
167
 
168
    public function testMigrateDestroy()
169
    {
170
        $this->session->set('migrate', 333);
171
        $this->session->migrate(true);
172
        $this->assertEquals(333, $this->session->get('migrate'));
173
    }
174
 
175
    public function testSave()
176
    {
177
        $this->session->start();
178
        $this->session->save();
179
    }
180
 
181
    public function testGetId()
182
    {
183
        $this->assertEquals('', $this->session->getId());
184
        $this->session->start();
185
        $this->assertNotEquals('', $this->session->getId());
186
    }
187
 
188
    public function testGetFlashBag()
189
    {
190
        $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface', $this->session->getFlashBag());
191
    }
192
 
193
    public function testGetIterator()
194
    {
195
        $attributes = array('hello' => 'world', 'symfony' => 'rocks');
196
        foreach ($attributes as $key => $val) {
197
            $this->session->set($key, $val);
198
        }
199
 
200
        $i = 0;
201
        foreach ($this->session as $key => $val) {
202
            $this->assertEquals($attributes[$key], $val);
203
            ++$i;
204
        }
205
 
206
        $this->assertEquals(count($attributes), $i);
207
    }
208
 
209
    public function testGetCount()
210
    {
211
        $this->session->set('hello', 'world');
212
        $this->session->set('symfony', 'rocks');
213
 
214
        $this->assertCount(2, $this->session);
215
    }
216
 
217
    public function testGetMeta()
218
    {
219
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\MetadataBag', $this->session->getMetadataBag());
220
    }
221
}