-
-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #186 from wittiws/master
Add basic search api implementation.
- Loading branch information
Showing
5 changed files
with
319 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
## Search API | ||
[Back to the navigation](index.md) | ||
|
||
Searching repositories, code, issues and users. | ||
Wrap [GitHub Search API](http://developer.github.com/v3/search/). All methods are described on that page. | ||
|
||
### Search repositories | ||
|
||
```php | ||
$repos = $client->api('search')->repositories('github language:php'); | ||
``` | ||
|
||
Returns a list of repositories found by such criteria. | ||
|
||
### Search code | ||
|
||
```php | ||
$repos = $client->api('search')->code('@todo language:php'); | ||
``` | ||
|
||
Returns a list of files found by such criteria (containing "@todo" and language==php). | ||
|
||
### Search issues | ||
|
||
```php | ||
$repos = $client->api('search')->issues('bug language:php'); | ||
``` | ||
|
||
Returns a list of issues found by such criteria. | ||
|
||
### Search users | ||
|
||
```php | ||
$repos = $client->api('search')->users('location:Amsterdam language:php'); | ||
``` | ||
|
||
Returns a list of users found by such criteria. | ||
|
||
### Sorting results | ||
|
||
You can sort results using 2-3 arguments. | ||
|
||
```php | ||
$repos = $client->api('repo')->repositories('...', 'created', 'asc'); | ||
$repos = $client->api('repo')->code('...........', 'indexed', 'desc'); | ||
$repos = $client->api('repo')->issues('.........', 'comments', 'asc'); | ||
$repos = $client->api('repo')->users('..........', 'followers', 'asc'); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Github\Api; | ||
|
||
use Github\Api\Issue\Comments; | ||
use Github\Api\Issue\Events; | ||
use Github\Api\Issue\Labels; | ||
use Github\Api\Issue\Milestones; | ||
use Github\Exception\MissingArgumentException; | ||
|
||
/** | ||
* Implement the Search API. | ||
* | ||
* @link https://developer.github.com/v3/search/ | ||
* @author Greg Payne <greg.payne@gmail.com> | ||
*/ | ||
class Search extends AbstractApi | ||
{ | ||
|
||
/** | ||
* Search repositories by filter (q) | ||
* @link https://developer.github.com/v3/search/#search-repositories | ||
* | ||
* @param string $q the filter | ||
* @param string $sort the sort field | ||
* @param string $order asc/desc | ||
* | ||
* @return array list of repositories found | ||
*/ | ||
public function repositories($q, $sort = 'updated', $order = 'desc') | ||
{ | ||
return $this->get('/search/repositories', array('q' => $q, 'sort' => $sort, 'order' => $order)); | ||
} | ||
|
||
/** | ||
* Search issues by filter (q) | ||
* @link https://developer.github.com/v3/search/#search-issues | ||
* | ||
* @param string $q the filter | ||
* @param string $sort the sort field | ||
* @param string $order asc/desc | ||
* | ||
* @return array list of issues found | ||
*/ | ||
public function issues($q, $sort = 'updated', $order = 'desc') | ||
{ | ||
return $this->get('/search/issues', array('q' => $q, 'sort' => $sort, 'order' => $order)); | ||
} | ||
|
||
/** | ||
* Search code by filter (q) | ||
* @link https://developer.github.com/v3/search/#search-code | ||
* | ||
* @param string $q the filter | ||
* @param string $sort the sort field | ||
* @param string $order asc/desc | ||
* | ||
* @return array list of code found | ||
*/ | ||
public function code($q, $sort = 'updated', $order = 'desc') | ||
{ | ||
return $this->get('/search/code', array('q' => $q, 'sort' => $sort, 'order' => $order)); | ||
} | ||
|
||
/** | ||
* Search users by filter (q) | ||
* @link https://developer.github.com/v3/search/#search-users | ||
* | ||
* @param string $q the filter | ||
* @param string $sort the sort field | ||
* @param string $order asc/desc | ||
* | ||
* @return array list of users found | ||
*/ | ||
public function users($q, $sort = 'updated', $order = 'desc') | ||
{ | ||
return $this->get('/search/users', array('q' => $q, 'sort' => $sort, 'order' => $order)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
<?php | ||
|
||
namespace Github\Tests\Api; | ||
|
||
class SearchTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function shouldSearchRepositoriesByQuery() | ||
{ | ||
$expectedArray = array(array('total_count' => '0')); | ||
|
||
$api = $this->getApiMock(); | ||
|
||
$api->expects($this->once()) | ||
->method('get') | ||
->with( | ||
'/search/repositories', | ||
array('q' => 'query text', 'sort' => 'updated', 'order' => 'desc') | ||
) | ||
->will($this->returnValue($expectedArray)); | ||
|
||
$this->assertEquals($expectedArray, $api->repositories('query text')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldSearchRepositoriesRegardingSortAndOrder() | ||
{ | ||
$expectedArray = array(array('total_count' => '0')); | ||
|
||
$api = $this->getApiMock(); | ||
|
||
$api->expects($this->once()) | ||
->method('get') | ||
->with( | ||
'/search/repositories', | ||
array('q' => 'query text', 'sort' => 'created', 'order' => 'asc') | ||
) | ||
->will($this->returnValue($expectedArray)); | ||
|
||
$this->assertEquals( | ||
$expectedArray, | ||
$api->repositories('query text', 'created', 'asc') | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldSearchIssuesByQuery() | ||
{ | ||
$expectedArray = array(array('total_count' => '0')); | ||
|
||
$api = $this->getApiMock(); | ||
|
||
$api->expects($this->once()) | ||
->method('get') | ||
->with( | ||
'/search/issues', | ||
array('q' => 'query text', 'sort' => 'updated', 'order' => 'desc') | ||
) | ||
->will($this->returnValue($expectedArray)); | ||
|
||
$this->assertEquals($expectedArray, $api->issues('query text')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldSearchIssuesRegardingSortAndOrder() | ||
{ | ||
$expectedArray = array(array('total_count' => '0')); | ||
|
||
$api = $this->getApiMock(); | ||
|
||
$api->expects($this->once()) | ||
->method('get') | ||
->with( | ||
'/search/issues', | ||
array('q' => 'query text', 'sort' => 'created', 'order' => 'asc') | ||
) | ||
->will($this->returnValue($expectedArray)); | ||
|
||
$this->assertEquals( | ||
$expectedArray, | ||
$api->issues('query text', 'created', 'asc') | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldSearchCodeByQuery() | ||
{ | ||
$expectedArray = array(array('total_count' => '0')); | ||
|
||
$api = $this->getApiMock(); | ||
|
||
$api->expects($this->once()) | ||
->method('get') | ||
->with( | ||
'/search/code', | ||
array('q' => 'query text', 'sort' => 'updated', 'order' => 'desc') | ||
) | ||
->will($this->returnValue($expectedArray)); | ||
|
||
$this->assertEquals($expectedArray, $api->code('query text')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldSearchCodeRegardingSortAndOrder() | ||
{ | ||
$expectedArray = array(array('total_count' => '0')); | ||
|
||
$api = $this->getApiMock(); | ||
|
||
$api->expects($this->once()) | ||
->method('get') | ||
->with( | ||
'/search/code', | ||
array('q' => 'query text', 'sort' => 'created', 'order' => 'asc') | ||
) | ||
->will($this->returnValue($expectedArray)); | ||
|
||
$this->assertEquals( | ||
$expectedArray, | ||
$api->code('query text', 'created', 'asc') | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldSearchUsersByQuery() | ||
{ | ||
$expectedArray = array(array('total_count' => '0')); | ||
|
||
$api = $this->getApiMock(); | ||
|
||
$api->expects($this->once()) | ||
->method('get') | ||
->with( | ||
'/search/users', | ||
array('q' => 'query text', 'sort' => 'updated', 'order' => 'desc') | ||
) | ||
->will($this->returnValue($expectedArray)); | ||
|
||
$this->assertEquals($expectedArray, $api->users('query text')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldSearchUsersRegardingSortAndOrder() | ||
{ | ||
$expectedArray = array(array('total_count' => '0')); | ||
|
||
$api = $this->getApiMock(); | ||
|
||
$api->expects($this->once()) | ||
->method('get') | ||
->with( | ||
'/search/users', | ||
array('q' => 'query text', 'sort' => 'created', 'order' => 'asc') | ||
) | ||
->will($this->returnValue($expectedArray)); | ||
|
||
$this->assertEquals( | ||
$expectedArray, | ||
$api->users('query text', 'created', 'asc') | ||
); | ||
} | ||
|
||
protected function getApiClass() | ||
{ | ||
return 'Github\Api\Search'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters