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