Symfony2功能测试检查环境用于性能目的

时间:2015-12-24 10:20:13

标签: php symfony functional-testing

我试图测试我的symfony2控制器,它是一个很大的控制器,但我的控制器中的关键点是确保生成了正确的规范网址。我对此控制器操作的唯一功能测试断言了这个规范网址。我的测试数据提供商用~400 urls测试这个控制器。这个控制器试图处理许多搜索模式。

我想在控制器中添加类似以下代码的内容。您是否认为这是错误的更改源代码用于测试目的?或者你有什么想法我怎么能停止我的控制器并返回所需的响应而不是执行一堆代码来生成搜索结果?

if ($this->get('kernel')->getEnvironment() == 'test') {
    return new Response(sprintf('<link rel="canonical" href="%s">', $canonicalUrl));
}

更新1:

我的控制器动作的一部分。

protected function unifiedSearch(Request $request, $isSluggedSearch)
{
    $searchService = $this->get("frontend.service.search");

    // get routes and their required parameters from route collection which routes start with "searchcontroller_"
    $routeCollection = $searchService->getSearchControllerRoutes("searchcontroller_");
    // get current route name.
    $currentRouteName = $request->get('_route');

    $parameterBag = new SearchParameterBag(
        $currentRouteName,
        // current route's parameters.
        $request->get('_route_params'),
        // current queryString parameters.
        $request->query->all(),
        // holds current route's required parameters.
        $routeCollection[$currentRouteName],
        // all routes with their respected paramaeters.
        $routeCollection
    );

    try {
        $parameterBag = $searchService->resolveUrl($parameterBag, $isSluggedSearch);

    } catch (ClientException $e) {
        throw $this->createNotFoundException($request->getUri());

    } catch (SearchNotFoundException $e) {
        $parameterBag->setCanonicalUrl($request->getUri());
        $searchResultNotFound = $e->getMessage();
    }

0 个答案:

没有答案