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\OutputProcessor\MockOutputProcessor; |
11
|
|
|
use ByJG\RestServer\Route\RouteDefinition; |
12
|
|
|
use ByJG\RestServer\Route\RouteDefinitionInterface; |
13
|
|
|
use ByJG\RestServer\Route\RoutePattern; |
14
|
|
|
use ByJG\Util\Psr7\Message; |
15
|
|
|
use ByJG\Util\Psr7\MessageException; |
16
|
|
|
use ByJG\Util\Psr7\Response; |
17
|
|
|
use MintWare\Streams\MemoryStream; |
18
|
|
|
use Psr\Http\Message\MessageInterface; |
19
|
|
|
use Psr\Http\Message\RequestInterface; |
20
|
|
|
|
21
|
|
|
class MockRequestHandler extends HttpRequestHandler |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var RequestInterface |
25
|
|
|
*/ |
26
|
|
|
protected $request; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* MockRequestHandler constructor. |
30
|
|
|
* @param RequestInterface $request |
31
|
|
|
*/ |
32
|
|
|
public function __construct(RequestInterface $request) |
33
|
|
|
{ |
34
|
|
|
$this->request = $request; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param RouteDefinitionInterface $routes |
40
|
|
|
* @param RequestInterface $request |
41
|
|
|
* @return Response |
42
|
|
|
* @throws ClassNotFoundException |
43
|
|
|
* @throws Error404Exception |
44
|
|
|
* @throws Error405Exception |
45
|
|
|
* @throws Error520Exception |
46
|
|
|
* @throws InvalidClassException |
47
|
|
|
* @throws MessageException |
48
|
|
|
*/ |
49
|
|
|
public static function mock(RouteDefinitionInterface $routes, RequestInterface $request) |
50
|
|
|
{ |
51
|
|
|
$handler = new MockRequestHandler($request); |
52
|
|
|
return $handler->handle($routes, true, true); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param RouteDefinitionInterface $routeDefinition |
57
|
|
|
* @param bool $outputBuffer |
58
|
|
|
* @param bool $session |
59
|
|
|
* @return bool|Response|void |
60
|
|
|
* @throws ClassNotFoundException |
61
|
|
|
* @throws Error404Exception |
62
|
|
|
* @throws Error405Exception |
63
|
|
|
* @throws Error520Exception |
64
|
|
|
* @throws InvalidClassException |
65
|
|
|
* @throws MessageException |
66
|
|
|
*/ |
67
|
|
|
public function handle(RouteDefinitionInterface $routeDefinition, $outputBuffer = true, $session = true) |
68
|
|
|
{ |
69
|
|
|
return $this->callParentHandle($routeDefinition); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return HttpRequest|MockHttpRequest |
74
|
|
|
*/ |
75
|
|
|
protected function getHttpRequest() |
76
|
|
|
{ |
77
|
|
|
return new MockHttpRequest($this->request); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param RouteDefinitionInterface $routeDefinition |
82
|
|
|
* @return RouteDefinition |
83
|
|
|
*/ |
84
|
|
|
protected function mockRoutes(RouteDefinitionInterface $routeDefinition) |
85
|
|
|
{ |
86
|
|
|
// Redo Route Definition |
87
|
|
|
$mockRoutes = new RouteDefinition(); |
88
|
|
|
foreach ($routeDefinition->getRoutes() as $route) { |
89
|
|
|
$class = $route->getClass(); |
90
|
|
|
$methodName = ""; |
91
|
|
|
if (is_array($class)) { |
92
|
|
|
$methodName = $class[1]; |
93
|
|
|
$class = $class[0]; |
94
|
|
|
} |
95
|
|
|
$mockRoutes->addRoute( |
96
|
|
|
new RoutePattern( |
97
|
|
|
$route->getMethod(), |
98
|
|
|
$route->getPattern(), |
99
|
|
|
function () use ($route) { |
|
|
|
|
100
|
|
|
return new MockOutputProcessor($route->getOutputProcessor()); |
101
|
|
|
}, |
102
|
|
|
$class, |
103
|
|
|
$methodName |
104
|
|
|
) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $mockRoutes; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param RouteDefinitionInterface $routeDefinition |
113
|
|
|
* @return Message|MessageInterface |
114
|
|
|
* @throws ClassNotFoundException |
115
|
|
|
* @throws Error404Exception |
116
|
|
|
* @throws Error405Exception |
117
|
|
|
* @throws Error520Exception |
118
|
|
|
* @throws InvalidClassException |
119
|
|
|
* @throws MessageException |
120
|
|
|
*/ |
121
|
|
|
protected function callParentHandle(RouteDefinitionInterface $routeDefinition) |
122
|
|
|
{ |
123
|
|
|
$this->useErrorHandler = false; |
124
|
|
|
try { |
125
|
|
|
parent::handle($this->mockRoutes($routeDefinition), true, false); |
126
|
|
|
} finally { |
127
|
|
|
$content = ob_get_contents(); |
128
|
|
|
ob_end_clean(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
$rawResponse = explode("\r\n", $content); |
133
|
|
|
$statusMatch = []; |
134
|
|
|
preg_match("/HTTP\/\d\.\d (\d+)/", array_shift($rawResponse), $statusMatch); |
135
|
|
|
|
136
|
|
|
$response = new Response(intval($statusMatch[1])); |
137
|
|
|
|
138
|
|
|
while (!empty($line = array_shift($rawResponse))) { |
139
|
|
|
$parts = explode(":", $line); |
140
|
|
|
$response = $response->withHeader($parts[0], trim($parts[1])); |
141
|
|
|
} |
142
|
|
|
$response = $response->withBody(new MemoryStream(implode("\r\n", $rawResponse))); |
143
|
|
|
|
144
|
|
|
return $response; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
} |
148
|
|
|
|