发布于 2015-08-27 16:48:46 | 127 次阅读 | 评论: 0 | 来源: 网络整理
Unit testing Doctrine repositories in a Symfony project is not recommended. When you’re dealing with a repository, you’re really dealing with something that’s meant to be tested against a real database connection.
Fortunately, you can easily test your queries against a real database, as described below.
If you need to actually execute a query, you will need to boot the kernel
to get a valid connection. In this case, you’ll extend the KernelTestCase
,
which makes all of this quite easy:
// src/Acme/StoreBundle/Tests/Entity/ProductRepositoryFunctionalTest.php
namespace AcmeStoreBundleTestsEntity;
use SymfonyBundleFrameworkBundleTestKernelTestCase;
class ProductRepositoryFunctionalTest extends KernelTestCase
{
/**
* @var DoctrineORMEntityManager
*/
private $em;
/**
* {@inheritDoc}
*/
public function setUp()
{
self::bootKernel();
$this->em = static::$kernel->getContainer()
->get('doctrine')
->getManager()
;
}
public function testSearchByCategoryName()
{
$products = $this->em
->getRepository('AcmeStoreBundle:Product')
->searchByCategoryName('foo')
;
$this->assertCount(1, $products);
}
/**
* {@inheritDoc}
*/
protected function tearDown()
{
parent::tearDown();
$this->em->close();
}
}