Symfony2 Doctrine MongoDB回滚

mok*_*gio 5 php tdd mongodb symfony doctrine-orm

我正在尝试使用Symfony2,Doctrine和MongoDB构建一组安全的测试.

我需要做的是在测试开始时加载很多灯具,并在它结束时卸载它们.我想过用一个交易来做,但是...我找不到有关如何用Doctrine和Mongo做的文档!

我在Doctrine文档中找到了关于如何与ORM进行交易的良好文档,但没有关于ODM.

于是我带着一看的源代码Connection.php的原则,蒙戈使用过类,我还没有发现beginTransaction,commit并且rollback这些方法DBAL版本使用.

我一无所知,然后我问自己"甚至可以在MongoDB中回滚?",如果在MongoDB常见问题解答中找到的答案是:

MongoDB不使用传统锁定或复杂事务与回滚

:(所以我想这就是为什么beginTransactionODM中没有或没有任何东西......

但我的问题仍然存在:如何为我的测试实现一种回滚?

我现在唯一的想法是手动获取我加载的文档的所有ID,然后删除它们tearDown().但是,嗯......它有点糟透了,不是吗?

其他想法??

编辑: 在我对这个问题的第一次评论之后,关于我想在测试和开发中拥有相同数据库的事实,我想:为什么不使用单独的测试数据库,在测试开始时开发数据库被复制,那可以轻松掉线?

这可能是一个更好的主意吗?它实际上对我来说更容易,更安全.你们有什么感想?

谢谢 :)

AD7*_*six 4

I am not using two separate DBs for development and testing

That's the first thing to address - because without a testing db, running tests will affect your development db and vice versa which is a terrible idea. You should be able to run tests in your production environment with absolute confidence that nothing you do in a test will affect your deployed site.

Setup a test connection

So, modify your parameters.yml to have something like this:

database.host: localhost
database.port: 27017
database.db:   myappname

database.test.host: localhost
database.test.port: 27017
database.test.db:   myappname-test
Run Code Online (Sandbox Code Playgroud)

In addition, in your app/config/config_test.yml file override the default connnection so that anything you trigger as part of a test which requests the default document manager will receive a manager pointing at your test db:

doctrine_mongodb:
    document_managers:
        default:
            database: %database.test.db%
Run Code Online (Sandbox Code Playgroud)

Prepare for tests with fixtures

Then, what you want to do effectively is:

  • truncate relevant collections
  • load fixtures

on your test db before each test.

Here's an example abstract test class:

<?php

use Doctrine\Common\DataFixtures\Executor\MongoDBExecutor as Executor,
    Doctrine\Common\DataFixtures\Purger\MongoDBPurger as Purger,
    Doctrine\Common\DataFixtures\Loader,
    Doctrine\Common\DataFixtures\ReferenceRepository,
    Symfony\Bundle\FrameworkBundle\Test\WebTestCase,
    Symfony\Bundle\FrameworkBundle\Console\Application;

abstract class AbstractTest extends WebTestCase
{
    /**
     * Array of fixtures to load.
     */
    protected $fixtures = array();

    /**
     * Setup test environment
     */
    public function setUp()
    {
        $kernel = static::createKernel(array('environment' => 'test', 'debug' => false));
        $kernel->boot();
        $this->container = $kernel->getContainer();
        $this->dm = $this->container->get('doctrine.odm.mongodb.document_manager');

        if ($this->fixtures) {
            $this->loadFixtures($this->fixtures, false);
        }
    }

    /**
     * Load fixtures
     *
     * @param array   $fixtures names of _fixtures to load
     * @param boolean $append   append data, or replace?
     */
    protected function loadFixtures($fixtures = array(), $append = true)
    {
        $defaultFixtures = false;

        $loader = new Loader();
        $refRepo = new ReferenceRepository($this->dm);

        foreach ((array) $fixtures as $name) {
            $fixture = new $name();
            $fixture->setReferenceRepository($refRepo);
            $loader->addFixture($fixture);
        }

        $purger = new Purger();
        $executor = new Executor($this->dm, $purger);
        $executor->execute($loader->getFixtures(), $append);
    }
}
Run Code Online (Sandbox Code Playgroud)

在测试中使用夹具

使用前面的抽象测试类,您可以根据需要编写使用或不使用夹具数据的测试。下面是一个简单的例子。

<?php

use Your\AbstractTest,
    Your\Document\Foo;

class RandomTest extends AbstractTest
{
    /**
     * fixtures to load before each test
     */
    protected $fixtures = array(
        'APP\FooBundle\DataFixtures\MongoDB\TestFoos',
        'APP\FooBundle\DataFixtures\MongoDB\TestBars'
    );

    ...

    /**
     * Check it gets an ID (insert succeeded)
     * 
     */
    public function testCreateDefaults()
    {
        $foo = new Foo();
        $this->dm->persist($foo);
        $this->dm->flush();

        $this->assertNotNull($foo->getId());
        $this->assertSame('default value', $foo->getSomeProperty());
        // etc.
    }

    /**
     * Check result of something with a given input
     * 
     */
    public function testSomething()
    {
        $foo = $this->dm->getRepository(APPFooBundle:Foo)->findByName('Some fixture object');

        $foo->doSomething();
        $this->assertSame('modified value', $foo->getSomeProperty());
        // etc.
    }
Run Code Online (Sandbox Code Playgroud)

在每次测试之前,您定义的固定装置将被加载(截断它们影响的集合),从而提供一致的数据库状态作为测试的基础。