2017-02-25 135 views
2

目前我有目錄結構,如: `訪問資源文件中的Symfony

src 
-App 
--MyBundle 
---Service 
----DefaultService 
tests 
-AppMyBundle 
--files 
---data.csv 
--Service 
---DefaultServiceTest 

I'm writting test for a file reader and I added data.csv file for demo file for a test. In my test I need to get a path of that data.csv file. But I couldn't find any way of getting it. I have tried accessing it via file_locator service like:

$locator = $this->container->get('file_locator'); 
$path = $locator->locate('@AppMyBundle/Tests/files/data.csv'); 

and

$locator = $this->container->get('file_locator'); 
$path = $locator->locate('@AppMyBundle/files/data. 

With no luck. namespaces if necessary of my DefaultService is應用程序\ MyBundle \服務and for DefaultServiceTest is應用程序\ MyBundle \測試\ Service`

回答

0

您是從應用程序/束系統之外,從而嘗試這種代碼,請:

$path = $this->get('kernel')->getRootDir() . '/../tests/AppMyBundle/files/data.csv'; 
+0

不幸的是,這是行不通的。給出'InvalidArgumentException:無法找到文件'@AppMyBundle/Tests/files/data.csv''錯誤 – Einius

+0

更新答案@Einius –

+0

是的,我想過這個選項,但也想到我可以得到一個更獨立的路徑,但我想這個將是我必須去的方式 – Einius

0

我用$client->getContainer()->getParameter('kernel.root_dir')這樣的:

$client = static::createClient(); 

$schemaPath = sprintf(
    '%s/../tests/MyBundle/Resources/rss2.xsd', 
    $this->_client->getContainer()->getParameter('kernel.root_dir') 
);