8000 Use rize/uri-template to build properly escaped URIs · clue/reactphp-packagist-api@40d97d2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 40d97d2

Browse files
committed
Use rize/uri-template to build properly escaped URIs
1 parent 393ce26 commit 40d97d2

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"require": {
1414
"php": ">=5.3",
1515
"knplabs/packagist-api": "~1.0",
16-
"clue/buzz-react": "^0.5"
16+
"clue/buzz-react": "^0.5",
17+
"rize/uri-template": "^0.3"
1718
},
1819
"autoload": {
1920
"psr-4": { "Clue\\React\\Packagist\\Api\\": "src/" }

src/Client.php

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,42 @@
55
use Packagist\Api\Result\Factory;
66
use Clue\React\Buzz\Browser;
77
use Psr\Http\Message\ResponseInterface;
8+
use Rize\UriTemplate;
89

910
class Client
1011
{
1112
private $http;
1213
private $resultFactory;
14+
private $uri;
1315

14-
public function __construct(Browser $http, Factory $resultFactory = null)
16+
public function __construct(Browser $http, Factory $resultFactory = null, UriTemplate $uri = null)
1517
{
16-
$this->http = $http;
18+
$this->http = $http->withBase('https://packagist.org/');
1719

1820
if (null === $resultFactory) {
1921
$resultFactory = new Factory();
2022
}
2123

24+
if (null === $uri) {
25+
$uri = new UriTemplate();
26+
}
27+
2228
$this->resultFactory = $resultFactory;
29+
$this->uri = $uri;
2330
}
2431

2532
public function search($query, array $filters = array())
2633
{
27-
$results = array();
2834
$filters['q'] = $query;
29-
$url = $this->url('/search.json?' . http_build_query($filters));
35+
36+
$url = $this->uri->expand(
37+
'/search.json{?filters*}',
38+
array(
39+
'filters' => $filters
40+
)
41+
);
42+
43+
$results = array();
3044
$that = $this;
3145

3246
$fetch = function ($url) use (&$results, $that, &$fetch) {
@@ -47,22 +61,26 @@ public function search($query, array $filters = array())
4761

4862
public function get($package)
4963
{
50-
return $this->respond(sprintf($this->url('/packages/%s.json'), $package));
64+
return $this->respond(
65+
$this->uri->expand(
66+
'/packages/{package}.json',
67+
array(
68+
'package' => $package
69+
)
70+
)
71+
);
5172
}
5273

5374
public function all(array $filters = array())
5475
{
55-
$url = '/packages/list.json';
56-
if ($filters) {
57-
$url .= '?'.http_build_query($filters);
58-
}
59-
60-
return $this->respond($this->url($url));
61-
}
62-
63-
protected function url($url)
64-
{
65-
return 'https://packagist.org'.$url;
76+
return $this->respond(
77+
$this->uri->expand(
78+
'/packages/list.json{?filters*}',
79+
array(
80+
'filters' => $filters
81+
)
82+
)
83+
);
6684
}
6785

6886
protected function respond($url)

tests/ClientTest.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,46 @@ class ClientTest extends TestCase
1313
public function setUp()
1414
{
1515
$this->browser = $this->getMockBuilder('Clue\React\Buzz\Browser')->disableOriginalConstructor()->getMock();
16+
$this->browser->expects($this->any())->method('withBase')->willReturn($this->browser);
1617

1718
$this->client = new Client($this->browser);
1819
}
1920

2021
public function testGet()
2122
{
22-
$this->setupBrowser('packages/clue/zenity-react.json', $this->createResponsePromise('{"package":{"name":"clue\/zenity-react", "versions": {}}}'));
23+
$this->setupBrowser('/packages/clue%2Fzenity-react.json', $this->createResponsePromise('{"package":{"name":"clue\/zenity-react", "versions": {}}}'));
2324

2425
$this->expectPromiseResolve($this->client->get('clue/zenity-react'));
2526
}
2627

2728
public function testAll()
2829
{
29-
$this->setupBrowser('packages/list.json', $this->createResponsePromise('{"packageNames":["a/a", "b/b"]}'));
30+
$this->setupBrowser('/packages/list.json', $this->createResponsePromise('{"packageNames":["a/a", "b/b"]}'));
3031

3132
$this->expectPromiseResolve($this->client->all());
3233
}
3334

3435
public function testAllVendor()
3536
{
36-
$this->setupBrowser('packages/list.json?vendor=a', $this->createResponsePromise('{"packageNames":["a/a"]}'));
37+
$this->setupBrowser('/packages/list.json?vendor=a', $this->createResponsePromise('{"packageNames":["a/a"]}'));
3738

3839
$this->expectPromiseResolve($this->client->all(array('vendor' => 'a')));
3940
}
4041

4142
public function testSearch()
4243
{
43-
$this->setupBrowser('search.json?q=zenity', $this->createResponsePromise('{"results":[{"name":"clue\/zenity-react","description":"Build graphical desktop (GUI) applications in PHP","url":"https:\/\/packagist.org\/packages\/clue\/zenity-react","downloads":57,"favers":0,"repository":"https:\/\/github.com\/clue\/reactphp-zenity"}],"total":1}'));
44+
$this->setupBrowser('/search.json?q=zenity', $this< F024 /span>->createResponsePromise('{"results":[{"name":"clue\/zenity-react","description":"Build graphical desktop (GUI) applications in PHP","url":"https:\/\/packagist.org\/packages\/clue\/zenity-react","downloads":57,"favers":0,"repository":"https:\/\/github.com\/clue\/reactphp-zenity"}],"total":1}'));
4445

4546
$this->expectPromiseResolve($this->client->search('zenity'));
4647
}
4748

49+
public function testSearchSpecialWithNoResults()
50+
{
51+
$this->setupBrowser('/search.json?q=%3C%C3%A4%3E', $this->createResponsePromise('{"results":[],"total":0}'));
52+
53+
$this->expectPromiseResolve($this->client->search('<ä>'));
54+
}
55+
4856
public function testSearchPagination()
4957
{
5058
$this->browser->expects($this->exactly(2))
@@ -59,7 +67,7 @@ public function testSearchPagination()
5967

6068
public function testHttpError()
6169
{
62-
$this->setupBrowser('packages/clue/invalid.json', $this->createRejectedPromise(new RuntimeException('error')));
70+
$this->setupBrowser('/packages/clue%2Finvalid.json', $this->createRejectedPromise(new RuntimeException('error')));
6371

6472
$this->expectPromiseReject($this->client->get('clue/invalid'));
6573
}
@@ -68,7 +76,7 @@ private function setupBrowser($expectedUrl, $promise)
6876
{
6977
$this->browser->expects($this->once())
7078
->method('get')
71-
->with($this->equalTo('https://packagist.org/' . $expectedUrl), array())
79+
->with($this->equalTo($expectedUrl), array())
7280
->will($this->returnValue($promise));
7381
}
7482

0 commit comments

Comments
 (0)
0