我使用 Doctrine2 为我的 symfony2 项目配置了 YAML。我不明白如何调整 cookbook entry到 YAML 设置。
我的学说映射在 /path/to/my/bundle/Resources/config/doctrine/IpRange.orm.yml
运行 PHPUnit 时,出现错误:
Doctrine\ORM\Mapping\MappingException:找不到名为“Yitznewton.FreermsBundle.Entity.IpRange.orm.yml”的类“Yitznewton\FreermsBundle\Entity\IpRange”的映射文件。
听起来我需要配置测试平台以使用 symfony 文件命名约定,但我不知道该怎么做。
使用 symfony-standard.git check out 到 v2.0.7
这是我的测试:
<?php
namespace Yitznewton\FreermsBundle\Tests\Utility;
use Doctrine\Tests\OrmTestCase;
use Doctrine\ORM\Mapping\Driver\DriverChain;
use Doctrine\ORM\Mapping\Driver\YamlDriver;
use Yitznewton\FreermsBundle\Entity\IpRange;
use Yitznewton\FreermsBundle\Entity\IpRangeRepository;
class IpRangeRepositoryTest extends OrmTestCase
{
private $_em;
protected function setup()
{
// FIXME: make this path relative
$metadataDriver = new YamlDriver('/var/www/symfony_2/src/Yitznewton/FreermsBundle/Resources/config/doctrine');
$metadataDriver->setFileExtension('.orm.yml');
$this->_em = $this->_getTestEntityManager();
$this->_em->getConfiguration()
->setMetadataDriverImpl($metadataDriver);
$this->_em->getConfiguration()->setEntityNamespaces(array(
'FreermsBundle' => 'Yitznewton\\FreermsBundle\\Entity'));
}
protected function getRepository()
{
return $this->_em->getRepository('FreermsBundle:IpRange');
}
public function testFindIntersecting_RangeWithin_ReturnsIpRange()
{
$ipRange = new IpRange();
$ipRange->setStartIp('192.150.1.1');
$ipRange->setEndIp('192.160.1.1');
$this->assertEquals(1, count($this->getRepository()
->findIntersecting($ipRange)),
'some message');
}
最佳答案
再次查看 symfony 文档,似乎使用实时测试数据库进行集成测试优于对存储库类进行单元测试。也就是说,不支持 stub EntityManagers。
关于testing - 学说 2 : testing repository classes with YAML config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8553517/