diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..a2eaf8c --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: php +php: + - "5.4" + - "5.3" + - "5.2" diff --git a/Readme.md b/Readme.md index c626a48..b3970f0 100644 --- a/Readme.md +++ b/Readme.md @@ -1,3 +1,5 @@ +[![Build Status](https://secure.travis-ci.org/aeroio/php-client.png)](http://travis-ci.org/aeroio/php-client) + PHP Client for Aero.cx ==================================== diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..60c97e0 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,10 @@ + + + + test + + + diff --git a/src/AeroIO.php b/src/AeroIO.php new file mode 100644 index 0000000..66088b7 --- /dev/null +++ b/src/AeroIO.php @@ -0,0 +1,97 @@ + $value) { + self::$$key = $value; + } + + if (!self::$engine) self::$engine = new Aero_Curl(); + + self::setOptions(); + } + + /** + * Set exception handler for certain project. + * + * @return void + */ + public static function handleExceptions() { + Aero_ExceptionHandler::for_project(self::$project); + } + + /** + * Set the options for the request. + * + * @return void + */ + protected static function setOptions() { + Aero_Connection::$site = self::$site; + Aero_Connection::$engine = self::$engine; + Aero_Connection::$credentials = array( + 'auth_token' => self::$auth_token, + 'sid' => self::$sid + ); + } +} + +?> + diff --git a/src/Connection.php b/src/Connection.php index a2caa91..3b69039 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -1,28 +1,56 @@ $type, - //'url' => $url, - //'auth_token' => self::$credentials['auth_token'], - //'sid' => self::$credentials['sid'], - //'attributes' => $resource - //); + /** + * Base url of the application. + * + * @static string + */ + public static $site = 'https://aero.io/api/v1'; + + /** + * Credentials to be used for user authorization. + * + * @static array + */ + public static $credentials; - //$request = new Aero_Request($params); + /** + * Engine to be used for the request execution. + * + * @static object + */ + public static $engine; + + /** + * Create a request object, to be used by the engine for execution and + * handle the response. + * + * @param object $resource + * @param string $type + * @return object + */ + public static function persist($resource, $type) { + $request = new Aero_Request($type, $resource, self::$credentials, self::$site); $engine = new self::$engine(); $response = $engine->execute($request); return Aero_Response::handle($response); } - - public static $engine; - public static $credentials; } + ?> diff --git a/src/ExceptionHandler.php b/src/ExceptionHandler.php new file mode 100644 index 0000000..48f12a3 --- /dev/null +++ b/src/ExceptionHandler.php @@ -0,0 +1,56 @@ + self::$project_id, + 'message' => $exception->getMessage(), + 'resolved' => false + ); + + $error = new Aero_Error($params); + $error->save(); + } + + /** + * Set exception handler for certain project. + * + * @param integer $id + * @return void + */ + public static function for_project($id) { + self::$project_id = $id; + + set_exception_handler(array(get_called_class(), "handle")); + } +} + +?> diff --git a/src/Exceptions.php b/src/Exceptions.php new file mode 100644 index 0000000..b785581 --- /dev/null +++ b/src/Exceptions.php @@ -0,0 +1,70 @@ + diff --git a/src/Request.php b/src/Request.php index 3604277..f65c2d5 100644 --- a/src/Request.php +++ b/src/Request.php @@ -1,30 +1,146 @@ $value) { - //$this->$attribute = $value; - //} - //} - //} - - protected $type; + + /** + * Used extension for the request. + * + * @var string + */ + const EXT = '.json'; + + /** + * Request method. + * + * @var string + */ + protected $method; + + /** + * Request url. + * + * @var string + */ protected $url; + + /** + * User auth_token. + * + * @var string + */ protected $auth_token; + + /** + * User SID. + * + * @var string + */ protected $sid; + + /** + * Record to be requested. + * + * @var object + */ protected $resource; - public function __construct($type, $resource, $credentials) { - $this->type = $type; + /** + * Assemble the request with the provided data. + * + * @param string $method + * @param object $resource + * @param array $credentials + * @param string $site + * @return void + */ + public function __construct($method, $resource, $credentials, $site) { + $this->method = $method; $this->setCredentials($credentials); - $this->url = UrlBuilder::assemble($resource); + + $this->assembleUrl($site, $resource); $this->resource = $resource; } - public function setCredentials($credentials) { + /** + * Get the SID used for the authorization. + * + * @return string + */ + public function getSid() { + return $this->sid; + } + + /** + * Get the auth token used for the authentication. + * + * @return string + */ + public function getAuthToken() { + return $this->auth_token; + } + + /** + * Get the type of method used for the request. + * + * @return string + */ + public function getMethod() { + return $this->method; + } + + /** + * Get the URL address for the request. + * + * @return string + */ + public function getUrl() { + return $this->url; + } + + /** + * Get the resource, for which the request will be executed for. + * + * @return object + */ + public function getResource() { + return $this->resource; + } + + /** + * Assemble the url from the base site url, the path to the required + * record and the used extension. + * + * @param string $site + * @param object $resource + * @return string + */ + public function assembleUrl($site, $resource) { + $this->url = $site . $resource->path() . self::EXT; + } + + /** + * Set the credentials used for authorization. + * + * @param array $credentials + * @return void + */ + protected function setCredentials($credentials) { foreach ($credentials as $key => $value) { $this->$key = $value; } } } + ?> diff --git a/src/Resource.php b/src/Resource.php index 830d884..d8998ff 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -1,41 +1,121 @@ $value) { - $this->$attribute = $value; - } + public function __construct(Array $attributes = array()) { + foreach ($attributes as $attribute => $value) { + $this->attributes[$attribute] = $value; } } /** - * Gets all elements connected to a certain model. + * Getting certain attribute from the attributes array, where the information + * for the object is contained. * - * @params object $parent - * @returns object + * @param string $property + * @return mixed */ - public static function all($params = null) { - $type = 'GET'; + public function __get($property) { + if (array_key_exists($property, $this->attributes)) { + return $this->attributes[$property]; + } + } + /** + * Check if the property that should be assigned exists in the schema + * of the object. If it exists, save it to the attributes array. + * + * @param string $property + * @param mixed $value + * @return void + */ + public function __set($property, $value) { + if (in_array($property, $this->schema)) { + $this->attributes[$property] = $value; + } + } + + /** + * Get all records that the called resource has. + * + * @param object $params + * @return object + */ + public static function all($params = array()) { $class = get_called_class(); $resource = new $class($params); - $response = Aero_Connection::persist($resource, $type); + $response = Aero_Connection::persist($resource, self::ALL); $array = array(); + foreach($response as $res) { $array[] = new $class($res); } @@ -44,81 +124,79 @@ public static function all($params = null) { } /** - * Gets the first element connected to a certain model. + * Get the first element with the supplied identificator that the called + * resource has. * - * @params number $id - * @params object $parent - * @returns object + * @param number $id + * @param object $parent + * @return object */ - public static function first($id, $params = null) { - $type = 'GET'; - + public static function first($id, $params = array()) { $class = get_called_class(); $resource = new $class($params); $resource->id = $id; - $response = Aero_Connection::persist($resource, $type); + $response = Aero_Connection::persist($resource, self::FIRST); return new $class($response); } /** - * Saves or updates resource, depending on the type. + * Save or update resource, depending on its state. If it's new it will be + * saved, if not it will be updated. * - * @returns object + * @return object */ public function save() { - $type = 'PUT'; + if ($this->isNew()) return $this->send(self::CREATE); - if ($this->is_new()) $type = 'POST'; - - return $this->send($type); + return $this->send(self::UPDATE); } /** - * Destroys resource. + * Destroy this record. * - * @returns object + * @return object */ public function destroy() { - $type = 'DELETE'; - - return $this->send($type); + return $this->send(self::DESTROY); } /** - * Loads new or updated attributes of the resource. + * Send the resource to the connection. * - * @params array $params - * @returns void + * @param string $type + * @return object */ - public function load_attributes($params) { - foreach ($this as $key => $value) { - if (array_key_exists($key, $params)) { - $this->$key = $params[$key]; - } - } + public function send($type) { + return Aero_Connection::persist($this, $type); } /** - * Checks if the resource is new. + * Check if the resource is new. * - * @returns bool + * @return bool */ - public function is_new() { + public function isNew() { if ($this->id) return false; return true; } /** - * Sends the resource to the connection + * Return an array containing all of the attributes of this record. * - * @params string $type - * @returns object + * @return array */ - public function send($type) { - return Aero_Connection::persist($this, $type); + public function toArray() { + $array = array(); + + foreach ($this->attributes as $key => $value) { + $array[$key] = $value; + } + + return $array; } } + ?> diff --git a/src/Response.php b/src/Response.php index 147422b..ca1dfad 100644 --- a/src/Response.php +++ b/src/Response.php @@ -1,21 +1,105 @@ = 200 && $code < 400): + return self::reshape($result['response']); + case 400: + throw new BadRequestException('The request cannot be understood by the server due to malformed syntax.'); + case 401: + throw new UnauthorizedException('The request requires user authentication.'); + case 403: + throw new ForbiddenAccessException('The server understood the request, but is refusing to fulfill it.'); + case 404: + throw new ResourceNotFoundException('The server has not found anything matching the Request-URI.'); + case 405: + throw new MethodNotAllowedException('The method specified in the Request-Line is not allowed for this resource.'); + case 409: + throw new ResourceConflictException('The request could not be completed due to a conflict with the current state of the resource.'); + case 410: + throw new ResourceGoneException('The requested resource is no longer available at the server.'); + case 422: + throw new ResourceInvalidException('The request was well formed, but was unable to be followed due to semantic errors.'); + case ($code >= 401 && $code <= 500): + throw new ClientException(''); + case ($code >= 500 && $code <= 600): + throw new ServerException(''); + default: + throw new ConnectionException(''); + } + } + + /** + * Turn the json response into its array alternative. + * + * @param string $response + * @return array + */ + public static function reshape($response) { + $response = json_decode($response); + + if (is_array($response)) { $array = array(); - foreach($result as $project) { + foreach($response as $project) { $array[] = get_object_vars($project); } return $array; } - if (is_object($result)) { - return get_object_vars($result); + if (is_object($response)) { + return get_object_vars($response); + } + } + + /** + * Get the response code from the header. + * + * @param array $response + * @return integer + */ + public static function responseCode($response) { + if (array_keys($response) !== range(0, count($response) - 1)) { + return $response['http_code']; + } else { + preg_match('/\s+\d+\s+/', $response[0], $matches); + + return trim($matches[0]); } } } + ?> diff --git a/src/UrlBuilder.php b/src/UrlBuilder.php deleted file mode 100644 index ece3a9e..0000000 --- a/src/UrlBuilder.php +++ /dev/null @@ -1,70 +0,0 @@ - $value) { - $parent = strstr($key, '_id', true); - - if ($parent) { - $parents = StringHelper::pluralize($parent); - return "/$parents/$value"; - } - } - } - - /** - * Turns the current resource attributes into url. - * - * @params object $resource - * @returns string - */ - public static function add_current($resource) { - $name = strtolower(get_class($resource)); - $name = end(explode('_', $name)); - $name = StringHelper::pluralize($name); - - if ($resource->id) return "/$name/$resource->id"; - - return "/$name"; - } -} -?> diff --git a/src/engines/Curl.php b/src/engines/Curl.php index f8ac8fe..f751a2d 100644 --- a/src/engines/Curl.php +++ b/src/engines/Curl.php @@ -1,32 +1,46 @@ process = $this->initialize(); } /** - * Assembles and executes the process. + * Assemble and execute the process. * - * @params object $request - * @returns object + * @param object $request + * @return object */ public function execute($request) { $this->createProcess($request); @@ -35,31 +49,33 @@ public function execute($request) { } /** - * Creates the whole request, which is to be sent. + * Create the whole process, which is to be executed. * - * @params object $request - * @returns void + * @param object $request + * @return void */ public function createProcess($request) { - $sid = $request->sid; - $auth_token = $request->auth_token; + $sid = $request->getSid(); + $auth_token = $request->getAuthToken(); $headers = array( "Authorization: Basic " . base64_encode("$sid:$auth_token") ); - $this->setOption(CURLOPT_URL, $request->url); + $this->setOption(CURLOPT_URL, $request->getUrl()); $this->setOption(CURLOPT_RETURNTRANSFER, true); $this->setOption(CURLOPT_HTTPHEADER, $headers); - switch($request->type) { + $attributes = $request->getResource()->toArray(); + + switch($request->getMethod()) { case 'POST': $this->setOption(CURLOPT_POST, true); - $this->setOption(CURLOPT_POSTFIELDS, $request->attributes); + $this->setOption(CURLOPT_POSTFIELDS, $attributes); break; case 'PUT': $this->setOption(CURLOPT_CUSTOMREQUEST, "PUT"); - $this->setOption(CURLOPT_POSTFIELDS, $request->attributes); + $this->setOption(CURLOPT_POSTFIELDS, $attributes); break; case 'DELETE': $this->setOption(CURLOPT_CUSTOMREQUEST, "DELETE"); @@ -70,33 +86,36 @@ public function createProcess($request) { } /** - * Process initialization. + * Initialize the main process. * - * @returns resource + * @return resource */ public function initialize() { return curl_init(); } /** - * Executes the process and fetches the data. + * Execute and close the process. * - * @params resource $process - * @returns object + * @param resource $process + * @return object */ public function fetch($process) { - $result = curl_exec($process); + $array = array(); + + $array['response'] = curl_exec($process); + $array['header'] = $this->getInfo(); curl_close($process); - return $result; + return $array; } /** - * Gets the process information. + * Get the process information. * - * @params string $name - * @returns array + * @param string $name + * @return array */ public function getInfo($name = null) { if ($name) return curl_getinfo($this->process, $name); @@ -105,11 +124,11 @@ public function getInfo($name = null) { } /** - * Process options setter. + * Set option to the process. * - * @params string $name - * @params mixed $value - * @returns void + * @param string $name + * @param mixed $value + * @return void */ public function setOption($name, $value) { curl_setopt($this->process, $name, $value); diff --git a/src/engines/Engine.php b/src/engines/Engine.php index a6f8d0e..e3437b8 100644 --- a/src/engines/Engine.php +++ b/src/engines/Engine.php @@ -1,5 +1,26 @@ diff --git a/src/engines/Http.php b/src/engines/Http.php index 8d6371c..4c2cdd1 100644 --- a/src/engines/Http.php +++ b/src/engines/Http.php @@ -1,20 +1,42 @@ sid; - $auth_token = $request->auth_token; + $sid = $request->getSid(); + $auth_token = $request->getAuthToken(); $query = $this->buildHttpQuery($request); - $this->setMethod($request->type); + $this->setMethod($request->getMethod()); $this->setHeader("Authorization: Basic " . base64_encode("$sid:$auth_token") . "\r\n" . "Connection: close\r\n" . "Content-type: application/x-www-form-urlencoded\r\n" . @@ -25,84 +47,91 @@ public function execute($request) { $context = $this->buildContext($this->request); - return $this->fetch($request->url, $context); + return $this->fetch($request->getUrl(), $context); } /** - * Fetches the data. + * Fetch the data. * - * @params string $url - * @params resource $context - * @returns object + * @param string $url + * @param resource $context + * @return object */ public function fetch($url, $context) { - return file_get_contents($url, false, $context); + $array = array(); + + $array['response'] = file_get_contents($url, false, $context); + $array['header'] = $http_response_header; + + return $array; } /** - * Gets the request method. + * Get the request method. * - * @returns string + * @return string */ public function getMethod() { return $this->request['method']; } /** - * Sets the request method. + * Set the request method. * - * @params string $type + * @param string $type */ public function setMethod($type) { $this->request['method'] = strtoupper($type); } /** - * Gets the request header. + * Get the request header. * - * @returns string + * @return string */ public function getHeader() { return $this->request['header']; } /** - * Sets the request header. + * Set the request header. * - * @params string $data + * @param string $data */ public function setHeader($data) { $this->request['header'] = $data; } /** - * Gets the request content. + * Get the request content. * - * @returns string + * @return string */ public function getContent() { return $this->request['content']; } /** - * Sets the request content. + * Set the request content. * - * @params string $data + * @param string $data */ public function setContent($data) { $this->request['content'] = $data; } /** - * Builds the http query used as content in the request. + * Build the http query used as content in the request. * - * @params object $resource - * @returns string + * @param object $resource + * @return string */ public function buildHttpQuery($resource) { $array = array(); - foreach ($resource->attributes as $key => $value) { + $attributes = $resource->getResource()->toArray(); + + foreach ($attributes as $key => $value) { if ($value) $array[$key] = $value; } @@ -110,13 +139,14 @@ public function buildHttpQuery($resource) { } /** - * Builds the context for the request. + * Build the context for the request. * - * @params array $data - * @returns response + * @param array $data + * @return response */ public function buildContext($data) { return stream_context_create(array('http' => $data)); } } + ?> diff --git a/src/helpers/StringHelper.php b/src/helpers/StringHelper.php deleted file mode 100644 index bbb48cb..0000000 --- a/src/helpers/StringHelper.php +++ /dev/null @@ -1,30 +0,0 @@ - diff --git a/src/resources/Error.php b/src/resources/Error.php index 8e95009..5562589 100644 --- a/src/resources/Error.php +++ b/src/resources/Error.php @@ -1,13 +1,63 @@ project_id . '/errors'; + + if ($this->id) { + $url .= "/$this->id"; + } + + return $url; + } } + ?> diff --git a/src/resources/Project.php b/src/resources/Project.php index 5add52f..9ed36c8 100644 --- a/src/resources/Project.php +++ b/src/resources/Project.php @@ -1,11 +1,49 @@ id) { + $url .= "/$this->id"; + } + + return $url; + } } + ?> diff --git a/src/validators/Validator.php b/src/validators/Validator.php index c74cc3f..045d263 100644 --- a/src/validators/Validator.php +++ b/src/validators/Validator.php @@ -1,5 +1,19 @@ validate as $attribute => $rule) { foreach($rule as $name => $value) { - $valid = self::$name($attribute, $value); + $array[] = self::$name($attribute, $value); } } - return $valid; + return !in_array(false, $array); } /** @@ -37,27 +51,6 @@ public static function is_valid($resource) { public static function presence($attribute, $value) { return isset(self::$resource->$attribute) == true; } - - /** - * Checks if certain attributes is in defined maximum range. - * - * @params string $attribute - * @params boolean $value - * @returns boolean - */ - public static function max_length($attribute, $value) { - return strlen(self::$resource->$attribute) <= $value; - } - - /** - * Checks if certain attributes is in defined minimum range. - * - * @params string $attribute - * @params boolean $value - * @returns boolean - */ - public static function min_length($attribute, $value) { - return strlen(self::$resource->$attribute) >= $value; - } } + ?> diff --git a/test/features/authorization.feature b/test/features/authorization.feature deleted file mode 100644 index 0548103..0000000 --- a/test/features/authorization.feature +++ /dev/null @@ -1,10 +0,0 @@ -Feature: Authorize user - As an user - I want to be able to provide my user information - So that I can authorize myself in Aero.cx - - @authorization - Scenario: Authorize myself - Given I have account in Aero.cx with token "AUTH_TOKEN" and sid "SID" - When I initialize AeroClient with this information - Then it should be set into the header of the request diff --git a/test/features/bootstrap/Autoload.php b/test/features/bootstrap/Autoload.php deleted file mode 100644 index 539b224..0000000 --- a/test/features/bootstrap/Autoload.php +++ /dev/null @@ -1,17 +0,0 @@ - diff --git a/test/features/bootstrap/FeatureContext.php b/test/features/bootstrap/FeatureContext.php deleted file mode 100644 index 118c7b6..0000000 --- a/test/features/bootstrap/FeatureContext.php +++ /dev/null @@ -1,46 +0,0 @@ -helper = new TestHelper(); - $this->phpunit = new PHPUnitHelper(); - } - - public function getStepDefinitionResources() - { - return Autoload::execute(__DIR__ . '/../steps/', 'steps.php'); - } - - public function getHookDefinitionResources() - { - //return array(__DIR__ . '/../support/hooks.php'); - } -} diff --git a/test/features/bootstrap/PHPUnitHelper.php b/test/features/bootstrap/PHPUnitHelper.php deleted file mode 100644 index d2daeaf..0000000 --- a/test/features/bootstrap/PHPUnitHelper.php +++ /dev/null @@ -1,5 +0,0 @@ - diff --git a/test/features/bootstrap/TestHelper.php b/test/features/bootstrap/TestHelper.php deleted file mode 100644 index 213cdf2..0000000 --- a/test/features/bootstrap/TestHelper.php +++ /dev/null @@ -1,21 +0,0 @@ -getRows(); - - $array = array(); - - foreach($values as $value) { - $array[] = $value[0]; - } - - return $array; - } -} -?> diff --git a/test/features/projects_with_curl.feature b/test/features/projects_with_curl.feature deleted file mode 100644 index 44584c5..0000000 --- a/test/features/projects_with_curl.feature +++ /dev/null @@ -1,43 +0,0 @@ -Feature: Manage projects - As an user - I want to be able to communicate with Aero.cx from my application - So that I can manage my projects there - - Background: - Given I have cURL on my server - - @curl @projects - Scenario: Get all projects - Given I have created the following projects in Aero.cx: - | Google | - | Twitter | - | Facebook | - When I initialize the AeroClient and want to get all of my projects - Then I should receive the following projects: - | Google | - | Twitter | - | Facebook | - - @curl @projects - Scenario: Get project with ID - Given I have created project "Google" with id "1" in Aero.cx - When I initialize the AeroCLient and want to get this project - Then I should receive project "Google" with id "1" - - @curl @projects - Scenario: Create project - Given I have built a project "Google" with description "Search engine" - When I initialize the AeroClient and want to save it there - Then I should receive project "Google" with description "Search engine" - - @curl @projects - Scenario: Update project - Given I have created project with id "1" - When I initialize the AeroClient and want to update it to "Facebook" with description "Meet your friends" - Then I should receive the updated project "Facebook" with description "Meet your friends" - - @http @projects - Scenario: Delete project - Given I have created project with id "1" - When I initialize the AeroClient and want to delete it - Then I should receive delete confirmation status diff --git a/test/features/projects_with_http.feature b/test/features/projects_with_http.feature deleted file mode 100644 index 734e796..0000000 --- a/test/features/projects_with_http.feature +++ /dev/null @@ -1,43 +0,0 @@ -Feature: Manage projects - As an user - I want to be able to communicate with Aero.cx from my application - So that I can manage my projects there - - Background: - Given I do not have cURL on my server - - @http @projects - Scenario: Get all projects - Given I have created the following projects in Aero.cx: - | Google | - | Twitter | - | Facebook | - When I initialize the AeroClient and want to get all of my projects - Then I should receive the following projects: - | Google | - | Twitter | - | Facebook | - - @http @projects - Scenario: Get project with ID - Given I have created project "Google" with id "1" in Aero.cx - When I initialize the AeroCLient and want to get this project - Then I should receive project "Google" with id "1" - - @http @projects - Scenario: Create project - Given I have built a project "Google" with description "Search engine" - When I initialize the AeroClient and want to save it there - Then I should receive project "Google" with description "Search engine" - - @http @projects - Scenario: Update project - Given I have created project with id "1" - When I initialize the AeroClient and want to update it to "Facebook" with description "Meet your friends" - Then I should receive the updated project "Facebook" with description "Meet your friends" - - @http @projects - Scenario: Delete project - Given I have created project with id "1" - When I initialize the AeroClient and want to delete it - Then I should receive delete confirmation status diff --git a/test/features/steps/authorization_steps.php b/test/features/steps/authorization_steps.php deleted file mode 100644 index a70715b..0000000 --- a/test/features/steps/authorization_steps.php +++ /dev/null @@ -1,35 +0,0 @@ -Given('/^I have account in Aero\.cx with token "([^"]*)" and sid "([^"]*)"$/', - function($world, $auth_token, $sid) { - $world->parameters = array( - 'auth_token' => $auth_token, - 'sid' => $sid, - 'curl' => false - ); - } - ); - - $steps->When('/^I initialize AeroClient with this information$/', - function($world) { - $aero = new AeroClient($world->parameters); - - $data_parser = $world->phpunit->getMock('DataParser', array('execute')); - $aero->setDataParser($data_parser); - $aero->getProjects(); - - $world->request = $aero->getRequest(); - } - ); - - $steps->Then('/^it should be set into the header of the request$/', - function($world) { - $auth_token = $world->parameters['auth_token']; - $sid = $world->parameters['sid']; - - $expected = "Authorization: Basic " . base64_encode("$auth_token:$sid"); - $result = $world->request->getHeader(); - - $world->phpunit->assertEquals($expected, $result); - } - ); -?> diff --git a/test/features/steps/projects_steps.php b/test/features/steps/projects_steps.php deleted file mode 100644 index df7742e..0000000 --- a/test/features/steps/projects_steps.php +++ /dev/null @@ -1,169 +0,0 @@ -Given('/^I do not have cURL on my server$/', - function($world) { - $world->parameters = array( - 'auth_token' => 'AUTH_TOKEN', - 'sid' => 'SID', - 'curl' => false - ); - } - ); - - $steps->Given('/^I have cURL on my server$/', - function($world) { - $world->parameters = array( - 'auth_token' => 'AUTH_TOKEN', - 'sid' => 'SID', - 'curl' => true - ); - } - ); - - //// GET ALL PROJECTS //// - - $steps->Given('/^I have created the following projects in Aero\.cx:$/', - function($world, $projectsTable) { - $projects = $world->helper->columnToArray($projectsTable); - - $world->data_parser = $world->phpunit->getMock('DataParser', array('execute')); - - $world->data_parser->expects($world->phpunit->once()) - ->method('execute') - ->will($world->phpunit->returnValue($projects)); - } - ); - - $steps->When('/^I initialize the AeroClient and want to get all of my projects$/', - function($world) { - $aero = new AeroClient($world->parameters); - $aero->setDataParser($world->data_parser); - $world->projects = $aero->getProjects(); - } - ); - - $steps->Then('/^I should receive the following projects:$/', - function($world, $projectsTable) { - $projects = $world->helper->columnToArray($projectsTable); - - assertEquals($projects, $world->projects); - } - ); - - //// GET PROJECT WITH ID //// - - $steps->Given('/^I have created project "([^"]*)" with id "([^"]*)" in Aero\.cx$/', - function($world, $project_name, $project_id) { - $project = array('id' => $project_id, 'name' => $project_name); - - $world->data_parser = $world->phpunit->getMock('DataParser', array('execute')); - - $world->data_parser->expects($world->phpunit->once()) - ->method('execute') - ->will($world->phpunit->returnValue($project)); - - } - ); - - $steps->When('/^I initialize the AeroCLient and want to get this project$/', - function($world) { - $aero = new AeroClient($world->parameters); - $aero->setDataParser($world->data_parser); - $world->project = $aero->getProject(1); - } - ); - - $steps->Then('/^I should receive project "([^"]*)" with id "([^"]*)"$/', - function($world, $project_name, $project_id) { - $project = array('id' => $project_id, 'name' => $project_name); - - assertEquals($project, $world->project); - } - ); - - //// CREATE PROJECT //// - - $steps->Given('/^I have built a project "([^"]*)" with description "([^"]*)"$/', - function($world, $project_name, $project_description) { - $world->project = array('name' => $project_name, 'description' => $project_description); - - $world->data_parser = $world->phpunit->getMock('DataParser', array('execute')); - - $world->data_parser->expects($world->phpunit->once()) - ->method('execute') - ->will($world->phpunit->returnValue($world->project)); - - } - ); - - $steps->When('/^I initialize the AeroClient and want to save it there$/', - function($world) { - $aero = new AeroClient($world->parameters); - $aero->setDataParser($world->data_parser); - $world->saved_project = $aero->createProject($world->project); - } - ); - - $steps->Then('/^I should receive project "([^"]*)" with description "([^"]*)"$/', - function($world, $project_name, $project_description) { - $world->project = array('name' => $project_name, 'description' => $project_description); - - assertEquals($world->project, $world->saved_project); - } - ); - - //// UPDATE PROJECT //// - - $steps->Given('/^I have created project with id "([^"]*)"$/', - function($world, $project_id) { - $world->project_id = $project_id; - } - ); - - $steps->When('/^I initialize the AeroClient and want to update it to "([^"]*)" with description "([^"]*)"$/', - function($world, $project_name, $project_description) { - $world->project = array('name' => $project_name, 'description' => $project_description); - - $data_parser = $world->phpunit->getMock('DataParser', array('execute')); - - $data_parser->expects($world->phpunit->once()) - ->method('execute') - ->will($world->phpunit->returnValue($world->project)); - - $aero = new AeroClient($world->parameters); - $aero->setDataParser($data_parser); - $world->project = $aero->updateProject($world->project_id, $world->project); - } - ); - - $steps->Then('/^I should receive the updated project "([^"]*)" with description "([^"]*)"$/', - function($world, $project_name, $project_description) { - $project = array('name' => $project_name, 'description' => $project_description); - - assertEquals($project, $world->project); - } - ); - - //// DELETE PROJECT //// - - $steps->When('/^I initialize the AeroClient and want to delete it$/', - function($world) { - $world->expected = 'deleted'; - - $data_parser = $world->phpunit->getMock('DataParser', array('execute')); - - $data_parser->expects($world->phpunit->once()) - ->method('execute') - ->will($world->phpunit->returnValue($world->expected)); - - $aero = new AeroClient($world->parameters); - $aero->setDataParser($data_parser); - $world->result = $aero->deleteProject($world->project_id); - } - ); - - $steps->Then('/^I should receive delete confirmation status$/', - function($world) { - $world->phpunit->assertEquals($world->expected, $world->result); - } - ); -?> diff --git a/test/unit/AeroIOTest.php b/test/unit/AeroIOTest.php new file mode 100644 index 0000000..f482b94 --- /dev/null +++ b/test/unit/AeroIOTest.php @@ -0,0 +1,29 @@ + $auth_token, + 'sid' => $sid + )); + + $this->assertEquals($auth_token, AeroIO::$auth_token); + } + + public function testConfigureProject() { + $project = 1; + + AeroIO::configure(array( + 'project' => $project + )); + + $this->assertEquals($project, AeroIO::$project); + } +} + +?> diff --git a/test/unit/ConnectionTest.php b/test/unit/ConnectionTest.php deleted file mode 100644 index 596f32d..0000000 --- a/test/unit/ConnectionTest.php +++ /dev/null @@ -1,8 +0,0 @@ - diff --git a/test/unit/ExceptionsTest.php b/test/unit/ExceptionsTest.php new file mode 100644 index 0000000..2928a3a --- /dev/null +++ b/test/unit/ExceptionsTest.php @@ -0,0 +1,134 @@ +setExpectedException('ClientException'); + + $exception = new ClientException('message'); + + $this->assertTrue($exception instanceof Exception); + + throw $exception; + } + + public function testServerException() { + $this->setExpectedException('ServerException'); + + $exception = new ServerException('message'); + + $this->assertTrue($exception instanceof Exception); + + throw $exception; + } + + public function testConnectionException() { + $this->setExpectedException('ConnectionException'); + + $exception = new ConnectionException('message'); + + $this->assertTrue($exception instanceof Exception); + + throw $exception; + } + + public function testRedirectionException() { + $this->setExpectedException('RedirectionException'); + + $exception = new RedirectionException('message'); + + $this->assertTrue($exception instanceof ConnectionException); + + throw $exception; + } + + public function testBadRequestException() { + $this->setExpectedException('BadRequestException'); + + $exception = new BadRequestException('message'); + + $this->assertTrue($exception instanceof ClientException); + + throw $exception; + } + + public function testUnauthorizedException() { + $this->setExpectedException('UnauthorizedException'); + + $exception = new UnauthorizedException('message'); + + $this->assertTrue($exception instanceof ClientException); + + throw $exception; + } + + public function testForbiddenAccessException() { + $this->setExpectedException('ForbiddenAccessException'); + + $exception = new ForbiddenAccessException('message'); + + $this->assertTrue($exception instanceof ClientException); + + throw $exception; + } + + public function testResourceNotFoundException() { + $this->setExpectedException('ResourceNotFoundException'); + + $exception = new ResourceNotFoundException('message'); + + $this->assertTrue($exception instanceof ClientException); + + throw $exception; + } + + public function testMethodNotAllowedException() { + $this->setExpectedException('MethodNotAllowedException'); + + $exception = new MethodNotAllowedException('message'); + + $this->assertTrue($exception instanceof ClientException); + + throw $exception; + } + + public function testResourceConflictException() { + $this->setExpectedException('ResourceConflictException'); + + $exception = new ResourceConflictException('message'); + + $this->assertTrue($exception instanceof ClientException); + + throw $exception; + } + + public function testResourceGoneException() { + $this->setExpectedException('ResourceGoneException'); + + $exception = new ResourceGoneException('message'); + + $this->assertTrue($exception instanceof ClientException); + + throw $exception; + } + + public function testResourceInvalidException() { + $this->setExpectedException('ResourceInvalidException'); + + $exception = new ResourceInvalidException('message'); + + $this->assertTrue($exception instanceof ClientException); + + throw $exception; + } +} + +?> diff --git a/test/unit/RequestTest.php b/test/unit/RequestTest.php index e9c1781..7729e17 100644 --- a/test/unit/RequestTest.php +++ b/test/unit/RequestTest.php @@ -1,19 +1,37 @@ id = 1; - $credentials = array( - 'auth_token' => 'AUTH', + $method = 'METHOD'; + $resource = new TestRequest_Error(); + $credentials = array( + 'auth_token' => 'AUTH_TOKEN', 'sid' => 'SID' ); + $site = 'URL/'; + + $request = new Aero_Request($method, $resource, $credentials, $site); - $result = new Aero_Request($type, $resource, $credentials); + $this->assertEquals('URL/PATH.json', $request->getUrl()); + $this->assertEquals($method, $request->getMethod()); + $this->assertEquals($credentials['auth_token'], $request->getAuthToken()); + $this->assertEquals($credentials['sid'], $request->getSid()); + $this->assertEquals($resource, $request->getResource()); } } -class AeroRequest_Error {} +class TestRequest_Error { + public function path() { + return 'PATH'; + } +} ?> diff --git a/test/unit/ResourceTest.php b/test/unit/ResourceTest.php index 27ea1c8..f9b9769 100644 --- a/test/unit/ResourceTest.php +++ b/test/unit/ResourceTest.php @@ -1,4 +1,12 @@ assertEquals($expected, $result); } - public function testLoadAttributes() { - $params = array( - 'title' => 'Example', - 'description' => 'Example Descriptions' - ); - - $aero = new Test_Resource($params); - - $expected = 'Changed Example'; - $changed = array( - 'title' => $expected - ); - - $aero->load_attributes($changed); - - $this->assertEquals($expected, $aero->title); - } - public function testIsNewNegative() { $params = array( 'id' => 1 @@ -47,7 +37,7 @@ public function testIsNewNegative() { $aero = new Test_Resource($params); $expected = false; - $result = $aero->is_new(); + $result = $aero->isNew(); $this->assertEquals($expected, $result); } @@ -56,32 +46,32 @@ public function testIsNewPositive() { $aero = new Test_Resource(); $expected = true; - $result = $aero->is_new(); + $result = $aero->isNew(); $this->assertEquals($expected, $result); } public function testSaveNew() { - $aero = $this->getMock('Test_Resource', array('send', 'is_new')); + $aero = $this->getMock('Test_Resource', array('send', 'isNew')); $aero->expects($this->once()) ->method('send') ->with('POST'); $aero->expects($this->once()) - ->method('is_new') + ->method('isNew') ->will($this->returnValue(true)); $aero->save(); } public function testSaveUpdate() { - $aero = $this->getMock('Test_Resource', array('send', 'is_new')); + $aero = $this->getMock('Test_Resource', array('send', 'isNew')); $aero->expects($this->once()) ->method('send') ->with('PUT'); $aero->expects($this->once()) - ->method('is_new') + ->method('isNew') ->will($this->returnValue(false)); $aero->save(); @@ -98,8 +88,11 @@ public function testDestroy() { } class Test_Resource extends Aero_Resource { - public $id; - public $title; - public $description; + public $schema = array( + 'id', + 'title', + 'description' + ); + public $attributes = array(); } ?> diff --git a/test/unit/ResponseTest.php b/test/unit/ResponseTest.php index 5351f67..4139c12 100644 --- a/test/unit/ResponseTest.php +++ b/test/unit/ResponseTest.php @@ -1,29 +1,172 @@ 'TITLE' ); - $result = Aero_Response::handle($response); + $result = Aero_Response::reshape($response); $this->assertEquals($expected, $result); } - public function testHandleMultipleObjects() { + public function testReshapeMultipleObjects() { $response = '[{"title":"TITLE"}, {"title":"TITLE2"}]'; $expected = array( array('title' => 'TITLE'), array('title' => 'TITLE2') ); - $result = Aero_Response::handle($response); + $result = Aero_Response::reshape($response); + + $this->assertEquals($expected, $result); + } + + public function testResponseCodeWhenCurl() { + $expected = 200; + + $array = array( + 'http_code' => $expected + ); + + $result = Aero_Response::responseCode($array); $this->assertEquals($expected, $result); } + + public function testResponseCodeWhenHttp() { + $expected = 200; + + $array = array("HTTP/1.1 $expected OK", 'OTHER', 'DATA'); + + $result = Aero_Response::responseCode($array); + + $this->assertEquals($expected, $result); + } + + public function testRaiseRedirectionExceptionWhen301() { + $this->setExpectedException('RedirectionException'); + + $response['header']['http_code'] = 301; + + Aero_Response::handle($response); + } + + public function testRaiseRedirectionExceptionWhen302() { + $this->setExpectedException('RedirectionException'); + + $response['header']['http_code'] = 302; + + Aero_Response::handle($response); + } + + public function testRaiseRedirectionExceptionWhen303() { + $this->setExpectedException('RedirectionException'); + + $response['header']['http_code'] = 303; + + Aero_Response::handle($response); + } + + public function testRaiseRedirectionExceptionExceptionWhen307() { + $this->setExpectedException('RedirectionException'); + + $response['header']['http_code'] = 307; + + Aero_Response::handle($response); + } + + public function testRaiseBadRequestExceptionWhen400() { + $this->setExpectedException('BadRequestException'); + + $response['header']['http_code'] = 400; + + Aero_Response::handle($response); + } + + public function testRaiseUnauthorizedExceptionWhen401() { + $this->setExpectedException('UnauthorizedException'); + + $response['header']['http_code'] = 401; + + Aero_Response::handle($response); + } + + public function testRaiseForbiddenAccessExceptionWhen403() { + $this->setExpectedException('ForbiddenAccessException'); + + $response['header']['http_code'] = 403; + + Aero_Response::handle($response); + } + + public function testRaiseResourceNotFoundExceptionWhen404() { + $this->setExpectedException('ResourceNotFoundException'); + + $response['header']['http_code'] = 404; + + Aero_Response::handle($response); + } + + public function testRaiseMethodNotAllowedExceptionWhen405() { + $this->setExpectedException('MethodNotAllowedException'); + + $response['header']['http_code'] = 405; + + Aero_Response::handle($response); + } + + public function testRaiseResourceConflictExceptionWhen409() { + $this->setExpectedException('ResourceConflictException'); + + $response['header']['http_code'] = 409; + + Aero_Response::handle($response); + } + + public function testRaiseResourceGoneExceptionWhen410() { + $this->setExpectedException('ResourceGoneException'); + + $response['header']['http_code'] = 410; + + Aero_Response::handle($response); + } + + public function testRaiseResourceInvalidExceptionWhen422() { + $this->setExpectedException('ResourceInvalidException'); + + $response['header']['http_code'] = 422; + + Aero_Response::handle($response); + } + + public function testRaiseServerExceptionWhenBetween500and600() { + $this->setExpectedException('ServerException'); + + $response['header']['http_code'] = rand(500, 600); + + Aero_Response::handle($response); + + } + + public function testRaiseConnectionExceptionWhenNotAnyCase() { + $this->setExpectedException('ConnectionException'); + + $response['header']['http_code'] = 1000; + + Aero_Response::handle($response); + } } -class Test_Response {} + ?> diff --git a/test/unit/UrlBuilderTest.php b/test/unit/UrlBuilderTest.php deleted file mode 100644 index 4dfaa21..0000000 --- a/test/unit/UrlBuilderTest.php +++ /dev/null @@ -1,97 +0,0 @@ -assertThat( - true, - $this->equalTo($reflector->isPrivate()) - ); - } - - public function testAssembleFullUrl() { - $resource = new Test_Error(); - $resource->id = 1; - $resource->project_id = 1; - $format = 'xml'; - - $expected = '/projects/1/errors/1.xml'; - - $result = UrlBuilder::assemble($resource, $format); - - $this->assertTrue(strpos($result, $expected) == true); - } - - public function testAssembleAddsParents() { - $resource = 'resource'; - - $urlBuilder = $this->getMockClass('UrlBuilder', array('add_parent', 'add_current')); - - $urlBuilder::staticExpects($this->once()) - ->method('add_parent') - ->with($resource); - - $urlBuilder::assemble($resource); - } - - public function testAssembleAddsCurrentResource() { - $resource = 'resource'; - - $urlBuilder = $this->getMockClass('UrlBuilder', array('add_current', 'add_parent')); - - $urlBuilder::staticExpects($this->once()) - ->method('add_current') - ->with($resource); - - $urlBuilder::assemble($resource); - } - - public function testReturnsRightFormat() { - $resource = 'resource'; - $format = 'json'; - $expected = '.json'; - - $urlBuilder = $this->getMockClass('UrlBuilder', array('add_parent', 'add_current')); - - $url = $urlBuilder::assemble($resource, $format); - - $length = strlen($expected); - $result = substr($expected, -$length); - - $this->assertEquals($expected, $result); - } - - public function testAddParentWhenThereIsSuch() { - $resource = $this->getMock('Resource'); - $resource->project_id = 1; - - $expected = '/projects/1'; - $result = UrlBuilder::add_parent($resource); - - $this->assertEquals($expected, $result); - } - - public function testAddParentWhenThereIsNot() { - $resource = $this->getMock('Resource'); - - $expected = ''; - $result = UrlBuilder::add_parent($resource); - - $this->assertEquals($expected, $result); - } - - public function testAddCurrent() { - $resource = new Test_Error(); - $resource->id = 1; - - $expected = '/errors/1'; - $result = UrlBuilder::add_current($resource); - - $this->assertEquals($expected, $result); - } -} - -class Test_Error {} -?> diff --git a/test/unit/engines/CurlTest.php b/test/unit/engines/CurlTest.php index 8e4da97..46272d2 100644 --- a/test/unit/engines/CurlTest.php +++ b/test/unit/engines/CurlTest.php @@ -1,4 +1,12 @@ getMock('Curl', array('createProcess', 'fetch')); + $curl = $this->getMock('Aero_Curl', array('createProcess', 'fetch')); $curl->expects($this->once()) ->method('createProcess') @@ -21,7 +29,7 @@ public function testExecute() { } public function testGetInfoWholeProcess() { - $curl = new Curl(); + $curl = new Aero_Curl(); $result = $curl->getInfo(); $this->assertEquals('array', gettype($result)); @@ -30,7 +38,7 @@ public function testGetInfoWholeProcess() { public function testGetInfoAttribute() { $expected = 'URL'; - $curl = new Curl(); + $curl = new Aero_Curl(); $curl->setOption(CURLOPT_URL, $expected); $result = $curl->getInfo(CURLINFO_EFFECTIVE_URL); @@ -39,7 +47,7 @@ public function testGetInfoAttribute() { } public function testSetOption() { - $curl = new Curl(); + $curl = new Aero_Curl(); $curl->initialize(); $expected = 'url'; diff --git a/test/unit/engines/HttpTest.php b/test/unit/engines/HttpTest.php index a6e066b..22ef681 100644 --- a/test/unit/engines/HttpTest.php +++ b/test/unit/engines/HttpTest.php @@ -1,9 +1,17 @@ assertThat( true, @@ -15,40 +23,57 @@ public function testExecuteWithTokenAndSid() { $auth_token = 'AUTH_TOKEN'; $sid = 'SID'; $expected = 'response'; + $method = 'GET'; + $url = '/v1/projects'; + $context = 'context'; + + $engine = $this->getMock('Aero_Http', array( + 'fetch', + 'buildHttpQuery', + 'buildContext', + 'setMethod', + 'setHeader', + 'setContent' + )); - $engine = $this->getMock('Http', array('fetch', 'buildHttpQuery')); - + $engine->expects($this->once()) + ->method('buildHttpQuery') + ->will($this->returnValue(true)); + $engine->expects($this->once()) + ->method('buildContext') + ->will($this->returnValue($context)); $engine->expects($this->once()) ->method('fetch') ->will($this->returnValue($expected)); - - $request->auth_token = $auth_token; - $request->sid = $sid; - $request->url = '/v1/projects'; - $request->type = 'GET'; + $engine->request = 'request'; + + $request = $this->getMock('AeroRequest', array( + 'getSid', + 'getAuthToken', + 'getMethod', + 'getUrl' + )); + + $request->expects($this->once()) + ->method('getSid') + ->will($this->returnValue($sid)); + $request->expects($this->once()) + ->method('getAuthToken') + ->will($this->returnValue($auth_token)); + $request->expects($this->once()) + ->method('getMethod') + ->will($this->returnValue($method)); + $request->expects($this->once()) + ->method('getUrl') + ->will($this->returnValue($url)); $result = $engine->execute($request); - $expectedMethod = 'GET'; - - $this->assertEquals($expectedMethod, $engine->getMethod()); $this->assertEquals($result, $expected); } - public function testBuildHttpQueryWithArray() { - $engine = new Http(); - - $params = $this->getMock('Aero_Request'); - $params->attributes = array('name' => 'Google', 'description' => 'Search engine'); - $expected = 'name=Google&description=Search+engine'; - - $result = $engine->buildHttpQuery($params); - - $this->assertEquals($expected, $result); - } - public function testSetAndGetHeader() { - $request = new Http(); + $request = new Aero_Http(); $expected = 'header'; $request->setHeader($expected); @@ -58,7 +83,7 @@ public function testSetAndGetHeader() { } public function testSetAndGetContent() { - $request = new Http(); + $request = new Aero_Http(); $expected = 'content'; $request->setContent($expected); @@ -68,7 +93,7 @@ public function testSetAndGetContent() { } public function testSetAndGetMethod() { - $request = new Http(); + $request = new Aero_Http(); $actual = 'method'; $request->setMethod($actual); @@ -79,7 +104,7 @@ public function testSetAndGetMethod() { } public function testBuildContext() { - $request = new Http(); + $request = new Aero_Http(); $data = array( 'method' => 'POST', diff --git a/test/unit/helpers/StringHelperTest.php b/test/unit/helpers/StringHelperTest.php deleted file mode 100644 index 08dc337..0000000 --- a/test/unit/helpers/StringHelperTest.php +++ /dev/null @@ -1,32 +0,0 @@ -assertThat( - true, - $this->equalTo($reflector->isPrivate()) - ); - } - - public function testPluralize() { - $string = 'test'; - $expected = 'tests'; - - $result = StringHelper::pluralize($string); - - $this->assertEquals($expected, $result); - } - - public function testSingularize() { - $string = 'tests'; - $expected = 'test'; - - $result = StringHelper::singularize($string); - - $this->assertEquals($expected, $result); - } -} -?> diff --git a/test/unit/resources/ErrorTest.php b/test/unit/resources/ErrorTest.php index 0e567dc..778534c 100644 --- a/test/unit/resources/ErrorTest.php +++ b/test/unit/resources/ErrorTest.php @@ -1,11 +1,41 @@ assertTrue($error instanceof Aero_Resource); + } + + public function testPathForAllErrors() { + $error = new Aero_Error(); + $error->project_id = 1; + + $expected = '/projects/1/errors'; + $result = $error->path(); + + $this->assertEquals($expected, $result); + } + + public function testPathForCertainError() { + $error = new Aero_Error(); + $error->project_id = 1; + $error->id = 1; + + $expected = '/projects/1/errors/1'; + $result = $error->path(); + + $this->assertEquals($expected, $result); - $this->assertTrue($Error instanceof Aero_Resource); } } ?> diff --git a/test/unit/resources/ProjectTest.php b/test/unit/resources/ProjectTest.php index 4e5a920..16f7006 100644 --- a/test/unit/resources/ProjectTest.php +++ b/test/unit/resources/ProjectTest.php @@ -1,4 +1,12 @@ assertTrue($project instanceof Aero_Resource); } + + public function testPathForAllProjects() { + $project = new Aero_Project(); + + $expected = '/projects'; + $result = $project->path(); + + $this->assertEquals($expected, $result); + } + + public function testPathForCertainProjects() { + $project = new Aero_Project(); + $project->id = 1; + + $expected = '/projects/1'; + $result = $project->path(); + + $this->assertEquals($expected, $result); + } } ?> diff --git a/test/unit/validators/ValidatorTest.php b/test/unit/validators/ValidatorTest.php index 8233ab2..d32b32e 100644 --- a/test/unit/validators/ValidatorTest.php +++ b/test/unit/validators/ValidatorTest.php @@ -1,22 +1,17 @@ validate = array( - 'title' => array( - 'presence' => true - ), - ); - - $this->assertFalse(Validator::is_valid($test)); - } +require_once 'src/validators/Validator.php'; - public function testValidateNotPresence() { +class AeroValidatorTest extends PHPUnit_Framework_TestCase { + public function testValidatePresent() { $test = new ValidationResourceTest(); - $test->title = 'present'; $test->validate = array( 'title' => array( @@ -24,59 +19,21 @@ public function testValidateNotPresence() { ), ); - $this->assertTrue(Validator::is_valid($test)); - } - - public function testValidateMaxLengthOver() { - $test = new ValidationResourceTest(); - $test->title = 'present'; - - $test->validate = array( - 'title' => array( - 'max_length' => 5 - ), - ); - - $this->assertFalse(Validator::is_valid($test)); + $this->assertFalse(Aero_Validator::is_valid($test)); } - public function testValidateMaxLengthUnder() { + public function testValidateNotPresent() { $test = new ValidationResourceTest(); $test->title = 'present'; + $test->description = null; $test->validate = array( 'title' => array( - 'max_length' => 10 - ), - ); - - $this->assertTrue(Validator::is_valid($test)); - } - - public function testValidateMinLengthOver() { - $test = new ValidationResourceTest(); - $test->title = 'present'; - - $test->validate = array( - 'title' => array( - 'min_length' => 5 - ), - ); - - $this->assertTrue(Validator::is_valid($test)); - } - - public function testValidateMinLengthUnder() { - $test = new ValidationResourceTest(); - $test->title = 'present'; - - $test->validate = array( - 'title' => array( - 'min_length' => 10 + 'presence' => true ), ); - $this->assertFalse(Validator::is_valid($test)); + $this->assertTrue(Aero_Validator::is_valid($test)); } }