Skip to content

Commit fcc9239

Browse files
Refactored to create Mock directory
1 parent 6ea1fc9 commit fcc9239

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

test/ConnectionHandler/ConnectionHandlerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use PHPFastCGI\FastCGIDaemon\ConnectionHandler\ConnectionHandler;
77
use PHPFastCGI\FastCGIDaemon\DaemonInterface;
88
use PHPFastCGI\Test\FastCGIDaemon\Client\ConnectionWrapper;
9-
use PHPFastCGI\Test\FastCGIDaemon\KernelMock;
9+
use PHPFastCGI\Test\FastCGIDaemon\Mock\KernelMock;
1010
use Zend\Diactoros\ServerRequest;
1111
use Zend\Diactoros\Response;
1212

test/Mock/KernelMock.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace PHPFastCGI\Test\FastCGIDaemon\Mock;
4+
5+
use PHPFastCGI\FastCGIDaemon\KernelInterface;
6+
use Psr\Http\Message\ResponseInterface;
7+
use Psr\Http\Message\ServerRequestInterface;
8+
9+
/**
10+
* Helper mock of a Kernel that can be used to check that the expected request
11+
* is received and then provide a predetermined response.
12+
*/
13+
class KernelMock implements KernelInterface
14+
{
15+
/**
16+
* @var \PHPUnit_Framework_TestCase
17+
*/
18+
protected $testCase;
19+
20+
/**
21+
* @var ServerRequestInterface
22+
*/
23+
protected $expectedRequest;
24+
25+
/**
26+
* @var ResponseInterface
27+
*/
28+
protected $response;
29+
30+
/**
31+
* Constructor.
32+
*
33+
* @param \PHPUnit_Framework_TestCase $testCase
34+
* @param ServerRequestInterface $expectedRequest
35+
* @param ResponseInterface $response
36+
*/
37+
public function __construct(\PHPUnit_Framework_TestCase $testCase, ServerRequestInterface $expectedRequest, ResponseInterface $response)
38+
{
39+
$this->testCase = $testCase;
40+
$this->expectedRequest = $expectedRequest;
41+
$this->response = $response;
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function handleRequest(ServerRequestInterface $request)
48+
{
49+
// Only checking params & body here (request parsing is tested in the environment builder)
50+
$this->testCase->assertEquals($this->expectedRequest->getServerParams(), $request->getServerParams());
51+
52+
$expectedBody = (string) $this->expectedRequest->getBody();
53+
$body = (string) $request->getBody();
54+
55+
if (null === $expectedBody) {
56+
$this->testCase->assertNull($body);
57+
} else {
58+
// Test body lengths before testing full body (looks ugly in readout when it fails)
59+
$this->testCase->assertEquals(strlen($expectedBody), strlen($body));
60+
$this->testCase->assertEquals($expectedBody, $body);
61+
}
62+
63+
return $this->response;
64+
}
65+
}

0 commit comments

Comments
 (0)