我正在关注 symfony 文档 ( https://symfony.com/doc/current/testing/doctrine.html ) 尝试针对我的 MySQL 数据库测试我的存储库类。相关的代码部分是这样的:
class ProductRepositoryTest extends KernelTestCase
{
/**
* @var \Doctrine\ORM\EntityManager
*/
private $entityManager;
/**
* {@inheritDoc}
*/
protected function setUp()
{
$kernel = self::bootKernel();
$this->entityManager = $kernel->getContainer()
->get('doctrine')
->getManager();
}
public function testSearchByCategoryName()
{
$products = $this->entityManager
->getRepository(Product::class)
->searchByCategoryName('foo')
;
$this->assertCount(1, $products);
}
/**
* {@inheritDoc}
*/
protected function tearDown()
{
parent::tearDown();
$this->entityManager->close();
$this->entityManager = null; // avoid memory leaks
}
}
当我使用 PhpUnit 执行测试类时,我得到一个 Doctrine\DBAL\Exception\ConnectionException 消息:“驱动程序中发生异常:SQLSTATE[HY000] [1045] 用户'root'@'localhost'的访问被拒绝(使用密码:不)”。
我将我的数据库连接数据存储在 .env 文件的 DATABASE_URL 中,并且在执行时连接工作正常:bin/console dictionary:migrations:migrate
但似乎此配置不用于测试(因为 'NO' 不是我在 .env 文件中的密码)。我是否必须将连接配置存储在另一个文件中进行测试?如果是,我必须在哪里存储配置,以便在我的测试用例中使用它?
提前感谢您的帮助!