2

目前我的目录结构如下:

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

我正在为文件阅读器编写测试,并data.csv为测试添加了演示文件的文件。在我的测试中,我需要获取该data.csv文件的路径。但我找不到任何获得它的方法。我尝试通过file_locator以下服务访问它:

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

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

没有运气。命名空间如果我DefaultService的是App\MyBundle\Service和 DefaultServiceTest 是必要的App\MyBundle\Tests\Service

4

3 回答 3

1

$client->getContainer()->getParameter('kernel.root_dir')是这样使用的:

$client = static::createClient();

$schemaPath = sprintf(
    '%s/../tests/MyBundle/Resources/rss2.xsd',
    $this->_client->getContainer()->getParameter('kernel.root_dir')
);
于 2017-08-03T12:23:21.403 回答
0

您不在应用程序/捆绑系统之外,因此请尝试以下代码:

$path = $this->get('kernel')->getRootDir() . '/../tests/AppMyBundle/files/data.csv';
于 2017-02-25T10:03:49.340 回答
0

我目前正在使用 Symfony 5、PHPUnit 7.5 编写单元测试(解析 Excel 文件)时这样做。我的方法要简单得多。我的测试结构如下:

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

然后在我的 ManifestReaderTest 类中,我有以下内容:

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

这样,我不依赖外部课程或服务。

于 2020-05-07T09:00:32.207 回答