PHPUnit-Symfony:如何用多个参数模拟同一个方法?

PHPUnit-Symfony:如何用多个参数模拟同一个方法?,php,unit-testing,mocking,phpunit,symfony4,Php,Unit Testing,Mocking,Phpunit,Symfony4,嗨,尝试测试我的控制器此控制器调用getRepository方法getRepository是DocumentManager的一个功能 我必须将不同的参数传递给getREpository(),但当我尝试模拟DocumentManager et set方法getREpository时,我无法使用两种不同的结果模拟该方法 我的测试: <?php public function testGetLetter() { $_box = new Box();

嗨,尝试测试我的控制器此控制器调用getRepository方法getRepository是DocumentManager的一个功能

我必须将不同的参数传递给getREpository(),但当我尝试模拟DocumentManager et set方法getREpository时,我无法使用两种不同的结果模拟该方法

我的测试:

<?php

  public function testGetLetter()
    {
        $_box = new Box();
        $_box->setName('test-A');
        $_box->setId('abc01');
        $_boxId = $_box->getId();
        $ids = [];

        for ($i = 0; $i < 10; $i++) {
            $letter = new letter();
            $letter->setContent('content: ' . $i);
            $_box->addLetter($letter);
            $letter->setBox($_box);
            $ids[] = $_boxId;
        }

        $request = $this->createMock("Symfony\Component\HttpFoundation\Request");

        $boxRepository = $this->createMock(BoxRepository::class);
        $boxRepository->expects($this->once())
            ->method('find')
            ->willReturn($_box);

        $letterRepo = $this->createMock(LetterRepository::class);


        $documentManager = $this->getMockBuilder(DocumentManager::class)
            ->disableOriginalConstructor()
            ->setMethods(['getRepository'])
            ->getMock();

        $documentManager->expects($this->once())
            ->method('getRepository')
            ->with($this->equalTo('Bundle:Box'))
            ->will($this->returnValue($boxRepository));


        $documentManager->expects($this->once())
            ->method('getRepository')
            ->with($this->equalTo('Bundle:Letter'))
            ->will($this->returnValue($letterRepo));


        $boxControler = new boxController();


        $boxControler->getletters($palletId, $documentManager, $request);

    }
错误:

Testing Tests\Controller\BoxControllerTest

Expectation failed for method name is equal to "getRepository" when invoked 1 time(s)
Parameter 0 for invocation DocumentManagerDecorator::getRepository('Bundle:Box') does not match expected value.
Failed asserting that two strings are equal.
Expected :'Bundle:Letter'
Actual   :'Bundle:Box'
 <Click to see difference>

 /var/www/html/Controller/BoxController.php:151
 /var/www/html/Tests/Controller/BoxControllerTest.php:147
测试测试\Controller\BoxControllerTest
调用1次时,方法名称等于“getRepository”的预期失败
调用DocumentManagerDecorator::getRepository('Bundle:Box')的参数0与预期值不匹配。
断言两个字符串相等失败。
预期:'Bundle:Letter'
实际:'Bundle:Box'
/var/www/html/Controller/BoxController.php:151
/var/www/html/Tests/Controller/BoxControllerTest.php:147
我看到了,但我无法将响应应用于我的情况

您可以使用
at()

或者正如@iainn提到的,你在那篇帖子上看到了吗?听起来像是
returnValueMap
就是你要找的。试试
->with('Bundle:Box')
而不是
->with($this->equalTo('Bundle:Box'))
Testing Tests\Controller\BoxControllerTest

Expectation failed for method name is equal to "getRepository" when invoked 1 time(s)
Parameter 0 for invocation DocumentManagerDecorator::getRepository('Bundle:Box') does not match expected value.
Failed asserting that two strings are equal.
Expected :'Bundle:Letter'
Actual   :'Bundle:Box'
 <Click to see difference>

 /var/www/html/Controller/BoxController.php:151
 /var/www/html/Tests/Controller/BoxControllerTest.php:147
$mock->expects($this->at(0))
    ->method('foo')
    ->willReturn('firstValue');

$mock->expects($this->at(1))
    ->method('foo')
    ->willReturn('secondValue');