Skip to content

Commit 16f6660

Browse files
committed
Packages: add filter by origin
1 parent 4f091c3 commit 16f6660

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ $client->customers()->removePackage($customerId, $packageName);
8282

8383
##### List an organization's packages
8484
```php
85-
$packages = $client->packages()->all();
85+
$filters = [
86+
'origin' => \PrivatePackagist\ApiClient\Api\Packages::ORIGIN_PRIVATE, // optional filter to only receive packages that can be added to customers
87+
];
88+
$packages = $client->packages()->all($filters);
8689
```
8790
Returns an array of packages.
8891

src/Api/Packages.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,32 @@
22

33
namespace PrivatePackagist\ApiClient\Api;
44

5+
use PrivatePackagist\ApiClient\Exception\InvalidArgumentException;
6+
57
class Packages extends AbstractApi
68
{
7-
public function all()
9+
/**
10+
* Packages that are mirrored from a public Composer repository like packagist.org.
11+
*/
12+
const ORIGIN_PUBLIC_PROXY = 'public-proxy';
13+
14+
/**
15+
* Packages that are mirrored from a private Composer repository requiring authentication like repo.magento.com.
16+
*/
17+
const ORIGIN_PRIVATE_PROXY = 'private-proxy';
18+
19+
/**
20+
* All other packages from a VCS repository or a custom JSON definition.
21+
*/
22+
const ORIGIN_PRIVATE = 'private';
23+
24+
public function all(array $filters = [])
825
{
9-
return $this->get('/packages/');
26+
$availableOrigins = [self::ORIGIN_PUBLIC_PROXY, self::ORIGIN_PRIVATE_PROXY, self::ORIGIN_PRIVATE];
27+
if (isset($filters['origin']) && !in_array($filters['origin'], $availableOrigins, true)) {
28+
throw new InvalidArgumentException('Filter "origin" has to be one of: "' . implode('", "', $availableOrigins) . '".');
29+
}
30+
31+
return $this->get('/packages/', $filters);
1032
}
1133
}

tests/Api/PackagesTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public function testAll()
99
$expected = [
1010
[
1111
'id' => 1,
12-
'name' => 'composer/composer',
12+
'name' => 'acme-website/package',
1313
],
1414
];
1515

@@ -23,6 +23,46 @@ public function testAll()
2323
$this->assertSame($expected, $api->all());
2424
}
2525

26+
public function testAllWithFilters()
27+
{
28+
$expected = [
29+
[
30+
'id' => 1,
31+
'name' => 'acme-website/package',
32+
],
33+
];
34+
35+
$filters = [
36+
'origin' => Packages::ORIGIN_PRIVATE,
37+
];
38+
39+
/** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */
40+
$api = $this->getApiMock();
41+
$api->expects($this->once())
42+
->method('get')
43+
->with($this->equalTo('/packages/'), $this->equalTo($filters))
44+
->will($this->returnValue($expected));
45+
46+
$this->assertSame($expected, $api->all($filters));
47+
}
48+
49+
/**
50+
* @expectedException \PrivatePackagist\ApiClient\Exception\InvalidArgumentException
51+
*/
52+
public function testAllWithInvalidFilters()
53+
{
54+
$filters = [
55+
'origin' => 'invalid'
56+
];
57+
58+
/** @var Packages&\PHPUnit_Framework_MockObject_MockObject $api */
59+
$api = $this->getApiMock();
60+
$api->expects($this->never())
61+
->method('get');
62+
63+
$api->all($filters);
64+
}
65+
2666
protected function getApiClass()
2767
{
2868
return Packages::class;

0 commit comments

Comments
 (0)