/** * Method caller * @param string $method * @param array $args * @throws Exception */ public function callMethod($method, $args) { $this->log("Calling rest method: " . $method, Zend_Log::INFO); if ($restMethod = $this->getRestMethod($method)) { //Start plugins $this->setupPlugins($method); //Plugins: Call prepareArguments hook $rawArguments = $this->prepareArguments(isset($args) ? $args : null); //Plugins: Call preparePath hook $path = $this->preparePath($restMethod); //Creating Rest Client $restClient = new App_Rest_Client($path); //Plugins: Call setupHttpClient hook $this->setupHttpClient($restClient->getHttpClient()); $query = null; if (isset($rawArguments['$query'])) { $query = $rawArguments['$query']; unset($rawArguments['$query']); } //Prepare arguments with output processor $arguments = $this->processOutputData($restMethod, $rawArguments); $this->processRequest($restClient->getUri()->getPath(), $arguments); try { $start = microtime(TRUE); if (!$this->getUseMocks()) { //Call RPC $this->_lastResult = $restClient->{'rest' . $restMethod->method}($restClient->getUri()->getPath(), $arguments, $query); } else { if (!isset($restMethod->mock)) { throw new Exception("No mock config for method '{$method}'."); } else { //Create a mock result $this->_lastResult = $this->_createMock($restMethod, $restClient->getUri()->getPath(), $arguments, $rawArguments, $query); } } $start = microtime(TRUE) - $start; $this->_lastResultTimeSpent = $start; $url = $restClient->getUri()->getPath(); if ($start > 0.2) { $this->log("Too much time spent ({$url}): {$start}", Zend_Log::WARN); } } catch (Exception $e) { $this->log($e, Zend_Log::CRIT); //Plugins: Call processException hook $this->processException($e); } //Plugins: Call processResponse hook $this->processResponse($this->_lastResult); //Plugins: Call shutdownHttpClient hook $this->shutdownHttpClient($restClient->getHttpClient()); try { //Parse response with input processor $result = $this->processInputData($restMethod, $this->_lastResult); } catch (Exception $e) { $this->log($e, Zend_Log::CRIT); //Plugins: Call processException hook $this->processException($e); throw $e; } //Plugins: Call processResponseData hook $this->processResponseData($result); return $result; } else { $this->log('Method does not exist: ' . $method, Zend_Log::CRIT); throw new Exception('Method does not exist: ' . $method); } }
public function testRestPut() { $expXml = file_get_contents($this->path . 'returnString.xml'); $response = "HTTP/1.0 200 OK\r\n" . "X-powered-by: PHP/5.2.0\r\n" . "Content-type: text/xml\r\n" . "Content-length: " . strlen($expXml) . "\r\n" . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n" . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n" . "Connection: close\r\n" . "\r\n" . $expXml; $this->adapter->setResponse($response); $reqXml = file_get_contents($this->path . 'returnInt.xml'); $response = $this->rest->restPut('/rest/', $reqXml); $this->assertTrue($response instanceof Zend_Http_Response); $body = $response->getBody(); $this->assertContains($expXml, $response->getBody()); $request = App_Rest_Client::getHttpClient()->getLastRequest(); $this->assertContains($reqXml, $request, $request); }