首页 > 解决方案 > Mock_Response::getBody() 的返回值必须是 Psr\Http\Message\StreamInterface 的实例,在 PHPUnit 测试中返回的字符串与 9.5.10

问题描述

我最近在我的 Symfony 4 应用程序中升级到 PHPUnit 9.5.10,现在我似乎有一堆失败的测试与我正在使用 Guzzle 对端点进行的 HTTP 调用相关。

失败是:

1) Tests\AppBundle\Services\CourseTest::testUpdateAssignedCourseFromKey with data set "valid Course, current refkey" (AppBundle\Entity\Referral Object (...), array(true), AppBundle\Entity\Site Object (...), 'lambda')
TypeError: Return value of Mock_Response_0807f175::getBody() must be an instance of Psr\Http\Message\StreamInterface, string returned

/private/var/www/crmpicco/src/AppBundle/Services/Course.php:250
/private/var/www/crmpicco/src/AppBundle/Services/Course.php:187
/private/var/www/crmpicco/src/AppBundle/Services/Course.php:92
/private/var/www/crmpicco/Tests/AppBundle/Services/CourseTest.php:99

我的测试方法如下所示:

    /**
     * @dataProvider refkeyProvider
     *
     * @param Referral $referral
     * @param Site     $site
     * @param $expectedpartner
     *
     * @internal param Person $person
     */
    public function testUpdateAssignedCourseFromKey(Referral $referral, $apiresult, Site $site, $expectedCourse)
    {
        $apiresult = json_encode($apiresult);
        $response = $this->getMockBuilder('GuzzleHttp\Psr7\Response')->getMock();
        $response->expects($this->any())
            ->method('getBody')
            ->will($this->returnValue($apiresult));
        $client = $this->getMockBuilder('\GuzzleHttp\Client')->getMock();
        $client->expects($this->any())
            ->method('request')
            ->will($this->returnValue($response));

        $partner = new Course($this->username, $this->password, $client);
        $partner->setDoctrine($this->getDoctrine());

        $owner = $site->getOwner();
        $partner->updateAssignedCourseFromKey($site, $referral);

        $this->assertEquals($owner->getRefkey()->getRefkey(), $referral->getRefkey());
        $this->assertEquals($site->getRefkey()->getRefkey(), $referral->getRefkey());

        if (self::ANY_COURSE == $expectedcourse) {
            $this->assertNotEmpty($site->getOwner()->getCourseShortname());
        } else {
            $this->assertEquals($site->getOwner()->getCourseShortname(), $expectedcourse);
        }
    }

第 250 行是$data = json_decode($response->getBody());使用 Guzzle 的 HTTP 调用的响应。

composer show | grep guzzle 
guzzlehttp/guzzle                    7.4.0              Guzzle is a PHP HTTP client library
guzzlehttp/promises                  1.5.1              Guzzle promises library
guzzlehttp/psr7                      2.1.0              PSR-7 message implementation that also provides common utility methods

composer show | grep phpunit
phpunit/php-code-coverage            9.2.7              Library that provides collection, processing, and rendering functionality for PHP code coverage i...
phpunit/php-file-iterator            3.0.5              FilterIterator implementation that filters files based on a list of suffixes.
phpunit/php-invoker                  3.1.1              Invoke callables with a timeout
phpunit/php-text-template            2.0.4              Simple template engine.
phpunit/php-timer                    5.0.3              Utility class for timing
phpunit/phpunit                      9.5.10             The PHP Unit Testing framework.

标签: phpunit-testingmockingphpunitguzzle

解决方案


推荐阅读