Example #1
0
 /**
  * Test action controller method with exception.
  */
 public function testCreateActionControllerWithException()
 {
     /** Create object of class which is not instance of Mage_Webapi_Controller_ActionAbstract. */
     $wrongController = new Varien_Object();
     /** Create request object. */
     $request = new Mage_Webapi_Controller_Request('SOAP');
     /** Mock object manager create method to return wrong controller */
     $this->_objectManagerMock->expects($this->any())->method('create')->will($this->returnValue($wrongController));
     $this->setExpectedException('InvalidArgumentException', 'The specified class is not a valid API action controller.');
     $this->_factory->createActionController('ClassName', $request);
 }
Example #2
0
 /**
  * Handler for all SOAP operations.
  *
  * @param string $operation
  * @param array $arguments
  * @return stdClass
  * @throws Mage_Webapi_Model_Soap_Fault
  * @throws Mage_Webapi_Exception
  */
 public function __call($operation, $arguments)
 {
     if (in_array($operation, $this->_requestHeaders)) {
         $this->_processSoapHeader($operation, $arguments);
     } else {
         try {
             if (is_null($this->_usernameToken)) {
                 throw new Mage_Webapi_Exception($this->_helper->__('WS-Security UsernameToken is not found in SOAP-request.'), Mage_Webapi_Exception::HTTP_UNAUTHORIZED);
             }
             $this->_authentication->authenticate($this->_usernameToken);
             $resourceVersion = $this->_getOperationVersion($operation);
             $resourceName = $this->_apiConfig->getResourceNameByOperation($operation, $resourceVersion);
             if (!$resourceName) {
                 throw new Mage_Webapi_Exception($this->_helper->__('Method "%s" is not found.', $operation), Mage_Webapi_Exception::HTTP_NOT_FOUND);
             }
             $controllerClass = $this->_apiConfig->getControllerClassByOperationName($operation);
             $controllerInstance = $this->_controllerFactory->createActionController($controllerClass, $this->_request);
             $method = $this->_apiConfig->getMethodNameByOperation($operation, $resourceVersion);
             $this->_authorization->checkResourceAcl($resourceName, $method);
             $arguments = reset($arguments);
             $arguments = get_object_vars($arguments);
             $versionAfterFallback = $this->_apiConfig->identifyVersionSuffix($operation, $resourceVersion, $controllerInstance);
             $this->_apiConfig->checkDeprecationPolicy($resourceName, $method, $versionAfterFallback);
             $action = $method . $versionAfterFallback;
             $arguments = $this->_helper->prepareMethodParams($controllerClass, $action, $arguments, $this->_apiConfig);
             $outputData = call_user_func_array(array($controllerInstance, $action), $arguments);
             return (object) array(self::RESULT_NODE_NAME => $outputData);
         } catch (Mage_Webapi_Exception $e) {
             throw new Mage_Webapi_Model_Soap_Fault($e->getMessage(), $e->getOriginator(), $e);
         } catch (Exception $e) {
             $maskedException = $this->_errorProcessor->maskException($e);
             throw new Mage_Webapi_Model_Soap_Fault($maskedException->getMessage(), Mage_Webapi_Model_Soap_Fault::FAULT_CODE_RECEIVER, $maskedException);
         }
     }
 }
Example #3
0
 /**
  * Handle REST request.
  *
  * @return Mage_Webapi_Controller_Dispatcher_Rest
  */
 public function dispatch()
 {
     try {
         $this->_authentication->authenticate();
         $route = $this->_router->match($this->_request);
         $operation = $this->_request->getOperationName();
         $resourceVersion = $this->_request->getResourceVersion();
         $this->_apiConfig->validateVersionNumber($resourceVersion, $this->_request->getResourceName());
         $method = $this->_apiConfig->getMethodNameByOperation($operation, $resourceVersion);
         $controllerClassName = $this->_apiConfig->getControllerClassByOperationName($operation);
         $controllerInstance = $this->_controllerFactory->createActionController($controllerClassName, $this->_request);
         $versionAfterFallback = $this->_apiConfig->identifyVersionSuffix($operation, $resourceVersion, $controllerInstance);
         /**
          * Route check has two stages:
          * The first is performed against full list of routes that is merged from all resources.
          * The second stage of route check can be performed only when actual version to be executed is known.
          */
         $this->_router->checkRoute($this->_request, $method, $versionAfterFallback);
         $this->_apiConfig->checkDeprecationPolicy($route->getResourceName(), $method, $versionAfterFallback);
         $action = $method . $versionAfterFallback;
         $this->_authorization->checkResourceAcl($route->getResourceName(), $method);
         $inputData = $this->_restPresentation->fetchRequestData($controllerInstance, $action);
         $outputData = call_user_func_array(array($controllerInstance, $action), $inputData);
         $this->_restPresentation->prepareResponse($method, $outputData);
     } catch (Exception $e) {
         $this->_response->setException($e);
     }
     $this->_response->sendResponse();
     return $this;
 }
Example #4
0
 /**
  * Test dispatch method.
  */
 public function testDispatch()
 {
     $this->_authenticationMock->expects($this->once())->method('authenticate');
     /** Init route mock. */
     $routeMock = $this->getMockBuilder('Mage_Webapi_Controller_Router_Route_Rest')->disableOriginalConstructor()->getMock();
     $routeMock->expects($this->any())->method('getResourceName');
     $this->_routerMock->expects($this->once())->method('match')->will($this->returnValue($routeMock));
     /** Mock Api Config getMethodNameByOperation method to return isDeleted method of Varien_Onject. */
     $this->_apiConfigMock->expects($this->once())->method('getMethodNameByOperation')->will($this->returnValue('isDeleted'));
     /** Mock Api config identifyVersionSuffix method to return empty string. */
     $this->_apiConfigMock->expects($this->once())->method('identifyVersionSuffix')->will($this->returnValue(''));
     $this->_apiConfigMock->expects($this->once())->method('checkDeprecationPolicy');
     $this->_authorizationMock->expects($this->once())->method('checkResourceAcl');
     /** Create fake controller mock, e. g. Varien_Object object. */
     $controllerMock = $this->getMockBuilder('Varien_Object')->disableOriginalConstructor()->getMock();
     /** Assert isDeleted method will be executed once. */
     $controllerMock->expects($this->once())->method('isDeleted');
     /** Mock factory mock to return fake action controller. */
     $this->_controllerFactory->expects($this->once())->method('createActionController')->will($this->returnValue($controllerMock));
     /** Mock Rest presentation fetchRequestData method to return empty array. */
     $this->_restPresentation->expects($this->once())->method('fetchRequestData')->will($this->returnValue(array()));
     /** Assert response sendResponse method will be executed once. */
     $this->_responseMock->expects($this->once())->method('sendResponse');
     $this->_restDispatcher->dispatch();
 }