|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByJG\RestServer; |
|
4
|
|
|
|
|
5
|
|
|
use ByJG\RestServer\Exception\ClassNotFoundException; |
|
6
|
|
|
use ByJG\RestServer\Exception\Error404Exception; |
|
7
|
|
|
use ByJG\RestServer\Exception\Error405Exception; |
|
8
|
|
|
use ByJG\RestServer\Exception\Error520Exception; |
|
9
|
|
|
use ByJG\RestServer\Exception\InvalidClassException; |
|
10
|
|
|
use ByJG\RestServer\Route\RouteListInterface; |
|
11
|
|
|
use ByJG\RestServer\Writer\MemoryWriter; |
|
12
|
|
|
use ByJG\Util\Psr7\MemoryStream; |
|
13
|
|
|
use ByJG\Util\Psr7\MessageException; |
|
14
|
|
|
use ByJG\Util\Psr7\Response; |
|
15
|
|
|
use Psr\Http\Message\RequestInterface; |
|
16
|
|
|
|
|
17
|
|
|
class MockRequestHandler extends HttpRequestHandler |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var RequestInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $request; |
|
23
|
|
|
|
|
24
|
|
|
/** @var MemoryWriter */ |
|
25
|
|
|
protected $writer; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* MockRequestHandler constructor. |
|
29
|
|
|
* @param RequestInterface $request |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(RequestInterface $request) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->request = $request; |
|
34
|
|
|
$this->writer = new MemoryWriter(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param RouteListInterface $routes |
|
40
|
|
|
* @param RequestInterface $request |
|
41
|
|
|
* @return MockRequestHandler |
|
42
|
|
|
* @throws ClassNotFoundException |
|
43
|
|
|
* @throws Error404Exception |
|
44
|
|
|
* @throws Error405Exception |
|
45
|
|
|
* @throws Error520Exception |
|
46
|
|
|
* @throws InvalidClassException |
|
47
|
|
|
* @throws MessageException |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function mock(RouteListInterface $routes, RequestInterface $request) |
|
50
|
|
|
{ |
|
51
|
|
|
$handler = new MockRequestHandler($request); |
|
52
|
|
|
$handler->handle($routes, true, false); |
|
53
|
|
|
return $handler; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return HttpRequest|MockHttpRequest |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function getHttpRequest() |
|
60
|
|
|
{ |
|
61
|
|
|
if (is_null($this->httpRequest)) { |
|
62
|
|
|
$this->httpRequest = new MockHttpRequest($this->request); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this->httpRequest; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected $psr7Response = null; |
|
69
|
|
|
|
|
70
|
|
|
public function getPs7Response() |
|
71
|
|
|
{ |
|
72
|
|
|
if (is_null($this->psr7Response)) { |
|
73
|
|
|
$this->psr7Response = new Response($this->writer->getStatusCode()); |
|
74
|
|
|
|
|
75
|
|
|
foreach ($this->writer->getHeaders() as $header => $value) { |
|
76
|
|
|
$this->psr7Response = $this->psr7Response->withHeader($header, $value); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->psr7Response = $this->psr7Response->withBody(new MemoryStream($this->writer->getData())); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
return $this->psr7Response; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|