2015-12-04 33 views
6

我正在使用教義裝置來加載我的symfony應用程序中的測試數據。如何在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類型的引用?我挖成

主義\ COMMON \ 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; 
     } 
    } 

,但是對於檢查類類型的代理學說entitites所以我得到的類類型

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

我如何從理論燈具實體類型的引用?前綴代理_CG__可能不是最好的方式來做到這一點?什麼是最可靠的方法?

回答

0

不要使用get_class,使用instanceof

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

學說代理繼承實體類,從而實現instanceof

相關問題