예제 #1
0
 /**
  * Tests the requestApi function triggering the error case
  * @test
  */
 public function requestApiTestError()
 {
     //Desired Results
     $responseBody = false;
     $responseInfo = array('http_code' => 666, 'content_type' => 'test');
     $responseError = array('test' => true);
     $this->_setMockProperties('_curlExec', $responseBody);
     $this->_setMockProperties('_curlInfo', $responseInfo);
     $this->_setMockProperties('_curlError', $responseError);
     //using POST to test the else case
     $result = $this->_curlObject->requestApi("", array(), 'POST');
     $this->assertEquals(array('header' => array('status' => $responseInfo['http_code'], 'reason' => null), 'body' => array('error' => $responseError)), $result);
 }
예제 #2
0
 /**
  * Sends a request based on the provided request model and according to the argumented method
  * @param \Paymill\Models\Request\Base $model
  * @param string $method (Create, update, delete, getAll, getOne)
  * @throws PaymillException
  * @return \Paymill\Models\Response\Base|\Paymill\Models\Response\Error
  */
 private function _request(Base $model, $method)
 {
     if (!is_a($this->_connectionClass, '\\Paymill\\API\\CommunicationAbstract')) {
         throw new PaymillException(null, 'The connenction class is missing!');
     }
     $httpMethod = $this->_getHTTPMethod($method);
     $parameter = $model->parameterize($method);
     $serviceResource = $model->getServiceResource() . $model->getId();
     try {
         $this->_lastRequest = $parameter;
         $response = $this->_connectionClass->requestApi($serviceResource, $parameter, $httpMethod);
         $this->_lastResponse = $response;
         $responseHandler = new ResponseHandler();
         if ($method === 'getAll') {
             if ($responseHandler->validateResponse($response)) {
                 $convertedResponse = $response['body']['data'];
             } else {
                 $convertedResponse = $responseHandler->convertResponse($response, $model->getServiceResource());
             }
         } else {
             $convertedResponse = $responseHandler->convertResponse($response, $model->getServiceResource());
         }
     } catch (Exception $e) {
         $errorModel = new Error();
         $convertedResponse = $errorModel->setErrorMessage($e->getMessage());
     }
     if (is_a($convertedResponse, '\\Paymill\\Models\\Response\\Error')) {
         throw new PaymillException($convertedResponse->getResponseCode(), $convertedResponse->getErrorMessage(), $convertedResponse->getHttpStatusCode());
     }
     return $convertedResponse;
 }
예제 #3
0
 /**
  * Sends a request based on the provided request model and according to the argumented method
  * @param \Paymill\Models\Request\Base $model
  * @param string $method (Create, update, delete, getAll, getOne)
  * @throws PaymillException
  * @return \Paymill\Models\Response\Base|\Paymill\Models\Response\Error
  */
 private function _request(Base $model, $method)
 {
     if (!is_a($this->_connectionClass, '\\Paymill\\API\\CommunicationAbstract')) {
         throw new PaymillException(null, 'The connection class is missing!');
     }
     $convertedResponse = null;
     $httpMethod = $this->_getHTTPMethod($method);
     $parameter = $model->parameterize($method);
     $serviceResource = $model->getServiceResource() . $model->getId();
     if (is_a($model, "\\Paymill\\Models\\Request\\Transaction") && $method === "create") {
         $source = !array_key_exists('source', $parameter) ? "PhpLib" . $this->getVersion() : "PhpLib" . $this->getVersion() . "_" . $parameter['source'];
         $parameter['source'] = $source;
     }
     try {
         $this->_lastRequest = $parameter;
         $response = $this->_connectionClass->requestApi($serviceResource, $parameter, $httpMethod);
         var_dump($parameter);
         var_dump($response);
         $this->_lastResponse = $response;
         $responseHandler = new ResponseHandler();
         if ($method === "getAllAsModel" && $responseHandler->validateResponse($response) && $this->_util->isNumericArray($response['body']['data'])) {
             foreach ($response['body']['data'] as $object) {
                 $convertedResponse[] = $responseHandler->convertResponse($object, $model->getServiceResource());
             }
         } elseif ($method === "getAll" && $responseHandler->validateResponse($response)) {
             $convertedResponse = $response['body']['data'];
         } elseif ($responseHandler->validateResponse($response)) {
             $convertedResponse = $responseHandler->convertResponse($response['body']['data'], $model->getServiceResource());
         } else {
             $convertedResponse = $responseHandler->convertErrorToModel($response, $model->getServiceResource());
         }
     } catch (\Exception $e) {
         $errorModel = new Error();
         $convertedResponse = $errorModel->setErrorMessage($e->getMessage());
     }
     if (is_a($convertedResponse, '\\Paymill\\Models\\Response\\Error')) {
         throw new PaymillException($convertedResponse->getResponseCode(), $convertedResponse->getErrorMessage(), $convertedResponse->getHttpStatusCode(), $convertedResponse->getRawObject());
     }
     return $convertedResponse;
 }