如何使用PHPUnit测试Symfony2模型

如何使用PHPUnit测试Symfony2模型

问题描述:

我一直在尝试在Symfony2项目中测试模型,但我不知道如何让实体管理器保存和检索记录。如何使用PHPUnit测试Symfony2模型

任何人都可以为我指出正确的文档吗?

为了测试你的模型,你可以使用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); 
     } 

希望这有助于你

+0

这对我有用。谢谢。 –

+1

这里是一个链接到文档@ symfony.com - http://symfony.com/doc/current/cookbook/testing/doctrine.html –

Symfony2模型预计是代表domain models代码中的域对象。

域对象应该单纯定义为实现相应的域概念的商业 行为,而不是通过更具体的技术框架的要求来定义 。 - Domain-driven design - Wikipedia, the free encyclopedia

域对象(和它的测试)不应该依赖于Symfony2的API和学说的API,除非你真的想考验一下自己。

写Symfony2单元测试与编写标准PHPUnit单元测试没有区别。 - Symfony - Testing

可以测试业务逻辑域对象使用PHPUnit(或贝哈特)表示的(流程,规则,行为等),并且通常test doubles

+0

是的,我已经阅读文档,并了解这些概念。但是,如果我想测试,例如,可扼要行为,它只在记录保存后才起作用。我如何测试?不要说该软件包可能已经过测试,我只想看看如何测试symfony2中的东西 – nerohc

+1

了解产品的核心问题对我们非常重要。 如果你正在开发可溶性行为,那么对你来说最重要的是slug字符串是基于可滞留字段的值正确生成的,所以你应该对它进行测试。当然,您可以测试slug字段的值是否正确存储到数据库中,但它不是该产品的核心问题,并且它是Doctrine本身的核心关注点(并且已经过测试)。 – iteman

+1

是的,但子弹创建后,所以为了测试生成的字符串是否正常,我*需要*保存对象,而不是测试保存本身(这当然是由教条团队测试),但检查生成的字符串,这是系统关心的问题。 – nerohc

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 解释如何测试实体和实体库

+1

虽然这可能在理论上回答这个问题,[这将是更可取的](http: //meta.stackexchange.com/q/8259)在这里包括答案的基本部分,并提供参考链接。 –