/
TransportTest.php
180 lines (148 loc) · 4.97 KB
/
TransportTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace Aura\Http;
use Aura\Http\Cookie\Collection as Cookies;
use Aura\Http\Cookie\Factory as CookieFactory;
use Aura\Http\Header\Collection as Headers;
use Aura\Http\Header\Factory as HeaderFactory;
use Aura\Http\Message\Request;
use Aura\Http\Message\Response;
use Aura\Http\MockAdapter as MockAdapter;
use Aura\Http\MockPhpFunc as MockPhpFunc;
use Aura\Http\Transport\Options;
use org\bovigo\vfs\vfsStream;
class TransportTest extends \PHPUnit_Framework_TestCase
{
protected $phpfunc;
protected $adapter;
protected $transport;
public function setUp()
{
$this->phpfunc = new MockPhpFunc;
$this->options = new Options;
$this->adapter = new MockAdapter;
$this->transport = new Transport(
$this->phpfunc,
$this->options,
$this->adapter
);
}
protected function newResponse()
{
$response = new Response(
new Headers(new HeaderFactory),
new Cookies(new CookieFactory)
);
$response->headers->setAll([
'Foo' => 'hello world',
'Bar' => 'hello world 2',
]);
$response->cookies->set('foo', [
'value' => 'bar',
]);
$response->setContent('Hola Mundo!');
return $response;
}
protected function newRequest()
{
$request = new Request(
new Headers(new HeaderFactory),
new Cookies(new CookieFactory)
);
return $request;
}
public function test__get()
{
$this->assertSame($this->phpfunc, $this->transport->phpfunc);
$this->assertSame($this->options, $this->transport->options);
$this->assertSame($this->adapter, $this->transport->adapter);
}
public function testSendResponse()
{
$response = $this->newResponse();
$this->assertFalse($this->transport->isCgi());
$this->transport->sendResponse($response);
$expect = 'Hola Mundo!';
$actual = $this->phpfunc->content;
$this->assertSame($expect, $actual);
$actual = $this->phpfunc->headers;
$expect = [
0 => 'HTTP/1.1 200 OK',
1 => 'Foo: hello world',
2 => 'Bar: hello world 2',
3 => 'Connection: close',
];
$this->assertSame($expect, $actual);
$actual = $this->phpfunc->cookies;
$expect = [
0 => [
'name' => 'foo',
'value' => 'bar',
'expire' => 0,
'path' => null,
'domain' => null,
'secure' => false,
'httponly' => true,
],
];
$this->assertSame($expect, $actual);
}
public function testSendResponse_cgi()
{
$response = $this->newResponse();
$this->transport->setCgi(true);
$this->transport->sendResponse($response);
$expect = 'Hola Mundo!';
$actual = $this->phpfunc->content;
$this->assertSame($expect, $actual);
$actual = $this->phpfunc->headers;
$expect = [
0 => 'Status: 200 OK',
1 => 'Foo: hello world',
2 => 'Bar: hello world 2',
3 => 'Connection: close',
];
$this->assertSame($expect, $actual);
$actual = $this->phpfunc->cookies;
$expect = [
0 => [
'name' => 'foo',
'value' => 'bar',
'expire' => 0,
'path' => null,
'domain' => null,
'secure' => false,
'httponly' => true,
],
];
$this->assertSame($expect, $actual);
}
public function testSendResponse_headersAlreadySent()
{
$this->phpfunc->headers_sent = true;
$response = $this->newResponse();
$this->setExpectedException('Aura\Http\Exception\HeadersSent');
$this->transport->sendResponse($response);
}
public function testSendResponse_resource()
{
$response = $this->newResponse();
$structure = array('resource.txt' => 'Hello Resource');
$root = vfsStream::setup('root', null, $structure);
$file = vfsStream::url('root/resource.txt');
$fh = fopen($file, 'r');
$response->setContent($fh);
$this->transport->sendResponse($response);
fclose($fh);
$expect = file_get_contents($file);
$actual = $this->phpfunc->content;
$this->assertSame($expect, $actual);
}
public function testSendRequest()
{
$request = $this->newRequest();
$request->setUrl('http://example.com');
$this->transport->sendRequest($request);
$this->assertSame($request, $this->adapter->request);
$this->assertSame($this->options, $this->adapter->options);
}
}