我一直在尝试在Symfony2项目中测试模型,但我不知道如何让实体管理器保存和检索记录。
有人能指出我正确的文档吗?
答案 0 :(得分:21)
为了测试模型,可以使用setUp()方法。 link to docs
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class MyModelTest extends WebTestCase
{
/**
* @var EntityManager
*/
private $_em;
protected function setUp()
{
$kernel = static::createKernel();
$kernel->boot();
$this->_em = $kernel->getContainer()->get('doctrine.orm.entity_manager');
$this->_em->beginTransaction();
}
/**
* Rollback changes.
*/
public function tearDown()
{
$this->_em->rollback();
}
public function testSomething()
{
$user = $this->_em->getRepository('MyAppMyBundle:MyModel')->find(1);
}
希望这有助于你
答案 1 :(得分:7)
Symfony2模型应该是代码中代表domain models的域对象。
应该纯粹定义域对象以实现业务 相应域概念的行为,而不是被定义 根据更具体的技术框架的要求。 - Domain-driven design - Wikipedia, the free encyclopedia
域对象(及其测试)不应依赖于Symfony2 API和Doctrine API,除非您真的想要自己测试。
编写Symfony2单元测试与编写标准PHPUnit单元测试没什么不同。 - Symfony - Testing
您可以使用PHPUnit(或Behat)以及test doubles来测试域对象中表示的业务逻辑(流程,规则,行为等)。
答案 2 :(得分:2)
namespace Ibw\JobeetBundle\Tests\Repository;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Input\ArrayInput;
use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand;
use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand;
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\CreateSchemaDoctrineCommand;
class CategoryRepositoryTest extends WebTestCase
{
private $em;
private $application;
public function setUp()
{
static::$kernel = static::createKernel();
static::$kernel->boot();
$this->application = new Application(static::$kernel);
// drop the database
$command = new DropDatabaseDoctrineCommand();
$this->application->add($command);
$input = new ArrayInput(array(
'command' => 'doctrine:database:drop',
'--force' => true
));
$command->run($input, new NullOutput());
// we have to close the connection after dropping the database so we don't get "No database selected" error
$connection = $this->application->getKernel()->getContainer()->get('doctrine')->getConnection();
if ($connection->isConnected()) {
$connection->close();
}
// create the database
$command = new CreateDatabaseDoctrineCommand();
$this->application->add($command);
$input = new ArrayInput(array(
'command' => 'doctrine:database:create',
));
$command->run($input, new NullOutput());
// create schema
$command = new CreateSchemaDoctrineCommand();
$this->application->add($command);
$input = new ArrayInput(array(
'command' => 'doctrine:schema:create',
));
$command->run($input, new NullOutput());
// get the Entity Manager
$this->em = static::$kernel->getContainer()
->get('doctrine')
->getManager();
// load fixtures
$client = static::createClient();
$loader = new \Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader($client->getContainer());
$loader->loadFromDirectory(static::$kernel->locateResource('@IbwJobeetBundle/DataFixtures/ORM'));
$purger = new \Doctrine\Common\DataFixtures\Purger\ORMPurger($this->em);
$executor = new \Doctrine\Common\DataFixtures\Executor\ORMExecutor($this->em, $purger);
$executor->execute($loader->getFixtures());
}
public function testFunction()
{
// here you test save any object or test insert any object
}
protected function tearDown()
{
parent::tearDown();
$this->em->close();
}
}
就像在此链接中一样:Jobeet Unit Test Tutorial 解释如何测试实体和实体存储库