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

    }

だから私の質問は:

  • 「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;

正常ですか?

4

1 に答える 1

1
  1. テストケースのすべてのテストは、独自のCalendarsDbTestオブジェクトを作成します。したがって、実際には、$ this-> Calendarは各テストで異なるオブジェクトです(テスト間で共有する場合は、setUpメソッドで作成する必要があります)

  2. 上記と同じです($ this-> CalendarsでsetDescriptionを呼び出すことはないため、nullがあります-最初のテストとは異なるオブジェクトです)

  3. 正確に何を意味するのかわかりません。あなたが意味することをより正確に(例えば、あなたがテストしたい方法)示すことができますか?

編集:

答えは:あなたはそれをテストしません。なんで?ユニットテストはUNITテストであるため、ここではエンティティのみをテストする必要があります。永続性、関係の維持などは教義の責任であり、そこでテストする必要があります-あなたはそれについて心配する必要はありません。

テストする必要があるのは、$jobIdプロパティのsetter/ getterだけです(ただし、整数ではなくJobクラスのオブジェクトであるため、「$jobId」ではなく「$job」にする必要があります)。例:

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

     (...)
}
于 2012-09-04T13:32:18.440 に答える