Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHPUnit&;博士:如何嘲笑我的实体?_Php_Symfony_Phpunit_Symfony 3.4 - Fatal编程技术网

PHPUnit&;博士:如何嘲笑我的实体?

PHPUnit&;博士:如何嘲笑我的实体?,php,symfony,phpunit,symfony-3.4,Php,Symfony,Phpunit,Symfony 3.4,我正在用Symfony3.4创建一个库。我正在用PHPUnit做测试 我有一个从数据库中检索数据的方法,我有两个实体,它们在一个关系中,Collection()和CollectionElement(): 然后我必须进行一些测试,但我无法模仿我的EntityManager: $collection = new Collection(); $collection->setId(3); $collection->setNomCollection("db1");

我正在用Symfony3.4创建一个库。我正在用PHPUnit做测试

我有一个从数据库中检索数据的方法,我有两个实体,它们在一个关系中,Collection()和CollectionElement():

然后我必须进行一些测试,但我无法模仿我的EntityManager:

    $collection = new Collection();
    $collection->setId(3);
    $collection->setNomCollection("db1");     
    $mockedEm = $this->createMock(EntityManager::class);

 $this->collectionMock = $this->getMockBuilder('AppBundle\Entity\IO\Collection')
            ->setMethods(['findOneBy'])
            ->getMock();
 $this->collectionMock->method("findOneBy")->will($this->returnValue($collection));
请问,我能做些什么来让它工作?此外,这两个实体都调用findOneBy()


谢谢:)

你用这个嘲弄经理是正确的

$mockedEm = $this->createMock(EntityManager::class);
您错过的是对getRepository的调用

$repo = $this->createMock(EntityRepository::class);
$mockedEm->expects($this->once())->method('getRepository')->with(CollectionElement::class)->willReturn($repo);
$repo->expects($this->exactly(2))->method('findOneBy')
  ->withConsecutive(['fileName' => 'f1'], ['fileName' => 'f2'])
  ->willReturnOnConsecutiveCalls($entity1, $entity2);
之后,您可以对存储库中的findOneBy抱有期望

$repo = $this->createMock(EntityRepository::class);
$mockedEm->expects($this->once())->method('getRepository')->with(CollectionElement::class)->willReturn($repo);
$repo->expects($this->exactly(2))->method('findOneBy')
  ->withConsecutive(['fileName' => 'f1'], ['fileName' => 'f2'])
  ->willReturnOnConsecutiveCalls($entity1, $entity2);

我有两份回购协议,然后我就这样做$mockedEm=$this->createMock(EntityManager::class)$repo=$this->createMock(EntityRepository::class)$mockedEm->expects($this->once())->method('getRepository')->with(CollectionElement::class)->willReturn($repo)$repo2=$this->createMock(EntityRepository::class)$mockedEm->expects($this->once())->method('getRepository')->with(Collection::class)->willReturn($repo2);异常:调用1次时,方法名称等于“getRepository”的预期失败:(请注意,我得到了2个repos=Collection和collectionelements,这就是为什么我为同一个方法上的两个不同调用添加了一个示例。这是一个模板,它应该为您指出正确的方向,而不是您需要的确切代码。好吧,我不知道我做得不好,我从示例中学到了。。。