Skip to content

Commit 18cb2cc

Browse files
Refactored testing suite classes to make testing of ConnectionHandler cleaner
1 parent 043aad6 commit 18cb2cc

File tree

2 files changed

+109
-132
lines changed

2 files changed

+109
-132
lines changed

test/Client/ConnectionWrapper.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,31 @@ public function __construct($stream)
2525
$this->stream = $stream;
2626
}
2727

28+
/**
29+
* Read a request from the connection
30+
*
31+
* @param \PHPUnit_Framework_TestCase $testCase
32+
* @param string $requestId
33+
*
34+
* @return string
35+
*/
36+
public function readResponse(\PHPUnit_Framework_TestCase $testCase, $requestId)
37+
{
38+
$response = '';
39+
40+
do {
41+
$record = $this->readRecord($testCase);
42+
43+
$testCase->assertEquals($requestId, $record['requestId']);
44+
45+
if (DaemonInterface::FCGI_STDOUT === $record['type']) {
46+
$response .= $record['contentData'];
47+
}
48+
} while (DaemonInterface::FCGI_END_REQUEST !== $record['type']);
49+
50+
return $response;
51+
}
52+
2853
/**
2954
* Read a record from the connection.
3055
*
@@ -62,6 +87,32 @@ public function readRecord(\PHPUnit_Framework_TestCase $testCase)
6287
return $record;
6388
}
6489

90+
/**
91+
* Write a request to the stream.
92+
*
93+
* @param string $requestId FastCGI request ID
94+
* @param array $params FastCGI environment variables
95+
* @param string $body FastCGI stdin resource stream
96+
*/
97+
public function writeRequest($requestId, array $params, $body)
98+
{
99+
$this->writeBeginRequestRecord($requestId, DaemonInterface::FCGI_RESPONDER, 0);
100+
101+
foreach ($params as $name => $value) {
102+
$this->writeParamsRecord($requestId, $name, $value);
103+
}
104+
105+
$this->writeParamsRecord($requestId);
106+
107+
$chunks = str_split($body, 65535);
108+
109+
foreach ($chunks as $chunk) {
110+
$this->writeStdinRecord($requestId, $chunk);
111+
}
112+
113+
$this->writeStdinRecord($requestId);
114+
}
115+
65116
/**
66117
* Write a record to the stream.
67118
*

test/ConnectionHandler/ConnectionHandlerTest.php

Lines changed: 58 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -78,161 +78,87 @@ protected function createTestingContext($callback = null)
7878
}
7979

8080
/**
81-
* Test that the daemon is correctly handling requests and HttpFoundation
82-
* responseswith large param records and message bodies.
81+
* Create test request data
82+
*
83+
* @return array
8384
*/
84-
public function testHttpFoundation()
85+
protected function createTestData()
8586
{
86-
// Set up body
87-
$body = str_repeat('X', 100000);
88-
89-
// Set up test request values
90-
$params = [
91-
str_repeat('A', 10) => str_repeat('b', 127),
92-
str_repeat('C', 128) => str_repeat('d', 256),
93-
str_repeat('E', 520) => str_repeat('f', 50),
87+
$testData = [
88+
'requestBody' => str_repeat('X', 100000),
89+
'requestParams' => [
90+
str_repeat('A', 10) => str_repeat('b', 127),
91+
str_repeat('C', 128) => str_repeat('d', 256),
92+
str_repeat('E', 520) => str_repeat('f', 50),
93+
],
94+
'responseStatusCode' => 201,
95+
'responseBody' => str_repeat('Y', 100000),
96+
'responseHeaders' => ['Header-1' => 'foo', 'Header-2' => 'bar']
9497
];
9598

96-
$response = new HttpFoundationResponse($body, 200, ['Header-1' => 'foo', 'Header-2' => 'bar']);
97-
98-
// Create test environment
99-
$context = $this->createTestingContext(function (RequestInterface $request) use ($params, $body, $response) {
100-
$this->assertEquals($params, $request->getParams());
101-
$this->assertEquals($body, stream_get_contents($request->getStdin()));
102-
103-
return $response;
104-
});
99+
$testData['responseBodyStream'] = $this->toStream($testData['responseBody']);
105100

106-
// Send request
107-
$requestId = 5;
101+
$testData['symfonyResponse'] = new HttpFoundationResponse($testData['responseBody'], $testData['responseStatusCode'], $testData['responseHeaders']);
102+
$testData['psr7Response'] = new Response($testData['responseBodyStream'], $testData['responseStatusCode'], $testData['responseHeaders']);
108103

109-
$context['clientWrapper']->writeBeginRequestRecord($requestId, DaemonInterface::FCGI_RESPONDER, 0);
104+
$testData['rawSymfonyResponse'] = "Status: 201\r\n";
105+
$testData['rawSymfonyResponse'] .= $testData['symfonyResponse']->headers . "\r\n";
106+
$testData['rawSymfonyResponse'] .= $testData['responseBody'];
110107

111-
foreach ($params as $name => $value) {
112-
$context['clientWrapper']->writeParamsRecord($requestId, $name, $value);
108+
$testData['rawPsr7Response'] = 'Status: 201 ' . $testData['psr7Response']->getReasonPhrase() . "\r\n";
109+
foreach ($testData['psr7Response']->getHeaders() as $name => $value) {
110+
$testData['rawPsr7Response'] .= $name . ': ' . implode(', ', $value) . "\r\n";
113111
}
112+
$testData['rawPsr7Response'] .= "\r\n" . $testData['responseBody'];
114113

115-
$context['clientWrapper']->writeParamsRecord($requestId);
116-
117-
$chunks = str_split($body, 65535);
118-
119-
foreach ($chunks as $chunk) {
120-
$context['clientWrapper']->writeStdinRecord($requestId, $chunk);
121-
}
122-
123-
$context['clientWrapper']->writeStdinRecord($requestId);
124-
125-
// Trigger Handler
126-
do {
127-
$context['handler']->ready();
128-
} while (!$context['connection']->isClosed());
129-
130-
// Receive response
131-
$rawResponse = '';
132-
133-
do {
134-
$record = $context['clientWrapper']->readRecord($this);
135-
136-
$this->assertEquals($requestId, $record['requestId']);
137-
138-
if (DaemonInterface::FCGI_STDOUT === $record['type']) {
139-
$rawResponse .= $record['contentData'];
140-
}
141-
} while (DaemonInterface::FCGI_END_REQUEST !== $record['type']);
142-
143-
$expectedRawResponse = "Status: 200\r\n";
144-
$expectedRawResponse .= $response->headers . "\r\n";
145-
$expectedRawResponse .= $body;
146-
147-
// Check response
148-
$this->assertEquals(strlen($expectedRawResponse), strlen($rawResponse));
149-
$this->assertEquals($expectedRawResponse, $rawResponse);
150-
151-
// Clean up
152-
fclose($context['sockets'][0]);
114+
return $testData;
153115
}
154116

155117
/**
156-
* Test that the daemon is correctly handling requests and PSR 7 responses
157-
* with large param records and message bodies.
118+
* Test that the daemon is correctly handling requests and PSR-7 and
119+
* HttpFoundation responses with large param records and message bodies.
158120
*/
159-
public function testPsr()
121+
public function testHandler()
160122
{
161-
// Set up body streams
162-
$body = str_repeat('X', 100000);
163-
$bodyStream = $this->toStream($body);
164-
165-
// Set up test request values
166-
$params = [
167-
str_repeat('A', 10) => str_repeat('b', 127),
168-
str_repeat('C', 128) => str_repeat('d', 256),
169-
str_repeat('E', 520) => str_repeat('f', 50),
170-
];
171-
172-
$response = new Response($bodyStream, 200, ['Header-1' => 'foo', 'Header-2' => 'bar']);
123+
$testData = $this->createTestData();
173124

174125
// Create test environment
175-
$context = $this->createTestingContext(function (RequestInterface $request) use ($params, $body, $response) {
176-
$stdin = $request->getStdin();
177-
178-
$this->assertEquals($params, $request->getParams());
179-
$this->assertEquals($body, stream_get_contents($stdin));
180-
181-
return $response;
182-
});
183-
184-
// Send request
185-
$requestId = 5;
186-
187-
$context['clientWrapper']->writeBeginRequestRecord($requestId, DaemonInterface::FCGI_RESPONDER, 0);
188-
189-
foreach ($params as $name => $value) {
190-
$context['clientWrapper']->writeParamsRecord($requestId, $name, $value);
191-
}
192-
193-
$context['clientWrapper']->writeParamsRecord($requestId);
194-
195-
$chunks = str_split($body, 65535);
196-
197-
foreach ($chunks as $chunk) {
198-
$context['clientWrapper']->writeStdinRecord($requestId, $chunk);
199-
}
200-
201-
$context['clientWrapper']->writeStdinRecord($requestId);
202-
203-
// Trigger Handler
204-
do {
205-
$context['handler']->ready();
206-
} while (!$context['connection']->isClosed());
126+
$kernelGenerator = function ($response) use ($testData) {
127+
return function (RequestInterface $request) use ($response, $testData) {
128+
$this->assertEquals($testData['requestParams'], $request->getParams());
129+
$this->assertEquals($testData['requestBody'], stream_get_contents($request->getStdin()));
130+
131+
return $response;
132+
};
133+
};
134+
135+
$scenarios = [
136+
['responseKey' => 'symfonyResponse', 'rawResponseKey' => 'rawSymfonyResponse'],
137+
['responseKey' => 'psr7Response', 'rawResponseKey' => 'rawPsr7Response']
138+
];
207139

208-
// Receive response
209-
$rawResponse = '';
140+
foreach ($scenarios as $scenario) {
141+
$kernel = $kernelGenerator($testData[$scenario['responseKey']]);
142+
$context = $this->createTestingContext($kernel);
143+
144+
$requestId = 1;
210145

211-
do {
212-
$record = $context['clientWrapper']->readRecord($this);
146+
$context['clientWrapper']->writeRequest($requestId, $testData['requestParams'], $testData['requestBody']);
213147

214-
$this->assertEquals($requestId, $record['requestId']);
148+
do {
149+
$context['handler']->ready();
150+
} while (!$context['connection']->isClosed());
215151

216-
if (DaemonInterface::FCGI_STDOUT === $record['type']) {
217-
$rawResponse .= $record['contentData'];
218-
}
219-
} while (DaemonInterface::FCGI_END_REQUEST !== $record['type']);
152+
$rawResponse = $context['clientWrapper']->readResponse($this, $requestId);
153+
$expectedRawResponse = $testData[$scenario['rawResponseKey']];
220154

221-
$expectedRawResponse = 'Status: '.$response->getStatusCode().' '.$response->getReasonPhrase()."\r\n";
155+
// Check response
156+
$this->assertEquals(strlen($expectedRawResponse), strlen($rawResponse));
157+
$this->assertEquals($expectedRawResponse, $rawResponse);
222158

223-
foreach ($response->getHeaders() as $name => $values) {
224-
$expectedRawResponse .= $name.': '.implode(', ', $values)."\r\n";
159+
// Clean up
160+
fclose($context['sockets'][0]);
225161
}
226-
227-
$expectedRawResponse .= "\r\n".$body;
228-
229-
// Check response
230-
$this->assertEquals(strlen($expectedRawResponse), strlen($rawResponse));
231-
$this->assertEquals($expectedRawResponse, $rawResponse);
232-
233-
// Clean up
234-
fclose($bodyStream);
235-
fclose($context['sockets'][0]);
236162
}
237163

238164
/**

0 commit comments

Comments
 (0)