From a3309c418d14c0b0df9992ff7da4b3bb8793c77d Mon Sep 17 00:00:00 2001 From: Veselin Todorov Date: Wed, 19 Sep 2012 10:38:14 +0300 Subject: [PATCH 01/16] Add some todo items --- src/Connection.php | 3 ++- src/Request.php | 6 ++++++ src/Resource.php | 26 +++++++++++++++++++------- src/Response.php | 3 +++ src/UrlBuilder.php | 8 +++++++- src/engines/Curl.php | 5 +++++ src/engines/Engine.php | 5 +++++ src/engines/Http.php | 4 ++++ src/helpers/StringHelper.php | 9 ++++++++- src/resources/Error.php | 7 +++++++ src/resources/Project.php | 13 +++++++++++++ src/validators/Validator.php | 4 ++-- 12 files changed, 81 insertions(+), 12 deletions(-) diff --git a/src/Connection.php b/src/Connection.php index a2caa91..e464032 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -3,7 +3,7 @@ class Aero_Connection { public static function persist($resource, $type) { - $request = Aero_Request($type, $resource, $credentials); + $request = new Aero_Request($type, $resource, $credentials); //$url = UrlBuilder::assemble($resource); //$params = array( @@ -25,4 +25,5 @@ public static function persist($resource, $type) { public static $engine; public static $credentials; } + ?> diff --git a/src/Request.php b/src/Request.php index 3604277..05be015 100644 --- a/src/Request.php +++ b/src/Request.php @@ -1,4 +1,8 @@ diff --git a/src/Resource.php b/src/Resource.php index 830d884..23477e3 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -7,17 +7,26 @@ require_once 'src/helpers/StringHelper.php'; class Aero_Resource { + + // TODO: use those instead of hardcoded string + const ALL = 0; + const FIRST = 1; + const UPDATE = 2; + const CREATE = 3; + const DESTROY = 4; + /** * Constructor, setting the options for the resource. * + * TODO: check if static typing is available in PHP 5.2 + * TODO: use the schema + * * @params array $attributes * @returns void */ - public function __construct(Array $attributes = null) { - if ($attributes) { - foreach ($attributes as $attribute => $value) { - $this->$attribute = $value; - } + public function __construct(Array $attributes = array()) { + foreach ($attributes as $attribute => $value) { + $this->$attribute = $value; } } @@ -36,6 +45,7 @@ public static function all($params = null) { $response = Aero_Connection::persist($resource, $type); $array = array(); + foreach($response as $res) { $array[] = new $class($res); } @@ -48,7 +58,8 @@ public static function all($params = null) { * * @params number $id * @params object $parent - * @returns object + * TODO: replace params with param and returns with return + * @return object */ public static function first($id, $params = null) { $type = 'GET'; @@ -68,7 +79,7 @@ public static function first($id, $params = null) { * @returns object */ public function save() { - $type = 'PUT'; + $type = 'update'; if ($this->is_new()) $type = 'POST'; @@ -103,6 +114,7 @@ public function load_attributes($params) { /** * Checks if the resource is new. * + * TODO: camelcase * @returns bool */ public function is_new() { diff --git a/src/Response.php b/src/Response.php index 147422b..6d9d269 100644 --- a/src/Response.php +++ b/src/Response.php @@ -1,6 +1,9 @@ diff --git a/src/engines/Http.php b/src/engines/Http.php index 8d6371c..b3a5418 100644 --- a/src/engines/Http.php +++ b/src/engines/Http.php @@ -1,6 +1,10 @@ getProjectId()); + } } + ?> diff --git a/src/resources/Project.php b/src/resources/Project.php index 5add52f..cb6846e 100644 --- a/src/resources/Project.php +++ b/src/resources/Project.php @@ -1,4 +1,5 @@ diff --git a/src/validators/Validator.php b/src/validators/Validator.php index c74cc3f..c3e6868 100644 --- a/src/validators/Validator.php +++ b/src/validators/Validator.php @@ -46,7 +46,7 @@ public static function presence($attribute, $value) { * @returns boolean */ public static function max_length($attribute, $value) { - return strlen(self::$resource->$attribute) <= $value; + return strlen(self::$resource->$attribute) <= $value; } /** @@ -57,7 +57,7 @@ public static function max_length($attribute, $value) { * @returns boolean */ public static function min_length($attribute, $value) { - return strlen(self::$resource->$attribute) >= $value; + return strlen(self::$resource->$attribute) >= $value; } } ?> From 750c45d9311bcd28e01aff632499f8cd35157679 Mon Sep 17 00:00:00 2001 From: CeRRuTiTo Date: Wed, 19 Sep 2012 13:30:27 +0300 Subject: [PATCH 02/16] Refactor the url assemblation --- src/Connection.php | 13 +--------- src/Request.php | 19 +++++++------- src/Resource.php | 51 ++++++++++++++++++++++++-------------- src/UrlBuilder.php | 12 ++++----- src/engines/Curl.php | 33 ++++++++++++------------ src/engines/Http.php | 35 ++++++++++++++------------ src/resources/Error.php | 10 ++++++++ src/resources/Project.php | 32 ++++++++++++++++++------ test/unit/ResourceTest.php | 14 +++++------ 9 files changed, 125 insertions(+), 94 deletions(-) diff --git a/src/Connection.php b/src/Connection.php index e464032..b619116 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -3,18 +3,7 @@ class Aero_Connection { public static function persist($resource, $type) { - $request = new Aero_Request($type, $resource, $credentials); - //$url = UrlBuilder::assemble($resource); - - //$params = array( - //'type' => $type, - //'url' => $url, - //'auth_token' => self::$credentials['auth_token'], - //'sid' => self::$credentials['sid'], - //'attributes' => $resource - //); - - //$request = new Aero_Request($params); + $request = new Aero_Request($type, $resource, self::$credentials); $engine = new self::$engine(); $response = $engine->execute($request); diff --git a/src/Request.php b/src/Request.php index 05be015..e77e909 100644 --- a/src/Request.php +++ b/src/Request.php @@ -4,28 +4,29 @@ * TODO: add docs */ class Aero_Request { - //public function __construct(Array $attributes = null) { - //if ($attributes) { - //foreach ($attributes as $attribute => $value) { - //$this->$attribute = $value; - //} - //} - //} - // TODO: FUCK, figure out why it WORKS?? protected $type; protected $url; protected $auth_token; protected $sid; protected $resource; + const ROOT_URL = 'http://localhost:3000/api/v1'; public function __construct($type, $resource, $credentials) { $this->type = $type; $this->setCredentials($credentials); - $this->url = UrlBuilder::assemble($resource); + + $this->url = self::ROOT_URL . $resource->path() . '.json'; + //$this->url = UrlBuilder::assemble($resource); $this->resource = $resource; } + public function __get($property) { + if (property_exists($this, $property)) { + return $this->$property; + } + } + public function setCredentials($credentials) { foreach ($credentials as $key => $value) { $this->$key = $value; diff --git a/src/Resource.php b/src/Resource.php index 23477e3..232f963 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -21,20 +21,32 @@ class Aero_Resource { * TODO: check if static typing is available in PHP 5.2 * TODO: use the schema * - * @params array $attributes - * @returns void + * @param array $attributes + * @return void */ public function __construct(Array $attributes = array()) { foreach ($attributes as $attribute => $value) { - $this->$attribute = $value; + $this->attributes[$attribute] = $value; } } + public function __get($property) { + if (in_array($property, $this->attributes)) { + return $this->attributes[$property]; + } + } + + public function __set($property, $value) { + if (in_array($property, $this->schema)) { + $this->attributes[$property] = $value; + } + } + /** * Gets all elements connected to a certain model. * - * @params object $parent - * @returns object + * @param object $parent + * @return object */ public static function all($params = null) { $type = 'GET'; @@ -56,32 +68,33 @@ public static function all($params = null) { /** * Gets the first element connected to a certain model. * - * @params number $id - * @params object $parent + * @param number $id + * @param object $parent * TODO: replace params with param and returns with return * @return object */ - public static function first($id, $params = null) { + public static function first($id, $params = array()) { $type = 'GET'; $class = get_called_class(); $resource = new $class($params); + //$resource->attributes['id'] = $id; $resource->id = $id; $response = Aero_Connection::persist($resource, $type); - return new $class($response); + return new $class($response); } /** * Saves or updates resource, depending on the type. * - * @returns object + * @return object */ public function save() { $type = 'update'; - if ($this->is_new()) $type = 'POST'; + if ($this->isNew()) $type = 'POST'; return $this->send($type); } @@ -89,7 +102,7 @@ public function save() { /** * Destroys resource. * - * @returns object + * @return object */ public function destroy() { $type = 'DELETE'; @@ -100,10 +113,10 @@ public function destroy() { /** * Loads new or updated attributes of the resource. * - * @params array $params - * @returns void + * @param array $params + * @return void */ - public function load_attributes($params) { + public function loadAttributes($params) { foreach ($this as $key => $value) { if (array_key_exists($key, $params)) { $this->$key = $params[$key]; @@ -115,9 +128,9 @@ public function load_attributes($params) { * Checks if the resource is new. * * TODO: camelcase - * @returns bool + * @return bool */ - public function is_new() { + public function isNew() { if ($this->id) return false; return true; @@ -126,8 +139,8 @@ public function is_new() { /** * Sends the resource to the connection * - * @params string $type - * @returns object + * @param string $type + * @return object */ public function send($type) { return Aero_Connection::persist($this, $type); diff --git a/src/UrlBuilder.php b/src/UrlBuilder.php index 525a6de..b5792d2 100644 --- a/src/UrlBuilder.php +++ b/src/UrlBuilder.php @@ -11,7 +11,7 @@ class UrlBuilder { const ROOT_URL = 'http://localhost:3000/api/v1'; // TODO: move ot resource - const EXTENSION = '.json' + const EXTENSION = '.json'; /** * Private constructor, so the class can't be instantiated. @@ -20,13 +20,13 @@ class UrlBuilder { */ private function __construct() {} - private function __clone() + private function __clone() {} /** * Assembles the url with the provided resource data and format. * - * @params object $resource - * @params string $format + * @param object $resource + * @param string $format * @returns string */ public static function assemble($resource, $format = 'json') { @@ -41,7 +41,7 @@ public static function assemble($resource, $format = 'json') { /** * Turns the parent attributes into url string. * - * @params object $resource + * @param object $resource * @returns string */ public static function add_parent($resource) { @@ -60,7 +60,7 @@ public static function add_parent($resource) { /** * Turns the current resource attributes into url. * - * @params object $resource + * @param object $resource * @returns string */ public static function add_current($resource) { diff --git a/src/engines/Curl.php b/src/engines/Curl.php index cf924e1..41373d3 100644 --- a/src/engines/Curl.php +++ b/src/engines/Curl.php @@ -8,20 +8,18 @@ class Curl implements Engine { /** * The process to be executed. * - * TODO: do not use private but protected * @var resource */ - private $process; + protected $process; /** * Contructor, checking if the engine could work. * - * @returns void + * @return void */ public function __construct() { if (!function_exists('curl_init')) { - // TODO: throw custom exception - throw new Exception('cURL not installed'); + throw new CurlException('cURL not installed'); } $this->process = $this->initialize(); @@ -30,8 +28,8 @@ public function __construct() { /** * Assembles and executes the process. * - * @params object $request - * @returns object + * @param object $request + * @return object */ public function execute($request) { $this->createProcess($request); @@ -42,8 +40,8 @@ public function execute($request) { /** * Creates the whole request, which is to be sent. * - * @params object $request - * @returns void + * @param object $request + * @return void */ public function createProcess($request) { $sid = $request->sid; @@ -77,7 +75,7 @@ public function createProcess($request) { /** * Process initialization. * - * @returns resource + * @return resource */ public function initialize() { return curl_init(); @@ -86,11 +84,12 @@ public function initialize() { /** * Executes the process and fetches the data. * - * @params resource $process - * @returns object + * @param resource $process + * @return object */ public function fetch($process) { $result = curl_exec($process); + print_r($this->getInfo()); curl_close($process); @@ -100,8 +99,8 @@ public function fetch($process) { /** * Gets 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); @@ -112,9 +111,9 @@ public function getInfo($name = null) { /** * Process options setter. * - * @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/Http.php b/src/engines/Http.php index b3a5418..8cb66b2 100644 --- a/src/engines/Http.php +++ b/src/engines/Http.php @@ -9,8 +9,8 @@ class Http implements Engine { /** * Assembles and executes the request. * - * @params object $request - * @returns object + * @param object $request + * @return object */ public function execute($request) { $sid = $request->sid; @@ -35,18 +35,21 @@ public function execute($request) { /** * Fetches 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); + $response = file_get_contents($url, false, $context); + + print_r($http_response_header); + return $response; } /** * Gets the request method. * - * @returns string + * @return string */ public function getMethod() { return $this->request['method']; @@ -55,7 +58,7 @@ public function getMethod() { /** * Sets the request method. * - * @params string $type + * @param string $type */ public function setMethod($type) { $this->request['method'] = strtoupper($type); @@ -64,7 +67,7 @@ public function setMethod($type) { /** * Gets the request header. * - * @returns string + * @return string */ public function getHeader() { return $this->request['header']; @@ -73,7 +76,7 @@ public function getHeader() { /** * Sets the request header. * - * @params string $data + * @param string $data */ public function setHeader($data) { $this->request['header'] = $data; @@ -82,7 +85,7 @@ public function setHeader($data) { /** * Gets the request content. * - * @returns string + * @return string */ public function getContent() { return $this->request['content']; @@ -91,7 +94,7 @@ public function getContent() { /** * Sets the request content. * - * @params string $data + * @param string $data */ public function setContent($data) { $this->request['content'] = $data; @@ -100,8 +103,8 @@ public function setContent($data) { /** * Builds 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(); @@ -116,8 +119,8 @@ public function buildHttpQuery($resource) { /** * Builds 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/resources/Error.php b/src/resources/Error.php index c23ab1b..08fa48f 100644 --- a/src/resources/Error.php +++ b/src/resources/Error.php @@ -11,6 +11,16 @@ class Aero_Error extends Aero_Resource { public $created_at; public $updated_at; + protected $schema = array( + 'id', + 'project_id', + 'message', + 'occured', + 'resolved', + 'created_at', + 'updated_at' + ); + // TODO: use this public function path() { return str_replace('/projects/:project_id/errors', ':project_id', $this->getProjectId()); diff --git a/src/resources/Project.php b/src/resources/Project.php index cb6846e..4b93d05 100644 --- a/src/resources/Project.php +++ b/src/resources/Project.php @@ -3,22 +3,38 @@ require_once 'src/Resource.php'; class Aero_Project extends Aero_Resource { - public $id; - public $title; - public $description; - public $created_at; - public $updated_at; - /** * TODO: use schema */ protected $schema = array( 'id', - 'title' - 'description' + 'title', + 'description', + 'created_at', + 'updated_at' ); protected $attributes = array(); + + public function path() { + $url = '/projects'; + + $id = $this->attributes['id']; + if ($id) $url .= "/$id"; + + return $url; + } } +Aero_Connection::$engine = new Http(); +Aero_Connection::$credentials = array( + 'auth_token' => '52b8963d43b3f9081f58977b5aa3c110', + 'sid' => '4ced755889ec99408be287e3ffb83b6b' +); + +$project = Aero_Project::first(2); +print_r($project); +print_r($project->created_at); + + ?> diff --git a/test/unit/ResourceTest.php b/test/unit/ResourceTest.php index 27ea1c8..1b64040 100644 --- a/test/unit/ResourceTest.php +++ b/test/unit/ResourceTest.php @@ -34,7 +34,7 @@ public function testLoadAttributes() { 'title' => $expected ); - $aero->load_attributes($changed); + $aero->loadAttributes($changed); $this->assertEquals($expected, $aero->title); } @@ -47,7 +47,7 @@ public function testIsNewNegative() { $aero = new Test_Resource($params); $expected = false; - $result = $aero->is_new(); + $result = $aero->isNew(); $this->assertEquals($expected, $result); } @@ -56,32 +56,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(); From d4478e53e64e2aef37906fdbfd2360fb7ceab2ce Mon Sep 17 00:00:00 2001 From: CeRRuTiTo Date: Wed, 19 Sep 2012 17:00:11 +0300 Subject: [PATCH 03/16] Change the resource structure --- src/Connection.php | 28 ++++++-- src/Request.php | 4 +- src/Resource.php | 8 +-- src/UrlBuilder.php | 76 -------------------- src/engines/Curl.php | 6 +- src/engines/Http.php | 5 +- src/helpers/StringHelper.php | 37 ---------- src/resources/Project.php | 33 +++++++-- test/unit/UrlBuilderTest.php | 97 -------------------------- test/unit/helpers/StringHelperTest.php | 32 --------- 10 files changed, 62 insertions(+), 264 deletions(-) delete mode 100644 src/UrlBuilder.php delete mode 100644 src/helpers/StringHelper.php delete mode 100644 test/unit/UrlBuilderTest.php delete mode 100644 test/unit/helpers/StringHelperTest.php diff --git a/src/Connection.php b/src/Connection.php index b619116..b01f9bb 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -1,7 +1,30 @@ diff --git a/src/Request.php b/src/Request.php index e77e909..5c7b0ca 100644 --- a/src/Request.php +++ b/src/Request.php @@ -11,13 +11,13 @@ class Aero_Request { protected $sid; protected $resource; const ROOT_URL = 'http://localhost:3000/api/v1'; + const EXT = '.json'; public function __construct($type, $resource, $credentials) { $this->type = $type; $this->setCredentials($credentials); - $this->url = self::ROOT_URL . $resource->path() . '.json'; - //$this->url = UrlBuilder::assemble($resource); + $this->url = self::ROOT_URL . $resource->path() . self::EXT; $this->resource = $resource; } diff --git a/src/Resource.php b/src/Resource.php index 232f963..f796be7 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -4,7 +4,6 @@ require_once 'src/Connection.php'; require_once 'src/Request.php'; require_once 'src/Response.php'; -require_once 'src/helpers/StringHelper.php'; class Aero_Resource { @@ -31,7 +30,7 @@ public function __construct(Array $attributes = array()) { } public function __get($property) { - if (in_array($property, $this->attributes)) { + if (array_key_exists($property, $this->attributes)) { return $this->attributes[$property]; } } @@ -48,7 +47,7 @@ public function __set($property, $value) { * @param object $parent * @return object */ - public static function all($params = null) { + public static function all($params = array()) { $type = 'GET'; $class = get_called_class(); @@ -78,7 +77,6 @@ public static function first($id, $params = array()) { $class = get_called_class(); $resource = new $class($params); - //$resource->attributes['id'] = $id; $resource->id = $id; $response = Aero_Connection::persist($resource, $type); @@ -92,7 +90,7 @@ public static function first($id, $params = array()) { * @return object */ public function save() { - $type = 'update'; + $type = 'PUT'; if ($this->isNew()) $type = 'POST'; diff --git a/src/UrlBuilder.php b/src/UrlBuilder.php deleted file mode 100644 index b5792d2..0000000 --- a/src/UrlBuilder.php +++ /dev/null @@ -1,76 +0,0 @@ - $value) { - $parent = strstr($key, '_id', true); - - if ($parent) { - $parents = StringHelper::pluralize($parent); - return "/$parents/$value"; - } - } - } - - /** - * Turns the current resource attributes into url. - * - * @param 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 41373d3..299fefd 100644 --- a/src/engines/Curl.php +++ b/src/engines/Curl.php @@ -1,8 +1,11 @@ getInfo()); curl_close($process); diff --git a/src/engines/Http.php b/src/engines/Http.php index 8cb66b2..e667e56 100644 --- a/src/engines/Http.php +++ b/src/engines/Http.php @@ -42,7 +42,6 @@ public function execute($request) { public function fetch($url, $context) { $response = file_get_contents($url, false, $context); - print_r($http_response_header); return $response; } @@ -109,7 +108,9 @@ public function setContent($data) { public function buildHttpQuery($resource) { $array = array(); - foreach ($resource->attributes as $key => $value) { + $attributes = $resource->resource->toArray(); + + foreach ($attributes as $key => $value) { if ($value) $array[$key] = $value; } diff --git a/src/helpers/StringHelper.php b/src/helpers/StringHelper.php deleted file mode 100644 index 2164526..0000000 --- a/src/helpers/StringHelper.php +++ /dev/null @@ -1,37 +0,0 @@ - diff --git a/src/resources/Project.php b/src/resources/Project.php index 4b93d05..fd9006a 100644 --- a/src/resources/Project.php +++ b/src/resources/Project.php @@ -19,11 +19,23 @@ class Aero_Project extends Aero_Resource { public function path() { $url = '/projects'; - $id = $this->attributes['id']; - if ($id) $url .= "/$id"; + if (isset($this->attributes['id'])) { + $id = $this->attributes['id']; + $url .= "/$id"; + } return $url; } + + public function toArray() { + $array = array(); + + foreach ($this->attributes as $key => $value) { + $array[$key] = $value; + } + + return $array; + } } Aero_Connection::$engine = new Http(); @@ -32,9 +44,16 @@ public function path() { 'sid' => '4ced755889ec99408be287e3ffb83b6b' ); -$project = Aero_Project::first(2); -print_r($project); -print_r($project->created_at); - - +//$project = Aero_Project::first(2); +$params = array( + 'title' => 'Pretty', + 'descriptions' => 'Desc' +); +//$project = new Aero_Project($params); +//$project->save(); +$project = Aero_Project::first(44); +//print_r($project); +$project->title = 'New title aaaexample'; +$project->save(); +print_r(Aero_Project::first(44)); ?> 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/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); - } -} -?> From 7a49d0a9ddb57f43ecf0139e3ee588fb9f57c8a8 Mon Sep 17 00:00:00 2001 From: CeRRuTiTo Date: Thu, 20 Sep 2012 17:01:46 +0300 Subject: [PATCH 04/16] Adding response code switching --- src/Connection.php | 21 +++-- src/Request.php | 96 +++++++++++++++++++--- src/Resource.php | 151 ++++++++++++++++++++++------------- src/Response.php | 90 ++++++++++++++++++--- src/engines/Curl.php | 34 ++++---- src/engines/Engine.php | 4 +- src/engines/Http.php | 36 +++++---- src/resources/Error.php | 61 +++++++++----- src/resources/Project.php | 72 ++++++++--------- src/validators/Validator.php | 1 + 10 files changed, 397 insertions(+), 169 deletions(-) diff --git a/src/Connection.php b/src/Connection.php index b01f9bb..7064ed4 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -1,32 +1,43 @@ execute($request); diff --git a/src/Request.php b/src/Request.php index 5c7b0ca..a75bdd7 100644 --- a/src/Request.php +++ b/src/Request.php @@ -1,37 +1,107 @@ type = $type; + /** + * Assemble the request. + * + * @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 = self::ROOT_URL . $resource->path() . self::EXT; + $this->assembleUrl($site, $resource); $this->resource = $resource; } - public function __get($property) { - if (property_exists($this, $property)) { - return $this->$property; - } - } + /** + * Get a certain property of this object. + * + * @param string $property + * @return mixed + */ + public function __get($property) { + if (property_exists($this, $property)) { + return $this->$property; + } + } + + /** + * Set the credentials used for authorization. + * + * @param array $credentials + * @return void + */ public function setCredentials($credentials) { foreach ($credentials as $key => $value) { $this->$key = $value; } } + + /** + * Assemblate 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; + } } ?> diff --git a/src/Resource.php b/src/Resource.php index f796be7..a7a1e44 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -1,24 +1,56 @@ attributes)) { - return $this->attributes[$property]; - } - } + /** + * Getting certain attribute from the attributes array, where the information + * for the object is contained. + * + * @param string $property + * @return mixed + */ + public function __get($property) { + if (array_key_exists($property, $this->attributes)) { + return $this->attributes[$property]; + } + } - public function __set($property, $value) { - if (in_array($property, $this->schema)) { - $this->attributes[$property] = $value; - } - } + /** + * 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; + } + } /** - * Gets all elements connected to a certain model. + * Get all records that the called resource has. * - * @param object $parent + * @param object $params * @return object */ public static function all($params = array()) { - $type = 'GET'; - $class = get_called_class(); $resource = new $class($params); - $response = Aero_Connection::persist($resource, $type); + $response = Aero_Connection::persist($resource, self::ALL); $array = array(); @@ -65,67 +110,57 @@ public static function all($params = array()) { } /** - * Gets the first element connected to a certain model. + * Get the first element with the supplied identificator that the called + * resource has. * * @param number $id * @param object $parent - * TODO: replace params with param and returns with return * @return object */ public static function first($id, $params = array()) { - $type = 'GET'; - $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); + 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. * * @return object */ public function save() { - $type = 'PUT'; + if ($this->isNew()) return $this->send(self::CREATE); - if ($this->isNew()) $type = 'POST'; - - return $this->send($type); + return $this->send(self::UPDATE); } /** - * Destroys resource. + * Destroy this record. * * @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. * - * @param array $params - * @return void + * @param string $type + * @return object */ - public function loadAttributes($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. * - * TODO: camelcase * @return bool */ public function isNew() { @@ -135,13 +170,19 @@ public function isNew() { } /** - * Sends the resource to the connection + * Return an array containing all of the attributes of this record. * - * @param string $type - * @return 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 6d9d269..a54f702 100644 --- a/src/Response.php +++ b/src/Response.php @@ -1,24 +1,96 @@ = 200 && $code < 400): + return self::reshape($result['response']); + case 400: + throw new BadRequestException(''); + case 401: + throw new UnauthorizedException(''); + case 403: + throw new ForbiddenAccessException(''); + case 404: + throw new ResourceNotFoundException(''); + case 405: + throw new MethodNotAllowedException(''); + case 409: + throw new ResourceConflictException(''); + case 410: + throw new ResourceGoneException(''); + case 422: + throw new ResourceInvalidException(''); + case ($code >= 401 && $code <= 500): + throw new ClientError(''); + case ($code >= 500 && $code <= 600): + throw new ServerError(''); + default: + throw new ConnectionError(''); + } + } + + /** + * 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 $matches[0]; } } } + ?> diff --git a/src/engines/Curl.php b/src/engines/Curl.php index 299fefd..7442b1d 100644 --- a/src/engines/Curl.php +++ b/src/engines/Curl.php @@ -5,9 +5,10 @@ /** * cURL engine. * - * This engine executes the submited request. + * This engine executes the submited request using the cURL library. */ -class Curl implements Engine { +class Aero_Curl implements Engine { + /** * The process to be executed. * @@ -16,7 +17,7 @@ class Curl implements Engine { protected $process; /** - * Contructor, checking if the engine could work. + * Contructor, checking if the cURL library is installed. * * @return void */ @@ -29,7 +30,7 @@ public function __construct() { } /** - * Assembles and executes the process. + * Assemble and execute the process. * * @param object $request * @return object @@ -41,7 +42,7 @@ public function execute($request) { } /** - * Creates the whole request, which is to be sent. + * Create the whole process, which is to be executed. * * @param object $request * @return void @@ -58,14 +59,16 @@ public function createProcess($request) { $this->setOption(CURLOPT_RETURNTRANSFER, true); $this->setOption(CURLOPT_HTTPHEADER, $headers); - switch($request->type) { + $attributes = $request->resource->toArray(); + + switch($request->method) { 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"); @@ -76,7 +79,7 @@ public function createProcess($request) { } /** - * Process initialization. + * Initialize the main process. * * @return resource */ @@ -85,21 +88,24 @@ public function initialize() { } /** - * Executes the process and fetches the data. + * Execute and close the process. * * @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. * * @param string $name * @return array @@ -111,7 +117,7 @@ public function getInfo($name = null) { } /** - * Process options setter. + * Set option to the process. * * @param string $name * @param mixed $value diff --git a/src/engines/Engine.php b/src/engines/Engine.php index 2944fa0..0ab4500 100644 --- a/src/engines/Engine.php +++ b/src/engines/Engine.php @@ -1,7 +1,9 @@ buildHttpQuery($request); - $this->setMethod($request->type); + $this->setMethod($request->method); $this->setHeader("Authorization: Basic " . base64_encode("$sid:$auth_token") . "\r\n" . "Connection: close\r\n" . "Content-type: application/x-www-form-urlencoded\r\n" . @@ -33,20 +35,23 @@ public function execute($request) { } /** - * Fetches the data. + * Fetch the data. * * @param string $url * @param resource $context * @return object */ public function fetch($url, $context) { - $response = file_get_contents($url, false, $context); + $array = array(); - return $response; + $array['response'] = file_get_contents($url, false, $context); + $array['header'] = $http_response_header; + + return $array; } /** - * Gets the request method. + * Get the request method. * * @return string */ @@ -55,7 +60,7 @@ public function getMethod() { } /** - * Sets the request method. + * Set the request method. * * @param string $type */ @@ -64,7 +69,7 @@ public function setMethod($type) { } /** - * Gets the request header. + * Get the request header. * * @return string */ @@ -73,7 +78,7 @@ public function getHeader() { } /** - * Sets the request header. + * Set the request header. * * @param string $data */ @@ -82,7 +87,7 @@ public function setHeader($data) { } /** - * Gets the request content. + * Get the request content. * * @return string */ @@ -91,7 +96,7 @@ public function getContent() { } /** - * Sets the request content. + * Set the request content. * * @param string $data */ @@ -100,7 +105,7 @@ public function setContent($data) { } /** - * Builds the http query used as content in the request. + * Build the http query used as content in the request. * * @param object $resource * @return string @@ -108,7 +113,7 @@ public function setContent($data) { public function buildHttpQuery($resource) { $array = array(); - $attributes = $resource->resource->toArray(); + $attributes = $resource->resource->toArray(); foreach ($attributes as $key => $value) { if ($value) $array[$key] = $value; @@ -118,7 +123,7 @@ public function buildHttpQuery($resource) { } /** - * Builds the context for the request. + * Build the context for the request. * * @param array $data * @return response @@ -127,4 +132,5 @@ public function buildContext($data) { return stream_context_create(array('http' => $data)); } } + ?> diff --git a/src/resources/Error.php b/src/resources/Error.php index 08fa48f..58abe16 100644 --- a/src/resources/Error.php +++ b/src/resources/Error.php @@ -2,28 +2,49 @@ require_once 'src/Resource.php'; +/** + * Aero_Error class. + * + * Resource that has coresponding records in the database, which are with + * the supplied in the schema format. + */ class Aero_Error extends Aero_Resource { - public $id; - public $project_id; - public $message; - public $occured; - public $resolved; - public $created_at; - public $updated_at; - - protected $schema = array( - 'id', - 'project_id', - 'message', - 'occured', - 'resolved', - 'created_at', - 'updated_at' - ); - - // TODO: use this + + /** + * Schema, containing the attributes that this record should have. + * + * @var array + */ + protected $schema = array( + 'id', + 'project_id', + 'message', + 'occured', + 'resolved', + 'created_at', + 'updated_at' + ); + + /** + * The actual attributes of the record with their values. + * + * @var array + */ + protected $attributes = array(); + + /** + * Assemble the relative path that should be requested. + * + * @return string + */ public function path() { - return str_replace('/projects/:project_id/errors', ':project_id', $this->getProjectId()); + $url = str_replace(':project_id', $this->project_id, '/projects/:project_id/errors'); + + if ($this->id) { + $url .= "/$this->id"; + } + + return $url; } } diff --git a/src/resources/Project.php b/src/resources/Project.php index fd9006a..b6534af 100644 --- a/src/resources/Project.php +++ b/src/resources/Project.php @@ -2,58 +2,56 @@ require_once 'src/Resource.php'; +/** + * Aero_Project class. + * + * Resource that has coresponding records in the database, which are with + * the supplied in the schema format. + */ class Aero_Project extends Aero_Resource { + /** - * TODO: use schema + * Schema, containing the attributes that this record should have. + * + * @var array */ protected $schema = array( 'id', 'title', - 'description', - 'created_at', - 'updated_at' + 'description', + 'created_at', + 'updated_at' ); + /** + * The actual attributes of the record with their values. + * + * @var array + */ protected $attributes = array(); - public function path() { - $url = '/projects'; - - if (isset($this->attributes['id'])) { - $id = $this->attributes['id']; - $url .= "/$id"; - } - - return $url; - } - - public function toArray() { - $array = array(); + /** + * Assemble the relative path that should be requested. + * + * @return string + */ + public function path() { + $url = '/projects'; - foreach ($this->attributes as $key => $value) { - $array[$key] = $value; - } + if ($this->id) { + $url .= "/$this->id"; + } - return $array; - } + return $url; + } } -Aero_Connection::$engine = new Http(); +Aero_Connection::$engine = new Aero_Http(); Aero_Connection::$credentials = array( - 'auth_token' => '52b8963d43b3f9081f58977b5aa3c110', - 'sid' => '4ced755889ec99408be287e3ffb83b6b' + 'auth_token' => '52b8963d43b3f9081f58977b5aa3c110', + 'sid' => '4ced755889ec99408be287e3ffb83b6b' ); -//$project = Aero_Project::first(2); -$params = array( - 'title' => 'Pretty', - 'descriptions' => 'Desc' -); -//$project = new Aero_Project($params); -//$project->save(); -$project = Aero_Project::first(44); -//print_r($project); -$project->title = 'New title aaaexample'; -$project->save(); -print_r(Aero_Project::first(44)); +$project = Aero_Project::all(); +print_r($project); ?> diff --git a/src/validators/Validator.php b/src/validators/Validator.php index c3e6868..9ebf469 100644 --- a/src/validators/Validator.php +++ b/src/validators/Validator.php @@ -6,6 +6,7 @@ class Validator { * @var object */ public static $resource; + public $opa; /** * Checks if the validations rules apply. From dd9c858b4f8567405833bb36507ffde75abaaceb Mon Sep 17 00:00:00 2001 From: CeRRuTiTo Date: Thu, 20 Sep 2012 18:55:08 +0300 Subject: [PATCH 05/16] Add exceptions and comments --- src/Exceptions.php | 27 +++++ src/Request.php | 2 +- src/Response.php | 34 +++--- src/engines/Http.php | 4 +- src/resources/Project.php | 8 -- src/validators/Validator.php | 41 +++---- test/unit/ExceptionsTest.php | 127 +++++++++++++++++++++ test/unit/RequestTest.php | 32 ++++-- test/unit/ResourceTest.php | 27 +---- test/unit/ResponseTest.php | 146 ++++++++++++++++++++++++- test/unit/engines/CurlTest.php | 8 +- test/unit/engines/HttpTest.php | 26 ++--- test/unit/validators/ValidatorTest.php | 64 ++--------- 13 files changed, 376 insertions(+), 170 deletions(-) create mode 100644 src/Exceptions.php create mode 100644 test/unit/ExceptionsTest.php diff --git a/src/Exceptions.php b/src/Exceptions.php new file mode 100644 index 0000000..093d95c --- /dev/null +++ b/src/Exceptions.php @@ -0,0 +1,27 @@ + diff --git a/src/Request.php b/src/Request.php index a75bdd7..b90aa2a 100644 --- a/src/Request.php +++ b/src/Request.php @@ -50,7 +50,7 @@ class Aero_Request { protected $resource; /** - * Assemble the request. + * Assemble the request with the provided data. * * @param string $method * @param object $resource diff --git a/src/Response.php b/src/Response.php index a54f702..3a507ca 100644 --- a/src/Response.php +++ b/src/Response.php @@ -1,5 +1,7 @@ = 200 && $code < 400): return self::reshape($result['response']); case 400: - throw new BadRequestException(''); + throw new BadRequestException('The request cannot be understood by the server due to malformed syntax.'); case 401: - throw new UnauthorizedException(''); + throw new UnauthorizedException('The request requires user authentication.'); case 403: - throw new ForbiddenAccessException(''); + throw new ForbiddenAccessException('The server understood the request, but is refusing to fulfill it.'); case 404: - throw new ResourceNotFoundException(''); + throw new ResourceNotFoundException('The server has not found anything matching the Request-URI.'); case 405: - throw new MethodNotAllowedException(''); + throw new MethodNotAllowedException('The method specified in the Request-Line is not allowed for this resource.'); case 409: - throw new ResourceConflictException(''); + 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(''); + throw new ResourceGoneException('The requested resource is no longer available at the server.'); case 422: - throw new ResourceInvalidException(''); + 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 ClientError(''); + throw new ClientException(''); case ($code >= 500 && $code <= 600): - throw new ServerError(''); + throw new ServerException(''); default: - throw new ConnectionError(''); + throw new ConnectionException(''); } } @@ -88,7 +90,7 @@ public static function responseCode($response) { } else { preg_match('/\s+\d+\s+/', $response[0], $matches); - return $matches[0]; + return trim($matches[0]); } } } diff --git a/src/engines/Http.php b/src/engines/Http.php index 118dea9..3e82fb8 100644 --- a/src/engines/Http.php +++ b/src/engines/Http.php @@ -3,8 +3,10 @@ require_once 'Engine.php'; /** - * TODO: prefix everything with aero :( + * Aero_Http class. * + * Engine that uses the file_get_contents method. It should be used + * when cURL is not available on the server. */ class Aero_Http implements Engine { diff --git a/src/resources/Project.php b/src/resources/Project.php index b6534af..7127e4d 100644 --- a/src/resources/Project.php +++ b/src/resources/Project.php @@ -46,12 +46,4 @@ public function path() { } } -Aero_Connection::$engine = new Aero_Http(); -Aero_Connection::$credentials = array( - 'auth_token' => '52b8963d43b3f9081f58977b5aa3c110', - 'sid' => '4ced755889ec99408be287e3ffb83b6b' -); - -$project = Aero_Project::all(); -print_r($project); ?> diff --git a/src/validators/Validator.php b/src/validators/Validator.php index 9ebf469..b34c83e 100644 --- a/src/validators/Validator.php +++ b/src/validators/Validator.php @@ -1,12 +1,18 @@ validate as $attribute => $rule) { foreach($rule as $name => $value) { - $valid = self::$name($attribute, $value); + $array[] = self::$name($attribute, $value); } } - return $valid; + if (in_array(false, $array)) return false; + + return true; } /** @@ -38,27 +46,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/unit/ExceptionsTest.php b/test/unit/ExceptionsTest.php new file mode 100644 index 0000000..4f04355 --- /dev/null +++ b/test/unit/ExceptionsTest.php @@ -0,0 +1,127 @@ +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..0d84b48 100644 --- a/test/unit/RequestTest.php +++ b/test/unit/RequestTest.php @@ -2,18 +2,28 @@ require_once 'src/Request.php'; class AeroRequestTest extends PHPUnit_Framework_TestCase { - public function testInitialization() { - $type = 'GET'; - $resource = new AeroRequest_Error(); - $resource->id = 1; - $credentials = array( - 'auth_token' => 'AUTH', - 'sid' => 'SID' - ); + public function testInitialization() { + $method = 'METHOD'; + $resource = new TestRequest_Error(); + $credentials = array( + 'auth_token' => 'AUTH_TOKEN', + 'sid' => 'SID' + ); + $site = 'URL/'; - $result = new Aero_Request($type, $resource, $credentials); - } + $request = new Aero_Request($method, $resource, $credentials, $site); + + $this->assertEquals('URL/PATH.json', $request->url); + $this->assertEquals($method, $request->method); + $this->assertEquals($credentials['auth_token'], $request->auth_token); + $this->assertEquals($credentials['sid'], $request->sid); + $this->assertEquals($resource, $request->resource); + } } -class AeroRequest_Error {} +class TestRequest_Error { + public function path() { + return 'PATH'; + } +} ?> diff --git a/test/unit/ResourceTest.php b/test/unit/ResourceTest.php index 1b64040..0584ffd 100644 --- a/test/unit/ResourceTest.php +++ b/test/unit/ResourceTest.php @@ -21,24 +21,6 @@ public function testInitialization() { $this->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->loadAttributes($changed); - - $this->assertEquals($expected, $aero->title); - } - public function testIsNewNegative() { $params = array( 'id' => 1 @@ -98,8 +80,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..5fff95a 100644 --- a/test/unit/ResponseTest.php +++ b/test/unit/ResponseTest.php @@ -1,29 +1,165 @@ '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/engines/CurlTest.php b/test/unit/engines/CurlTest.php index 8e4da97..02242bf 100644 --- a/test/unit/engines/CurlTest.php +++ b/test/unit/engines/CurlTest.php @@ -6,7 +6,7 @@ public function testExecute() { $request = 'request'; $expected = 'response'; - $curl = $this->getMock('Curl', array('createProcess', 'fetch')); + $curl = $this->getMock('Aero_Curl', array('createProcess', 'fetch')); $curl->expects($this->once()) ->method('createProcess') @@ -21,7 +21,7 @@ public function testExecute() { } public function testGetInfoWholeProcess() { - $curl = new Curl(); + $curl = new Aero_Curl(); $result = $curl->getInfo(); $this->assertEquals('array', gettype($result)); @@ -30,7 +30,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 +39,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..f1b6608 100644 --- a/test/unit/engines/HttpTest.php +++ b/test/unit/engines/HttpTest.php @@ -3,7 +3,7 @@ class HttpTest extends PHPUnit_Framework_TestCase { public function testExecutePublicity() { - $reflector = new ReflectionMethod('Http', 'execute'); + $reflector = new ReflectionMethod('Aero_Http', 'execute'); $this->assertThat( true, @@ -16,7 +16,7 @@ public function testExecuteWithTokenAndSid() { $sid = 'SID'; $expected = 'response'; - $engine = $this->getMock('Http', array('fetch', 'buildHttpQuery')); + $engine = $this->getMock('Aero_Http', array('fetch', 'buildHttpQuery')); $engine->expects($this->once()) ->method('fetch') @@ -25,7 +25,7 @@ public function testExecuteWithTokenAndSid() { $request->auth_token = $auth_token; $request->sid = $sid; $request->url = '/v1/projects'; - $request->type = 'GET'; + $request->method = 'GET'; $result = $engine->execute($request); @@ -35,20 +35,8 @@ public function testExecuteWithTokenAndSid() { $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 +46,7 @@ public function testSetAndGetHeader() { } public function testSetAndGetContent() { - $request = new Http(); + $request = new Aero_Http(); $expected = 'content'; $request->setContent($expected); @@ -68,7 +56,7 @@ public function testSetAndGetContent() { } public function testSetAndGetMethod() { - $request = new Http(); + $request = new Aero_Http(); $actual = 'method'; $request->setMethod($actual); @@ -79,7 +67,7 @@ public function testSetAndGetMethod() { } public function testBuildContext() { - $request = new Http(); + $request = new Aero_Http(); $data = array( 'method' => 'POST', diff --git a/test/unit/validators/ValidatorTest.php b/test/unit/validators/ValidatorTest.php index 8233ab2..06b1254 100644 --- a/test/unit/validators/ValidatorTest.php +++ b/test/unit/validators/ValidatorTest.php @@ -1,8 +1,9 @@ validate = array( @@ -11,12 +12,13 @@ public function testValidatePresence() { ), ); - $this->assertFalse(Validator::is_valid($test)); + $this->assertFalse(Aero_Validator::is_valid($test)); } - public function testValidateNotPresence() { + public function testValidateNotPresent() { $test = new ValidationResourceTest(); $test->title = 'present'; + $test->description = null; $test->validate = array( 'title' => array( @@ -24,59 +26,7 @@ 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)); - } - - public function testValidateMaxLengthUnder() { - $test = new ValidationResourceTest(); - $test->title = 'present'; - - $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 - ), - ); - - $this->assertFalse(Validator::is_valid($test)); + $this->assertTrue(Aero_Validator::is_valid($test)); } } From c6a5bae696c4d1ae5e070114d408bd9bef203ee1 Mon Sep 17 00:00:00 2001 From: CeRRuTiTo Date: Fri, 21 Sep 2012 09:26:35 +0300 Subject: [PATCH 06/16] Add license to each file --- src/Connection.php | 7 +++++++ src/Exceptions.php | 7 +++++++ src/Request.php | 7 +++++++ src/Resource.php | 7 +++++++ src/Response.php | 7 +++++++ src/engines/Curl.php | 7 +++++++ src/engines/Engine.php | 7 +++++++ src/engines/Http.php | 7 +++++++ src/resources/Error.php | 7 +++++++ src/resources/Project.php | 7 +++++++ src/validators/Validator.php | 7 +++++++ test/unit/ConnectionTest.php | 8 ++++++++ test/unit/ExceptionsTest.php | 7 +++++++ test/unit/RequestTest.php | 8 ++++++++ test/unit/ResourceTest.php | 8 ++++++++ test/unit/ResponseTest.php | 7 +++++++ test/unit/engines/CurlTest.php | 8 ++++++++ test/unit/engines/HttpTest.php | 8 ++++++++ test/unit/resources/ErrorTest.php | 8 ++++++++ test/unit/resources/ProjectTest.php | 8 ++++++++ test/unit/validators/ValidatorTest.php | 7 +++++++ 21 files changed, 154 insertions(+) diff --git a/src/Connection.php b/src/Connection.php index 7064ed4..ec5cd61 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -1,5 +1,12 @@ Date: Sun, 14 Oct 2012 10:13:26 +0300 Subject: [PATCH 07/16] Add comments and fix some stuff --- src/AeroIO.php | 86 ++++++++++++ src/Connection.php | 2 +- src/ExceptionHandler.php | 71 ++++++++++ src/Exceptions.php | 36 +++++ src/Request.php | 62 ++++++--- src/Resource.php | 7 + src/Response.php | 2 +- src/engines/Curl.php | 10 +- src/engines/Engine.php | 7 + src/engines/Http.php | 10 +- src/resources/Error.php | 9 +- src/resources/Project.php | 7 - src/validators/Validator.php | 4 +- test/unit/AeroIOTest.php | 29 +++++ test/unit/ExceptionHandlerTest.php | 18 +++ test/unit/ExceptionsTest.php | 154 +++++++++++----------- test/unit/RequestTest.php | 36 ++--- test/unit/ResourceTest.php | 12 +- test/unit/ResponseTest.php | 174 ++++++++++++------------- test/unit/validators/ValidatorTest.php | 2 +- 20 files changed, 505 insertions(+), 233 deletions(-) create mode 100644 src/AeroIO.php create mode 100644 src/ExceptionHandler.php create mode 100644 test/unit/AeroIOTest.php create mode 100644 test/unit/ExceptionHandlerTest.php diff --git a/src/AeroIO.php b/src/AeroIO.php new file mode 100644 index 0000000..5dfddda --- /dev/null +++ b/src/AeroIO.php @@ -0,0 +1,86 @@ + $value) { + self::$$key = $value; + } + + if (!self::$engine) self::$engine = new Aero_Curl(); + } + + /** + * Set exception handler for certain project with the provided data. + * + * @return void + */ + public static function handleExceptions() { + Aero_Connection::$site = self::$site; + Aero_Connection::$engine = self::$engine; + Aero_Connection::$credentials = array( + 'auth_token' => self::$auth_token, + 'sid' => self::$sid + ); + + Aero_ExceptionHandler::for_project(self::$project); + } +} + +?> diff --git a/src/Connection.php b/src/Connection.php index ec5cd61..3b69039 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -19,7 +19,7 @@ class Aero_Connection { * * @static string */ - public static $site = 'http://localhost:3000/api/v1'; + public static $site = 'https://aero.io/api/v1'; /** * Credentials to be used for user authorization. diff --git a/src/ExceptionHandler.php b/src/ExceptionHandler.php new file mode 100644 index 0000000..6447008 --- /dev/null +++ b/src/ExceptionHandler.php @@ -0,0 +1,71 @@ + 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")); + } +} + +AeroIO::configure(array( + 'site' => 'http://localhost:3000/api/v1', + 'auth_token' => '52b8963d43b3f9081f58977b5aa3c110', + 'sid' => '4ced755889ec99408be287e3ffb83b6b', + 'project' => 56 +)); + +AeroIO::handleExceptions(); + +print_r(Aero_Error::all(array('project_id' => 56))); +throw new Exception("And another error just like that", 20); + + +?> diff --git a/src/Exceptions.php b/src/Exceptions.php index 920073c..b785581 100644 --- a/src/Exceptions.php +++ b/src/Exceptions.php @@ -7,28 +7,64 @@ * @license The MIT License */ +/** + * Client side exceptions. + */ class ClientException extends Exception {} +/** + * Server side exceptions. + */ class ServerException extends Exception {} +/** + * Exceptions, when the connection has timed out or something uncaught happened. + */ class ConnectionException extends Exception {} +/** + * Exception, when new URI was assigned. + */ class RedirectionException extends ConnectionException {} +/** + * Exception, when the request cannot be understood by the server. + */ class BadRequestException extends ClientException {} +/** + * Exception, when user authentications is required. + */ class UnauthorizedException extends ClientException {} +/** + * Exception, when the server is refusing to fulfill the request. + */ class ForbiddenAccessException extends ClientException {} +/** + * Exception, when nothing mathing is found. + */ class ResourceNotFoundException extends ClientException {} +/** + * Exception, when the method type is not allowed. + */ class MethodNotAllowedException extends ClientException {} +/** + * Exception, when the request cannot be fulfiled due to a resource conflict. + */ class ResourceConflictException extends ClientException {} +/** + * Exception, when the resource is no longer available. + */ class ResourceGoneException extends ClientException {} +/** + * Exception, when there were semantic errors in the request. + */ class ResourceInvalidException extends ClientException {} ?> diff --git a/src/Request.php b/src/Request.php index de58300..f65c2d5 100644 --- a/src/Request.php +++ b/src/Request.php @@ -73,33 +73,53 @@ public function __construct($method, $resource, $credentials, $site) { $this->resource = $resource; } + /** + * Get the SID used for the authorization. + * + * @return string + */ + public function getSid() { + return $this->sid; + } /** - * Get a certain property of this object. + * Get the auth token used for the authentication. * - * @param string $property - * @return mixed + * @return string */ - public function __get($property) { - if (property_exists($this, $property)) { - return $this->$property; - } + public function getAuthToken() { + return $this->auth_token; } /** - * Set the credentials used for authorization. + * Get the type of method used for the request. * - * @param array $credentials - * @return void + * @return string */ - public function setCredentials($credentials) { - foreach ($credentials as $key => $value) { - $this->$key = $value; - } + public function getMethod() { + return $this->method; + } + + /** + * Get the URL address for the request. + * + * @return string + */ + public function getUrl() { + return $this->url; } /** - * Assemblate the url from the base site url, the path to the required + * 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 @@ -109,6 +129,18 @@ public function setCredentials($credentials) { 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 0bbf68b..d8998ff 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -56,6 +56,13 @@ class Aero_Resource { */ const DESTROY = 'DELETE'; + /** + * The actual attributes of the record with their values. + * + * @var array + */ + protected $attributes = array(); + /** * Constructor, setting the attributes of the resource. * diff --git a/src/Response.php b/src/Response.php index 19a9cc1..ca1dfad 100644 --- a/src/Response.php +++ b/src/Response.php @@ -25,7 +25,7 @@ class Aero_Response { public static function handle($result) { $code = self::responseCode($result['header']); - switch($code) { + switch ($code) { case 301: throw new RedirectionException('The data requested has been assigned new URI.'); case 302: diff --git a/src/engines/Curl.php b/src/engines/Curl.php index 11693d0..f751a2d 100644 --- a/src/engines/Curl.php +++ b/src/engines/Curl.php @@ -55,20 +55,20 @@ public function execute($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); - $attributes = $request->resource->toArray(); + $attributes = $request->getResource()->toArray(); - switch($request->method) { + switch($request->getMethod()) { case 'POST': $this->setOption(CURLOPT_POST, true); $this->setOption(CURLOPT_POSTFIELDS, $attributes); diff --git a/src/engines/Engine.php b/src/engines/Engine.php index 296b94c..e3437b8 100644 --- a/src/engines/Engine.php +++ b/src/engines/Engine.php @@ -13,6 +13,13 @@ * Interface that should be used by all of the supplied engines. */ interface Engine { + + /** + * Assemble and execute the request. + * + * @param object $request + * @return object + */ public function execute($request); } diff --git a/src/engines/Http.php b/src/engines/Http.php index 5b082a4..ba9e292 100644 --- a/src/engines/Http.php +++ b/src/engines/Http.php @@ -24,12 +24,12 @@ class Aero_Http implements Engine { * @return object */ public function execute($request) { - $sid = $request->sid; - $auth_token = $request->auth_token; + $sid = $request->getSid(); + $auth_token = $request->getAuthToken(); $query = $this->buildHttpQuery($request); - $this->setMethod($request->method); + $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" . @@ -40,7 +40,7 @@ public function execute($request) { $context = $this->buildContext($this->request); - return $this->fetch($request->url, $context); + return $this->fetch($request->getUrl(), $context); } /** @@ -122,7 +122,7 @@ public function setContent($data) { public function buildHttpQuery($resource) { $array = array(); - $attributes = $resource->resource->toArray(); + $attributes = $resource->getResource()->toArray(); foreach ($attributes as $key => $value) { if ($value) $array[$key] = $value; diff --git a/src/resources/Error.php b/src/resources/Error.php index 84df06c..b6d7648 100644 --- a/src/resources/Error.php +++ b/src/resources/Error.php @@ -32,20 +32,13 @@ class Aero_Error extends Aero_Resource { 'updated_at' ); - /** - * The actual attributes of the record with their values. - * - * @var array - */ - protected $attributes = array(); - /** * Assemble the relative path that should be requested. * * @return string */ public function path() { - $url = str_replace(':project_id', $this->project_id, '/projects/:project_id/errors'); + $url = '/projects/' . $this->project_id . '/errors'; if ($this->id) { $url .= "/$this->id"; diff --git a/src/resources/Project.php b/src/resources/Project.php index 70d2dae..9ed36c8 100644 --- a/src/resources/Project.php +++ b/src/resources/Project.php @@ -30,13 +30,6 @@ class Aero_Project extends Aero_Resource { 'updated_at' ); - /** - * The actual attributes of the record with their values. - * - * @var array - */ - protected $attributes = array(); - /** * Assemble the relative path that should be requested. * diff --git a/src/validators/Validator.php b/src/validators/Validator.php index dd27746..eb8b5ba 100644 --- a/src/validators/Validator.php +++ b/src/validators/Validator.php @@ -38,9 +38,9 @@ public static function is_valid($resource) { } } - if (in_array(false, $array)) return false; + if (in_array(false, $array)) return false; - return true; + return true; } /** diff --git a/test/unit/AeroIOTest.php b/test/unit/AeroIOTest.php new file mode 100644 index 0000000..3ef1104 --- /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/ExceptionHandlerTest.php b/test/unit/ExceptionHandlerTest.php new file mode 100644 index 0000000..893c1d5 --- /dev/null +++ b/test/unit/ExceptionHandlerTest.php @@ -0,0 +1,18 @@ + diff --git a/test/unit/ExceptionsTest.php b/test/unit/ExceptionsTest.php index 0e228c6..2928a3a 100644 --- a/test/unit/ExceptionsTest.php +++ b/test/unit/ExceptionsTest.php @@ -10,125 +10,125 @@ require_once 'src/Exceptions.php'; class ExceptionsTest extends PHPUnit_Framework_TestCase { - public function testClientException() { - $this->setExpectedException('ClientException'); + public function testClientException() { + $this->setExpectedException('ClientException'); - $exception = new ClientException('message'); + $exception = new ClientException('message'); - $this->assertTrue($exception instanceof Exception); + $this->assertTrue($exception instanceof Exception); - throw $exception; - } + throw $exception; + } - public function testServerException() { - $this->setExpectedException('ServerException'); + public function testServerException() { + $this->setExpectedException('ServerException'); - $exception = new ServerException('message'); + $exception = new ServerException('message'); - $this->assertTrue($exception instanceof Exception); + $this->assertTrue($exception instanceof Exception); - throw $exception; - } + throw $exception; + } - public function testConnectionException() { - $this->setExpectedException('ConnectionException'); + public function testConnectionException() { + $this->setExpectedException('ConnectionException'); - $exception = new ConnectionException('message'); + $exception = new ConnectionException('message'); - $this->assertTrue($exception instanceof Exception); + $this->assertTrue($exception instanceof Exception); - throw $exception; - } + throw $exception; + } - public function testRedirectionException() { - $this->setExpectedException('RedirectionException'); + public function testRedirectionException() { + $this->setExpectedException('RedirectionException'); - $exception = new RedirectionException('message'); + $exception = new RedirectionException('message'); - $this->assertTrue($exception instanceof ConnectionException); + $this->assertTrue($exception instanceof ConnectionException); - throw $exception; - } + throw $exception; + } - public function testBadRequestException() { - $this->setExpectedException('BadRequestException'); + public function testBadRequestException() { + $this->setExpectedException('BadRequestException'); - $exception = new BadRequestException('message'); + $exception = new BadRequestException('message'); - $this->assertTrue($exception instanceof ClientException); + $this->assertTrue($exception instanceof ClientException); - throw $exception; - } - - public function testUnauthorizedException() { - $this->setExpectedException('UnauthorizedException'); + throw $exception; + } + + public function testUnauthorizedException() { + $this->setExpectedException('UnauthorizedException'); - $exception = new UnauthorizedException('message'); + $exception = new UnauthorizedException('message'); - $this->assertTrue($exception instanceof ClientException); + $this->assertTrue($exception instanceof ClientException); - throw $exception; - } - - public function testForbiddenAccessException() { - $this->setExpectedException('ForbiddenAccessException'); + throw $exception; + } + + public function testForbiddenAccessException() { + $this->setExpectedException('ForbiddenAccessException'); - $exception = new ForbiddenAccessException('message'); + $exception = new ForbiddenAccessException('message'); - $this->assertTrue($exception instanceof ClientException); + $this->assertTrue($exception instanceof ClientException); - throw $exception; - } - - public function testResourceNotFoundException() { - $this->setExpectedException('ResourceNotFoundException'); + throw $exception; + } + + public function testResourceNotFoundException() { + $this->setExpectedException('ResourceNotFoundException'); - $exception = new ResourceNotFoundException('message'); + $exception = new ResourceNotFoundException('message'); - $this->assertTrue($exception instanceof ClientException); + $this->assertTrue($exception instanceof ClientException); - throw $exception; - } - - public function testMethodNotAllowedException() { - $this->setExpectedException('MethodNotAllowedException'); + throw $exception; + } + + public function testMethodNotAllowedException() { + $this->setExpectedException('MethodNotAllowedException'); - $exception = new MethodNotAllowedException('message'); + $exception = new MethodNotAllowedException('message'); - $this->assertTrue($exception instanceof ClientException); + $this->assertTrue($exception instanceof ClientException); - throw $exception; - } - - public function testResourceConflictException() { - $this->setExpectedException('ResourceConflictException'); + throw $exception; + } + + public function testResourceConflictException() { + $this->setExpectedException('ResourceConflictException'); - $exception = new ResourceConflictException('message'); + $exception = new ResourceConflictException('message'); - $this->assertTrue($exception instanceof ClientException); + $this->assertTrue($exception instanceof ClientException); - throw $exception; - } + throw $exception; + } - public function testResourceGoneException() { - $this->setExpectedException('ResourceGoneException'); + public function testResourceGoneException() { + $this->setExpectedException('ResourceGoneException'); - $exception = new ResourceGoneException('message'); + $exception = new ResourceGoneException('message'); - $this->assertTrue($exception instanceof ClientException); + $this->assertTrue($exception instanceof ClientException); - throw $exception; - } + throw $exception; + } - public function testResourceInvalidException() { - $this->setExpectedException('ResourceInvalidException'); + public function testResourceInvalidException() { + $this->setExpectedException('ResourceInvalidException'); - $exception = new ResourceInvalidException('message'); + $exception = new ResourceInvalidException('message'); - $this->assertTrue($exception instanceof ClientException); + $this->assertTrue($exception instanceof ClientException); - throw $exception; - } + throw $exception; + } } ?> diff --git a/test/unit/RequestTest.php b/test/unit/RequestTest.php index 3e0da9e..e38922e 100644 --- a/test/unit/RequestTest.php +++ b/test/unit/RequestTest.php @@ -10,28 +10,28 @@ require_once 'src/Request.php'; class AeroRequestTest extends PHPUnit_Framework_TestCase { - public function testInitialization() { - $method = 'METHOD'; - $resource = new TestRequest_Error(); - $credentials = array( - 'auth_token' => 'AUTH_TOKEN', - 'sid' => 'SID' - ); - $site = 'URL/'; + public function testInitialization() { + $method = 'METHOD'; + $resource = new TestRequest_Error(); + $credentials = array( + 'auth_token' => 'AUTH_TOKEN', + 'sid' => 'SID' + ); + $site = 'URL/'; - $request = new Aero_Request($method, $resource, $credentials, $site); + $request = new Aero_Request($method, $resource, $credentials, $site); - $this->assertEquals('URL/PATH.json', $request->url); - $this->assertEquals($method, $request->method); - $this->assertEquals($credentials['auth_token'], $request->auth_token); - $this->assertEquals($credentials['sid'], $request->sid); - $this->assertEquals($resource, $request->resource); - } + $this->assertEquals('URL/PATH.json', $request->url); + $this->assertEquals($method, $request->method); + $this->assertEquals($credentials['auth_token'], $request->auth_token); + $this->assertEquals($credentials['sid'], $request->sid); + $this->assertEquals($resource, $request->resource); + } } class TestRequest_Error { - public function path() { - return 'PATH'; - } + public function path() { + return 'PATH'; + } } ?> diff --git a/test/unit/ResourceTest.php b/test/unit/ResourceTest.php index e7aa8df..f9b9769 100644 --- a/test/unit/ResourceTest.php +++ b/test/unit/ResourceTest.php @@ -88,11 +88,11 @@ public function testDestroy() { } class Test_Resource extends Aero_Resource { - public $schema = array( - 'id', - 'title', - 'description' - ); - public $attributes = array(); + public $schema = array( + 'id', + 'title', + 'description' + ); + public $attributes = array(); } ?> diff --git a/test/unit/ResponseTest.php b/test/unit/ResponseTest.php index 8664dfb..efba79c 100644 --- a/test/unit/ResponseTest.php +++ b/test/unit/ResponseTest.php @@ -33,140 +33,140 @@ public function testReshapeMultipleObjects() { $this->assertEquals($expected, $result); } - public function testResponseCodeWhenCurl() { - $expected = 200; + public function testResponseCodeWhenCurl() { + $expected = 200; - $array = array( - 'http_code' => $expected - ); + $array = array( + 'http_code' => $expected + ); - $result = Aero_Response::responseCode($array); + $result = Aero_Response::responseCode($array); - $this->assertEquals($expected, $result); - } + $this->assertEquals($expected, $result); + } - public function testResponseCodeWhenHttp() { - $expected = 200; + public function testResponseCodeWhenHttp() { + $expected = 200; - $array = array("HTTP/1.1 $expected OK", 'OTHER', 'DATA'); + $array = array("HTTP/1.1 $expected OK", 'OTHER', 'DATA'); - $result = Aero_Response::responseCode($array); + $result = Aero_Response::responseCode($array); - $this->assertEquals($expected, $result); - } + $this->assertEquals($expected, $result); + } - public function testRaiseRedirectionExceptionWhen301() { - $this->setExpectedException('RedirectionException'); + public function testRaiseRedirectionExceptionWhen301() { + $this->setExpectedException('RedirectionException'); - $response['header']['http_code'] = 301; + $response['header']['http_code'] = 301; - Aero_Response::handle($response); - } + Aero_Response::handle($response); + } - public function testRaiseRedirectionExceptionWhen302() { - $this->setExpectedException('RedirectionException'); + public function testRaiseRedirectionExceptionWhen302() { + $this->setExpectedException('RedirectionException'); - $response['header']['http_code'] = 302; + $response['header']['http_code'] = 302; - Aero_Response::handle($response); - } + Aero_Response::handle($response); + } - public function testRaiseRedirectionExceptionWhen303() { - $this->setExpectedException('RedirectionException'); + public function testRaiseRedirectionExceptionWhen303() { + $this->setExpectedException('RedirectionException'); - $response['header']['http_code'] = 303; + $response['header']['http_code'] = 303; - Aero_Response::handle($response); - } + Aero_Response::handle($response); + } - public function testRaiseRedirectionExceptionExceptionWhen307() { - $this->setExpectedException('RedirectionException'); + public function testRaiseRedirectionExceptionExceptionWhen307() { + $this->setExpectedException('RedirectionException'); - $response['header']['http_code'] = 307; + $response['header']['http_code'] = 307; - Aero_Response::handle($response); - } + Aero_Response::handle($response); + } - public function testRaiseBadRequestExceptionWhen400() { - $this->setExpectedException('BadRequestException'); + public function testRaiseBadRequestExceptionWhen400() { + $this->setExpectedException('BadRequestException'); - $response['header']['http_code'] = 400; + $response['header']['http_code'] = 400; - Aero_Response::handle($response); - } + Aero_Response::handle($response); + } - public function testRaiseUnauthorizedExceptionWhen401() { - $this->setExpectedException('UnauthorizedException'); + public function testRaiseUnauthorizedExceptionWhen401() { + $this->setExpectedException('UnauthorizedException'); - $response['header']['http_code'] = 401; + $response['header']['http_code'] = 401; - Aero_Response::handle($response); - } - - public function testRaiseForbiddenAccessExceptionWhen403() { - $this->setExpectedException('ForbiddenAccessException'); + Aero_Response::handle($response); + } + + public function testRaiseForbiddenAccessExceptionWhen403() { + $this->setExpectedException('ForbiddenAccessException'); - $response['header']['http_code'] = 403; + $response['header']['http_code'] = 403; - Aero_Response::handle($response); - } - - public function testRaiseResourceNotFoundExceptionWhen404() { - $this->setExpectedException('ResourceNotFoundException'); + Aero_Response::handle($response); + } + + public function testRaiseResourceNotFoundExceptionWhen404() { + $this->setExpectedException('ResourceNotFoundException'); - $response['header']['http_code'] = 404; + $response['header']['http_code'] = 404; - Aero_Response::handle($response); - } - - public function testRaiseMethodNotAllowedExceptionWhen405() { - $this->setExpectedException('MethodNotAllowedException'); + Aero_Response::handle($response); + } + + public function testRaiseMethodNotAllowedExceptionWhen405() { + $this->setExpectedException('MethodNotAllowedException'); - $response['header']['http_code'] = 405; + $response['header']['http_code'] = 405; - Aero_Response::handle($response); - } + Aero_Response::handle($response); + } - public function testRaiseResourceConflictExceptionWhen409() { - $this->setExpectedException('ResourceConflictException'); + public function testRaiseResourceConflictExceptionWhen409() { + $this->setExpectedException('ResourceConflictException'); - $response['header']['http_code'] = 409; + $response['header']['http_code'] = 409; - Aero_Response::handle($response); - } + Aero_Response::handle($response); + } - public function testRaiseResourceGoneExceptionWhen410() { - $this->setExpectedException('ResourceGoneException'); + public function testRaiseResourceGoneExceptionWhen410() { + $this->setExpectedException('ResourceGoneException'); - $response['header']['http_code'] = 410; + $response['header']['http_code'] = 410; - Aero_Response::handle($response); - } + Aero_Response::handle($response); + } - public function testRaiseResourceInvalidExceptionWhen422() { - $this->setExpectedException('ResourceInvalidException'); + public function testRaiseResourceInvalidExceptionWhen422() { + $this->setExpectedException('ResourceInvalidException'); - $response['header']['http_code'] = 422; + $response['header']['http_code'] = 422; - Aero_Response::handle($response); - } + Aero_Response::handle($response); + } - public function testRaiseServerExceptionWhenBetween500and600() { - $this->setExpectedException('ServerException'); + public function testRaiseServerExceptionWhenBetween500and600() { + $this->setExpectedException('ServerException'); - $response['header']['http_code'] = rand(500, 600); + $response['header']['http_code'] = rand(500, 600); - Aero_Response::handle($response); + Aero_Response::handle($response); - } + } - public function testRaiseConnectionExceptionWhenNotAnyCase() { - $this->setExpectedException('ConnectionException'); + public function testRaiseConnectionExceptionWhenNotAnyCase() { + $this->setExpectedException('ConnectionException'); - $response['header']['http_code'] = 1000; + $response['header']['http_code'] = 1000; - Aero_Response::handle($response); - } + Aero_Response::handle($response); + } } ?> diff --git a/test/unit/validators/ValidatorTest.php b/test/unit/validators/ValidatorTest.php index 258283a..d32b32e 100644 --- a/test/unit/validators/ValidatorTest.php +++ b/test/unit/validators/ValidatorTest.php @@ -25,7 +25,7 @@ public function testValidatePresent() { public function testValidateNotPresent() { $test = new ValidationResourceTest(); $test->title = 'present'; - $test->description = null; + $test->description = null; $test->validate = array( 'title' => array( From 05f9de550fab2a953b9945b39e4a03447f68e32b Mon Sep 17 00:00:00 2001 From: CeRRuTiTo Date: Sun, 14 Oct 2012 10:49:38 +0300 Subject: [PATCH 08/16] Add more attributes to the schema of the errors --- src/AeroIO.php | 15 ++++++++++++--- src/ExceptionHandler.php | 20 +++++++++----------- src/resources/Error.php | 12 ++++++++++++ 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/AeroIO.php b/src/AeroIO.php index 5dfddda..56eaa66 100644 --- a/src/AeroIO.php +++ b/src/AeroIO.php @@ -64,22 +64,31 @@ public static function configure(Array $options = array()) { } if (!self::$engine) self::$engine = new Aero_Curl(); + + self::setOptions(); } /** - * Set exception handler for certain project with the provided data. + * 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 + */ + public 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 ); - - Aero_ExceptionHandler::for_project(self::$project); } } diff --git a/src/ExceptionHandler.php b/src/ExceptionHandler.php index 6447008..62b50c9 100644 --- a/src/ExceptionHandler.php +++ b/src/ExceptionHandler.php @@ -55,17 +55,15 @@ public static function for_project($id) { } } -AeroIO::configure(array( - 'site' => 'http://localhost:3000/api/v1', - 'auth_token' => '52b8963d43b3f9081f58977b5aa3c110', - 'sid' => '4ced755889ec99408be287e3ffb83b6b', - 'project' => 56 -)); - -AeroIO::handleExceptions(); - -print_r(Aero_Error::all(array('project_id' => 56))); -throw new Exception("And another error just like that", 20); +//AeroIO::configure(array( + //'site' => 'http://localhost:3000/api/v1', + //'auth_token' => '52b8963d43b3f9081f58977b5aa3c110', + //'sid' => '4ced755889ec99408be287e3ffb83b6b', + //'project' => 56 +//)); +//AeroIO::handleExceptions(); +//print_r(Aero_Error::all(array('project_id' => 56))); +//throw new Exception("And another error just like that", 20); ?> diff --git a/src/resources/Error.php b/src/resources/Error.php index b6d7648..9971e22 100644 --- a/src/resources/Error.php +++ b/src/resources/Error.php @@ -26,6 +26,18 @@ class Aero_Error extends Aero_Resource { 'id', 'project_id', 'message', + 'backtrace', + 'class_name', + 'file', + 'line', + 'environment', + 'user_agent', + 'url', + 'hostname', + 'session', + 'params', + 'env_vars', + 'additional_data', 'occured', 'resolved', 'created_at', From 3567b14dd43e782cd0f2f34db33d328435565fc2 Mon Sep 17 00:00:00 2001 From: CeRRuTiTo Date: Sun, 14 Oct 2012 12:26:45 +0300 Subject: [PATCH 09/16] Fixing some test cases --- src/AeroIO.php | 4 ++- src/ExceptionHandler.php | 13 --------- src/resources/Error.php | 2 +- src/validators/Validator.php | 4 +-- test/unit/AeroIOTest.php | 32 ++++++++++----------- test/unit/ConnectionTest.php | 16 ----------- test/unit/ExceptionHandlerTest.php | 18 ------------ test/unit/RequestTest.php | 10 +++---- test/unit/ResponseTest.php | 6 ++-- test/unit/engines/HttpTest.php | 44 +++++++++++++++++++++++------ test/unit/resources/ErrorTest.php | 26 +++++++++++++++-- test/unit/resources/ProjectTest.php | 19 +++++++++++++ 12 files changed, 107 insertions(+), 87 deletions(-) delete mode 100644 test/unit/ConnectionTest.php delete mode 100644 test/unit/ExceptionHandlerTest.php diff --git a/src/AeroIO.php b/src/AeroIO.php index 56eaa66..66088b7 100644 --- a/src/AeroIO.php +++ b/src/AeroIO.php @@ -7,6 +7,7 @@ * @license The MIT License */ +require_once 'src/ExceptionHandler.php'; require_once 'src/Connection.php'; require_once 'src/engines/Curl.php'; @@ -82,7 +83,7 @@ public static function handleExceptions() { * * @return void */ - public static function setOptions() { + protected static function setOptions() { Aero_Connection::$site = self::$site; Aero_Connection::$engine = self::$engine; Aero_Connection::$credentials = array( @@ -93,3 +94,4 @@ public static function setOptions() { } ?> + diff --git a/src/ExceptionHandler.php b/src/ExceptionHandler.php index 62b50c9..48f12a3 100644 --- a/src/ExceptionHandler.php +++ b/src/ExceptionHandler.php @@ -7,9 +7,7 @@ * @license The MIT License */ -require_once 'src/resources/Project.php'; require_once 'src/resources/Error.php'; -require_once 'src/AeroIO.php'; /** * Aero_ExceptionHandler class. @@ -55,15 +53,4 @@ public static function for_project($id) { } } -//AeroIO::configure(array( - //'site' => 'http://localhost:3000/api/v1', - //'auth_token' => '52b8963d43b3f9081f58977b5aa3c110', - //'sid' => '4ced755889ec99408be287e3ffb83b6b', - //'project' => 56 -//)); - -//AeroIO::handleExceptions(); - -//print_r(Aero_Error::all(array('project_id' => 56))); -//throw new Exception("And another error just like that", 20); ?> diff --git a/src/resources/Error.php b/src/resources/Error.php index 9971e22..5562589 100644 --- a/src/resources/Error.php +++ b/src/resources/Error.php @@ -50,7 +50,7 @@ class Aero_Error extends Aero_Resource { * @return string */ public function path() { - $url = '/projects/' . $this->project_id . '/errors'; + $url = '/projects/' . $this->project_id . '/errors'; if ($this->id) { $url .= "/$this->id"; diff --git a/src/validators/Validator.php b/src/validators/Validator.php index eb8b5ba..045d263 100644 --- a/src/validators/Validator.php +++ b/src/validators/Validator.php @@ -38,9 +38,7 @@ public static function is_valid($resource) { } } - if (in_array(false, $array)) return false; - - return true; + return !in_array(false, $array); } /** diff --git a/test/unit/AeroIOTest.php b/test/unit/AeroIOTest.php index 3ef1104..f482b94 100644 --- a/test/unit/AeroIOTest.php +++ b/test/unit/AeroIOTest.php @@ -3,27 +3,27 @@ require_once 'src/AeroIO.php'; class AeroIOTest extends PHPUnit_Framework_TestCase { - public function testConfigureCredentials() { - $auth_token = 'AUTH_TOKEN'; - $sid = 'SID'; + public function testConfigureCredentials() { + $auth_token = 'AUTH_TOKEN'; + $sid = 'SID'; - AeroIO::configure(array( - 'auth_token' => $auth_token, - 'sid' => $sid - )); + AeroIO::configure(array( + 'auth_token' => $auth_token, + 'sid' => $sid + )); - $this->assertEquals($auth_token, AeroIO::$auth_token); - } + $this->assertEquals($auth_token, AeroIO::$auth_token); + } - public function testConfigureProject() { - $project = 1; + public function testConfigureProject() { + $project = 1; - AeroIO::configure(array( - 'project' => $project - )); + AeroIO::configure(array( + 'project' => $project + )); - $this->assertEquals($project, AeroIO::$project); - } + $this->assertEquals($project, AeroIO::$project); + } } ?> diff --git a/test/unit/ConnectionTest.php b/test/unit/ConnectionTest.php deleted file mode 100644 index f86cc11..0000000 --- a/test/unit/ConnectionTest.php +++ /dev/null @@ -1,16 +0,0 @@ - diff --git a/test/unit/ExceptionHandlerTest.php b/test/unit/ExceptionHandlerTest.php deleted file mode 100644 index 893c1d5..0000000 --- a/test/unit/ExceptionHandlerTest.php +++ /dev/null @@ -1,18 +0,0 @@ - diff --git a/test/unit/RequestTest.php b/test/unit/RequestTest.php index e38922e..7729e17 100644 --- a/test/unit/RequestTest.php +++ b/test/unit/RequestTest.php @@ -21,11 +21,11 @@ public function testInitialization() { $request = new Aero_Request($method, $resource, $credentials, $site); - $this->assertEquals('URL/PATH.json', $request->url); - $this->assertEquals($method, $request->method); - $this->assertEquals($credentials['auth_token'], $request->auth_token); - $this->assertEquals($credentials['sid'], $request->sid); - $this->assertEquals($resource, $request->resource); + $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()); } } diff --git a/test/unit/ResponseTest.php b/test/unit/ResponseTest.php index efba79c..4139c12 100644 --- a/test/unit/ResponseTest.php +++ b/test/unit/ResponseTest.php @@ -102,7 +102,7 @@ public function testRaiseUnauthorizedExceptionWhen401() { Aero_Response::handle($response); } - + public function testRaiseForbiddenAccessExceptionWhen403() { $this->setExpectedException('ForbiddenAccessException'); @@ -110,7 +110,7 @@ public function testRaiseForbiddenAccessExceptionWhen403() { Aero_Response::handle($response); } - + public function testRaiseResourceNotFoundExceptionWhen404() { $this->setExpectedException('ResourceNotFoundException'); @@ -118,7 +118,7 @@ public function testRaiseResourceNotFoundExceptionWhen404() { Aero_Response::handle($response); } - + public function testRaiseMethodNotAllowedExceptionWhen405() { $this->setExpectedException('MethodNotAllowedException'); diff --git a/test/unit/engines/HttpTest.php b/test/unit/engines/HttpTest.php index 818dea4..ab73666 100644 --- a/test/unit/engines/HttpTest.php +++ b/test/unit/engines/HttpTest.php @@ -23,23 +23,49 @@ 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')); + $engine = $this->getMock('Aero_Http', array( + 'fetch', + 'buildHttpQuery', + 'buildContext' + )); + $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->method = '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); } diff --git a/test/unit/resources/ErrorTest.php b/test/unit/resources/ErrorTest.php index e08250a..778534c 100644 --- a/test/unit/resources/ErrorTest.php +++ b/test/unit/resources/ErrorTest.php @@ -11,9 +11,31 @@ class AeroErrorTest extends PHPUnit_Framework_TestCase { public function testExtends() { - $Error = new Aero_Error(); + $error = new Aero_Error(); + + $this->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 f3a3520..16f7006 100644 --- a/test/unit/resources/ProjectTest.php +++ b/test/unit/resources/ProjectTest.php @@ -15,5 +15,24 @@ public function testExtends() { $this->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); + } } ?> From 99ec1cf4463b0e2e58efeba04e0c0aed9c1b1e3f Mon Sep 17 00:00:00 2001 From: CeRRuTiTo Date: Sun, 14 Oct 2012 12:41:22 +0300 Subject: [PATCH 10/16] Add travis configuration --- .travis.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .travis.yml 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" From 326f48801e1a3d0e2042ab1433309cd2729276b4 Mon Sep 17 00:00:00 2001 From: CeRRuTiTo Date: Sun, 14 Oct 2012 12:48:37 +0300 Subject: [PATCH 11/16] Remove the features, since they are not used anymore --- test/features/authorization.feature | 10 -- test/features/bootstrap/Autoload.php | 17 -- test/features/bootstrap/FeatureContext.php | 46 ------ test/features/bootstrap/PHPUnitHelper.php | 5 - test/features/bootstrap/TestHelper.php | 21 --- test/features/projects_with_curl.feature | 43 ----- test/features/projects_with_http.feature | 43 ----- test/features/steps/authorization_steps.php | 35 ---- test/features/steps/projects_steps.php | 169 -------------------- 9 files changed, 389 deletions(-) delete mode 100644 test/features/authorization.feature delete mode 100644 test/features/bootstrap/Autoload.php delete mode 100644 test/features/bootstrap/FeatureContext.php delete mode 100644 test/features/bootstrap/PHPUnitHelper.php delete mode 100644 test/features/bootstrap/TestHelper.php delete mode 100644 test/features/projects_with_curl.feature delete mode 100644 test/features/projects_with_http.feature delete mode 100644 test/features/steps/authorization_steps.php delete mode 100644 test/features/steps/projects_steps.php 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); - } - ); -?> From 58cfd049cd88c33b47e73f6702e5194ba30f917a Mon Sep 17 00:00:00 2001 From: CeRRuTiTo Date: Sun, 14 Oct 2012 13:00:01 +0300 Subject: [PATCH 12/16] Add phpunit.xml --- phpunit.xml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 phpunit.xml 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 + + + From 4f65d624bc54806d2464e255d4d6cd1676fb3b43 Mon Sep 17 00:00:00 2001 From: CeRRuTiTo Date: Sun, 14 Oct 2012 13:06:38 +0300 Subject: [PATCH 13/16] Add options holder in the engine --- src/engines/Http.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/engines/Http.php b/src/engines/Http.php index ba9e292..4c2cdd1 100644 --- a/src/engines/Http.php +++ b/src/engines/Http.php @@ -17,6 +17,13 @@ */ class Aero_Http implements Engine { + /** + * Options of the request. + * + * @var array + */ + public $request = array(); + /** * Assemble and execute the request. * From 5f84057a8aa79be8d7f53355aec3de2267d1dbe7 Mon Sep 17 00:00:00 2001 From: CeRRuTiTo Date: Sun, 14 Oct 2012 13:13:22 +0300 Subject: [PATCH 14/16] Add build status to the readme --- Readme.md | 2 ++ 1 file changed, 2 insertions(+) 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 ==================================== From c4c6b0db7497ec778c2a2d3197664a441cbea283 Mon Sep 17 00:00:00 2001 From: CeRRuTiTo Date: Sun, 14 Oct 2012 13:17:22 +0300 Subject: [PATCH 15/16] Stub more methods of the Http Engine --- test/unit/engines/HttpTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/unit/engines/HttpTest.php b/test/unit/engines/HttpTest.php index ab73666..78b9df6 100644 --- a/test/unit/engines/HttpTest.php +++ b/test/unit/engines/HttpTest.php @@ -30,7 +30,10 @@ public function testExecuteWithTokenAndSid() { $engine = $this->getMock('Aero_Http', array( 'fetch', 'buildHttpQuery', - 'buildContext' + 'buildContext', + 'setMethod', + 'setHeader', + 'setContent' )); $engine->expects($this->once()) From 388e72b7a863863db42f70d8e93a6b0fcc1088cc Mon Sep 17 00:00:00 2001 From: CeRRuTiTo Date: Sun, 14 Oct 2012 13:57:38 +0300 Subject: [PATCH 16/16] Convert missed tabs to spaces --- test/unit/engines/HttpTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/unit/engines/HttpTest.php b/test/unit/engines/HttpTest.php index 78b9df6..22ef681 100644 --- a/test/unit/engines/HttpTest.php +++ b/test/unit/engines/HttpTest.php @@ -30,10 +30,10 @@ public function testExecuteWithTokenAndSid() { $engine = $this->getMock('Aero_Http', array( 'fetch', 'buildHttpQuery', - 'buildContext', - 'setMethod', - 'setHeader', - 'setContent' + 'buildContext', + 'setMethod', + 'setHeader', + 'setContent' )); $engine->expects($this->once())