Skip to content

Commit 476c87b

Browse files
authored
Merge pull request #9 from packagist/t/projects-api
Projects: add initial set of endpoints
2 parents 38642ef + e4b0a3b commit 476c87b

File tree

8 files changed

+370
-2
lines changed

8 files changed

+370
-2
lines changed

Diff for: README.md

+70
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ $jobs = $client->organization()->sync();
4242
```
4343
Returns an array of created jobs. One for every synchronization.
4444

45+
#### Team
46+
47+
##### List an organization's teams
48+
```php
49+
$teams = $client->teams()->all();
50+
```
51+
Returns an array of teams.
52+
4553
#### Customer
4654

4755
##### List an organization's customers
@@ -107,6 +115,68 @@ $composerRepository = $client->customers()->regenerateToken($customerId, $confir
107115
```
108116
Returns the updated Composer repository.
109117

118+
#### Project
119+
120+
##### List an organization's projects
121+
```php
122+
$projects = $client->projects()->all();
123+
```
124+
Returns an array of projects.
125+
126+
##### Show a project
127+
```php
128+
$projectId = 42;
129+
$project = $client->projects()->show($projectId);
130+
```
131+
Returns a single project.
132+
133+
##### Create a project
134+
```php
135+
$project = $client->projects()->create('New project name');
136+
```
137+
Returns the project.
138+
139+
##### Delete a project
140+
```php
141+
$projectId = 42;
142+
$client->projects()->remove($projectId);
143+
```
144+
145+
##### List a projects's teams
146+
```php
147+
$projectId = 42;
148+
$teams = $client->projects()->listTeams($projectId);
149+
```
150+
Returns an array of projects teams.
151+
152+
##### Add a team to a project or update the permission
153+
```php
154+
$projectId = 42;
155+
$teams = [
156+
[
157+
'id' => 12,
158+
'permission' => 'owner',
159+
],
160+
];
161+
$teams = $client->customers()->addOrUpdateTeams($projectId, $teams);
162+
```
163+
Returns an array of added project teams.
164+
165+
166+
##### Remove a team from a customer
167+
```php
168+
$projectId = 42;
169+
$teamId = 12;
170+
$client->customers()->removeTeam($projectId, $teamId);
171+
```
172+
173+
##### List a projects's packages
174+
```php
175+
$projectId = 42;
176+
$packages = $client->projects()->listPackages($projectId);
177+
```
178+
Returns an array of projects packages.
179+
110180
#### Package
111181

112182
##### List an organization's packages

Diff for: src/Api/Customers.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function addOrUpdatePackages($customerId, array $packages)
3535
{
3636
foreach ($packages as $package) {
3737
if (!isset($package['name'])) {
38-
throw new InvalidArgumentException('Parameter "name" is requried.');
38+
throw new InvalidArgumentException('Parameter "name" is required.');
3939
}
4040
}
4141

Diff for: src/Api/Projects.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace PrivatePackagist\ApiClient\Api;
4+
5+
use PrivatePackagist\ApiClient\Exception\InvalidArgumentException;
6+
7+
class Projects extends AbstractApi
8+
{
9+
public function all()
10+
{
11+
return $this->get('/projects/');
12+
}
13+
14+
public function show($projectId)
15+
{
16+
return $this->get(sprintf('/projects/%s/', $projectId));
17+
}
18+
19+
public function create($name)
20+
{
21+
return $this->post('/projects/', ['name' => $name]);
22+
}
23+
24+
public function remove($projectId)
25+
{
26+
return $this->delete(sprintf('/projects/%s/', $projectId));
27+
}
28+
29+
public function listTeams($projectsId)
30+
{
31+
return $this->get(sprintf('/projects/%s/teams/', $projectsId));
32+
}
33+
34+
public function addOrUpdateTeams($projectId, array $teams)
35+
{
36+
foreach ($teams as $team) {
37+
if (!isset($team['id'])) {
38+
throw new InvalidArgumentException('Parameter "id" is required.');
39+
}
40+
41+
if (!isset($team['permission'])) {
42+
throw new InvalidArgumentException('Parameter "permission" is required.');
43+
}
44+
}
45+
46+
return $this->post(sprintf('/projects/%s/teams/', $projectId), $teams);
47+
}
48+
49+
public function removeTeam($projectId, $teamId)
50+
{
51+
return $this->delete(sprintf('/projects/%s/teams/%s/', $projectId, $teamId));
52+
}
53+
54+
public function listPackages($projectsId)
55+
{
56+
return $this->get(sprintf('/projects/%s/packages/', $projectsId));
57+
}
58+
}

Diff for: src/Api/Teams.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace PrivatePackagist\ApiClient\Api;
4+
5+
class Teams extends AbstractApi
6+
{
7+
public function all()
8+
{
9+
return $this->get('/teams/');
10+
}
11+
}

Diff for: src/Client.php

+10
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,21 @@ public function credentials()
4848
return new Api\Credentials($this, $this->responseMediator);
4949
}
5050

51+
public function teams()
52+
{
53+
return new Api\Teams($this, $this->responseMediator);
54+
}
55+
5156
public function customers()
5257
{
5358
return new Api\Customers($this, $this->responseMediator);
5459
}
5560

61+
public function projects()
62+
{
63+
return new Api\Projects($this, $this->responseMediator);
64+
}
65+
5666
public function organization()
5767
{
5868
return new Api\Organization($this, $this->responseMediator);

Diff for: tests/Api/CustomersTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function testAddOrUpdatePackages()
122122

123123
/**
124124
* @expectedException \PrivatePackagist\ApiClient\Exception\InvalidArgumentException
125-
* @expectedExceptionMessage Parameter "name" is requried.
125+
* @expectedExceptionMessage Parameter "name" is required.
126126
*/
127127
public function testAddOrUpdatePackagesMissingName()
128128
{

Diff for: tests/Api/ProjectsTest.php

+177
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?php
2+
3+
namespace PrivatePackagist\ApiClient\Api;
4+
5+
class ProjectsTest extends ApiTestCase
6+
{
7+
public function testAll()
8+
{
9+
$expected = [
10+
$this->getProjectDefinition(),
11+
];
12+
13+
/** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */
14+
$api = $this->getApiMock();
15+
$api->expects($this->once())
16+
->method('get')
17+
->with($this->equalTo('/projects/'))
18+
->will($this->returnValue($expected));
19+
20+
$this->assertSame($expected, $api->all());
21+
}
22+
23+
public function testShow()
24+
{
25+
$expected = $this->getProjectDefinition();
26+
27+
$projectId = 1;
28+
29+
/** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */
30+
$api = $this->getApiMock();
31+
$api->expects($this->once())
32+
->method('get')
33+
->with($this->equalTo('/projects/1/'))
34+
->will($this->returnValue($expected));
35+
36+
$this->assertSame($expected, $api->show($projectId));
37+
}
38+
39+
public function testCreate()
40+
{
41+
$expected = $this->getProjectDefinition();
42+
43+
/** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */
44+
$api = $this->getApiMock();
45+
$api->expects($this->once())
46+
->method('post')
47+
->with($this->equalTo('/projects/'), $this->equalTo(['name' => 'ACME Websites']))
48+
->will($this->returnValue($expected));
49+
50+
$this->assertSame($expected, $api->create('ACME Websites'));
51+
}
52+
53+
public function testRemove()
54+
{
55+
$expected = '';
56+
57+
/** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */
58+
$api = $this->getApiMock();
59+
$api->expects($this->once())
60+
->method('delete')
61+
->with($this->equalTo('/projects/1/'))
62+
->will($this->returnValue($expected));
63+
64+
$this->assertSame($expected, $api->remove(1));
65+
}
66+
67+
public function testListTeams()
68+
{
69+
$expected = [
70+
[
71+
'id' => 42,
72+
'name' => 'Owners',
73+
'permission' => 'owner',
74+
'members' => [],
75+
'projects' => [],
76+
],
77+
];
78+
79+
/** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */
80+
$api = $this->getApiMock();
81+
$api->expects($this->once())
82+
->method('get')
83+
->with($this->equalTo('/projects/1/teams/'))
84+
->will($this->returnValue($expected));
85+
86+
$this->assertSame($expected, $api->listTeams(1));
87+
}
88+
89+
public function testAddOrUpdateTeam()
90+
{
91+
$expected = [
92+
[
93+
'id' => 42,
94+
'name' => 'Owners',
95+
'permission' => 'owner',
96+
'members' => [],
97+
'projects' => [],
98+
],
99+
];
100+
101+
$teams = [['id' => 42, 'permission' => 'owner']];
102+
103+
/** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */
104+
$api = $this->getApiMock();
105+
$api->expects($this->once())
106+
->method('post')
107+
->with($this->equalTo('/projects/1/teams/'), $this->equalTo($teams))
108+
->will($this->returnValue($expected));
109+
110+
$this->assertSame($expected, $api->addOrUpdateTeams(1, $teams));
111+
}
112+
113+
public function testRemoveTeam()
114+
{
115+
$expected = '';
116+
117+
/** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */
118+
$api = $this->getApiMock();
119+
$api->expects($this->once())
120+
->method('delete')
121+
->with($this->equalTo('/projects/1/teams/42/'))
122+
->will($this->returnValue($expected));
123+
124+
$this->assertSame($expected, $api->removeTeam(1, 42));
125+
}
126+
127+
public function testListPackages()
128+
{
129+
$expected = [
130+
[
131+
'name' => 'composer/composer',
132+
'origin' => 'private',
133+
'versionConstraint' => null,
134+
'expirationDate' => null,
135+
],
136+
];
137+
138+
/** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */
139+
$api = $this->getApiMock();
140+
$api->expects($this->once())
141+
->method('get')
142+
->with($this->equalTo('/projects/1/packages/'))
143+
->will($this->returnValue($expected));
144+
145+
$this->assertSame($expected, $api->listPackages(1));
146+
}
147+
148+
private function getProjectDefinition()
149+
{
150+
return [
151+
'id' => 1,
152+
'name' => 'ACME Websites',
153+
'urlName' => 'acme-websites',
154+
'teams' => [
155+
[
156+
'id' => 1,
157+
'name' => 'Owners',
158+
'permission' => 'owner',
159+
'members' => [
160+
[
161+
'id' => 12,
162+
'username' => 'username'
163+
]
164+
],
165+
]
166+
]
167+
];
168+
}
169+
170+
/**
171+
* @return string
172+
*/
173+
protected function getApiClass()
174+
{
175+
return Projects::class;
176+
}
177+
}

0 commit comments

Comments
 (0)