1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Spiral\Framework\Http; |
12
|
|
|
|
13
|
|
|
use Spiral\Cookies\Cookie; |
14
|
|
|
use Spiral\Cookies\CookieManager; |
15
|
|
|
use Spiral\Encrypter\EncrypterFactory; |
16
|
|
|
use Spiral\Encrypter\EncrypterInterface; |
17
|
|
|
use Spiral\Framework\HttpTest; |
18
|
|
|
use Spiral\Http\Http; |
19
|
|
|
|
20
|
|
|
class CookiesTest extends HttpTest |
21
|
|
|
{ |
22
|
|
|
public function testOutsideOfScopeOK(): void |
23
|
|
|
{ |
24
|
|
|
$cookies = $this->cookies(); |
25
|
|
|
$this->assertInstanceOf(CookieManager::class, $cookies); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @expectedException \Spiral\Core\Exception\ScopeException |
30
|
|
|
*/ |
31
|
|
|
public function testOutsideOfScopeFail(): void |
32
|
|
|
{ |
33
|
|
|
$this->cookies()->get('name'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testHasCookie(): void |
37
|
|
|
{ |
38
|
|
|
$this->http->setHandler(function () { |
39
|
|
|
return (int)$this->cookies()->has('a'); |
40
|
|
|
}); |
41
|
|
|
|
42
|
|
|
$result = $this->get('/'); |
43
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
44
|
|
|
$this->assertSame('0', $result->getBody()->__toString()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testHasCookie2(): void |
48
|
|
|
{ |
49
|
|
|
$key = $this->app->get(EncrypterFactory::class)->generateKey(); |
50
|
|
|
|
51
|
|
|
$this->app = $this->makeApp([ |
52
|
|
|
'ENCRYPTER_KEY' => $key |
53
|
|
|
]); |
54
|
|
|
$this->http = $this->app->get(Http::class); |
55
|
|
|
|
56
|
|
|
$this->http->setHandler(function () { |
|
|
|
|
57
|
|
|
return (int)$this->cookies()->has('a'); |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
$result = $this->get('/', [], [], [ |
61
|
|
|
'a' => $this->app->get(EncrypterInterface::class)->encrypt('hello') |
62
|
|
|
]); |
63
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
64
|
|
|
$this->assertSame('1', $result->getBody()->__toString()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testGetCookie2(): void |
68
|
|
|
{ |
69
|
|
|
$key = $this->app->get(EncrypterFactory::class)->generateKey(); |
70
|
|
|
$this->app = $this->makeApp(['ENCRYPTER_KEY' => $key]); |
71
|
|
|
$this->http = $this->app->get(Http::class); |
72
|
|
|
|
73
|
|
|
$this->http->setHandler(function () { |
74
|
|
|
return $this->cookies()->get('a'); |
75
|
|
|
}); |
76
|
|
|
|
77
|
|
|
$result = $this->get('/', [], [], [ |
78
|
|
|
'a' => $this->app->get(EncrypterInterface::class)->encrypt('hello') |
79
|
|
|
]); |
80
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
81
|
|
|
$this->assertSame('hello', $result->getBody()->__toString()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testSetCookie(): void |
85
|
|
|
{ |
86
|
|
|
$key = $this->app->get(EncrypterFactory::class)->generateKey(); |
87
|
|
|
$this->app = $this->makeApp(['ENCRYPTER_KEY' => $key]); |
88
|
|
|
$this->http = $this->app->get(Http::class); |
89
|
|
|
|
90
|
|
|
$this->http->setHandler(function () { |
91
|
|
|
$this->cookies()->set('a', 'value'); |
92
|
|
|
|
93
|
|
|
return 'ok'; |
94
|
|
|
}); |
95
|
|
|
|
96
|
|
|
$result = $this->get('/'); |
97
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
98
|
|
|
$this->assertSame('ok', $result->getBody()->__toString()); |
99
|
|
|
|
100
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
101
|
|
|
|
102
|
|
|
$this->assertSame( |
103
|
|
|
'value', |
104
|
|
|
$this->app->get(EncrypterInterface::class)->decrypt($cookies['a']) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testSetCookie2(): void |
109
|
|
|
{ |
110
|
|
|
$key = $this->app->get(EncrypterFactory::class)->generateKey(); |
111
|
|
|
$this->app = $this->makeApp(['ENCRYPTER_KEY' => $key]); |
112
|
|
|
$this->http = $this->app->get(Http::class); |
113
|
|
|
|
114
|
|
|
$this->http->setHandler(function () { |
115
|
|
|
$this->cookies()->schedule(Cookie::create('a', 'value')); |
116
|
|
|
|
117
|
|
|
return 'ok'; |
118
|
|
|
}); |
119
|
|
|
|
120
|
|
|
$result = $this->get('/'); |
121
|
|
|
$this->assertSame(200, $result->getStatusCode()); |
122
|
|
|
$this->assertSame('ok', $result->getBody()->__toString()); |
123
|
|
|
|
124
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
125
|
|
|
|
126
|
|
|
$this->assertSame( |
127
|
|
|
'value', |
128
|
|
|
$this->app->get(EncrypterInterface::class)->decrypt($cookies['a']) |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private function cookies(): CookieManager |
133
|
|
|
{ |
134
|
|
|
return $this->app->get(CookieManager::class); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
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.