Skip to content

Commit

Permalink
Merge pull request #186 from wittiws/master
Browse files Browse the repository at this point in the history
Add basic search api implementation.
  • Loading branch information
pilot committed Dec 13, 2014
2 parents 4905606 + 92633d1 commit eac0ecc
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 1 deletion.
48 changes: 48 additions & 0 deletions doc/search.md
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');
```
80 changes: 80 additions & 0 deletions lib/Github/Api/Search.php
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));
}

}
7 changes: 6 additions & 1 deletion lib/Github/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* @method Api\Repo repos()
* @method Api\Repo repository()
* @method Api\Repo repositories()
* @method Api\Search search()
* @method Api\Organization team()
* @method Api\Organization teams()
* @method Api\User user()
Expand Down Expand Up @@ -162,6 +163,10 @@ public function api($name)
$api = new Api\Repo($this);
break;

case 'search':
$api = new Api\Search($this);
break;

case 'team':
case 'teams':
$api = new Api\Organization\Teams($this);
Expand Down Expand Up @@ -310,7 +315,7 @@ public function getSupportedApiVersions()

/**
* @param string $name
*
*
* @return ApiInterface
*
* @throws InvalidArgumentException
Expand Down
183 changes: 183 additions & 0 deletions test/Github/Tests/Api/SearchTest.php
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';
}
}
2 changes: 2 additions & 0 deletions test/Github/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ public function getApiClassesProvider()
array('repository', 'Github\Api\Repo'),
array('repositories', 'Github\Api\Repo'),

array('search', 'Github\Api\Search'),

array('pr', 'Github\Api\PullRequest'),
array('pullRequest', 'Github\Api\PullRequest'),
array('pull_request', 'Github\Api\PullRequest'),
Expand Down

0 comments on commit eac0ecc

Please sign in to comment.