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\Cookie;
15
 
16
/**
17
 * CookieTest.
18
 *
19
 * @author John Kary <john@johnkary.net>
20
 * @author Hugo Hamon <hugo.hamon@sensio.com>
21
 *
22
 * @group time-sensitive
23
 */
24
class CookieTest extends \PHPUnit_Framework_TestCase
25
{
26
    public function invalidNames()
27
    {
28
        return array(
29
            array(''),
30
            array(',MyName'),
31
            array(';MyName'),
32
            array(' MyName'),
33
            array("\tMyName"),
34
            array("\rMyName"),
35
            array("\nMyName"),
36
            array("\013MyName"),
37
            array("\014MyName"),
38
        );
39
    }
40
 
41
    /**
42
     * @dataProvider invalidNames
43
     * @expectedException \InvalidArgumentException
44
     */
45
    public function testInstantiationThrowsExceptionIfCookieNameContainsInvalidCharacters($name)
46
    {
47
        new Cookie($name);
48
    }
49
 
50
    /**
51
     * @expectedException \InvalidArgumentException
52
     */
53
    public function testInvalidExpiration()
54
    {
55
        $cookie = new Cookie('MyCookie', 'foo', 'bar');
56
    }
57
 
58
    public function testGetValue()
59
    {
60
        $value = 'MyValue';
61
        $cookie = new Cookie('MyCookie', $value);
62
 
63
        $this->assertSame($value, $cookie->getValue(), '->getValue() returns the proper value');
64
    }
65
 
66
    public function testGetPath()
67
    {
68
        $cookie = new Cookie('foo', 'bar');
69
 
70
        $this->assertSame('/', $cookie->getPath(), '->getPath() returns / as the default path');
71
    }
72
 
73
    public function testGetExpiresTime()
74
    {
75
        $cookie = new Cookie('foo', 'bar', 3600);
76
 
77
        $this->assertEquals(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
78
    }
79
 
80
    public function testConstructorWithDateTime()
81
    {
82
        $expire = new \DateTime();
83
        $cookie = new Cookie('foo', 'bar', $expire);
84
 
85
        $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
86
    }
87
 
88
    /**
89
     * @requires PHP 5.5
90
     */
91
    public function testConstructorWithDateTimeImmutable()
92
    {
93
        $expire = new \DateTimeImmutable();
94
        $cookie = new Cookie('foo', 'bar', $expire);
95
 
96
        $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
97
    }
98
 
99
    public function testGetExpiresTimeWithStringValue()
100
    {
101
        $value = '+1 day';
102
        $cookie = new Cookie('foo', 'bar', $value);
103
        $expire = strtotime($value);
104
 
105
        $this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date', 1);
106
    }
107
 
108
    public function testGetDomain()
109
    {
110
        $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com');
111
 
112
        $this->assertEquals('.myfoodomain.com', $cookie->getDomain(), '->getDomain() returns the domain name on which the cookie is valid');
113
    }
114
 
115
    public function testIsSecure()
116
    {
117
        $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', true);
118
 
119
        $this->assertTrue($cookie->isSecure(), '->isSecure() returns whether the cookie is transmitted over HTTPS');
120
    }
121
 
122
    public function testIsHttpOnly()
123
    {
124
        $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true);
125
 
126
        $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns whether the cookie is only transmitted over HTTP');
127
    }
128
 
129
    public function testCookieIsNotCleared()
130
    {
131
        $cookie = new Cookie('foo', 'bar', time() + 3600 * 24);
132
 
133
        $this->assertFalse($cookie->isCleared(), '->isCleared() returns false if the cookie did not expire yet');
134
    }
135
 
136
    public function testCookieIsCleared()
137
    {
138
        $cookie = new Cookie('foo', 'bar', time() - 20);
139
 
140
        $this->assertTrue($cookie->isCleared(), '->isCleared() returns true if the cookie has expired');
141
    }
142
 
143
    public function testToString()
144
    {
145
        $cookie = new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
146
        $this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly', $cookie->__toString(), '->__toString() returns string representation of the cookie');
147
 
148
        $cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com');
149
        $this->assertEquals('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; path=/admin/; domain=.myfoodomain.com; httponly', $cookie->__toString(), '->__toString() returns string representation of a cleared cookie if value is NULL');
150
 
151
        $cookie = new Cookie('foo', 'bar', 0, '/', '');
152
        $this->assertEquals('foo=bar; path=/; httponly', $cookie->__toString());
153
    }
154
 
155
    public function testRawCookie()
156
    {
157
        $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true);
158
        $this->assertFalse($cookie->isRaw());
159
 
160
        $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true, true);
161
        $this->assertTrue($cookie->isRaw());
162
    }
163
}