2011-09-06 140 views

回答

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); 
     } 

希望這有助於你

+0

這對我有用。謝謝。 –

+1

這裏是一個鏈接到文檔@ symfony.com - http://symfony.com/doc/current/cookbook/testing/doctrine.html –

7

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

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 解釋如何測試實體和實體庫

+1

雖然這可能在理論上回答這個問題,[這將是更可取的](http: //meta.stackexchange.com/q/8259)在這裏包括答案的基本部分,並提供參考鏈接。 –