/** * Perform API request * * @param string $endpoint * @param string $apiKey * @return Varien_Object * @throws Exception * @throws Zend_Http_Client_Exception */ public function performHTTPRequest($endpoint, $apiKey) { /** @var RapidCampaign_Promotions_Model_HttpClient $client */ $client = Mage::getSingleton('rapidcampaign_promotions/httpClient'); $client->setUri($endpoint)->setMethod(Varien_Http_Client::GET)->setHeaders('Accept', 'application/json')->setHeaders('x-api-key', $apiKey); $response = $client->request(); $responseObject = new Varien_Object(); $responseObject->setResponse($response); // Magento 1.7 compatibility fix: chuncked body is already decoded, but header is not deleted if (strtolower($response->getHeader('transfer-encoding')) === 'chunked') { try { $body = $response->getBody(); } catch (Zend_Http_Exception $e) { $body = $response->getRawBody(); } } else { $body = $response->getBody(); } $responseObject->setBody($body); /** @var RapidCampaign_Promotions_Model_Log $logger */ $logger = Mage::getSingleton('rapidcampaign_promotions/log'); // Log API communication $logger->log('Request: ' . $endpoint . "\n\n" . $client->getHeadersAsString() . "\n" . $client->getBody() . "\n" . $response->getHeadersAsString() . "\n" . $responseObject->getBody() . "\n"); return $responseObject; }
/** * Create api client mock and replace `performHTTPRequest` method with callback * * @return $this * @throws PHPUnit_Framework_Exception */ protected function registerApiClientStub($httpCode, $body) { $responseObject = new Varien_Object(); $responseObject->setResponse(new Zend_Http_Response($httpCode, array(), $body)); $responseObject->setBody($body); $apiClientModelMock = $this->getModelMock('rapidcampaign_promotions/api_client', array('performHTTPRequest')); $apiClientModelMock->expects($this->any())->method('performHTTPRequest')->will($this->returnValue($responseObject)); if ($httpCode == 0) { $apiClientModelMock->method('performHTTPRequest')->will($this->throwException(new Zend_Http_Client_Exception('Unable to read response, or response is empty'))); } $this->replaceByMock('model', 'rapidcampaign_promotions/api_client', $apiClientModelMock); return $this; }
/** * @loadFixture ../../../var/fixtures/orders.yaml */ public function testControllerActionCheckoutOnepagePostdispatch() { $fakeController = new Varien_Object(); $fakeResponse = new Varien_Object(); $quotePayment = Mage::getModel('sales/quote_payment')->load(4); $pmMock = $this->getModelMock('ops/payment_bankTransfer', array('isAvailable')); $pmMock->expects($this->any())->method('isAvailable')->will($this->returnValue(true)); $quote = $this->getModelMock('sales/quote', array('getPayment')); $quote->expects($this->any())->method('getPayment')->will($this->returnValue($quotePayment)); $quotePayment->setQuote($quote); $quote->setPayment($quotePayment); $this->replaceByMock('model', 'ops/payment_bankTransfer', $pmMock); $fakeOnePage = new Varien_Object(); $fakeOnePage->setQuote($quote); $observerMock = $this->getModelMock('ops/observer', array('getOnepage')); $observerMock->expects($this->once())->method('getOnepage')->will($this->returnValue($fakeOnePage)); $helperMock = $this->getHelperMock('ops/payment_request', array('getOwnerParams', 'extractShipToParameters')); $helperMock->expects($this->once())->method('getOwnerParams')->will($this->returnValue(array())); $helperMock->expects($this->once())->method('extractShipToParameters')->will($this->returnValue(array())); $this->replaceByMock('helper', 'ops/payment_request', $helperMock); $validatorMock = $this->getModelMock('ops/validator_parameter_validator', array('isValid', 'getMessages')); $validatorMock->expects($this->once())->method('isValid')->will($this->returnValue(false)); $validatorMock->expects($this->any())->method('getMessages')->will($this->returnValue(array('Foo' => 'Not Valid'))); $this->replaceByMock('model', 'ops/validator_parameter_validator', $validatorMock); $fakeResponse->setBody(Mage::helper('core/data')->jsonEncode(array('error' => false, 'update_section' => 'foo'))); $fakeController->setResponse($fakeResponse); $event = new Varien_Event_Observer(); $event->setControllerAction($fakeController); $observerMock->controllerActionCheckoutOnepagePostdispatch($event); $result = Mage::helper('core/data')->jsonDecode($fakeResponse->getBody()); $this->assertArrayHasKey('error', $result); $this->assertArrayHasKey('goto_section', $result); $this->assertArrayHasKey('fields', $result); $this->assertArrayNotHasKey('update_section', $result); }