HttpClientTest.php 6.01 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
<?php
/**
 * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
 */
namespace Temando\Shipping\Webservice;

use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\Helper\Bootstrap;
use Temando\Shipping\Webservice\Exception\HttpRequestException;
use Temando\Shipping\Webservice\Exception\HttpResponseException;

/**
 * HttpClientTest
 *
 * @package  Temando\Shipping\Test\Integration
 * @author   Christoph Aßmann <christoph.assmann@netresearch.de>
 * @license  http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 * @link     http://www.temando.com/
 */
class HttpClientTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var ObjectManager
     */
    private $objectManager;

    /**
     * Init object manager
     */
    public function setUp()
    {
        parent::setUp();

        $this->objectManager = Bootstrap::getObjectManager();
    }

    /**
     * @test
     */
    public function setHeaders()
    {
        $zendClient = new \Zend\Http\Client();
        $headers = [
            'foo' => 'bar',
            'fox' => 'baz',
        ];

        /** @var HttpClient $httpClient */
        $httpClient = $this->objectManager->create(HttpClient::class, ['client' => $zendClient]);
        $httpClient->setHeaders($headers);

        foreach ($headers as $key => $value) {
            $this->assertEquals($value, $zendClient->getHeader($key));
        }
    }

    /**
     * @test
     */
    public function setUri()
    {
        $zendClient = new \Zend\Http\Client();
        $uri = 'https://example.org/';

        /** @var HttpClient $httpClient */
        $httpClient = $this->objectManager->create(HttpClient::class, ['client' => $zendClient]);
        $httpClient->setUri($uri);

        $this->assertEquals($uri, $zendClient->getUri());
    }

    /**
     * @test
     */
    public function setOptions()
    {
        $zendClient = new \Zend\Http\Client();
        $options = ['trace' => 1, 'maxredirects' => 23, 'timeout' => 42, 'useragent' => 'Foo'];

        /** @var HttpClient $httpClient */
        $httpClient = $this->objectManager->create(HttpClient::class, ['client' => $zendClient]);
        $httpClient->setOptions($options);

        foreach ($options as $key => $value) {
            $adapterConfig = $zendClient->getAdapter()->getConfig();
            $this->assertEquals($value, $adapterConfig[$key]);
        }
    }

    /**
     * @test
     */
    public function setBody()
    {
        $zendClient = new \Zend\Http\Client();
        $body = '{"error": "foo"}';

        /** @var HttpClient $httpClient */
        $httpClient = $this->objectManager->create(HttpClient::class, ['client' => $zendClient]);
        $httpClient->setRawBody($body);

        $this->assertEquals($body, $zendClient->getRequest()->getContent());
    }

    /**
     * @test
     */
    public function setQueryParams()
    {
        $zendClient = new \Zend\Http\Client();
        $query = [
            'limit' => 23,
            'offset' => 42
        ];

        /** @var HttpClient $httpClient */
        $httpClient = $this->objectManager->create(HttpClient::class, ['client' => $zendClient]);
        $httpClient->setParameterGet($query);

        foreach ($query as $key => $value) {
            $this->assertEquals($value, $zendClient->getRequest()->getQuery($key));
        }
    }

    /**
     * @test
     */
    public function sendRequestError()
    {
        $eMsg = 'Unknown Foo';
        $this->expectException(HttpRequestException::class);
        $this->expectExceptionMessage($eMsg);

        $zendClient = $this->getMockBuilder(\Zend\Http\Client::class)
            ->setMethods(['send', 'setMethod'])
            ->getMock();
        $zendClient
            ->expects($this->once())
            ->method('send')
            ->willThrowException(new \Zend\Http\Exception\RuntimeException($eMsg));

        /** @var HttpClient $httpClient */
        $httpClient = $this->objectManager->create(HttpClient::class, ['client' => $zendClient]);
        $httpClient->send('FOO');
    }

    /**
     * @test
     */
    public function sendResponseError()
    {
        $eMsg = 'Unknown Foo';
        $this->expectException(HttpResponseException::class);
        $this->expectExceptionMessage($eMsg);

        $response = $this->getMockBuilder(\Zend\Http\Response::class)
            ->setMethods(['isSuccess', 'getBody'])
            ->getMock();
        $response
            ->expects($this->once())
            ->method('isSuccess')
            ->willReturn(false);
        $response
            ->expects($this->once())
            ->method('getBody')
            ->willReturn($eMsg);

        $zendClient = $this->getMockBuilder(\Zend\Http\Client::class)
            ->setMethods(['send', 'setMethod'])
            ->getMock();
        $zendClient
            ->expects($this->once())
            ->method('send')
            ->willReturn($response);

        /** @var HttpClient $httpClient */
        $httpClient = $this->objectManager->create(HttpClient::class, ['client' => $zendClient]);
        $httpClient->send('FOO');
    }

    /**
     * @test
     */
    public function sendSuccess()
    {
        $responseBody = '{"hooray": true}';

        $response = $this->getMockBuilder(\Zend\Http\Response::class)
            ->setMethods(['isSuccess', 'getBody'])
            ->getMock();
        $response
            ->expects($this->once())
            ->method('isSuccess')
            ->willReturn(true);
        $response
            ->expects($this->once())
            ->method('getBody')
            ->willReturn($responseBody);

        $zendClient = $this->getMockBuilder(\Zend\Http\Client::class)
            ->setMethods(['send', 'setMethod'])
            ->getMock();
        $zendClient
            ->expects($this->once())
            ->method('send')
            ->willReturn($response);

        /** @var HttpClient $httpClient */
        $httpClient = $this->objectManager->create(HttpClient::class, ['client' => $zendClient]);
        $this->assertEquals($responseBody, $httpClient->send('FOO'));
    }
}