Tôi không sử dụng hai DBS riêng biệt cho sự phát triển và thử nghiệm
Đó là điều đầu tiên cần giải quyết - bởi vì không có một db thử nghiệm, kiểm tra chạy sẽ ảnh hưởng đến db phát triển của bạn và ngược lại đó là một ý tưởng khủng khiếp. Bạn sẽ có thể chạy thử nghiệm trong môi trường sản xuất của bạn với sự tự tin tuyệt đối rằng không có gì bạn làm trong một thử nghiệm sẽ ảnh hưởng đến trang web được triển khai của bạn.
Thiết lập một kết nối thử nghiệm
Vì vậy, sửa đổi parameters.yml của bạn để có một cái gì đó như thế này:
database.host: localhost
database.port: 27017
database.db: myappname
database.test.host: localhost
database.test.port: 27017
database.test.db: myappname-test
Bên cạnh đó, trong app/config/tập tin config_test.yml bạn ghi đè mặc định connnection để bất kỳ thứ gì bạn kích hoạt như một phần của thử nghiệm yêu cầu trình quản lý tài liệu mặc định sẽ nhận được người quản lý chỉ vào db thử nghiệm của bạn:
doctrine_mongodb:
document_managers:
default:
database: %database.test.db%
Chuẩn bị cho thử nghiệm với đồ đạc
Sau đó, những gì bạn muốn làm một cách hiệu quả là:
- cắt xén bộ sưu tập liên quan
- đồ đạc tải
trên db thử nghiệm của bạn trước mỗi bài kiểm tra .
Dưới đây là một ví dụ trừu tượng lớp thử nghiệm:
<?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);
}
}
Sử dụng đồ đạc trong các thử nghiệm của bạn
Với lớp thử nghiệm trừu tượng trước, sau đó bạn có thể viết các bài kiểm tra mà sử dụng dữ liệu cố định của bạn - hay không - khi thích hợp. Dưới đây là một ví dụ tầm thường.
<?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.
}
Trước khi thử nghiệm, các đồ đạc bạn đã xác định sẽ được tải (cắt ngắn các bộ sưu tập mà chúng ảnh hưởng), đưa ra trạng thái db nhất quán để căn cứ vào các thử nghiệm của bạn.
Tôi quên nói rằng tôi không sử dụng hai DB riêng biệt để phát triển và thử nghiệm, vì vậy một 'tearDown()' bình thường làm giảm mọi thứ không phải là giải pháp cho tôi ... – mokagio