diff --git a/lib/Github/Client.php b/lib/Github/Client.php index 5c102ba11f5..6909264ed66 100644 --- a/lib/Github/Client.php +++ b/lib/Github/Client.php @@ -4,12 +4,41 @@ use Github\Api\ApiInterface; use Github\Exception\InvalidArgumentException; +use Github\Exception\BadMethodCallException; use Github\HttpClient\HttpClient; use Github\HttpClient\HttpClientInterface; /** * Simple yet very cool PHP GitHub client * + * @method Api\CurrentUser currentUser() + * @method Api\CurrentUser me() + * @method Api\Enterprise ent() + * @method Api\Enterprise enterprise() + * @method Api\GitData git() + * @method Api\GitData gitData() + * @method Api\Gists gist() + * @method Api\Gists gists() + * @method Api\Issue issue() + * @method Api\Issue issues() + * @method Api\Markdown markdown() + * @method Api\Organization organization() + * @method Api\Organization organizations() + * @method Api\PullRequest pr() + * @method Api\PullRequest pullRequest() + * @method Api\PullRequest pullRequests() + * @method Api\Repo repo() + * @method Api\Repo repos() + * @method Api\Repo repository() + * @method Api\Repo repositories() + * @method Api\Organization team() + * @method Api\Organization teams() + * @method Api\User user() + * @method Api\User users() + * @method Api\Authorizations authorization() + * @method Api\Authorizations authorizations() + * @method Api\Meta meta() + * * @author Joseph Bielawski * * Website: http://github.com/KnpLabs/php-github-api @@ -84,6 +113,7 @@ public function api($name) switch ($name) { case 'me': case 'current_user': + case 'currentUser': $api = new Api\CurrentUser($this); break; @@ -94,6 +124,7 @@ public function api($name) case 'git': case 'git_data': + case 'gitData': $api = new Api\GitData($this); break; @@ -117,7 +148,9 @@ public function api($name) break; case 'pr': + case 'pullRequest': case 'pull_request': + case 'pullRequests': case 'pull_requests': $api = new Api\PullRequest($this); break; @@ -274,4 +307,19 @@ public function getSupportedApiVersions() { return array('v3', 'beta'); } + + /** + * @param string $name + * + * @return ApiInterface + * + * @throws InvalidArgumentException + */ + public function __call($name, $args) { + try { + return $this->api($name); + } catch (InvalidArgumentException $e) { + throw new BadMethodCallException(sprintf('Undefined method called: "%s"', $name)); + } + } } diff --git a/lib/Github/Exception/BadMethodCallException.php b/lib/Github/Exception/BadMethodCallException.php new file mode 100644 index 00000000000..5c43d20a485 --- /dev/null +++ b/lib/Github/Exception/BadMethodCallException.php @@ -0,0 +1,13 @@ + + */ +class BadMethodCallException extends \BadMethodCallException implements ExceptionInterface +{ + +} diff --git a/test/Github/Tests/ClientTest.php b/test/Github/Tests/ClientTest.php index 8ebe27f785f..04b3a710397 100644 --- a/test/Github/Tests/ClientTest.php +++ b/test/Github/Tests/ClientTest.php @@ -4,6 +4,7 @@ use Github\Client; use Github\Exception\InvalidArgumentException; +use Github\Exception\BadMethodCallException; class ClientTest extends \PHPUnit_Framework_TestCase { @@ -124,6 +125,17 @@ public function shouldGetApiInstance($apiName, $class) $this->assertInstanceOf($class, $client->api($apiName)); } + /** + * @test + * @dataProvider getApiClassesProvider + */ + public function shouldGetMagicApiInstance($apiName, $class) + { + $client = new Client(); + + $this->assertInstanceOf($class, $client->$apiName()); + } + /** * @test * @expectedException InvalidArgumentException @@ -134,6 +146,16 @@ public function shouldNotGetApiInstance() $client->api('do_not_exist'); } + /** + * @test + * @expectedException BadMethodCallException + */ + public function shouldNotGetMagicApiInstance() + { + $client = new Client(); + $client->doNotExist(); + } + public function getApiClassesProvider() { return array( @@ -142,9 +164,11 @@ public function getApiClassesProvider() array('me', 'Github\Api\CurrentUser'), array('current_user', 'Github\Api\CurrentUser'), + array('currentUser', 'Github\Api\CurrentUser'), array('git', 'Github\Api\GitData'), array('git_data', 'Github\Api\GitData'), + array('gitData', 'Github\Api\GitData'), array('gist', 'Github\Api\Gists'), array('gists', 'Github\Api\Gists'), @@ -163,7 +187,9 @@ public function getApiClassesProvider() array('repositories', 'Github\Api\Repo'), array('pr', 'Github\Api\PullRequest'), + array('pullRequest', 'Github\Api\PullRequest'), array('pull_request', 'Github\Api\PullRequest'), + array('pullRequests', 'Github\Api\PullRequest'), array('pull_requests', 'Github\Api\PullRequest'), array('authorization', 'Github\Api\Authorizations'),