8

我正在使用教义夹具在我的 symfony 应用程序中加载测试数据。

 $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 类型的引用?我深入研究

Doctrine\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;
        }
    }

但是引用是代理学说实体,所以我得到了类类型

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

如何从理论固定装置中获取实体类型的引用?前缀 Proxies__CG__ 可能不是最好的方法吗?最可靠的方法是什么?

4

1 回答 1

1

不要使用get_class,使用instanceof

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

Doctrine 代理继承了实体类,从而实现了instanceof.

于 2017-02-02T19:58:23.087 回答