2012-09-04 30 views
1

我做我的实体的测试单位:

namespace PathtomyBundle\Tests; 

require_once dirname(__DIR__).'/../../../app/AppKernel.php'; 

use Doctrine\ORM\Tools\SchemaTool; 

abstract class TestCase extends \PHPUnit_Framework_TestCase 
{ 
/** 
* @var Symfony\Component\HttpKernel\AppKernel 
*/ 
protected $kernel; 

/** 
* @var Doctrine\ORM\EntityManager 
*/ 
protected $entityManager; 

/** 
* @var Symfony\Component\DependencyInjection\Container 
*/ 
protected $container; 

public function setUp() 
{ 
    // Boot the AppKernel in the test environment and with the debug. 
    $this->kernel = new \AppKernel('test', true); 
    $this->kernel->boot(); 

    // Store the container and the entity manager in test case properties 
    $this->container = $this->kernel->getContainer(); 
    $this->entityManager = $this->container->get('doctrine')->getEntityManager(); 

    // Build the schema for sqlite 
    //$this->generateSchema(); 

    parent::setUp(); 
} 

public function tearDown() 
{ 
    // Shutdown the kernel. 
    $this->kernel->shutdown(); 

    parent::tearDown(); 
} 

protected function generateSchema() 
{ 
    // Get the metadatas of the application to create the schema. 
    $metadatas = $this->getMetadatas(); 

    if (! empty($metadatas)) { 
     // Create SchemaTool 
     $tool = new SchemaTool($this->entityManager); 
     $tool->createSchema($metadatas); 
    } else { 
     throw new Doctrine\DBAL\Schema\SchemaException('No Metadata Classes to process.'); 
    } 
} 

/** 
* Overwrite this method to get specific metadatas. 
* 
* @return Array 
*/ 
protected function getMetadatas() 
{ 
    return $this->entityManager->getMetadataFactory()->getAllMetadata(); 
} 
} 

也:

namespace pathtomybundle\Tests\Entity; 
use pathtomybundle\Tests\TestCase; 
use pathtomybundle\Entity\Calendars; 

require_once dirname(__DIR__).'/TestCase.php'; 

class CalendarsDbTest extends TestCase 
{ 

protected $Calendars; 

    public function setUp() 
     { 
      parent::setUp(); 

      $this->Calendars = new Calendars(); 
     } 


    public function testGenerateCalendars() 
    { 

     $this->Calendars->setBeginDate(new \DateTime('now')); 
     $this->Calendars->setDescription('Description'); 
     $this->Calendars->setEndDate(new \DateTime('now')); 
     $this->Calendars->setType('sur titre'); 

     // Save the ExCalendars 
     $this->entityManager->persist($this->Calendars); 
     $this->entityManager->flush(); 



    } 

     public function testUser(){ 

    $this->assertEquals('Description', $this->Calendars->getDescription()); 

    } 

所以我的问题是:

  • 它为什么会引发这个错误“无法断言空符合预期”?

  • 为什么getDescription()返回NULL

  • 如何测试两个表与一对多的关系,例如我的表日历与数据库中的另一个表?

编辑

对于第三个问题:

比如我有两个表工作和压延带多到一的关系,所以我会在日历表有JOB_ID场,所以我怎么会做我的测试单位有外键“JOB_ID”

在日历实体:

/** 
* @var Job 
* 
* @ORM\ManyToOne(targetEntity="Job") 
* @ORM\JoinColumns({ 
* @ORM\JoinColumn(name="job_id", referencedColumnName="job_id") 
* }) 
*/ 
private $jobId; 

编辑-2-

当我跑我的PHPUnit测试“的PHPUnit -c应用程序”来测试制定者功能和数据库坚持,所以我有一个与每个测试中DATABSE insered一个新的数据,我的问题是有可能做很多测试,但我只在一次数据库中插入数据,因为实际上我必须在每次测试时从数据库中删除数据。

2 - 另一个问题:创建一个database_test,我使用“$ this-> generateSchema(); ”这样在第一次创建数据库并且测试时再次调用“TestCase”类(上面的代码)所以他试图再次创建database_test,那么我必须在第一次之后删除该行,并且它不太好,那么当我运行我的测试时,我可以做什么来在第一次运行此行时只执行一次?

编辑-3

/** 
* @var Job 
* 
* @ORM\ManyToOne(targetEntity="Job") 
* @ORM\JoinColumns({ 
* @ORM\JoinColumn(name="job_id", referencedColumnName="id") 
* }) 
*/ 
private $job; 

这是正常的吗?

+1

的第一个问题:因为每个测试中得到有自己的范围,另一个测试可以在不影响东西其他测试。 –

+0

请如果妳代码和证明回答着,像你剥夺了其他成员正确地回答我,请你一定要多尊重...... – Nll

+0

我是怎么了?我不知道要回答所有3个问题,我不确定我对第一个问题的回答。这正是Stackoverflow发明评论的原因:如果你想与世界其他地方分享你的意见/想法,但你不能给出正确的答案或不确定你的答案。 –

回答

1
  1. 测试用例中的每个测试都创建了自己的CalendarsDbTest对象。因此,实际上,$ this-> Calendar在每次测试中都是不同的对象(如果您想在测试之间共享它,您需要在setUp方法中创建它)

  2. 与上面相同(因为您从未致电setDescription with $ this-> Calendars - 它与第一次测试中的对象不同)

  3. 我不确定你的意思。你能展示更精确的(例如你想测试的方法)你的意思是什么?

编辑:

答案是:你不对它进行测试。为什么?由于单元测试是UNIT测试 - 您应该在这里只测试您的实体。持久性,保持关系等是教义的责任,应该在那里测试 - 你不用担心。

你应该测试的唯一的事情是$的jobId setter/getter属性(顺便说一句应该是“$工作”,而不是“$的jobId”,因为它的工作类的对象 - 不是整数),例如:

class CalendarTest extends \PHPUnit_Framework_TestCase 
{ 
    (...) 

    public function testSetGetJob() 
    { 
     $job = new Job(); 
     $job->setSomeProperty('someValue'); 

     $expectedJob = clone $job; // you clone this because in setter you pass object by reference 

     $calendar = new Calendar(); 
     $calendar->setJob($job); 

     $this->assertEquals($expectedJob, $calendar->getJob()); 
    } 

    (...) 
} 
+0

你可以看看我的编辑在我的第一个消息上方... – Nll

+0

我不很了解你的代码,使用“JobTest你能解释我更多你的代码“和”CalendarsTest“课程......还有另一个问题,您可以在我的第一条消息 – Nll

+0

中看到我的编辑好的。在Calendars实体中,可能你有像setJob和getJob这样的方法,并且你想测试它们。所以我的代码正在为$ job属性测试setter和getter(这是CalendarsTest的示例)。 1)构建样本$ job对象,并使用它与$ calendar-> setJob()方法一起使用。 2)你生成Job对象$ expectedJob,它应该与$ calendar-> getJob()一起返回。在我们的例子中,它与setJob方法中使用的对象相同(因为它是简单的setter)。 3)您检查由$ calendar-> getJob返回的对象是否与$ expectedJob相同。如果是 - 那么一切都会好起来,并通过测试。 – Cyprian