|
| 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