/** * 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); }
/** * 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'); }
/** * 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(); }