1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Framework\Http; |
13
|
|
|
|
14
|
|
|
use Spiral\Cookies\Cookie; |
15
|
|
|
use Spiral\Cookies\CookieManager; |
16
|
|
|
use Spiral\Encrypter\EncrypterFactory; |
17
|
|
|
use Spiral\Encrypter\EncrypterInterface; |
18
|
|
|
use Spiral\Framework\HttpTest; |
19
|
|
|
use Spiral\Http\Http; |
20
|
|
|
|
21
|
|
|
class CookiesTest extends HttpTest |
22
|
|
|
{ |
23
|
|
|
public function testOutsideOfScopeOK(): void |
24
|
|
|
{ |
25
|
|
|
$cookies = $this->cookies(); |
26
|
|
|
$this->assertInstanceOf(CookieManager::class, $cookies); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @expectedException \Spiral\Core\Exception\ScopeException |
31
|
|
|
*/ |
32
|
|
|
public function testOutsideOfScopeFail(): void |
33
|
|
|
{ |
34
|
|
|
$this->cookies()->get('name'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testHasCookie(): void |
38
|
|
|
{ |
39
|
|
|
$this->http->setHandler(function () { |
40
|
|
|
return (int)$this->cookies()->has('a'); |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
$result = $this->get('/'); |
44
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
45
|
|
|
$this->assertSame('0', $result->getBody()->__toString()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testHasCookie2(): void |
49
|
|
|
{ |
50
|
|
|
$key = $this->app->get(EncrypterFactory::class)->generateKey(); |
51
|
|
|
|
52
|
|
|
$this->app = $this->makeApp([ |
53
|
|
|
'ENCRYPTER_KEY' => $key |
54
|
|
|
]); |
55
|
|
|
$this->http = $this->app->get(Http::class); |
56
|
|
|
|
57
|
|
|
$this->http->setHandler(function () { |
|
|
|
|
58
|
|
|
return (int)$this->cookies()->has('a'); |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
$result = $this->get('/', [], [], [ |
62
|
|
|
'a' => $this->app->get(EncrypterInterface::class)->encrypt('hello') |
63
|
|
|
]); |
64
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
65
|
|
|
$this->assertSame('1', $result->getBody()->__toString()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testGetCookie2(): void |
69
|
|
|
{ |
70
|
|
|
$key = $this->app->get(EncrypterFactory::class)->generateKey(); |
71
|
|
|
$this->app = $this->makeApp(['ENCRYPTER_KEY' => $key]); |
72
|
|
|
$this->http = $this->app->get(Http::class); |
73
|
|
|
|
74
|
|
|
$this->http->setHandler(function () { |
75
|
|
|
return $this->cookies()->get('a'); |
76
|
|
|
}); |
77
|
|
|
|
78
|
|
|
$result = $this->get('/', [], [], [ |
79
|
|
|
'a' => $this->app->get(EncrypterInterface::class)->encrypt('hello') |
80
|
|
|
]); |
81
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
82
|
|
|
$this->assertSame('hello', $result->getBody()->__toString()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testSetCookie(): void |
86
|
|
|
{ |
87
|
|
|
$key = $this->app->get(EncrypterFactory::class)->generateKey(); |
88
|
|
|
$this->app = $this->makeApp(['ENCRYPTER_KEY' => $key]); |
89
|
|
|
$this->http = $this->app->get(Http::class); |
90
|
|
|
|
91
|
|
|
$this->http->setHandler(function () { |
92
|
|
|
$this->cookies()->set('a', 'value'); |
93
|
|
|
|
94
|
|
|
return 'ok'; |
95
|
|
|
}); |
96
|
|
|
|
97
|
|
|
$result = $this->get('/'); |
98
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
99
|
|
|
$this->assertSame('ok', $result->getBody()->__toString()); |
100
|
|
|
|
101
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
102
|
|
|
|
103
|
|
|
$this->assertSame( |
104
|
|
|
'value', |
105
|
|
|
$this->app->get(EncrypterInterface::class)->decrypt($cookies['a']) |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testSetCookie2(): void |
110
|
|
|
{ |
111
|
|
|
$key = $this->app->get(EncrypterFactory::class)->generateKey(); |
112
|
|
|
$this->app = $this->makeApp(['ENCRYPTER_KEY' => $key]); |
113
|
|
|
$this->http = $this->app->get(Http::class); |
114
|
|
|
|
115
|
|
|
$this->http->setHandler(function () { |
116
|
|
|
$this->cookies()->schedule(Cookie::create('a', 'value')); |
117
|
|
|
$this->assertSame([], $this->cookies()->getAll()); |
118
|
|
|
$this->assertCount(1, $this->cookies()->getScheduled()); |
119
|
|
|
|
120
|
|
|
return 'ok'; |
121
|
|
|
}); |
122
|
|
|
|
123
|
|
|
$result = $this->get('/'); |
124
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
125
|
|
|
$this->assertSame('ok', $result->getBody()->__toString()); |
126
|
|
|
|
127
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
128
|
|
|
|
129
|
|
|
$this->assertSame( |
130
|
|
|
'value', |
131
|
|
|
$this->app->get(EncrypterInterface::class)->decrypt($cookies['a']) |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testDeleteCookie(): void |
136
|
|
|
{ |
137
|
|
|
$key = $this->app->get(EncrypterFactory::class)->generateKey(); |
138
|
|
|
$this->app = $this->makeApp(['ENCRYPTER_KEY' => $key]); |
139
|
|
|
$this->http = $this->app->get(Http::class); |
140
|
|
|
|
141
|
|
|
$this->http->setHandler(function () { |
142
|
|
|
$this->cookies()->delete('cookie'); |
143
|
|
|
|
144
|
|
|
return 'ok'; |
145
|
|
|
}); |
146
|
|
|
|
147
|
|
|
$result = $this->get('/'); |
148
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
149
|
|
|
$this->assertSame('ok', $result->getBody()->__toString()); |
150
|
|
|
|
151
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
152
|
|
|
|
153
|
|
|
$this->assertSame('', $cookies['cookie']); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
private function cookies(): CookieManager |
157
|
|
|
{ |
158
|
|
|
return $this->app->get(CookieManager::class); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.