1

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

我正在使用 ZF2。

我正在使用返回纯文本(CSV)的网络服务。我想对我创建的服务进行单元测试。我目前有一个工作配置,我认为这不是正确的方法。我现在在对我的模型进行单元测试时使用模拟,我已经看到 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
}

SteeringWheel 服务

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
    }
}
4

1 回答 1

3

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

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

于 2013-03-12T22:23:03.807 回答