/** * @param string $currencyFrom * @param string $currencyTo * @param int $retry * @return float|null */ protected function _convert($currencyFrom, $currencyTo, $retry = 0) { $url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, self::CURRENCY_CONVERTER_URL); $url = str_replace('{{CURRENCY_TO}}', $currencyTo, $url); try { sleep($this->_scopeConfig->getValue('currency/google/delay', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)); $response = $this->_httpClient->setUri($url)->setConfig(['timeout' => $this->_scopeConfig->getValue('currency/google/timeout', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)])->request('GET')->getBody(); $data = explode('bld>', $response); if (empty($data[1])) { $this->_messages[] = __('We can\'t retrieve a rate from %1.', $url); return null; } $data = explode($currencyTo, $data[1]); $rate = null; if (empty($data[0])) { $this->_messages[] = __('We can\'t retrieve a rate from %1.', $url); return null; } else { $rate = $data[0]; } return (double) $rate; } catch (\Exception $e) { if ($retry == 0) { $this->_convert($currencyFrom, $currencyTo, 1); } else { $this->_messages[] = __('We can\'t retrieve a rate from %1.', $url); } } }
/** * Create and return mock for http client factory * @return \PHPUnit_Framework_MockObject_MockObject */ private function getHttpClientFactoryMock() { $this->httpClientMock = $this->getMockBuilder('Magento\\Framework\\HTTP\\ZendClient')->disableOriginalConstructor()->setMethods(['request', 'getBody', '__wakeup'])->getMock(); $this->httpClientMock->expects(static::once())->method('request')->willReturnSelf(); $httpClientFactoryMock = $this->getMockBuilder('Magento\\Framework\\HTTP\\ZendClientFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $httpClientFactoryMock->expects(static::once())->method('create')->willReturn($this->httpClientMock); return $httpClientFactoryMock; }
/** * @magentoDataFixture Magento/Sales/_files/order_paid_with_payflowpro.php */ public function testReviewPaymentNullResponce() { /** @var \Magento\Sales\Model\Order $order */ $order = $this->_objectManager->create('Magento\\Sales\\Model\\Order'); $order->loadByIncrementId('100000001'); $this->_httpClientMock->expects($this->any())->method('request')->will($this->returnValue(new \Magento\Framework\Object(['body' => 'RESULTval=12&val2=34']))); $expectedResult = ['resultval' => '12', 'val2' => '34', 'result_code' => null, 'respmsg' => null]; $this->assertEquals($expectedResult, $this->_model->acceptPayment($order->getPayment())); }
/** * @expectedException \Exception */ public function testPostRequestFail() { $configInterfaceMock = $this->getMockBuilder('\\Magento\\Payment\\Model\\Method\\ConfigInterface')->getMockForAbstractClass(); $zendResponseMock = $this->getMockBuilder('\\Zend_Http_Response')->setMethods(['getBody'])->disableOriginalConstructor()->getMock(); $zendResponseMock->expects($this->never())->method('getBody'); $this->zendClientMock->expects($this->once())->method('request')->willThrowException(new \Exception()); $object = new \Magento\Framework\DataObject(); $this->object->postRequest($object, $configInterfaceMock); }
/** * @expectedException \Zend_Http_Client_Exception */ public function testPostRequestFail() { /** @var ConfigInterface|\PHPUnit_Framework_MockObject_MockObject $configInterfaceMock */ $configInterfaceMock = $this->getMockBuilder(ConfigInterface::class)->getMockForAbstractClass(); $zendResponseMock = $this->getMockBuilder(\Zend_Http_Response::class)->setMethods(['getBody'])->disableOriginalConstructor()->getMock(); $zendResponseMock->expects(static::never())->method('getBody'); $this->zendClientMock->expects(static::once())->method('request')->willThrowException(new \Zend_Http_Client_Exception()); $object = new DataObject(); $this->object->postRequest($object, $configInterfaceMock); }
/** * Returns HTTP request to events url * * @return \Magento\Framework\HTTP\ZendClient */ protected function getRequest() { if (!isset($this->request)) { $this->request = $this->clientFactory->create(); $this->request->setUri($this->getEventsUrl()); $insertKey = $this->config->getInsightsInsertKey(); $this->request->setMethod(ZendClient::POST); $this->request->setHeaders(['X-Insert-Key' => $insertKey, 'Content-Type' => 'application/json']); } return $this->request; }
/** * {@inheritdoc} */ public function postToConsumer($consumerId, $endpointUrl) { try { $consumer = $this->_consumerFactory->create()->load($consumerId); if (!$consumer->getId()) { throw new \Magento\Framework\Oauth\Exception(__('A consumer with ID %1 does not exist', $consumerId), OauthInterface::ERR_PARAMETER_REJECTED); } $consumerData = $consumer->getData(); $verifier = $this->_tokenFactory->create()->createVerifierToken($consumerId); $storeBaseUrl = $this->_storeManager->getStore()->getBaseUrl(); $this->_httpClient->setUri($endpointUrl); $this->_httpClient->setParameterPost(array('oauth_consumer_key' => $consumerData['key'], 'oauth_consumer_secret' => $consumerData['secret'], 'store_base_url' => $storeBaseUrl, 'oauth_verifier' => $verifier->getVerifier())); $maxredirects = $this->_dataHelper->getConsumerPostMaxRedirects(); $timeout = $this->_dataHelper->getConsumerPostTimeout(); $this->_httpClient->setConfig(array('maxredirects' => $maxredirects, 'timeout' => $timeout)); $this->_httpClient->request(\Magento\Framework\HTTP\ZendClient::POST); return $verifier->getVerifier(); } catch (\Magento\Framework\Model\Exception $exception) { throw $exception; } catch (\Magento\Framework\Oauth\Exception $exception) { throw $exception; } catch (\Exception $exception) { $this->_logger->logException($exception); throw new \Magento\Framework\Oauth\Exception('Unable to post data to consumer due to an unexpected error'); } }
/** * Tests client request with Bad status * * @return void */ public function testSendRequestStatusBad() { $json = '{"eventType":"Cron","appName":"app_name","appId":"app_id"}'; $statusBad = '401'; $uri = 'https://example.com/listener'; $method = ZendClient::POST; $headers = ['X-Insert-Key' => 'insert_key_value', 'Content-Type' => 'application/json']; $accId = 'acc_id'; $appId = 'app_id'; $appName = 'app_name'; $insightApiKey = 'insert_key_value'; $this->zendClientMock->expects($this->once())->method('setUri')->with($uri)->willReturnSelf(); $this->zendClientMock->expects($this->once())->method('setMethod')->with($method)->willReturnSelf(); $this->zendClientMock->expects($this->once())->method('setHeaders')->with($headers)->willReturnSelf(); $this->zendClientMock->expects($this->once())->method('setRawData')->with($json)->willReturnSelf(); $this->configMock->expects($this->once())->method('getNewRelicAccountId')->willReturn($accId); $this->configMock->expects($this->once())->method('getInsightsApiUrl')->willReturn($uri); $this->configMock->expects($this->once())->method('getInsightsInsertKey')->willReturn($insightApiKey); $this->configMock->expects($this->once())->method('getNewRelicAppName')->willReturn($appName); $this->configMock->expects($this->once())->method('getNewRelicAppId')->willReturn($appId); $this->jsonEncoderMock->expects($this->once())->method('encode')->willReturn($json); $zendHttpResponseMock = $this->getMockBuilder('Zend_Http_Response')->disableOriginalConstructor()->getMock(); $zendHttpResponseMock->expects($this->any())->method('getStatus')->willReturn($statusBad); $this->zendClientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock); $this->zendClientFactoryMock->expects($this->once())->method('create')->willReturn($this->zendClientMock); $this->assertInternalType('bool', $this->model->sendRequest()); }
public function testPostToConsumer() { $consumerId = 1; $key = $this->_generateRandomString(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_CONSUMER_KEY); $secret = $this->_generateRandomString(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_CONSUMER_SECRET); $oauthVerifier = $this->_generateRandomString(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN_VERIFIER); $consumerData = ['entity_id' => $consumerId, 'key' => $key, 'secret' => $secret]; $this->_consumerMock->expects($this->once())->method('load')->with($this->equalTo($consumerId))->will($this->returnSelf()); $this->_consumerMock->expects($this->once())->method('getId')->will($this->returnValue($consumerId)); $this->_consumerMock->expects($this->once())->method('getData')->will($this->returnValue($consumerData)); $this->_httpClientMock->expects($this->once())->method('setUri')->with('http://www.magento.com')->will($this->returnSelf()); $this->_httpClientMock->expects($this->once())->method('setParameterPost')->will($this->returnSelf()); $this->_tokenMock->expects($this->once())->method('createVerifierToken')->with($consumerId)->will($this->returnSelf()); $this->_tokenMock->expects($this->any())->method('getVerifier')->will($this->returnValue($oauthVerifier)); $this->_dataHelper->expects($this->once())->method('getConsumerPostMaxRedirects')->will($this->returnValue(5)); $this->_dataHelper->expects($this->once())->method('getConsumerPostTimeout')->will($this->returnValue(120)); $verifier = $this->_oauthService->postToConsumer($consumerId, 'http://www.magento.com'); $this->assertEquals($oauthVerifier, $verifier, 'Checking Oauth Verifier'); }
/** * @param string $currencyFrom * @param string $currencyTo * @param int $retry * @return float|null */ protected function _convert($currencyFrom, $currencyTo, $retry = 0) { $url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, $this->_url); $url = str_replace('{{CURRENCY_TO}}', $currencyTo, $url); try { $response = $this->_httpClient->setUri($url)->setConfig(array('timeout' => $this->_scopeConfig->getValue('currency/webservicex/timeout', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)))->request('GET')->getBody(); $xml = simplexml_load_string($response, null, LIBXML_NOERROR); if (!$xml) { $this->_messages[] = __('We can\'t retrieve a rate from %1.', $url); return null; } return (double) $xml; } catch (\Exception $e) { if ($retry == 0) { $this->_convert($currencyFrom, $currencyTo, 1); } else { $this->_messages[] = __('We can\'t retrieve a rate from %1.', $url); } } }
/** * Tests client request will fail */ public function testSetDeploymentRequestFail() { $data = $this->getDataVariables(); $this->zendClientMock->expects($this->once())->method('setUri')->with($data['uri'])->willReturnSelf(); $this->zendClientMock->expects($this->once())->method('setMethod')->with($data['method'])->willReturnSelf(); $this->zendClientMock->expects($this->once())->method('setHeaders')->with($data['headers'])->willReturnSelf(); $this->zendClientMock->expects($this->once())->method('setParameterPost')->with($data['params'])->willReturnSelf(); $this->configMock->expects($this->once())->method('getNewRelicApiUrl')->willReturn($data['uri']); $this->configMock->expects($this->once())->method('getNewRelicApiKey')->willReturn($data['api_key']); $this->configMock->expects($this->once())->method('getNewRelicAppName')->willReturn($data['app_name']); $this->configMock->expects($this->once())->method('getNewRelicAppId')->willReturn($data['app_id']); $this->zendClientMock->expects($this->once())->method('request')->willThrowException(new \Zend_Http_Client_Exception()); $this->loggerMock->expects($this->once())->method('critical'); $this->zendClientFactoryMock->expects($this->once())->method('create')->willReturn($this->zendClientMock); $this->assertInternalType('bool', $this->model->setDeployment($data['description'], $data['change'], $data['user'])); }
private function setClientTransferObjects() { $config = ['key1' => 'value1', 'key2' => 'value2']; $method = \Zend_Http_Client::POST; $headers = ['key1' => 'value1', 'key2' => 'value2']; $body = 'Body content'; $uri = 'https://example.com/listener'; $shouldEncode = true; $this->transferObjectMock->expects($this->once())->method('getClientConfig')->willReturn($config); $this->transferObjectMock->expects($this->atLeastOnce())->method('getMethod')->willReturn($method); $this->transferObjectMock->expects($this->once())->method('getHeaders')->willReturn($headers); $this->transferObjectMock->expects($this->once())->method('getBody')->willReturn($body); $this->transferObjectMock->expects($this->once())->method('shouldEncode')->willReturn($shouldEncode); $this->transferObjectMock->expects($this->once())->method('getUri')->willReturn($uri); $this->clientMock->expects($this->once())->method('setConfig')->with($config)->willReturnSelf(); $this->clientMock->expects($this->once())->method('setMethod')->with($method)->willReturnSelf(); $this->clientMock->expects($this->once())->method('setParameterPost')->with($body)->willReturnSelf(); $this->clientMock->expects($this->once())->method('setHeaders')->with($headers)->willReturnSelf(); $this->clientMock->expects($this->once())->method('setUrlEncodeBody')->with($shouldEncode)->willReturnSelf(); $this->clientMock->expects($this->once())->method('setUri')->with($uri)->willReturnSelf(); }