ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
SetCookiesTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Dflydev\FigCookies;
4 
6 {
7  const INTERFACE_PSR_HTTP_MESSAGE_RESPONSE = 'Psr\Http\Message\ResponseInterface';
8 
16  public function it_creates_from_response($setCookieStrings, array $expectedSetCookies)
17  {
18  $response = $this->prophesize(static::INTERFACE_PSR_HTTP_MESSAGE_RESPONSE);
19  $response->getHeader(SetCookies::SET_COOKIE_HEADER)->willReturn($setCookieStrings);
20 
21  $setCookies = SetCookies::fromResponse($response->reveal());
22 
23  $this->assertEquals($expectedSetCookies, $setCookies->getAll());
24  }
25 
33  public function it_creates_from_set_cookie_strings($setCookieStrings, array $expectedSetCookies)
34  {
35  $setCookies = SetCookies::fromSetCookieStrings($setCookieStrings);
36 
37  $this->assertEquals($expectedSetCookies, $setCookies->getAll());
38  }
39 
47  public function it_knows_which_set_cookies_are_available($setCookieStrings, array $expectedSetCookies)
48  {
49  $setCookies = SetCookies::fromSetCookieStrings($setCookieStrings);
50 
51  foreach ($expectedSetCookies as $expectedSetCookie) {
52  $this->assertTrue($setCookies->has($expectedSetCookie->getName()));
53  }
54 
55  $this->assertFalse($setCookies->has('i know this cookie does not exist'));
56  }
57 
62  public function it_gets_set_cookie_by_name($setCookieStrings, $setCookieName, SetCookie $expectedSetCookie = null)
63  {
64  $setCookies = SetCookies::fromSetCookieStrings($setCookieStrings);
65 
66  $this->assertEquals($expectedSetCookie, $setCookies->get($setCookieName));
67  }
68 
73  {
74  $setCookies = SetCookies::fromSetCookieStrings(['theme=light', 'sessionToken=abc123', 'hello=world'])
75  ->with(SetCookie::create('theme', 'blue'))
76  ->without('sessionToken')
77  ->with(SetCookie::create('who', 'me'))
78  ;
79 
80  $originalResponse = new FigCookieTestingResponse();
81  $response = $setCookies->renderIntoSetCookieHeader($originalResponse);
82 
83  $this->assertNotEquals($response, $originalResponse);
84 
85  $this->assertEquals(
86  ['theme=blue', 'hello=world', 'who=me'],
88  );
89  }
90 
95  {
96  //
97  // Example of naive cookie encryption middleware.
98  //
99  // Shows how to access and manipulate cookies using PSR-7 Response
100  // instances from outside the Response object itself.
101  //
102 
103  // Simulate a response coming in with several cookies.
106  ->withAddedHeader(SetCookies::SET_COOKIE_HEADER, 'sessionToken=ENCRYPTED')
107  ->withAddedHeader(SetCookies::SET_COOKIE_HEADER, 'hello=world')
108  ;
109 
110  // Get our set cookies from the response.
111  $setCookies = SetCookies::fromResponse($response);
112 
113  // Ask for the encrypted session token.
114  $decryptedSessionToken = $setCookies->get('sessionToken');
115 
116  // Get the encrypted value from the cookie and decrypt it.
117  $decryptedValue = $decryptedSessionToken->getValue();
118  $encryptedValue = str_rot13($decryptedValue);
119 
120  // Create a new set cookie with the encrypted value.
121  $encryptedSessionToken = $decryptedSessionToken->withValue($encryptedValue);
122 
123  // Include our encrypted session token with the rest of our cookies.
124  $setCookies = $setCookies->with($encryptedSessionToken);
125 
126  // Render our cookies, along with the newly decrypted session token, into a response.
127  $response = $setCookies->renderIntoSetCookieHeader($response);
128 
129  // From this point on, any response based on this one can get the encrypted version
130  // of the session token.
131  $this->assertEquals(
132  ['theme=light', 'sessionToken=RAPELCGRQ', 'hello=world'],
134  );
135  }
136 
138  {
139  return [
140  [
141  [],
142  [],
143  ],
144  [
145  [
146  'someCookie=',
147  ],
148  [
149  SetCookie::create('someCookie'),
150  ],
151  ],
152  [
153  [
154  'someCookie=someValue',
155  'LSID=DQAAAK%2FEaem_vYg; Path=/accounts; Expires=Wed, 13 Jan 2021 22:23:01 GMT; Secure; HttpOnly',
156  ],
157  [
158  SetCookie::create('someCookie', 'someValue'),
159  SetCookie::create('LSID')
160  ->withValue('DQAAAK/Eaem_vYg')
161  ->withPath('/accounts')
162  ->withExpires('Wed, 13 Jan 2021 22:23:01 GMT')
163  ->withSecure(true)
164  ->withHttpOnly(true),
165  ],
166  ],
167  [
168  [
169  'a=AAA',
170  'b=BBB',
171  'c=CCC',
172  ],
173  [
174  SetCookie::create('a', 'AAA'),
175  SetCookie::create('b', 'BBB'),
176  SetCookie::create('c', 'CCC'),
177  ],
178  ],
179  ];
180  }
181 
183  {
184  return [
185  [
186  [
187  'a=AAA',
188  'b=BBB',
189  'c=CCC',
190  ],
191  'b',
192  SetCookie::create('b', 'BBB'),
193  ],
194  [
195  [
196  'a=AAA',
197  'b=BBB',
198  'c=CCC',
199  'LSID=DQAAAK%2FEaem_vYg; Path=/accounts; Expires=Wed, 13 Jan 2021 22:23:01 GMT; Secure; HttpOnly',
200  ],
201  'LSID',
202  SetCookie::create('LSID')
203  ->withValue('DQAAAK/Eaem_vYg')
204  ->withPath('/accounts')
205  ->withExpires('Wed, 13 Jan 2021 22:23:01 GMT')
206  ->withSecure(true)
207  ->withHttpOnly(true),
208  ],
209  [
210  [
211  'a=AAA',
212  'b=BBB',
213  'c=CCC',
214  ],
215  'LSID',
216  null,
217  ],
218  ];
219  }
220 }
it_knows_which_set_cookies_are_available($setCookieStrings, array $expectedSetCookies)
it_creates_from_set_cookie_strings($setCookieStrings, array $expectedSetCookies)
static fromResponse(ResponseInterface $response)
Create SetCookies from a Response.
Definition: SetCookies.php:124
static fromSetCookieStrings($setCookieStrings)
Create SetCookies from a collection of SetCookie header value strings.
Definition: SetCookies.php:111
static create($name, $value=null)
Definition: SetCookie.php:173
const SET_COOKIE_HEADER
The name of the Set-Cookie header.
Definition: SetCookies.php:12
it_gets_set_cookie_by_name($setCookieStrings, $setCookieName, SetCookie $expectedSetCookie=null)
it_creates_from_response($setCookieStrings, array $expectedSetCookies)
$response