Skip to content

Commit 24a6bb5

Browse files
committed
tests
1 parent 60a058d commit 24a6bb5

File tree

6 files changed

+224
-10
lines changed

6 files changed

+224
-10
lines changed

src/PHPixie/HTTP/Context.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace PHPixie\HTTP;
4+
5+
class Context
6+
{
7+
protected $cookies;
8+
protected $session;
9+
10+
public function __construct($cookies, $session)
11+
{
12+
$this->cookies = $cookies;
13+
$this->session = $session;
14+
}
15+
16+
public function cookies()
17+
{
18+
return $this->cookies;
19+
}
20+
21+
public function session()
22+
{
23+
return $this->session;
24+
}
25+
}

src/PHPixie/HTTP/Context/Cookies/Update.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,31 @@ public function httpOnly()
5757
{
5858
return $this->httpOnly;
5959
}
60+
61+
public function asHeader()
62+
{
63+
$header = urlencode($this->name).'='.urlencode((string) $this->value);
64+
65+
if($this->domain !== null) {
66+
$header.= '; domain='.$this->domain;
67+
}
68+
69+
if($this->path !== null) {
70+
$header.= '; path='.$this->path;
71+
}
72+
73+
if($this->expires !== null) {
74+
$header.= '; expires=' . gmdate('D, d-M-Y H:i:s e', $this->expires);
75+
}
76+
77+
if($this->secure) {
78+
$header.= '; secure';
79+
}
80+
81+
if($this->httpOnly) {
82+
$header.= '; HttpOnly';
83+
}
84+
85+
return $header;
86+
}
6087
}
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,46 @@
22

33
namespace PHPixie\HTTP;
44

5-
class Writer
5+
class Output
66
{
7-
public function a()
7+
public function response($response, $context = null)
88
{
99
$this->outputStatusHeader(
1010
$response->statusCode(),
1111
$response->reasonPhrase()
1212
);
1313

14-
$this->outputHeaders($response->headers()->asArray());
15-
$this->outputCookies($context->cookies);
14+
$this->headers($response->headers()->asArray());
15+
if($context !== null) {
16+
$this->outputCookies($context->cookies);
17+
}
1618
$this->outputStream($response->body());
1719
}
1820

19-
public function sapi($response)
21+
public function responseMessage($response)
2022
{
2123
$this->outputStatusHeader(
2224
$response->getStatusCode(),
2325
$response->getReasonPhrase(),
2426
$response->getProtocolVersion()
2527
);
2628

27-
$this->outputHeaders($response->getHeaders());
29+
$this->headers($response->getHeaders());
2830
$this->outputStream($response->getBody());
2931
}
3032

31-
protected function outputHeaders($headers)
33+
protected function headers($headers)
3234
{
3335
foreach($headers as $name => $lines) {
3436
foreach($lines as $key => $line) {
35-
$this->outputHeader("$name: $line", $key === 0);
37+
$this->header("$name: $line", $key === 0);
3638
}
3739
}
3840
}
3941

40-
protected function outputHeader($header)
42+
protected function header($header, $replace = true)
4143
{
42-
header($header);
44+
header($header, $replace);
4345
}
4446

4547
protected function outputCookies($cookies)

src/PHPixie/HTTP/Responses/Response.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,46 @@ public function setStatus($code, $reasonPhrase = null)
4040
$this->statusCode = $code;
4141
$this->reasonPhrase = $reasonPhrase;
4242
}
43+
44+
public function asResponseMessage($context = null)
45+
{
46+
return $this->messages->response(
47+
'1.1',
48+
$this->mergeContextHeaders($context),
49+
$this->body,
50+
$this->statusCode,
51+
$this->reasonPhrase
52+
);
53+
}
54+
55+
protected function mergeContextHeaders($context)
56+
{
57+
$headers = $this->headers->asArray();
58+
59+
if($context === null ) {
60+
return $headers;
61+
}
62+
63+
$cookieUpdates = $context->cookies->getUpdates();
64+
if(empty($cookieUpdates)) {
65+
return $headers;
66+
}
67+
68+
$cookieHeaders = array();
69+
foreach($cookieUpdates as $update) {
70+
$cookieHeaders[] = $update->asHeader();
71+
}
72+
73+
foreach($headers as $name => $value) {
74+
if(strtolower($name) === 'set-cookie') {
75+
foreach($cookieHeaders as $header) {
76+
$headers[$name][] = $header;
77+
}
78+
return $headers;
79+
}
80+
}
81+
82+
$headers['Set-Cookie'] = $cookieHeaders;
83+
return $headers;
84+
}
4385
}

tests/PHPixie/Tests/HTTP/Context/Cookies/UpdateTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,29 @@ public function testMethods()
5151
$this->methodsTest();
5252
}
5353

54+
/**
55+
* @covers ::asHeader
56+
* @covers ::<protected>
57+
*/
58+
public function testAsHeader()
59+
{
60+
$this->assertSame(
61+
'pixie=Trixie; domain=fairies; path=/fairy; expires=Thu, 01-Jan-1970 00:00:05 UTC; secure; HttpOnly',
62+
$this->update->asHeader()
63+
);
64+
65+
$this->update = new \PHPixie\HTTP\Context\Cookies\Update(
66+
'Pixie Fairy',
67+
'Stella Blum',
68+
null,
69+
null
70+
);
71+
$this->assertSame(
72+
'Pixie+Fairy=Stella+Blum',
73+
$this->update->asHeader()
74+
);
75+
}
76+
5477
protected function methodsTest()
5578
{
5679
$methods = array(
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace PHPixie\Tests\HTTP;
4+
5+
/**
6+
* @coversDefaultClass PHPixie\HTTP\Output
7+
*/
8+
class OutputTest extends \PHPixie\Test\Testcase
9+
{
10+
protected $output;
11+
12+
public function setUp()
13+
{
14+
$this->output = $this->getMock(
15+
'\PHPixie\HTTP\Output',
16+
array(),
17+
array(
18+
'header',
19+
'setCookie',
20+
'fpassthru',
21+
'echo'
22+
)
23+
);
24+
}
25+
26+
/**
27+
* @covers ::response
28+
* @covers ::<protected>
29+
*/
30+
public function testResponse()
31+
{
32+
$headers = array(
33+
'pixie' => array(
34+
'Trixie',
35+
'Blum'
36+
),
37+
'fairy' => array(
38+
'Stella'
39+
)
40+
);
41+
$body = $this->getStream();
42+
43+
$response = $this->response(
44+
$headers,
45+
$body,
46+
200,
47+
'OK'
48+
);
49+
}
50+
51+
protected function response($headerArray, $body, $statusCode, $reasonPhrase)
52+
{
53+
$headers = $this->getHeaders();
54+
$this->method($headers, 'headerArray', $headerArray, array());
55+
56+
$response = $this->quickMock('\PHPixie\HTTP\Responses\Response');
57+
$methods = array(
58+
'headers' => $headers,
59+
'body' => $body,
60+
'statusCode' => $statusCode,
61+
'reasonPhrase' => $reasonPhrase
62+
);
63+
foreach($methods as $method => $value) {
64+
$this->method($response, $method, $value, array());
65+
}
66+
67+
return $response;
68+
}
69+
70+
protected function getHeaders()
71+
{
72+
return $this->quickMock('\PHPixie\HTTP\Data\Headers');
73+
}
74+
75+
protected function getStream()
76+
{
77+
return $this->quickMock('\Psr\Http\Message\StreamInterface');
78+
}
79+
80+
protected function getContext()
81+
{
82+
return $this->quickMock('\PHPixie\HTTP\Context');
83+
}
84+
85+
protected function getCookies()
86+
{
87+
return $this->quickMock('\PHPixie\HTTP\Context\Cookies');
88+
}
89+
90+
protected function getCookieUpdate()
91+
{
92+
return $this->quickMock('\PHPixie\HTTP\Context\Cookies\Update');
93+
}
94+
95+
}

0 commit comments

Comments
 (0)