简体   繁体   English

Symfony 2.8功能测试:找不到对本地文件的访问

[英]Symfony 2.8 functional testing: access to local file not found

I'm coding a functional test that calls a route whose controller has to read on local file system a specific file. 我正在编写一个功能测试,该测试调用一个路由,该路由的控制器必须在本地文件系统上读取特定文件。 The code in my controller is: 我的控制器中的代码是:

$document_root = $_SERVER['DOCUMENT_ROOT'];
$patch = $document_root . "/../app/config/tabelle/config.json";
if (file_exists($patch)) { ... }else{ die; }

The problem is that when I run the controller visiting by myself the URL and loading the page on my browser, everything works properly. 问题是,当我运行控制器自己访问URL并在浏览器中加载页面时,一切正常。 But when I run my functional test, that is supposed to load exactly the same URL I visited manually by browser, the test fails because controller cannot find the file. 但是,当我运行功能测试(应该加载与浏览器手动访问的URL完全相同的URL)时,测试失败,因为控制器找不到文件。 Maybe I have to configure phpunit in a way that tells him which root folder he has to consider? 也许我必须以一种告诉他必须考虑哪个根文件夹的方式配置phpunit? Thanks 谢谢

I solved adopting the Symfony2 native way to get the ROOT_DIR. 我解决了采用Symfony2本机方式获取ROOT_DIR的问题。 Rather than using $_SERVER['DOCUMENT_ROOT'] I used: $document_root = $this->container->getParameter('kernel.root_dir'); 而不是使用$ _SERVER ['DOCUMENT_ROOT']:$ document_root = $ this-> container-> getParameter('kernel.root_dir');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM