私は自分のエンティティのテストユニットを実行しています:
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() );
}
だから私の質問は:
「nullが期待どおりに一致することを表明できませんでした」というエラーが発生するのはなぜですか?
なぜ
getDescription()
戻るのNULL
ですか?たとえば、データベース内の別のテーブルを使用したテーブルカレンダーなど、1対多の関係で2つのテーブルをテストするにはどうすればよいですか?
編集
3番目の質問の場合:
たとえば、2つのテーブルジョブと多対1の関係を持つカレンダーがあるため、カレンダーテーブルに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-capp」を実行してセッター機能をテストし、データベースに保持すると、すべてのテストでdatabseに新しいデータが挿入されます。私の質問は、多くのテストを実行できることですが、データを挿入します。実際には、テストごとにデータベースからデータを削除する必要があるため、データベースは1回だけです。
2-別の質問:database_testを作成するには、「$ this-> generateSchema();」を使用します。データベースを初めて作成した後、テストで「TestCase」クラス(上記のコード)を再度呼び出したときに、作成しようとしました。 database_testをもう一度実行すると、最初にその行を削除する必要がありますが、それは良くありません。テストを初めて実行するときに、この行を1回だけ実行するにはどうすればよいですか。
編集-3
/**
* @var Job
*
* @ORM\ManyToOne(targetEntity="Job")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="job_id", referencedColumnName="id")
* })
*/
private $job;
正常ですか?