|
| 1 | +<?php |
| 2 | +namespace Codeception\Lib\Connector; |
| 3 | + |
| 4 | +use Goutte\Client; |
| 5 | +use Symfony\Component\BrowserKit\Request; |
| 6 | +use Symfony\Component\BrowserKit\Response; |
| 7 | + |
| 8 | +class Universal extends Client |
| 9 | +{ |
| 10 | + protected $mockedResponse; |
| 11 | + |
| 12 | + public function setIndex($index) |
| 13 | + { |
| 14 | + $this->index = $index; |
| 15 | + } |
| 16 | + |
| 17 | + public function mockResponse($response) |
| 18 | + { |
| 19 | + $this->mockedResponse = $response; |
| 20 | + } |
| 21 | + |
| 22 | + public function doRequest($request) |
| 23 | + { |
| 24 | + if ($this->mockedResponse) { |
| 25 | + $response = $this->mockedResponse; |
| 26 | + $this->mockedResponse = null; |
| 27 | + return $response; |
| 28 | + } |
| 29 | + |
| 30 | + $_COOKIE = $request->getCookies(); |
| 31 | + $_SERVER = $request->getServer(); |
| 32 | + $_FILES = $request->getFiles(); |
| 33 | + |
| 34 | + $uri = str_replace('http://localhost', '', $request->getUri()); |
| 35 | + |
| 36 | + if (strtoupper($request->getMethod()) == 'GET') { |
| 37 | + $_GET = $request->getParameters(); |
| 38 | + } else { |
| 39 | + $_POST = $request->getParameters(); |
| 40 | + } |
| 41 | + $_REQUEST = $request->getParameters(); |
| 42 | + |
| 43 | + $_SERVER['REQUEST_METHOD'] = strtoupper($request->getMethod()); |
| 44 | + $_SERVER['REQUEST_URI'] = strtoupper($uri); |
| 45 | + |
| 46 | + ob_start(); |
| 47 | + include $this->index; |
| 48 | + |
| 49 | + $content = ob_get_contents(); |
| 50 | + ob_end_clean(); |
| 51 | + |
| 52 | + $headers = array(); |
| 53 | + $php_headers = headers_list(); |
| 54 | + foreach ($php_headers as $value) { |
| 55 | + // Get the header name |
| 56 | + $parts = explode(':', $value); |
| 57 | + if (count($parts) > 1) { |
| 58 | + $name = trim(array_shift($parts)); |
| 59 | + // Build the header hash map |
| 60 | + $headers[$name] = trim(implode(':', $parts)); |
| 61 | + } |
| 62 | + } |
| 63 | + $headers['Content-type'] = isset($headers['Content-type']) ? $headers['Content-type'] : "text/html; charset=UTF-8"; |
| 64 | + |
| 65 | + $response = new Response($content, 200, $headers); |
| 66 | + return $response; |
| 67 | + } |
| 68 | +} |
0 commit comments