123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?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'));
- }
- }
|