Phpunit 在Symfony中访问测试目录中的资源文件
目前我有如下目录结构:Phpunit 在Symfony中访问测试目录中的资源文件,phpunit,symfony,file-location,Phpunit,Symfony,File Location,目前我有如下目录结构: src -App --MyBundle ---Service ----DefaultService tests -AppMyBundle --files ---data.csv --Service ---DefaultServiceTest 我正在为文件读取器编写测试,并为测试的演示文件添加了data.csv文件。在我的测试中,我需要获得该data.csv文件的路径。但我找不到任何办法得到它。我尝试通过文件定位器服务访问它,例如: $locator = $this-&g
src
-App
--MyBundle
---Service
----DefaultService
tests
-AppMyBundle
--files
---data.csv
--Service
---DefaultServiceTest
我正在为文件读取器编写测试,并为测试的演示文件添加了data.csv
文件。在我的测试中,我需要获得该data.csv
文件的路径。但我找不到任何办法得到它。我尝试通过文件定位器服务访问它,例如:
$locator = $this->container->get('file_locator');
$path = $locator->locate('@AppMyBundle/Tests/files/data.csv');
及
没有运气。
如果需要myDefaultService
的名称空间是App\MyBundle\Service
而DefaultServiceTest的名称空间是App\MyBundle\Tests\Service
您不在App/bundle系统中,请尝试以下代码:
$path = $this->get('kernel')->getRootDir() . '/../tests/AppMyBundle/files/data.csv';
我使用了$client->getContainer()->getParameter('kernel.root\u dir')
如下所示:
$client = static::createClient();
$schemaPath = sprintf(
'%s/../tests/MyBundle/Resources/rss2.xsd',
$this->_client->getContainer()->getParameter('kernel.root_dir')
);
我目前正在使用symfony5、phpunit7.5编写单元测试(解析Excel文件)时这样做。我的方法简单得多。我的测试结构如下:
tests
- Service
-- ManifestReaderTest.php
-- testdata
--- import_test_good.csv
然后在我的ManifestReaderTest类中,我有以下内容:
$this->assertFileExists(__DIR__ . "/testdata/import_test_good.csv" );
这样,我就不再依赖外部类或服务。遗憾的是,这不起作用。给出InvalidArgumentException:找不到文件“@AppMyBundle/Tests/files/data.csv”
error是的,我考虑过这个选项,但也在想我可以获得更独立的路径,但我想这将是我必须使用goGiad来帮助您的方式:)
$this->assertFileExists(__DIR__ . "/testdata/import_test_good.csv" );