ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Dflydev\FigCookies\CookiesTest Class Reference
+ Inheritance diagram for Dflydev\FigCookies\CookiesTest:
+ Collaboration diagram for Dflydev\FigCookies\CookiesTest:

Public Member Functions

 it_creates_from_request ($cookieString, array $expectedCookies)
 
 it_creates_from_cookie_string ($cookieString, array $expectedCookies)
 
 it_knows_which_cookies_are_available ($cookieString, array $expectedCookies)
 
 it_gets_cookie_by_name ($cookieString, $cookieName, Cookie $expectedCookie)
 
 it_sets_overrides_and_removes_cookie ()
 
 it_renders_new_cookies_into_empty_cookie_header ()
 
 it_renders_added_and_removed_cookies_header ()
 
 it_gets_cookie_value_from_request ()
 
 it_gets_and_updates_cookie_value_on_request ()
 
 provideCookieStringAndExpectedCookiesData ()
 
 provideGetsCookieByNameData ()
 

Data Fields

const INTERFACE_PSR_HTTP_MESSAGE_REQUEST = 'Psr\Http\Message\RequestInterface'
 

Detailed Description

Definition at line 5 of file CookiesTest.php.

Member Function Documentation

◆ it_creates_from_cookie_string()

Dflydev\FigCookies\CookiesTest::it_creates_from_cookie_string (   $cookieString,
array  $expectedCookies 
)
Parameters
string[]$cookieString
Cookie[]$expectedCookies
Test:
provideCookieStringAndExpectedCookiesData

Definition at line 33 of file CookiesTest.php.

References Dflydev\FigCookies\Cookies\fromCookieString().

34  {
35  $cookies = Cookies::fromCookieString($cookieString);
36 
37  $this->assertEquals($expectedCookies, $cookies->getAll());
38  }
static fromCookieString($string)
Create Cookies from a Cookie header value string.
Definition: Cookies.php:110
+ Here is the call graph for this function:

◆ it_creates_from_request()

Dflydev\FigCookies\CookiesTest::it_creates_from_request (   $cookieString,
array  $expectedCookies 
)
Parameters
string[]$cookieString
Cookie[]$expectedCookies
Test:
provideCookieStringAndExpectedCookiesData

Definition at line 16 of file CookiesTest.php.

References Dflydev\FigCookies\Cookies\COOKIE_HEADER, and Dflydev\FigCookies\Cookies\fromRequest().

17  {
18  $request = $this->prophesize(static::INTERFACE_PSR_HTTP_MESSAGE_REQUEST);
19  $request->getHeaderLine(Cookies::COOKIE_HEADER)->willReturn($cookieString);
20 
21  $cookies = Cookies::fromRequest($request->reveal());
22 
23  $this->assertEquals($expectedCookies, $cookies->getAll());
24  }
const COOKIE_HEADER
The name of the Cookie header.
Definition: Cookies.php:12
static fromRequest(RequestInterface $request)
Create Cookies from a Request.
Definition: Cookies.php:121
+ Here is the call graph for this function:

◆ it_gets_and_updates_cookie_value_on_request()

Dflydev\FigCookies\CookiesTest::it_gets_and_updates_cookie_value_on_request ( )
Test:

Definition at line 148 of file CookiesTest.php.

References Dflydev\FigCookies\Cookies\COOKIE_HEADER, Dflydev\FigCookies\Cookies\fromRequest(), and Dflydev\FigCookies\withHeader().

149  {
150  //
151  // Example of naive cookie decryption middleware.
152  //
153  // Shows how to access and manipulate cookies using PSR-7 Request
154  // instances from outside the Request object itself.
155  //
156 
157  // Simulate a request coming in with several cookies.
158  $request = (new FigCookieTestingRequest())
159  ->withHeader(Cookies::COOKIE_HEADER, 'theme=light; sessionToken=RAPELCGRQ; hello=world')
160  ;
161 
162  // Get our cookies from the request.
163  $cookies = Cookies::fromRequest($request);
164 
165  // Ask for the encrypted session token.
166  $encryptedSessionToken = $cookies->get('sessionToken');
167 
168  // Get the encrypted value from the cookie and decrypt it.
169  $encryptedValue = $encryptedSessionToken->getValue();
170  $decryptedValue = str_rot13($encryptedValue);
171 
172  // Create a new cookie with the decrypted value.
173  $decryptedSessionToken = $encryptedSessionToken->withValue($decryptedValue);
174 
175  // Include our decrypted session token with the rest of our cookies.
176  $cookies = $cookies->with($decryptedSessionToken);
177 
178  // Render our cookies, along with the newly decrypted session token, into a request.
179  $request = $cookies->renderIntoCookieHeader($request);
180 
181  // From this point on, any request based on this one can get the plaintext version
182  // of the session token.
183  $this->assertEquals(
184  'theme=light; sessionToken=ENCRYPTED; hello=world',
185  $request->getHeaderLine(Cookies::COOKIE_HEADER)
186  );
187  }
const COOKIE_HEADER
The name of the Cookie header.
Definition: Cookies.php:12
static fromRequest(RequestInterface $request)
Create Cookies from a Request.
Definition: Cookies.php:121
+ Here is the call graph for this function:

◆ it_gets_cookie_by_name()

Dflydev\FigCookies\CookiesTest::it_gets_cookie_by_name (   $cookieString,
  $cookieName,
Cookie  $expectedCookie 
)
Test:
provideGetsCookieByNameData

Definition at line 62 of file CookiesTest.php.

References $cookieName, and Dflydev\FigCookies\Cookies\fromCookieString().

63  {
64  $cookies = Cookies::fromCookieString($cookieString);
65 
66  $this->assertEquals($expectedCookie, $cookies->get($cookieName));
67  }
static fromCookieString($string)
Create Cookies from a Cookie header value string.
Definition: Cookies.php:110
$cookieName
+ Here is the call graph for this function:

◆ it_gets_cookie_value_from_request()

Dflydev\FigCookies\CookiesTest::it_gets_cookie_value_from_request ( )
Test:

Definition at line 129 of file CookiesTest.php.

References Dflydev\FigCookies\Cookies\COOKIE_HEADER, Dflydev\FigCookies\Cookies\fromRequest(), and Dflydev\FigCookies\withHeader().

130  {
131  //
132  // Example of accessing a cookie value.
133  //
134 
135  // Simulate a request coming in with several cookies.
136  $request = (new FigCookieTestingRequest())
137  ->withHeader(Cookies::COOKIE_HEADER, 'theme=light; sessionToken=RAPELCGRQ; hello=world')
138  ;
139 
140  $theme = Cookies::fromRequest($request)->get('theme')->getValue();
141 
142  $this->assertEquals('light', $theme);
143  }
const COOKIE_HEADER
The name of the Cookie header.
Definition: Cookies.php:12
static fromRequest(RequestInterface $request)
Create Cookies from a Request.
Definition: Cookies.php:121
+ Here is the call graph for this function:

◆ it_knows_which_cookies_are_available()

Dflydev\FigCookies\CookiesTest::it_knows_which_cookies_are_available (   $cookieString,
array  $expectedCookies 
)
Parameters
string[]$cookieString
Cookie[]$expectedCookies
Test:
provideCookieStringAndExpectedCookiesData

Definition at line 47 of file CookiesTest.php.

References Dflydev\FigCookies\Cookies\fromCookieString().

48  {
49  $cookies = Cookies::fromCookieString($cookieString);
50 
51  foreach ($expectedCookies as $expectedCookie) {
52  $this->assertTrue($cookies->has($expectedCookie->getName()));
53  }
54 
55  $this->assertFalse($cookies->has('i know this cookie does not exist'));
56  }
static fromCookieString($string)
Create Cookies from a Cookie header value string.
Definition: Cookies.php:110
+ Here is the call graph for this function:

◆ it_renders_added_and_removed_cookies_header()

Dflydev\FigCookies\CookiesTest::it_renders_added_and_removed_cookies_header ( )
Test:

Definition at line 110 of file CookiesTest.php.

References Dflydev\FigCookies\Cookies\COOKIE_HEADER, Dflydev\FigCookies\Cookie\create(), and Dflydev\FigCookies\Cookies\fromCookieString().

111  {
112  $cookies = Cookies::fromCookieString('theme=light; sessionToken=abc123; hello=world')
113  ->with(Cookie::create('theme', 'blue'))
114  ->without('sessionToken')
115  ->with(Cookie::create('who', 'me'))
116  ;
117 
118  $originalRequest = new FigCookieTestingRequest();
119  $request = $cookies->renderIntoCookieHeader($originalRequest);
120 
121  $this->assertNotEquals($request, $originalRequest);
122 
123  $this->assertEquals('theme=blue; hello=world; who=me', $request->getHeaderLine(Cookies::COOKIE_HEADER));
124  }
static fromCookieString($string)
Create Cookies from a Cookie header value string.
Definition: Cookies.php:110
static create($name, $value=null)
Create a Cookie.
Definition: Cookie.php:73
const COOKIE_HEADER
The name of the Cookie header.
Definition: Cookies.php:12
+ Here is the call graph for this function:

◆ it_renders_new_cookies_into_empty_cookie_header()

Dflydev\FigCookies\CookiesTest::it_renders_new_cookies_into_empty_cookie_header ( )
Test:

Definition at line 92 of file CookiesTest.php.

References Dflydev\FigCookies\Cookies\COOKIE_HEADER, and Dflydev\FigCookies\Cookie\create().

93  {
94  $cookies = (new Cookies())
95  ->with(Cookie::create('theme', 'light'))
96  ->with(Cookie::create('sessionToken', 'abc123'))
97  ;
98 
99  $originalRequest = new FigCookieTestingRequest();
100  $request = $cookies->renderIntoCookieHeader($originalRequest);
101 
102  $this->assertNotEquals($request, $originalRequest);
103 
104  $this->assertEquals('theme=light; sessionToken=abc123', $request->getHeaderLine(Cookies::COOKIE_HEADER));
105  }
static create($name, $value=null)
Create a Cookie.
Definition: Cookie.php:73
const COOKIE_HEADER
The name of the Cookie header.
Definition: Cookies.php:12
+ Here is the call graph for this function:

◆ it_sets_overrides_and_removes_cookie()

Dflydev\FigCookies\CookiesTest::it_sets_overrides_and_removes_cookie ( )
Test:

Definition at line 72 of file CookiesTest.php.

References Dflydev\FigCookies\Cookie\create().

73  {
74  $cookies = new Cookies();
75 
76  $cookies = $cookies->with(Cookie::create('theme', 'blue'));
77 
78  $this->assertEquals('blue', $cookies->get('theme')->getValue());
79 
80  $cookies = $cookies->with(Cookie::create('theme', 'red'));
81 
82  $this->assertEquals('red', $cookies->get('theme')->getValue());
83 
84  $cookies = $cookies->without('theme');
85 
86  $this->assertFalse($cookies->has('theme'));
87  }
static create($name, $value=null)
Create a Cookie.
Definition: Cookie.php:73
+ Here is the call graph for this function:

◆ provideCookieStringAndExpectedCookiesData()

Dflydev\FigCookies\CookiesTest::provideCookieStringAndExpectedCookiesData ( )

Definition at line 189 of file CookiesTest.php.

References Dflydev\FigCookies\Cookie\create().

190  {
191  return [
192  [
193  '',
194  []
195  ],
196  [
197  'theme=light',
198  [
199  Cookie::create('theme', 'light'),
200  ]
201  ],
202  [
203  'theme=light; sessionToken=abc123',
204  [
205  Cookie::create('theme', 'light'),
206  Cookie::create('sessionToken', 'abc123'),
207  ]
208  ]
209  ];
210  }
static create($name, $value=null)
Create a Cookie.
Definition: Cookie.php:73
+ Here is the call graph for this function:

◆ provideGetsCookieByNameData()

Dflydev\FigCookies\CookiesTest::provideGetsCookieByNameData ( )

Definition at line 212 of file CookiesTest.php.

References Dflydev\FigCookies\Cookie\create().

213  {
214  return [
215  ['theme=light', 'theme', Cookie::create('theme', 'light')],
216  ['theme=', 'theme', Cookie::create('theme')],
217  ['hello=world; theme=light; sessionToken=abc123', 'theme', Cookie::create('theme', 'light')],
218  ['hello=world; theme=; sessionToken=abc123', 'theme', Cookie::create('theme')],
219  ];
220  }
static create($name, $value=null)
Create a Cookie.
Definition: Cookie.php:73
+ Here is the call graph for this function:

Field Documentation

◆ INTERFACE_PSR_HTTP_MESSAGE_REQUEST

const Dflydev\FigCookies\CookiesTest::INTERFACE_PSR_HTTP_MESSAGE_REQUEST = 'Psr\Http\Message\RequestInterface'

Definition at line 7 of file CookiesTest.php.


The documentation for this class was generated from the following file: