testing - 学说 2 : testing repository classes with YAML config

标签 testing symfony doctrine-orm

我使用 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/

相关文章:

ruby-on-rails - RSpec: Controller 规范的多个断言

symfony - 如何在 Symfony2 中通过 Swiftmailer 发送 12 000 封电子邮件?

php - Doctrine 查询 findBy 返回 Doctrine\ODM\MongoDB\LoggableCursor 对象

c++ - 为什么自定义静态断言的实现不会立即静态断言 "true"?

java - 如何检查我的交易方式是否真的支持交易?

javascript - [vue-test-utils] : wrapper. setChecked() 必须传递一个 bool 值

Symfony - ManyToOne 关系中的循环引用错误

Symfony 2 FOSUserBundle 具有其余登录和注册功能

php - Symfony2 TWIG 显示已知 ID 的用户

php - 如何阻止 Symfony 记录 Doctrine 的 sql 查询?