如何在symfony WebTestCase中根据测试夹具的类型获取条带引用?

时间:2021-09-07 06:47:35

I am using doctrine fixtures to load test data in my symfony application.

我正在使用doctrine fixture在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'
]);

In my test case, I want to test get paginated entities.

在我的测试用例中,我希望测试获得分页实体。

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

}

I want to compare page from my fixtures and from api response. The problem is below line returns all fixture references. The entity I want to test is of type Entity6. Entity6 has dependency on all other types so I have to load all of them.

我想比较一下我的夹具和api响应。下面的问题将返回所有fixture引用。我要测试的实体类型为Entity6。Entity6依赖于所有其他类型,所以我必须加载所有类型。

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

$ = $ this - > fixtureLoader实体- > getReferenceRepository()- > getReferences();

How do I get refernces of type Entity6 only ? I digged into

如何获得实体6类型的引用?我挖到

Doctrine\Common\DataFixtures\ReferenceRepository::getReferences code

学说\常见\ DataFixtures \ ReferenceRepository:getReferences代码

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

There is no option to get references of particular type. I tried looping on all references to check the class type using get_class

无法获取特定类型的引用。我尝试对所有引用进行循环,以使用get_class检查类类型

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

But references are proxy doctrine entitites so I am getting class type

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

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

How do I get references of entity type from doctrine fixtures ? Prefixing Proxies__CG__ might not be best way to do this ? What is the most reliable way ?

如何从doctrine fixture中获得实体类型的引用?前缀Proxies__CG__可能不是最好的方法吗?最可靠的方法是什么?

1 个解决方案

#1


0  

Don't use get_class, use instanceof:

不要使用get_class,使用instanceof:

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

Doctrine proxies inherit the entity class, thus fulfilling instanceof.

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

#1


0  

Don't use get_class, use instanceof:

不要使用get_class,使用instanceof:

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

Doctrine proxies inherit the entity class, thus fulfilling instanceof.

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