2015-12-04 4 views
6

symfonyアプリケーションでテストデータを読み込むためにdoctrineフィクスチャを使用しています。symfony WebTestCaseのテストでフィクスチャの種類別にdoctrine fixtureの参照を取得するには?

$this->fixtureLoader = $this->loadFixtures([ 
      "My\DemonBundle\DataFixtures\ORM\LoadEntity1Data", 
      "My\DemonBundle\DataFixtures\ORM\LoadEntity2Data", 
      "My\DemonBundle\DataFixtures\ORM\LoadEntity3Data", 
      "My\DemonBundle\DataFixtures\ORM\LoadEntity4Data", 
      "My\DemonBundle\DataFixtures\ORM\LoadEntity5Data", 
      'My\DemonBundle\DataFixtures\ORM\LoadEntity6Data' 
]); 

私のテストケースでは、ページ区切りのエンティティをテストしたいと思います。

public function testGetPaginated() 
{ 

    $entities6 = $this->fixtureLoader->getReferenceRepository()->getReferences(); 

    $expected = array_slice($entities6, 3, 3); 

    $this->client = static::makeClient(); 
    $this->client->request('GET', '/api/v1/entities6', ["page" => 2, "limit" => 3, "order" => "id", "sort" => "asc"], array(), array(
     'CONTENT_TYPE' => 'application/json', 
     'HTTP_ACCEPT' => 'application/json' 
    )); 


    $this->assertSame($expected, $this->client->getResponse()->getContent()); 

} 

私の什器とapiの応答からページを比較したいと思います。問題は、下の行ですべてのフィクスチャ参照を返します。私がテストしたいエンティティはEntity6型です。 Entity6は他のすべての型に依存しているので、それらのすべてをロードする必要があります。

$ entities- $ this-> fixtureLoader-> getReferenceRepository() - > getReferences();

タイプEntity6の参照のみを取得するにはどうすればよいですか?私は特定の型の参照を取得するオプションはありません

教義\共通\ DataFixtures \ ReferenceRepository :: getReferencesコード

/** 
* Get all stored references 
* 
* @return array 
*/ 
public function getReferences() 
{ 
    return $this->references; 
} 

に掘っ。私はget_class

foreach ($references as $reference) { 
     $class = get_class($obj); 
     if ($class == "My\DemonBundle\Entity\ORM\Entity6") { 
      $expected[] = $obj; 
     } 
    } 

を使用して、クラスの種類を確認するために、すべての参照にループしてみました。しかし、私は、私は教義器具からのエンティティタイプの参照を取得するにはどうすればよいのクラス型に

Proxies\__CG__\My\DemonBundle\Entity\ORM\Entity6 

を取得していますので、参照は、プロキシ教義のentititesです? Prefixing Proxies__CG__はこれを行う最良の方法ではないかもしれませんか?最も信頼できる方法は何ですか? get_classを使用しないでください

答えて

0

instanceofを使用します。

foreach ($references as $reference) { 
    if ($reference instanceof \My\DemonBundle\Entity\ORM\Entity6) { 
     $expected[] = $obj; 
    } 
} 

Doctrineのプロキシは、このようにinstanceofを果たし、エンティティクラスを継承します。

関連する問題