单元测试Guzzle服务描述

时间:2015-05-18 13:00:05

标签: php symfony guzzle

我无法测试Guzzle服务说明:

http://guzzle3.readthedocs.org/webservice-client/guzzle-service-descriptions.html

我试图遵循本指南:

http://docs.guzzlephp.org/en/latest/testing.html

创建了一个抽象的TestCase:

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use MyNamespace\ClientFactory;
use MyNamespace\Client;
use GuzzleHttp\Subscriber\Mock;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Stream\Stream;

class AbstractTestCase extends WebTestCase
{

    protected $client;
    protected $container;

    public function setUp()
    {
        $factory = new ClientFactory(
            new \GuzzleHttp\Client(),
            'baseUrl',
            []
        );
        $this->client = $factory->create();
        $this->container = static::createClient()->getContainer();
    }

    protected function setMock($status, $body,
        array $headers = ['Content-Type' => 'application/json'])
    {
        $mock = new Mock([
            new Response($status, $headers, Stream::factory($body))
        ]);
        $this->client->getEmitter()->attach($mock);
    }
}

我的测试:

class ClientTest extends \AbstractTestCase
{
    public function testGetBasket()
    {
        $this->setMock(200, '{"Basket": {"foo": "bar"}}');

        $response = $this->client->getBasket([
            'sessionId' => 'sessionId',
            'storeId' => 123,
            'transactionId' => 123
        ]);
    }
}

代码失败了:

Caused by
GuzzleHttp\Ring\Exception\ConnectException: cURL error 6: Could not resolve host: brestrictions

我根据Guzzle文档嘲笑客户端响应,但它似乎不适用于服务描述。

0 个答案:

没有答案