Skip to content

Commit eac0ecc

Browse files
committed
Merge pull request #186 from wittiws/master
Add basic search api implementation.
2 parents 4905606 + 92633d1 commit eac0ecc

File tree

5 files changed

+319
-1
lines changed

5 files changed

+319
-1
lines changed

doc/search.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Search API
2+
[Back to the navigation](index.md)
3+
4+
Searching repositories, code, issues and users.
5+
Wrap [GitHub Search API](http://developer.github.com/v3/search/). All methods are described on that page.
6+
7+
### Search repositories
8+
9+
```php
10+
$repos = $client->api('search')->repositories('github language:php');
11+
```
12+
13+
Returns a list of repositories found by such criteria.
14+
15+
### Search code
16+
17+
```php
18+
$repos = $client->api('search')->code('@todo language:php');
19+
```
20+
21+
Returns a list of files found by such criteria (containing "@todo" and language==php).
22+
23+
### Search issues
24+
25+
```php
26+
$repos = $client->api('search')->issues('bug language:php');
27+
```
28+
29+
Returns a list of issues found by such criteria.
30+
31+
### Search users
32+
33+
```php
34+
$repos = $client->api('search')->users('location:Amsterdam language:php');
35+
```
36+
37+
Returns a list of users found by such criteria.
38+
39+
### Sorting results
40+
41+
You can sort results using 2-3 arguments.
42+
43+
```php
44+
$repos = $client->api('repo')->repositories('...', 'created', 'asc');
45+
$repos = $client->api('repo')->code('...........', 'indexed', 'desc');
46+
$repos = $client->api('repo')->issues('.........', 'comments', 'asc');
47+
$repos = $client->api('repo')->users('..........', 'followers', 'asc');
48+
```

lib/Github/Api/Search.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Github\Api;
4+
5+
use Github\Api\Issue\Comments;
6+
use Github\Api\Issue\Events;
7+
use Github\Api\Issue\Labels;
8+
use Github\Api\Issue\Milestones;
9+
use Github\Exception\MissingArgumentException;
10+
11+
/**
12+
* Implement the Search API.
13+
*
14+
* @link https://developer.github.com/v3/search/
15+
* @author Greg Payne <[email protected]>
16+
*/
17+
class Search extends AbstractApi
18+
{
19+
20+
/**
21+
* Search repositories by filter (q)
22+
* @link https://developer.github.com/v3/search/#search-repositories
23+
*
24+
* @param string $q the filter
25+
* @param string $sort the sort field
26+
* @param string $order asc/desc
27+
*
28+
* @return array list of repositories found
29+
*/
30+
public function repositories($q, $sort = 'updated', $order = 'desc')
31+
{
32+
return $this->get('/search/repositories', array('q' => $q, 'sort' => $sort, 'order' => $order));
33+
}
34+
35+
/**
36+
* Search issues by filter (q)
37+
* @link https://developer.github.com/v3/search/#search-issues
38+
*
39+
* @param string $q the filter
40+
* @param string $sort the sort field
41+
* @param string $order asc/desc
42+
*
43+
* @return array list of issues found
44+
*/
45+
public function issues($q, $sort = 'updated', $order = 'desc')
46+
{
47+
return $this->get('/search/issues', array('q' => $q, 'sort' => $sort, 'order' => $order));
48+
}
49+
50+
/**
51+
* Search code by filter (q)
52+
* @link https://developer.github.com/v3/search/#search-code
53+
*
54+
* @param string $q the filter
55+
* @param string $sort the sort field
56+
* @param string $order asc/desc
57+
*
58+
* @return array list of code found
59+
*/
60+
public function code($q, $sort = 'updated', $order = 'desc')
61+
{
62+
return $this->get('/search/code', array('q' => $q, 'sort' => $sort, 'order' => $order));
63+
}
64+
65+
/**
66+
* Search users by filter (q)
67+
* @link https://developer.github.com/v3/search/#search-users
68+
*
69+
* @param string $q the filter
70+
* @param string $sort the sort field
71+
* @param string $order asc/desc
72+
*
73+
* @return array list of users found
74+
*/
75+
public function users($q, $sort = 'updated', $order = 'desc')
76+
{
77+
return $this->get('/search/users', array('q' => $q, 'sort' => $sort, 'order' => $order));
78+
}
79+
80+
}

lib/Github/Client.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* @method Api\Repo repos()
3232
* @method Api\Repo repository()
3333
* @method Api\Repo repositories()
34+
* @method Api\Search search()
3435
* @method Api\Organization team()
3536
* @method Api\Organization teams()
3637
* @method Api\User user()
@@ -162,6 +163,10 @@ public function api($name)
162163
$api = new Api\Repo($this);
163164
break;
164165

166+
case 'search':
167+
$api = new Api\Search($this);
168+
break;
169+
165170
case 'team':
166171
case 'teams':
167172
$api = new Api\Organization\Teams($this);
@@ -310,7 +315,7 @@ public function getSupportedApiVersions()
310315

311316
/**
312317
* @param string $name
313-
*
318+
*
314319
* @return ApiInterface
315320
*
316321
* @throws InvalidArgumentException

test/Github/Tests/Api/SearchTest.php

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
<?php
2+
3+
namespace Github\Tests\Api;
4+
5+
class SearchTest extends TestCase
6+
{
7+
/**
8+
* @test
9+
*/
10+
public function shouldSearchRepositoriesByQuery()
11+
{
12+
$expectedArray = array(array('total_count' => '0'));
13+
14+
$api = $this->getApiMock();
15+
16+
$api->expects($this->once())
17+
->method('get')
18+
->with(
19+
'/search/repositories',
20+
array('q' => 'query text', 'sort' => 'updated', 'order' => 'desc')
21+
)
22+
->will($this->returnValue($expectedArray));
23+
24+
$this->assertEquals($expectedArray, $api->repositories('query text'));
25+
}
26+
27+
/**
28+
* @test
29+
*/
30+
public function shouldSearchRepositoriesRegardingSortAndOrder()
31+
{
32+
$expectedArray = array(array('total_count' => '0'));
33+
34+
$api = $this->getApiMock();
35+
36+
$api->expects($this->once())
37+
->method('get')
38+
->with(
39+
'/search/repositories',
40+
array('q' => 'query text', 'sort' => 'created', 'order' => 'asc')
41+
)
42+
->will($this->returnValue($expectedArray));
43+
44+
$this->assertEquals(
45+
$expectedArray,
46+
$api->repositories('query text', 'created', 'asc')
47+
);
48+
}
49+
50+
/**
51+
* @test
52+
*/
53+
public function shouldSearchIssuesByQuery()
54+
{
55+
$expectedArray = array(array('total_count' => '0'));
56+
57+
$api = $this->getApiMock();
58+
59+
$api->expects($this->once())
60+
->method('get')
61+
->with(
62+
'/search/issues',
63+
array('q' => 'query text', 'sort' => 'updated', 'order' => 'desc')
64+
)
65+
->will($this->returnValue($expectedArray));
66+
67+
$this->assertEquals($expectedArray, $api->issues('query text'));
68+
}
69+
70+
/**
71+
* @test
72+
*/
73+
public function shouldSearchIssuesRegardingSortAndOrder()
74+
{
75+
$expectedArray = array(array('total_count' => '0'));
76+
77+
$api = $this->getApiMock();
78+
79+
$api->expects($this->once())
80+
->method('get')
81+
->with(
82+
'/search/issues',
83+
array('q' => 'query text', 'sort' => 'created', 'order' => 'asc')
84+
)
85+
->will($this->returnValue($expectedArray));
86+
87+
$this->assertEquals(
88+
$expectedArray,
89+
$api->issues('query text', 'created', 'asc')
90+
);
91+
}
92+
93+
/**
94+
* @test
95+
*/
96+
public function shouldSearchCodeByQuery()
97+
{
98+
$expectedArray = array(array('total_count' => '0'));
99+
100+
$api = $this->getApiMock();
101+
102+
$api->expects($this->once())
103+
->method('get')
104+
->with(
105+
'/search/code',
106+
array('q' => 'query text', 'sort' => 'updated', 'order' => 'desc')
107+
)
108+
->will($this->returnValue($expectedArray));
109+
110+
$this->assertEquals($expectedArray, $api->code('query text'));
111+
}
112+
113+
/**
114+
* @test
115+
*/
116+
public function shouldSearchCodeRegardingSortAndOrder()
117+
{
118+
$expectedArray = array(array('total_count' => '0'));
119+
120+
$api = $this->getApiMock();
121+
122+
$api->expects($this->once())
123+
->method('get')
124+
->with(
125+
'/search/code',
126+
array('q' => 'query text', 'sort' => 'created', 'order' => 'asc')
127+
)
128+
->will($this->returnValue($expectedArray));
129+
130+
$this->assertEquals(
131+
$expectedArray,
132+
$api->code('query text', 'created', 'asc')
133+
);
134+
}
135+
136+
/**
137+
* @test
138+
*/
139+
public function shouldSearchUsersByQuery()
140+
{
141+
$expectedArray = array(array('total_count' => '0'));
142+
143+
$api = $this->getApiMock();
144+
145+
$api->expects($this->once())
146+
->method('get')
147+
->with(
148+
'/search/users',
149+
array('q' => 'query text', 'sort' => 'updated', 'order' => 'desc')
150+
)
151+
->will($this->returnValue($expectedArray));
152+
153+
$this->assertEquals($expectedArray, $api->users('query text'));
154+
}
155+
156+
/**
157+
* @test
158+
*/
159+
public function shouldSearchUsersRegardingSortAndOrder()
160+
{
161+
$expectedArray = array(array('total_count' => '0'));
162+
163+
$api = $this->getApiMock();
164+
165+
$api->expects($this->once())
166+
->method('get')
167+
->with(
168+
'/search/users',
169+
array('q' => 'query text', 'sort' => 'created', 'order' => 'asc')
170+
)
171+
->will($this->returnValue($expectedArray));
172+
173+
$this->assertEquals(
174+
$expectedArray,
175+
$api->users('query text', 'created', 'asc')
176+
);
177+
}
178+
179+
protected function getApiClass()
180+
{
181+
return 'Github\Api\Search';
182+
}
183+
}

test/Github/Tests/ClientTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ public function getApiClassesProvider()
186186
array('repository', 'Github\Api\Repo'),
187187
array('repositories', 'Github\Api\Repo'),
188188

189+
array('search', 'Github\Api\Search'),
190+
189191
array('pr', 'Github\Api\PullRequest'),
190192
array('pullRequest', 'Github\Api\PullRequest'),
191193
array('pull_request', 'Github\Api\PullRequest'),

0 commit comments

Comments
 (0)