W3Cschool
恭喜您成為首批注冊(cè)用戶(hù)
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
當(dāng)應(yīng)用程序需要和 web 服務(wù)進(jìn)行交互時(shí),會(huì)想要在不與 web 服務(wù)進(jìn)行實(shí)際交互的情況下對(duì)其進(jìn)行測(cè)試。為了給 web 服務(wù)創(chuàng)建樁件或仿件,可以像使用 ?getMock()
?那樣使用 ?getMockFromWsdl()
?。唯一的區(qū)別是 ?getMockFromWsdl()
? 所返回的樁件或者仿件是基于以 WSDL 描述的 web 服務(wù),而 ?getMock()
? 返回的樁件或者仿件是基于 PHP 類(lèi)或接口的。
示例 8.20 展示了如何用 ?getMockFromWsdl()
? 來(lái)對(duì)(例如)?GoogleSearch.wsdl
? 中描述的 web 服務(wù)上樁。
示例 8.20 給 web 服務(wù)上樁
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class GoogleTest extends TestCase
{
public function testSearch(): void
{
$googleSearch = $this->getMockFromWsdl(
'GoogleSearch.wsdl', 'GoogleSearch'
);
$directoryCategory = new stdClass;
$directoryCategory->fullViewableName = '';
$directoryCategory->specialEncoding = '';
$element = new stdClass;
$element->summary = '';
$element->URL = 'https://phpunit.de/';
$element->snippet = '...';
$element->title = '<b>PHPUnit</b>';
$element->cachedSize = '11k';
$element->relatedInformationPresent = true;
$element->hostName = 'phpunit.de';
$element->directoryCategory = $directoryCategory;
$element->directoryTitle = '';
$result = new stdClass;
$result->documentFiltering = false;
$result->searchComments = '';
$result->estimatedTotalResultsCount = 3.9000;
$result->estimateIsExact = false;
$result->resultElements = [$element];
$result->searchQuery = 'PHPUnit';
$result->startIndex = 1;
$result->endIndex = 1;
$result->searchTips = '';
$result->directoryCategories = [];
$result->searchTime = 0.248822;
$googleSearch->expects($this->any())
->method('doGoogleSearch')
->will($this->returnValue($result));
/**
* $googleSearch->doGoogleSearch() 現(xiàn)在會(huì)返回樁結(jié)果,
* 并不會(huì)調(diào)用 web 服務(wù)的 doGoogleSearch() 方法。
*/
$this->assertEquals(
$result,
$googleSearch->doGoogleSearch(
'00000000000000000000000000000000',
'PHPUnit',
0,
1,
false,
'',
false,
'',
'',
''
)
);
}
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話(huà):173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: