【问题标题】:phpunit mock web service(not WSDL)phpunit 模拟 Web 服务(不是 WSDL)
【发布时间】:2013-03-12 17:09:40
【问题描述】:

我有一个小问题,我认为对于有经验的 PHPUnit 用户来说很容易解决。

我正在使用 ZF2。

我正在使用返回纯文本 (CSV) 的 Web 服务。我想对我创建的服务进行单元测试。 我目前有一个工作配置,我认为这不是正确的方法。我现在在对我的模型进行单元测试时使用模拟,我已经看到 PHPUnit 有一个特殊的网络服务模拟,但只有支持 WSDL。

您会在下面找到我的代码,我希望有人能帮助我解释一下这种情况下的最佳做法。

这里的文档和主题并没有帮助我(还)。

提前致谢!

测试本身:

public function testCanSearchSteeringWheels()
{
    // Create the entry and fill it with the data that should be retrieved from the web service
    $steeringWheelEntity = new SteeringWheelEntity();
    $steeringWheelEntity->setId('170633')
                        ->setName('Nice steering wheel one')
                        ->setGrossPrice(100)
                        ->setNetPrice(75);    

    // Setup the http client which whill make the final call to the web service
    $httpClient = new Client();
    $httpClient->setOptions(array(
        'maxredirects' => 5,
        'timeout'      => 60,
    ))
    ->setAuth($this->config['supplier_name']['api']['username'], $this->config['supplier_name']['api']['password'])
    ;

    $steeringWheelService = new SteeringWheelService($httpClient, new Request(), $this->config['supplier_name']);

    // Search for a steering wheel by id code
    $searchResult = $steeringWheelService->search('ID=5221552658987');

    $this->assertEquals($steeringWheelEntity, $searchResult[0]);
}

SteeringWheelEntity

namespace SupplierName\Entity;

class SteeringWheelEntity
{
    // vars
    // exchange array method
    // getters methods
    // setters methods
}

SteeringWheelService

namespace SupplierName\Service;

use SupplierName\Entity\SteeringWheelEntity;

class SteeringWheelService extends AbstractWebService
{           
    /**
     * search()
     * 
     * @param string $param
     * @return array
     */
    public function search($param)
    {
        $this->appendUrl('ww0800?3,' . $param);

        $response   = $this->dispatch();                
        $parsedBody = $this->parse($response->getBody());
        $entities   = array();

        foreach ($parsedBody as $data)
        {
            $steeringWheel = new SteeringWheelEntity();
            // Fill SteeringWheelEntity with data
            $entities[] = $steeringWheel;
        }

        return $entities;
    }
}

抽象网络服务

use \Zend\Http\Client;
use \Zend\Http\Request;

class AbstractWebService
{
    private $httpClient;
    private $request;
    private $response;

    protected $config;

    private $url;

    public function __construct(Client $httpClient, Request $request, Array $config)
    {  
        $this->url        = $config['api']['url'];        
        $this->httpClient = $httpClient;        
        $this->request    = $request;        
        $this->config     = $config;
    }

    protected function setUrl($url)
    {
        $this->url = $url;

        return $this->url;
    }

    protected function appendUrl($string)
    {
        $this->url .= $string;
    }

    protected function getUrl()
    {
        return $this->url;
    }

    public function dispatch()
    {
        $this->request->setUri($this->getUrl());

        $this->response = $this->httpClient->dispatch($this->request);

        if (!$this->response->isSuccess()) {
            throw new \Exception('HTTP error #' . $this->response->getStatusCode() . ' when connecting to ' . $this->getUrl() . '.');
        }

        return $this->response;
    }

    public function parse()
    {
        // Parse the content
    }
}

【问题讨论】:

    标签: unit-testing mocking phpunit automated-tests zend-framework2


    【解决方案1】:

    而不是为 Web 服务使用模拟。您可以模拟 \Zend\Http\Request\Zend\Http\Client 对象,因为它们正在为您工作吗?这样,您就可以控制 Zend 对象返回给您的内容,而不必尝试模拟 Web 服务。

    这就是我测试服务的方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多