Search code examples
phpunitsymfonyfile-location

Accessing resource file in test directory in Symfony


Currently I have directory structure like:

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 App\MyBundle\Service and for DefaultServiceTest is App\MyBundle\Tests\Service


Solution

  • You are outside from app/bundle system so try this code please:

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