function testWrapCommandResultEncodesResult()
 {
     $result = new tx_caretakerinstance_CommandResult(true, new tx_caretakerinstance_OperationResult(true, array('foo' => 'bar')));
     $data = $result->toJson();
     $this->securityManager->expects($this->once())->method('encodeResult')->with($this->equalTo($data))->will($this->returnValue('Encoded result data'));
     $wrap = $this->commandService->wrapCommandResult($result);
     $this->assertEquals('Encoded result data', $wrap);
 }
 function testCommandResultFromJson()
 {
     $json = '{"status":0,"results":[{"status":true,"value":"foo"},{"status":true,"value":false},{"status":true,"value":["foo","bar"]}],"message":"Test message"}';
     $result = tx_caretakerinstance_CommandResult::fromJson($json);
     $this->assertInstanceOf('tx_caretakerinstance_CommandResult', $result);
     $this->assertEquals('Test message', $result->getMessage());
     $this->assertTrue($result->isSuccessful());
     $this->assertEquals(array(new tx_caretakerinstance_OperationResult(true, 'foo'), new tx_caretakerinstance_OperationResult(true, false), new tx_caretakerinstance_OperationResult(true, array('foo', 'bar'))), $result->getOperationResults());
 }
 /**
  * Execute the given command request
  *
  * @param tx_caretakerinstance_CommandRequest $commandRequest
  * @return tx_caretakerinstance_CommandResult
  */
 public function executeRequest($commandRequest)
 {
     $httpRequestResult = $this->executeHttpRequest($commandRequest->getServerUrl(), array('st' => $commandRequest->getSessionToken(), 'd' => $commandRequest->getData(), 's' => $commandRequest->getSignature()));
     if (is_array($httpRequestResult)) {
         if ($httpRequestResult['info']['http_code'] === 200) {
             $json = $this->securityManager->decodeResult($httpRequestResult['response']);
             // TODO: check if valid json
             if ($json) {
                 return tx_caretakerinstance_CommandResult::fromJson($json);
             } else {
                 if (!empty($httpRequestResult['response'])) {
                     $json = json_decode($httpRequestResult['response'], TRUE);
                     if ($json && $json['status'] == -1) {
                         return $this->getCommandResult(tx_caretakerinstance_CommandResult::status_undefined, NULL, 'Error while executing remote command: ' . $json['message'] . ' (' . $json['exception']['code'] . ')');
                     }
                 }
                 return $this->getCommandResult(tx_caretakerinstance_CommandResult::status_undefined, NULL, 'Cant decode remote command result');
             }
         } else {
             if ($httpRequestResult['info']['http_code'] === 0) {
                 // seems to be a timeout
                 return $this->getCommandResult(tx_caretakerinstance_CommandResult::status_undefined, NULL, 'No Response/Timeout (Total-Time: ' . $httpRequestResult['info']['total_time'] . ')');
             } else {
                 return $this->getCommandResult(tx_caretakerinstance_CommandResult::status_error, NULL, 'Invalid result: ' . $httpRequestResult['response'] . chr(10) . 'CURL Info: ' . var_export($httpRequestResult['info'], true));
             }
         }
     } else {
         return $this->getCommandResult(tx_caretakerinstance_CommandResult::status_error, NULL, 'Invalid result request could not be executed' . chr(10) . 'CURL Info: ' . var_export($httpRequestResult['info'], true));
     }
 }
 /**
  * Get the test result for a failed command result
  * @param tx_caretakerinstance_CommandResult $commandResult
  * @return tx_caretaker_TestResult
  */
 protected function getFailedCommandResultTestResult($commandResult)
 {
     return tx_caretaker_TestResult::create($commandResult instanceof tx_caretakerinstance_CommandResult ? $commandResult->getStatus() : tx_caretaker_Constants::state_error, 0, 'Command execution failed: ' . ($commandResult instanceof tx_caretakerinstance_CommandResult ? $commandResult->getMessage() : 'undefined'));
 }
 /**
  * Encode / encrypt the command result with the security manager
  *
  * @param tx_caretakerinstance_CommandResult $commandResult
  * @return string
  */
 public function wrapCommandResult(tx_caretakerinstance_CommandResult $commandResult)
 {
     $json = $commandResult->toJson();
     return $this->securityManager->encodeResult($json);
 }