<?php
include_once("PHPUnit/Framework/TestCase.php");
abstract class ARESTAPITest extends PHPUnit_Framework_TestCase{

    /**
     * @var string user token for use in getInfoCall
     **/
    public $token=null;
    
    /**
     * @var int userid value
     **/
    public $userId=null;
    
    /**
     * @var string module name
     **/
    public $module="";
    
    /**
     * @var string API version to call
     **/
    public $version="v1";
    
    /**
     * @var string realm identifier for API access
     **/
    public $realm="apitestcases";
    
    /**
     * @var string signing secret for making API calls with the realm above
     **/
    public $signingsecret="6dc5606A325e2d4d654eacb8f5cd8368";
    
    /**
     * @var string endpoint url of the API to test
     **/
    public $endpoint="http://alpha.rest.outspark.net/";    

    /**
     * setUp
     *
     * this method is repsonsible for initializing before any test is run
     *
     **/
    public function setUp(){
        $params = array();
        $params['user']="apitestcases";
        $params['password']="7c6a180b36896a0a8c02787eeafb0e4c"; // post-md5 password
        $params['version']="1";
        $result = $this->makeRequest($this->module, $this->version, "login", $params);
        $this->token = $result['token'];
        
        $params = array();
        $params['token']= $this->token;
        $result = $this->makeSignedRequest($this->module, $this->version, "getInfo", $params);
        $this->userId= ($result['user_id']);
        
    }
    
    public function tearDown(){
        
        //this will clean up the api test
        
    }
    
    public function createGetString($params){
        $get_params = "realm={$this->realm}&";
        foreach($params as $key => $value){
            $get_params .= "{$key}={$value}&";
        }
        $get_params = rtrim($get_params, "&");
        return $get_params;
        
        
    }
    /**
     * makeRequest
     *
     * Make a request to the API for the given module version and command
     *
     * @param string $module API module
     * @param string $version API module version to test.
     * @param string $command The command to execute on the API
     * @param array $params Associative array containing the parameters to the command
     *
     * @return array JSON result array
     **/
    public function makeRequest($module, $version, $command, $params){ // TODO : make it work for both GET and POST requests
        // set up curl
        $ch= curl_init();
        $url= $this->endpoint;
        $get_params = $this->createGetString($params);
        $url .="{$module}/{$version}/$command?{$get_params}";//this still needs both username and password, which it will get form the the other parts of the test
        // execute curl call
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        $json = curl_exec($ch);
        curl_close($ch);
        // assert curl call success/fail
        
            // if success parse json
        $result=json_decode($json,true);
        return $result;
                // assert parse success/fail
                    // if success return parsed json array

                    // if fail - report assert failed

            // if fail report assert failed
    }

    /**
     * makeSignedRequest
     *
     * Make a Signed request to the API for the given module version and command
     *
     * @param string $module API module
     * @param string $version API module version to test.
     * @param string $command The command to execute on the API
     * @param array $params Associative array containing the parameters to the command
     *
     * @return array JSON result array
     **/
    public function makeSignedRequest($module, $version, $command, $params){
        // sign the request
        
        $get_params = $this->createGetString($params);
        $sig = hash('sha256', "/{$module}/{$version}/{$command}?{$get_params}" . $this->signingsecret);
        $params['sig'] = $sig;
        // hash the whole thing through SHA256 to get signature
        // add "sig" parameter to $params array with value signature
        // make the request
        return $this->makeRequest($module, $version, $command, $params);
    }

    
}

?>