我做我的实体的测试单位:
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;
这是正常的吗?
的第一个问题:因为每个测试中得到有自己的范围,另一个测试可以在不影响东西其他测试。 –
请如果妳代码和证明回答着,像你剥夺了其他成员正确地回答我,请你一定要多尊重...... – Nll
我是怎么了?我不知道要回答所有3个问题,我不确定我对第一个问题的回答。这正是Stackoverflow发明评论的原因:如果你想与世界其他地方分享你的意见/想法,但你不能给出正确的答案或不确定你的答案。 –