php - 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.
解决方案
推荐阅读
- python-3.x - 按索引自然排序 pandas DataFrame
- python - 如果列表中包含特定列中的值,我想在另一列中检查它
- python - seaborn relplot:如何控制图例的位置并添加标题
- python - 使用 BeautifulSoup 查找所有 HTML 元素
- javascript - onClick 事件后 React 组件未更新
- .net - 如何在 .NET 中以编程方式将颜色代码编号从 0 到 140 转换为 RBG 代码?
- bash - 如何让退格键在 Vim 中发送 ^H?
- android-architecture-components - 有什么方法可以让 retrofit2 返回 LiveData
- html - Angular 2 方式绑定,一个字段启用,另一个字段禁用
- html - ruby on rails 为什么在视图中加载css时布局没有改变?