2

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

Vertex
  • 2,682
  • 3
  • 29
  • 43
Einius
  • 1,352
  • 2
  • 20
  • 45

3 Answers3

1

I used $client->getContainer()->getParameter('kernel.root_dir') like this:

$client = static::createClient();

$schemaPath = sprintf(
    '%s/../tests/MyBundle/Resources/rss2.xsd',
    $this->_client->getContainer()->getParameter('kernel.root_dir')
);
totas
  • 10,288
  • 6
  • 35
  • 32
0

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

$path = $this->get('kernel')->getRootDir() . '/../tests/AppMyBundle/files/data.csv';
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
0

I'm currently doing this when writing unit tests (which parse an Excel file) using Symfony 5, PHPUnit 7.5. My approach is much simpler. My tests are structured this:

tests
- Service
-- ManifestReaderTest.php
-- testdata
--- import_test_good.csv

Then within my ManifestReaderTest class, I have the following:

$this->assertFileExists(__DIR__ . "/testdata/import_test_good.csv" );

This way, I'm not reliant on external classes or services.