PHPUnitManual:10.3

From 흡혈양파의 번역工房
Jump to navigation Jump to search
10.3 웹 서비스의 stub 및 mock

때로는 웹 서비스와 상호 작용하는 어플리케이션을, 실제로 웹 서비스와 상호 작용하지 않고 테스트할 필요가 있습니다. 웹 서비스의 stub 나 mock 를 쉽게 만들 수 있도록 getMockFromWsdl() 가 준비되어 있습니다. 이는 getMock() (앞 내용을 참조하세요) 와 거의 같은 방식으로 사용할 수 있습니다. 유일한 차이는, getMockFromWsdl() 가 반환하는 stub 나 mock 이 PHP 의 클래스나 인터페이스를 기반으로 한다는 점입니다.


예10.16 "웹 서비스의 stub" 는 getMockFromWsdl() 를 사용하여 GoogleSearch.wsdl 에 기술된 웹 서비스의 stub 를 생성하는 예입니다.


예10.16 웹 서비스의 stub

<?php
class GoogleTest extends PHPUnit_Framework_TestCase
{
    public function testSearch()
    {
        $googleSearch = $this->getMockFromWsdl(
          'GoogleSearch.wsdl', 'GoogleSearch'
        );
 
        $directoryCategory = new StdClass;
        $directoryCategory->fullViewableName = '';
        $directoryCategory->specialEncoding = '';
 
        $element = new StdClass;
        $element->summary = '';
        $element->URL = 'http://www.phpunit.de/';
        $element->snippet = '...';
        $element->title = '<b>PHPUnit</b>';
        $element->cachedSize = '11k';
        $element->relatedInformationPresent = TRUE;
        $element->hostName = 'www.phpunit.de';
        $element->directoryCategory = $directoryCategory;
        $element->directoryTitle = '';
 
        $result = new StdClass;
        $result->documentFiltering = FALSE;
        $result->searchComments = '';
        $result->estimatedTotalResultsCount = 378000;
        $result->estimateIsExact = FALSE;
        $result->resultElements = array($element);
        $result->searchQuery = 'PHPUnit';
        $result->startIndex = 1;
        $result->endIndex = 1;
        $result->searchTips = '';
        $result->directoryCategories = array();
        $result->searchTime = 0.248822;
 
        $googleSearch->expects($this->any())
                     ->method('doGoogleSearch')
                     ->will($this->returnValue($result));
 
        /**
         * $googleSearch->doGoogleSearch() will now return a stubbed result and
         * the web service's doGoogleSearch() method will not be invoked.
         */
        $this->assertEquals(
          $result,
          $googleSearch->doGoogleSearch(
            '00000000000000000000000000000000',
            'PHPUnit',
            0,
            1,
            FALSE,
            '',
            FALSE,
            '',
            '',
            ''
          )
        );
    }
}
?>


Notes