public function read(Request $request, $name) { $fileName = $this->getFileName($request, $name); if (!file_exists($fileName)) { return []; } return Util::deserialize(file_get_contents($fileName)); }
/** * @param Response $response * @param string $path * @throws UnexpectedValueException * @return UnifiedRequest */ private function parseRequestFromResponse(Response $response, $path) { try { $requestInfo = Util::deserialize($response->getBody()); } catch (UnexpectedValueException $e) { throw new UnexpectedValueException(sprintf('Cannot deserialize response from "%s": "%s"', $path, $response->getBody()), null, $e); } $request = RequestFactory::getInstance()->fromMessage($requestInfo['request']); $params = $this->configureRequest($request, $requestInfo['server'], isset($requestInfo['enclosure']) ? $requestInfo['enclosure'] : []); return new UnifiedRequest($request, $params); }
return is_callable($closure); }; if (!is_array($matcher) || count(array_filter($matcher, $validator)) !== count($matcher)) { return new Response('POST data key "matcher" must be a serialized list of closures', Response::HTTP_EXPECTATION_FAILED); } } if (!$request->request->has('response')) { return new Response('POST data key "response" not found in POST data', Response::HTTP_EXPECTATION_FAILED); } $response = Util::silentDeserialize($request->request->get('response')); if (!$response instanceof Response) { return new Response('POST data key "response" must be a serialized Symfony response', Response::HTTP_EXPECTATION_FAILED); } $limiter = null; if ($request->request->has('limiter')) { $limiter = Util::silentDeserialize($request->request->get('limiter')); if (!is_callable($limiter)) { return new Response('POST data key "limiter" must be a serialized closure', Response::HTTP_EXPECTATION_FAILED); } } // Fix issue with silex default error handling $response->headers->set('X-Status-Code', $response->getStatusCode()); $app['storage']->prepend($request, 'expectations', ['matcher' => $matcher, 'response' => $response, 'limiter' => $limiter, 'runs' => 0]); return new Response('', Response::HTTP_CREATED); }); $app->error(static function (Exception $e) use($app) { if ($e instanceof NotFoundHttpException) { /** @var Request $request */ $request = $app['request_stack']->getCurrentRequest(); $app['storage']->append($request, 'requests', serialize(['server' => $request->server->all(), 'request' => (string) $request, 'enclosure' => $request->request->all()])); $notFoundResponse = new Response('No matching expectation found', Response::HTTP_NOT_FOUND);