crm*_*cco 3 php phpunit unit-testing mocking guzzle
我最近在我的 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
Run Code Online (Sandbox Code Playgroud)
我的测试方法如下所示:
/**
* @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);
}
}
Run Code Online (Sandbox Code Playgroud)
第 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.
Run Code Online (Sandbox Code Playgroud)
小智 5
有我的解决方法。
use GuzzleHttp\Psr7\Stream;
$responseMock->expects($this->once())->method('getBody')->willReturn(new Stream(fopen('data://text/plain,' . $yourString,'r')));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1661 次 |
最近记录: |