From 60a8128a21a2dd893133cdb3c9c11eeec1faab28 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Thu, 8 Apr 2010 04:25:31 +0200 Subject: [PATCH] completed v2.0 : complete rewrite of code, docs and tests. BC compatibility preserved. --- CHANGELOG | 10 + README.markdown | 97 ++++-- lib/apis/phpGitHubApiAbstract.php | 17 + lib/apis/phpGitHubApiCommit.php | 46 +++ lib/apis/phpGitHubApiIssue.php | 62 ++++ lib/apis/phpGitHubApiObject.php | 63 ++++ lib/apis/phpGitHubApiUser.php | 56 ++++ lib/phpGitHubApi.php | 313 ++++++++---------- lib/{ => request}/phpGitHubApiRequest.php | 31 +- .../phpGitHubApiRequestException.php | 4 +- test/unit/apiTest.php | 8 +- test/unit/commitTest.php | 14 +- test/unit/issueTest.php | 27 +- test/unit/objectTest.php | 28 +- test/unit/userTest.php | 24 +- 15 files changed, 569 insertions(+), 231 deletions(-) create mode 100644 lib/apis/phpGitHubApiAbstract.php create mode 100644 lib/apis/phpGitHubApiCommit.php create mode 100644 lib/apis/phpGitHubApiIssue.php create mode 100644 lib/apis/phpGitHubApiObject.php create mode 100644 lib/apis/phpGitHubApiUser.php rename lib/{ => request}/phpGitHubApiRequest.php (87%) rename lib/{ => request}/phpGitHubApiRequestException.php (94%) diff --git a/CHANGELOG b/CHANGELOG index c85dfe3..f9bf744 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,13 @@ +v2.0 - 2010-04-09 + + * refactored the whole API for better extensibility and performance; BC compatibility preserved + * each API part has a dedicated class + * request instance can be injected with phpGitHubApi->setRequest($request) + * API instances can be injected with phpGitHubApi->setApi($name, $api) + * request and API instances are lazy loaded for best performance + * added phpGitHubApiUser->update method + * 100% unit test coverage + v1.2 - 2010-04-08 * addition of the deAuthenticate method diff --git a/README.markdown b/README.markdown index 8446d71..57b8c29 100644 --- a/README.markdown +++ b/README.markdown @@ -1,99 +1,142 @@ # PHP GitHub API + A simple, Object Oriented API wrapper for GitHub written with PHP5. + Uses [GitHub API v2](http://develop.github.com/). -Requires [curl](http://php.net/manual/en/book.curl.php). + +Requires [php curl](http://php.net/manual/en/book.curl.php). ## Instanciate a new API - $api = new phpGitHubApi(); + $github = new phpGitHubApi(); ### Authenticate a user This step is facultative, as most of GitHub services do not require authentication. - $api->authenticate('ornicar', 'my-token'); + $github->authenticate('ornicar', 'my-token'); -All next requests will use the user "ornicar", instead of anonymous access. +Next requests will use the user "ornicar", instead of anonymous access. ### Deauthenticate a user Cancels authentication. - $api->deAuthenticate(); + $github->deAuthenticate(); -All next requests will anonymous access. +Next requests will use anonymous access. ## Users +[Searching users, getting user information and managing authenticated user account information.](http://develop.github.com/p/users.html) + ### Search for users by username - $users = $api->searchUsers('ornicar'); + $users = $github->getUserApi()->search('ornicar'); Returns an array of users as described in [http://develop.github.com/p/users.html#searching_for_users](http://develop.github.com/p/users.html#searching_for_users) ### Get information about a user - $user = $api->showUser('ornicar'); + $user = $github->getUserApi()->show('ornicar'); Returns an array of information about the user as described in [http://develop.github.com/p/users.html#getting_user_information](http://develop.github.com/p/users.html#getting_user_information) ## Issues +[Listing issues, searching, editing and closing your projects issues.](http://develop.github.com/p/issues.html) + ### List issues in a project - $issues = $api->listIssues('ornicar', 'php-github-api', 'open'); + $issues = $github->getIssueApi()->getList('ornicar', 'php-github-api', 'open'); Returns an array of issues as described in [http://develop.github.com/p/issues.html#list_a_projects_issues](http://develop.github.com/p/issues.html#list_a_projects_issues) ### Search issues in a project - $issues = $api->searchIssues('ornicar', 'php-github-api', 'closed', 'bug'); + $issues = $github->getIssueApi()->search('ornicar', 'php-github-api', 'closed', 'bug'); Returns an array of closed issues matching the "bug" term, as described in [http://develop.github.com/p/issues.html#search_issues](http://develop.github.com/p/issues.html#search_issues) ### Get information about an issue - $users = $api->showIssue('ornicar', 'php-github-api', 1); + $users = $github->getIssueApi()->show('ornicar', 'php-github-api', 1); Returns an array of information about the issue as described in [http://develop.github.com/p/issues.html#view_an_issue](http://develop.github.com/p/issues.html#view_an_issue) ## Commits +[Getting information on specific commits, the diffs they introduce, the files they've changed.](http://develop.github.com/p/commits.html) + ### List commits in a branch - $commits = $api->listBranchCommits('ornicar', 'php-github-api', 'master'); + $commits = $github->getCommitApi()->getBranchCommits('ornicar', 'php-github-api', 'master'); Returns an array of commits as described in [http://develop.github.com/p/commits.html#listing_commits_on_a_branch](http://develop.github.com/p/commits.html#listing_commits_on_a_branch) ### List commits for a file - $commits = $api->listFileCommits('ornicar', 'php-github-api', 'master', 'README'); + $commits = $github->getCommitApi()->getFileCommits('ornicar', 'php-github-api', 'master', 'README'); Returns an array of commits as described in [http://develop.github.com/p/commits.html#listing_commits_for_a_file](http://develop.github.com/p/commits.html#listing_commits_for_a_file) +## Objects + +[Getting full versions of specific files and trees in your Git repositories.](http://develop.github.com/p/objects.html) + +### List contents of a tree + + $tree = $github->getObjectApi()->showTree('ornicar', 'php-github-api', '691c2ec7fd0b948042047b515886fec40fe76e2b'); + +Returns an array containing a tree of the repository + +### Show the data of a blob + + $blob = $github->getObjectApi()->showBlob('ornicar', 'php-github-api', '691c2ec7fd0b948042047b515886fec40fe76e2b', 'CHANGELOG'); + +Returns array of blob of specified path + +### List all blobs of repository + + $blobs = $github->getObjectApi()->listBlobs('ornicar', 'php-github-api', '691c2ec7fd0b948042047b515886fec40fe76e2b'); + +Returns an array containing all blobs + ## Request any route +The method you need does not exist yet? You can access any GitHub route by using the "get" and "post" methods. +For example, - $repo = $api->get('repos/show/ornicar/php-github-api'); + $repo = $github->get('repos/show/ornicar/php-github-api'); Returns an array describing the php-github-api repository. See all GitHub API routes: [http://develop.github.com/](http://develop.github.com/) -## Objects +## Configure the request -### List contents of a tree - $tree = $api->listObjectTree('ornicar', 'php-github-api', '691c2ec7fd0b948042047b515886fec40fe76e2b'); -Returns an array containing a tree of the repository +Wanna change, let's say, the request User Agent? -### Show the data of a blob - $blob = $api->showObjectBlob('ornicar', 'php-github-api', '691c2ec7fd0b948042047b515886fec40fe76e2b', 'CHANGELOG'); -Returns array of blob of specified path + $github->getRequest()->setOption('user_agent', 'My new User Agent'); -### List all blobs of repository - $blobs = $api->listObjectBlobs('ornicar', 'php-github-api', '691c2ec7fd0b948042047b515886fec40fe76e2b'); -Returns an array containing all blobs +See all request available options in request/phpGitHubApiRequest.php + +## Inject a new request instance + +If you want to use your own request implementation, inject it to the GitHubApi: + + $github->setRequest($myRequest); + +$myRequest must extend phpGitHubApiRequest. + +## Inject a new API part instance + +If you want to use your own implementation of the user API, inject it to the GitHubApi: + + $github->setApi('user', $myUserApi); + +$myUserApi should extend phpGitHubApiUser. ## Run test suite @@ -103,4 +146,8 @@ All code is fully unit tested. To run tests on your server, from a CLI, run ## Credits -This library borrows ideas, code and tests from [phptwitterbot](http://code.google.com/p/phptwitterbot/). \ No newline at end of file +This library borrows ideas, code and tests from [phptwitterbot](http://code.google.com/p/phptwitterbot/). + +Thanks to [noloh](http://github.com/noloh) for his contribution on the Object API. + +Thanks to GitHub for the high quality API and documentation. \ No newline at end of file diff --git a/lib/apis/phpGitHubApiAbstract.php b/lib/apis/phpGitHubApiAbstract.php new file mode 100644 index 0000000..f5f5c23 --- /dev/null +++ b/lib/apis/phpGitHubApiAbstract.php @@ -0,0 +1,17 @@ + + * @license MIT License + */ +abstract class phpGitHubApiAbstract +{ + protected $api; + + public function __construct(phpGitHubApi $api) + { + $this->api = $api; + } +} \ No newline at end of file diff --git a/lib/apis/phpGitHubApiCommit.php b/lib/apis/phpGitHubApiCommit.php new file mode 100644 index 0000000..e9f3287 --- /dev/null +++ b/lib/apis/phpGitHubApiCommit.php @@ -0,0 +1,46 @@ + + * @license MIT License + */ +class phpGitHubApiCommit extends phpGitHubApiAbstract +{ + + /** + * List commits by username, repo and branch + * http://develop.github.com/p/commits.html#listing_commits_on_a_branch + * + * @param string $username the username + * @param string $repo the repo + * @param string $branch the branch + * @return array list of users found + */ + public function getBranchCommits($username, $repo, $branch) + { + $response = $this->api->get('commits/list/'.$username.'/'.$repo.'/'.$branch); + + return $response['commits']; + } + + /** + * List commits by username, repo, branch and path + * http://develop.github.com/p/commits.html#listing_commits_for_a_file + * + * @param string $username the username + * @param string $repo the repo + * @param string $branch the branch + * @param string $path the path + * @return array list of users found + */ + public function getFileCommits($username, $repo, $branch, $path) + { + $response = $this->api->get('commits/list/'.$username.'/'.$repo.'/'.$branch.'/'.$path); + + return $response['commits']; + } +} \ No newline at end of file diff --git a/lib/apis/phpGitHubApiIssue.php b/lib/apis/phpGitHubApiIssue.php new file mode 100644 index 0000000..782c823 --- /dev/null +++ b/lib/apis/phpGitHubApiIssue.php @@ -0,0 +1,62 @@ + + * @license MIT License + */ +class phpGitHubApiIssue extends phpGitHubApiAbstract +{ + + /** + * List issues by username, repo and state + * http://develop.github.com/p/issues.html#list_a_projects_issues + * + * @param string $username the username + * @param string $repo the repo + * @param string $state the issue state, can be open or closed + * @return array list of users found + */ + public function getList($username, $repo, $state = 'open') + { + $response = $this->api->get('issues/list/'.$username.'/'.$repo.'/'.$state); + + return $response['issues']; + } + + /** + * Search issues by username, repo, state and search term + * http://develop.github.com/p/issues.html#list_a_projects_issues + * + * @param string $username the username + * @param string $repo the repo + * @param string $state the issue state, can be open or closed + * @param string $searchTerm the search term to filter issues by + * @return array list of users found + */ + public function search($username, $repo, $state, $searchTerm) + { + $response = $this->api->get('issues/search/'.$username.'/'.$repo.'/'.$state.'/'.$searchTerm); + + return $response['issues']; + } + + /** + * Get extended information about an issue by its username, repo and number + * http://develop.github.com/p/issues.html#view_an_issue + * + * @param string $username the username + * @param string $repo the repo + * @param string $number the issue number + * @return array informations about the issue + */ + public function show($username, $repo, $number) + { + $response = $this->api->get('issues/show/'.$username.'/'.$repo.'/'.$number); + + return $response['issue']; + } +} \ No newline at end of file diff --git a/lib/apis/phpGitHubApiObject.php b/lib/apis/phpGitHubApiObject.php new file mode 100644 index 0000000..812f873 --- /dev/null +++ b/lib/apis/phpGitHubApiObject.php @@ -0,0 +1,63 @@ + + * @license MIT License + */ +class phpGitHubApiObject extends phpGitHubApiAbstract +{ + + /** + * Get a listing of the root tree of a project by the username, repo, and tree SHA + * http://develop.github.com/p/object.html#trees + * + * @param string $username the username + * @param string $repo the repo + * @param string $treeSHA the tree sha + * @return array root tree of the project + */ + public function showTree($username, $repo, $treeSHA) + { + $response = $this->api->get('tree/show/'.$username.'/'.$repo.'/'.$treeSHA); + + return $response['tree']; + } + + /** + * Get the data about a blob by tree SHA and file path. + * http://develop.github.com/p/object.html#blobs + * + * @param string $username the username + * @param string $repo the repo + * @param string $treeSHA the tree sha + * @param string $path the path + * @return array data blob of tree and path + */ + public function showBlob($username, $repo, $treeSHA, $path) + { + $response = $this->api->get('blob/show/'.$username.'/'.$repo.'/'.$treeSHA .'/'.$path); + + return $response['blob']; + } + + /** + * Lists the data blobs of a tree by tree SHA + * http://develop.github.com/p/object.html#blobs + * + * @param string $username the username + * @param string $repo the repo + * @param string $treeSHA the tree sha + * @param string $path the path + * @return array data blobs of tree + */ + public function listBlobs($username, $repo, $treeSHA) + { + $response = $this->api->get('blob/all/'.$username.'/'.$repo.'/'.$treeSHA); + + return $response['blobs']; + } +} \ No newline at end of file diff --git a/lib/apis/phpGitHubApiUser.php b/lib/apis/phpGitHubApiUser.php new file mode 100644 index 0000000..09bbcd5 --- /dev/null +++ b/lib/apis/phpGitHubApiUser.php @@ -0,0 +1,56 @@ + + * @license MIT License + */ +class phpGitHubApiUser extends phpGitHubApiAbstract +{ + /** + * Search users by username + * http://develop.github.com/p/users.html#searching_for_users + * + * @param string $username the username to search + * @return array list of users found + */ + public function search($username) + { + $response = $this->api->get('user/search/'.$username); + + return $response['users']; + } + + /** + * Get extended information about a user by its username + * http://develop.github.com/p/users.html#getting_user_information + * + * @param string $username the username to search + * @return array informations about the user + */ + public function show($username) + { + $response = $this->api->get('user/show/'.$username); + + return $response['user']; + } + + /** + * Update user informations. Requires authentication. + * http://develop.github.com/p/users.html#authenticated_user_management + * + * @param string $username the username to search + * @param array $data key=>value user attributes to update. + * key can be name, email, blog, company or location + * @return array informations about the user + */ + public function update($username, array $data) + { + $response = $this->api->post('user/show/'.$username, array('values' => $data)); + + return $response['user']; + } +} \ No newline at end of file diff --git a/lib/phpGitHubApi.php b/lib/phpGitHubApi.php index 3834ef1..b0b164b 100644 --- a/lib/phpGitHubApi.php +++ b/lib/phpGitHubApi.php @@ -1,34 +1,32 @@ - * @license MIT License + * @license MIT License */ class phpGitHubApi { - protected - $requestClass = 'phpGitHubApiRequest', - $requestOptions = array(), - $debug; + protected $request = null; + protected $apis = array(); + protected $debug; /** - * Instanciates a new API + * Instanciate a new GitHub API * * @param bool $debug print debug messages */ public function __construct($debug = false) { $this->debug = $debug; - $this->requestOptions['debug'] = $debug; } /** - * Authenticates a user for all next requests + * Authenticate a user for all next requests * * @param string $login GitHub username * @param string $token GitHub private token @@ -36,280 +34,255 @@ public function __construct($debug = false) */ public function authenticate($login, $token) { - $this->requestOptions['login'] = $login; - $this->requestOptions['token'] = $token; + $this->getRequest() + ->setOption('login', $login) + ->setOption('token', $token); return $this; } /** - * Deauthenticates a user for all next requests + * Deauthenticate a user for all next requests * * @return phpGitHubApi fluent interface */ public function deAuthenticate() { - unset($this->requestOptions['login'], $this->requestOptions['token']); - - return $this; + return $this->authenticate(null, null); } - + /** - * Search users by username - * http://develop.github.com/p/users.html#searching_for_users + * Call any route, GET method + * Ex: $api->get('repos/show/my-username/my-repo') * - * @param string $username the username to search - * @return array list of users found + * @param string $route the GitHub route + * @param array $parameters GET parameters + * @return array data returned */ - public function searchUsers($username) + public function get($route, array $parameters = array()) { - $response = $this->get('user/search/'.$username); - - return $response['users']; + return $this->getRequest()->get($route, $parameters); } /** - * Get extended information about a user by its username - * http://develop.github.com/p/users.html#getting_user_information + * Call any route, POST method + * Ex: $api->post('repos/show/my-username', array('email' => 'my-new-email@provider.org')) * - * @param string $username the username to search - * @return array informations about the user + * @param string $route the GitHub route + * @param array $parameters POST parameters + * @return array data returned */ - public function showUser($username) + public function post($route, array $parameters = array()) { - $response = $this->get('user/show/'.$username); - - return $response['user']; + return $this->getRequest()->post($route, $parameters); } /** - * Update user informations. Requires authentication. - * http://develop.github.com/p/users.html#authenticated_user_management + * Get the request * - * @param string $username the username to search - * @param array $data key=>value user attributes to update. - * key can be name, email, blog, company or location - * @return array informations about the user + * @return phpGitHubApiRequest a request instance */ - public function updateUser($username, array $data) + protected function getRequest() { - $response = $this->post('user/show/'.$username, array('values' => $data)); - - return $response['user']; + if(!isset($this->request)) + { + $this->request = new phpGitHubApiRequest(); + } + + return $this->request; } /** - * List issues by username, repo and state - * http://develop.github.com/p/issues.html#list_a_projects_issues + * Inject another request * - * @param string $username the username - * @param string $repo the repo - * @param string $state the issue state, can be open or closed - * @return array list of users found + * @param phpGitHubApiRequest a request instance + * @return phpGitHubApi fluent interface */ - public function listIssues($username, $repo, $state = 'open') + protected function setRequest(phpGitHubApiRequest $request) { - $response = $this->get('issues/list/'.$username.'/'.$repo.'/'.$state); + $this->request = $request; - return $response['issues']; + return $this; } /** - * Search issues by username, repo, state and search term - * http://develop.github.com/p/issues.html#list_a_projects_issues + * Get the user API * - * @param string $username the username - * @param string $repo the repo - * @param string $state the issue state, can be open or closed - * @param string $searchTerm the search term to filter issues by - * @return array list of users found + * @return phpGitHubApiUser the user API */ - public function searchIssues($username, $repo, $state, $searchTerm) + public function getUserApi() { - $response = $this->get('issues/search/'.$username.'/'.$repo.'/'.$state.'/'.$searchTerm); + if(!isset($this->apis['user'])) + { + require_once(dirname(__FILE__).'/apis/phpGitHubApiUser.php'); + $this->apis['user'] = new phpGitHubApiUser($this); + } - return $response['issues']; + return $this->apis['user']; } /** - * Get extended information about an issue by its username, repo and number - * http://develop.github.com/p/issues.html#view_an_issue + * Get the issue API * - * @param string $username the username - * @param string $repo the repo - * @param string $number the issue number - * @return array informations about the issue + * @return phpGitHubApiIssue the issue API */ - public function showIssue($username, $repo, $number) + public function getIssueApi() { - $response = $this->get('issues/show/'.$username.'/'.$repo.'/'.$number); + if(!isset($this->apis['issue'])) + { + require_once(dirname(__FILE__).'/apis/phpGitHubApiIssue.php'); + $this->apis['issue'] = new phpGitHubApiIssue($this); + } - return $response['issue']; + return $this->apis['issue']; } /** - * List commits by username, repo and branch - * http://develop.github.com/p/commits.html#listing_commits_on_a_branch + * Get the commit API * - * @param string $username the username - * @param string $repo the repo - * @param string $branch the branch - * @return array list of users found + * @return phpGitHubApiCommit the commit API */ - public function listBranchCommits($username, $repo, $branch) + public function getCommitApi() { - $response = $this->get('commits/list/'.$username.'/'.$repo.'/'.$branch); + if(!isset($this->apis['commit'])) + { + require_once(dirname(__FILE__).'/apis/phpGitHubApiCommit.php'); + $this->apis['commit'] = new phpGitHubApiCommit($this); + } - return $response['commits']; + return $this->apis['commit']; } /** - * List commits by username, repo, branch and path - * http://develop.github.com/p/commits.html#listing_commits_for_a_file + * Get the object API * - * @param string $username the username - * @param string $repo the repo - * @param string $branch the branch - * @param string $path the path - * @return array list of users found + * @return phpGitHubApiObject the object API */ - public function listFileCommits($username, $repo, $branch, $path) + public function getObjectApi() { - $response = $this->get('commits/list/'.$username.'/'.$repo.'/'.$branch.'/'.$path); + if(!isset($this->apis['object'])) + { + require_once(dirname(__FILE__).'/apis/phpGitHubApiObject.php'); + $this->apis['object'] = new phpGitHubApiObject($this); + } - return $response['commits']; + return $this->apis['object']; } /** - * Get a listing of the root tree of a project by the username, repo, and tree SHA - * http://develop.github.com/p/object.html#trees + * Inject another api * - * @param string $username the username - * @param string $repo the repo - * @param string $treeSHA the tree sha - * @return array root tree of the project + * @param string $name the API name + * @param phpGitHubApiAbstract $api the API instance + * @return phpGitHubApi fluent interface */ - public function listObjectTree($username, $repo, $treeSHA) + public function setApi($name, phpGitHubApiAbstract $instance) { - $response = $this->get('tree/show/'.$username.'/'.$repo.'/'.$treeSHA); + $this->apis[$name] = $instance; - return $response['tree']; + return $this; } - + /** - * Get the data about a blob by tree SHA and file path. - * http://develop.github.com/p/object.html#blobs + * Get any API * - * @param string $username the username - * @param string $repo the repo - * @param string $treeSHA the tree sha - * @param string $path the path - * @return array data blob of tree and path + * @param string $name the API name + * @return phpGitHubApiAbstract the API instance */ - public function showObjectBlob($username, $repo, $treeSHA, $path) + public function getApi($name) { - $response = $this->get('blob/show/'.$username.'/'.$repo.'/'.$treeSHA .'/'.$path); - - return $response['blob']; + return $this->apis[$name]; } - + + // DEPRECATED METHODS (BC COMPATIBILITY) + /** - * Lists the data blobs of a tree by tree SHA - * http://develop.github.com/p/object.html#blobs - * - * @param string $username the username - * @param string $repo the repo - * @param string $treeSHA the tree sha - * @param string $path the path - * @return array data blobs of tree + * @deprecated use ->getUserApi()->search() + * @see phpGitHubApiUser::search() */ - public function listObjectBlobs($username, $repo, $treeSHA) + public function searchUsers($username) { - $response = $this->get('blob/all/'.$username.'/'.$repo.'/'.$treeSHA); - - return $response['blobs']; + return $this->getUserApi()->search($username); } - + /** - * Call any route, GET method - * Ex: $api->get('repos/show/my-username/my-repo') - * - * @param string $route the GitHub route - * @param array $parameters GET parameters - * @return array data returned + * @deprecated use ->getUserApi()->show() + * @see phpGitHubApiUser::show() */ - public function get($route, array $parameters = array()) + public function showUser($username) { - return $this->createRequest()->get($route, $parameters); + return $this->getUserApi()->show($username); } /** - * Call any route, POST method - * Ex: $api->post('repos/show/my-username', array('email' => 'my-new-email')) - * - * @param string $route the GitHub route - * @param array $parameters POST parameters - * @return array data returned + * @deprecated use ->getIssueApi()->getList() + * @see phpGitHubApiIssue::getList() */ - public function post($route, array $parameters = array()) + public function listIssues($username, $repo, $state = 'open') { - return $this->createRequest()->post($route, $parameters); + return $this->getIssueApi()->getList($username, $repo, $state); } /** - * Creates a new request - * - * @return phpGitHubApiRequest a request instance + * @deprecated use ->getIssueApi()->search() + * @see phpGitHubApiIssue::search() */ - protected function createRequest() + public function searchIssues($username, $repo, $state, $searchTerm) { - return new $this->requestClass($this->getRequestOptions()); + return $this->getIssueApi()->search($username, $repo, $state, $searchTerm); } /** - * Get the request class - * - * @return string the request class + * @deprecated use ->getIssueApi()->show() + * @see phpGitHubApiIssue::show() */ - public function getRequestClass() + public function showIssue($username, $repo, $number) { - return $this->requestClass; + return $this->getIssueApi()->show($username, $repo, $number); } /** - * Set the request class - * - * @param string $requestClass the new request class - * @return phpGitHubApi fluent interface + * @deprecated use ->getCommitApi()->getBranchCommits() + * @see phpGitHubApiCommit::getBranchCommits() */ - public function setRequestClass($requestClass) + public function listBranchCommits($username, $repo, $branch) { - $this->requestClass = $requestClass; - - return $this; + return $this->getCommitApi()->getBranchCommits($username, $repo, $branch); } /** - * Get the request options - * - * @return array the request options + * @deprecated use ->getCommitApi()->getFileCommits() + * @see phpGitHubApiCommit::getFileCommits() */ - public function getRequestOptions() + public function listFileCommits($username, $repo, $branch, $path) { - return $this->requestOptions; + return $this->getCommitApi()->getFileCommits($username, $repo, $branch, $path); } /** - * Set the request options - * - * @param array $requestOptions the new request options - * @return phpGitHubApi fluent interface + * @deprecated use ->getObjectApi()->showTree() + * @see phpGitHubApiObject::showTree() */ - public function setRequestOptions($requestOptions) + public function listObjectTree($username, $repo, $treeSHA) { - $this->requestOptions = $requestOptions; + return $this->getObjectApi()->showTree($username, $repo, $treeSHA); + } - return $this; + /** + * @deprecated use ->getObjectApi()->showBlob() + * @see phpGitHubApiObject::showBlob() + */ + public function showObjectBlob($username, $repo, $treeSHA, $path) + { + return $this->getObjectApi()->showBlob($username, $repo, $treeSHA, $path); } + /** + * @deprecated use ->getObjectApi()->listBlobs() + * @see phpGitHubApiObject::listBlobs() + */ + public function listObjectBlobs($username, $repo, $treeSHA) + { + return $this->getObjectApi()->listBlobs($username, $repo, $treeSHA); + } } \ No newline at end of file diff --git a/lib/phpGitHubApiRequest.php b/lib/request/phpGitHubApiRequest.php similarity index 87% rename from lib/phpGitHubApiRequest.php rename to lib/request/phpGitHubApiRequest.php index 3431e22..be0b91a 100644 --- a/lib/phpGitHubApiRequest.php +++ b/lib/request/phpGitHubApiRequest.php @@ -5,8 +5,8 @@ /** * Performs requests on GitHub API. API documentation should be self-explanatory. * - * @author Thibault Duplessis - * @license MIT License + * @author Thibault Duplessis + * @license MIT License */ class phpGitHubApiRequest { @@ -173,6 +173,33 @@ public function doSend($apiPath, array $parameters = array(), $httpMethod = 'GET return $response; } + /** + * Changes an option value. + * + * @param string $name The option name + * @param mixed $value The value + * + * @return dmConfigurable The current object instance + */ + public function setOption($name, $value) + { + $this->options[$name] = $value; + + return $this; + } + + /** + * Gets an option value. + * + * @param string $name The option name + * + * @return mixed The option value + */ + public function getOption($name, $default = null) + { + return isset($this->options[$name]) ? $this->options[$name] : $default; + } + protected function debug($message) { if($this->options['debug']) diff --git a/lib/phpGitHubApiRequestException.php b/lib/request/phpGitHubApiRequestException.php similarity index 94% rename from lib/phpGitHubApiRequestException.php rename to lib/request/phpGitHubApiRequestException.php index 7284015..04a6ccc 100644 --- a/lib/phpGitHubApiRequestException.php +++ b/lib/request/phpGitHubApiRequestException.php @@ -3,8 +3,8 @@ /** * Request communication error * - * @author Thibault Duplessis - * @license MIT License + * @author Thibault Duplessis + * @license MIT License */ class phpGitHubApiRequestException extends Exception { diff --git a/test/unit/apiTest.php b/test/unit/apiTest.php index be7d82a..30b2a02 100644 --- a/test/unit/apiTest.php +++ b/test/unit/apiTest.php @@ -18,17 +18,17 @@ $t->comment('Test api'); -$api = new phpGitHubApi(); +$github = new phpGitHubApi(); -$t->isa_ok($api, 'phpGitHubApi', 'Got a phpGitHubApi instance'); +$t->isa_ok($github, 'phpGitHubApi', 'Got a phpGitHubApi instance'); -$repo = $api->get('repos/show/ornicar/php-github-api'); +$repo = $github->get('repos/show/ornicar/php-github-api'); $t->is($repo['repository']['name'], 'php-github-api', 'Found information about php-github-api repo'); try { - $api->get('non-existing-url/for-sure'); + $github->get('non-existing-url/for-sure'); $t->fail('Call to non-existing-url/for-sure throws a phpGitHubApiRequestException'); } catch(phpGitHubApiRequestException $e) diff --git a/test/unit/commitTest.php b/test/unit/commitTest.php index 10adb3f..8660801 100644 --- a/test/unit/commitTest.php +++ b/test/unit/commitTest.php @@ -3,19 +3,27 @@ require_once dirname(__FILE__).'/../../vendor/lime.php'; require_once dirname(__FILE__).'/../../lib/phpGitHubApi.php'; -$t = new lime_test(2); +$t = new lime_test(4); + +$username = 'ornicar'; +$repo = 'php-github-api'; +$branch = 'master'; $api = new phpGitHubApi(true); $t->comment('Test ->listBranchCommits'); -$commits = $api->listBranchCommits('ornicar', 'php-github-api', 'master'); +$commits = $api->getCommitApi()->getBranchCommits($username, $repo, $branch); + +$t->is_deeply($api->listBranchCommits($username, $repo, $branch), $commits, 'Both new and BC syntax work'); $firstCommit = array_pop($commits); $t->is($firstCommit['message'], 'first commit', 'Found master commits'); -$commits = $api->listFileCommits('ornicar', 'php-github-api', 'master', 'README'); +$commits = $api->getCommitApi()->getFileCommits($username, $repo, $branch, 'README'); + +$t->is_deeply($api->listFileCommits($username, $repo, $branch, 'README'), $commits, 'Both new and BC syntax work'); $firstCommit = array_pop($commits); diff --git a/test/unit/issueTest.php b/test/unit/issueTest.php index e6d5208..d2b32bc 100644 --- a/test/unit/issueTest.php +++ b/test/unit/issueTest.php @@ -3,22 +3,33 @@ require_once dirname(__FILE__).'/../../vendor/lime.php'; require_once dirname(__FILE__).'/../../lib/phpGitHubApi.php'; -$t = new lime_test(3); +$t = new lime_test(6); -$api = new phpGitHubApi(true); +$username = 'ornicar'; +$repo = 'php-github-api'; -$t->comment('Test ->listIssues'); +$github = new phpGitHubApi(true); -$issues = $api->listIssues('ornicar', 'php-github-api', 'closed'); +$t->comment('List issues'); + +$issues = $github->getIssueApi()->getList($username, $repo, 'closed'); $t->is($issues[0]['state'], 'closed', 'Found closed issues'); -$t->comment('Test ->searchIssues'); +$t->is_deeply($github->listIssues($username, $repo, 'closed'), $issues, 'Both new and BC syntax work'); + +$t->comment('Search issues'); -$issues = $api->searchIssues('ornicar', 'php-github-api', 'closed', 'documentation'); +$issues = $github->getIssueApi()->search($username, $repo, 'closed', 'documentation'); $t->is($issues[0]['state'], 'closed', 'Found closed issues matching "documentation"'); -$issue = $api->showIssue('ornicar', 'php-github-api', 1); +$t->is_deeply($github->searchIssues($username, $repo, 'closed', 'documentation'), $issues, 'Both new and BC syntax work'); + +$t->comment('Show issue'); + +$issue = $github->getIssueApi()->show($username, $repo, 1); + +$t->is($issue['title'], 'Provide documentation', 'Found issue #1'); -$t->is($issue['title'], 'Provide documentation', 'Found issue #1'); \ No newline at end of file +$t->is_deeply($github->showIssue($username, $repo, 1), $issue, 'Both new and BC syntax work'); \ No newline at end of file diff --git a/test/unit/objectTest.php b/test/unit/objectTest.php index 2b97a09..9af4c2f 100644 --- a/test/unit/objectTest.php +++ b/test/unit/objectTest.php @@ -2,18 +2,32 @@ require_once dirname(__FILE__).'/../../vendor/lime.php'; require_once dirname(__FILE__).'/../../lib/phpGitHubApi.php'; -$t = new lime_test(3); +$t = new lime_test(6); + +$username = 'ornicar'; +$repo = 'php-github-api'; +$treeSha = '691c2ec7fd0b948042047b515886fec40fe76e2b'; $api = new phpGitHubApi(true); -$t->comment('Test ->listObjectTree'); +$t->comment('Show tree'); -$tree = $api->listObjectTree('ornicar', 'php-github-api', '691c2ec7fd0b948042047b515886fec40fe76e2b'); +$tree = $api->getObjectApi()->showTree($username, $repo, $treeSha); $firstFile = array_pop($tree); $t->is($firstFile['sha'], '5ac35496a1cbb2a914ff4325e7d6e8cae61f90b9', 'Tree returned with SHA listings'); -$blob = $api->showObjectBlob('ornicar', 'php-github-api', '691c2ec7fd0b948042047b515886fec40fe76e2b', 'CHANGELOG'); -print_r($blob); +$t->is_deeply($github->listObjectTree($username, $repo, $treeSha), $tree, 'Both new and BC syntax work'); + +$t->comment('Show blob'); + +$blob = $api->getObjectApi()->showBlob($username, $repo, $treeSha, 'CHANGELOG'); $t->is($blob['name'], 'CHANGELOG', 'Returned CHANGELOG blob'); -$blobs = $api->listObjectBlobs('ornicar', 'php-github-api', '691c2ec7fd0b948042047b515886fec40fe76e2b'); -$t->ok(count($blobs) > 0, 'Returned array of blobs'); \ No newline at end of file + +$t->is_deeply($github->showObjectBlob($username, $repo, $treeSha, 'CHANGELOG'), $blob, 'Both new and BC syntax work'); + +$t->comment('List blobs'); + +$blobs = $api->getObjectApi()->listBlobs($username, $repo, $treeSha); +$t->ok(count($blobs) > 0, 'Returned array of blobs'); + +$t->is_deeply($github->listObjectBlobs($username, $repo, $treeSha), $blobs, 'Both new and BC syntax work'); \ No newline at end of file diff --git a/test/unit/userTest.php b/test/unit/userTest.php index e82f2ba..b6c94b0 100644 --- a/test/unit/userTest.php +++ b/test/unit/userTest.php @@ -2,32 +2,36 @@ require_once dirname(__FILE__).'/../../vendor/lime.php'; require_once dirname(__FILE__).'/../../lib/phpGitHubApi.php'; -$t = new lime_test(); +$t = new lime_test(8); $username = 'ornicartest'; $token = 'fd8144e29b4a85e9487d1cacbcd4e26c'; -$api = new phpGitHubApi(true); +$github = new phpGitHubApi(true); $t->comment('Search users'); -$users = $api->searchUsers($username); +$users = $github->getUserApi()->search($username); $t->is(count($users), 1, 'Found one user'); $t->is(array_keys($users), array($username), 'Found '.$username.' user'); +$t->is_deeply($github->searchUsers($username), $users, 'Both new and BC syntax work'); + $t->comment('Show a user'); -$user = $api->showUser($username); +$user = $github->getUserApi()->show($username); $t->is($user['login'], $username, 'Found infos about '.$username.' user'); +$t->is_deeply($github->showUser($username), $user, 'Both new and BC syntax work'); + $t->comment('Show a non-existing user'); try { - $user = $api->showUser('this-user-probably-doesnt-exist'); + $user = $github->getUserApi()->show('this-user-probably-doesnt-exist'); $t->fail('Found no infos about this-user-probably-doesnt-exist user'); } catch(phpGitHubApiRequestException $e) @@ -37,20 +41,20 @@ $t->comment('Authenticate '.$username); -$api->authenticate($username, $token); +$github->authenticate($username, $token); $t->comment('Update user location to Argentina'); -$api->updateUser($username, array('location' => 'Argentina')); +$github->getUserApi()->update($username, array('location' => 'Argentina')); -$user = $api->showUser($username); +$user = $github->getUserApi()->show($username); $t->is($user['location'], 'Argentina', 'User new location is Argentina'); $t->comment('Update user location to France'); -$api->updateUser($username, array('location' => 'France')); +$github->getUserApi()->update($username, array('location' => 'France')); -$user = $api->showUser($username); +$user = $github->getUserApi()->show($username); $t->is($user['location'], 'France', 'User new location is France'); \ No newline at end of file