diff --git a/README.md b/README.md index 2d0aa51..48ff7a3 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,14 @@ $jobs = $client->organization()->sync(); ``` Returns an array of created jobs. One for every synchronization. +#### Team + +##### List an organization's teams +```php +$teams = $client->teams()->all(); +``` +Returns an array of teams. + #### Customer ##### List an organization's customers @@ -107,6 +115,68 @@ $composerRepository = $client->customers()->regenerateToken($customerId, $confir ``` Returns the updated Composer repository. +#### Project + +##### List an organization's projects +```php +$projects = $client->projects()->all(); +``` +Returns an array of projects. + +##### Show a project +```php +$projectId = 42; +$project = $client->projects()->show($projectId); +``` +Returns a single project. + +##### Create a project +```php +$project = $client->projects()->create('New project name'); +``` +Returns the project. + +##### Delete a project +```php +$projectId = 42; +$client->projects()->remove($projectId); +``` + +##### List a projects's teams +```php +$projectId = 42; +$teams = $client->projects()->listTeams($projectId); +``` +Returns an array of projects teams. + +##### Add a team to a project or update the permission +```php +$projectId = 42; +$teams = [ + [ + 'id' => 12, + 'permission' => 'owner', + ], +]; +$teams = $client->customers()->addOrUpdateTeams($projectId, $teams); +``` +Returns an array of added project teams. + + +##### Remove a team from a customer +```php +$projectId = 42; +$teamId = 12; +$client->customers()->removeTeam($projectId, $teamId); +``` + +##### List a projects's packages +```php +$projectId = 42; +$packages = $client->projects()->listPackages($projectId); +``` +Returns an array of projects packages. + #### Package ##### List an organization's packages diff --git a/src/Api/Customers.php b/src/Api/Customers.php index 2b7077c..8e999c8 100644 --- a/src/Api/Customers.php +++ b/src/Api/Customers.php @@ -35,7 +35,7 @@ public function addOrUpdatePackages($customerId, array $packages) { foreach ($packages as $package) { if (!isset($package['name'])) { - throw new InvalidArgumentException('Parameter "name" is requried.'); + throw new InvalidArgumentException('Parameter "name" is required.'); } } diff --git a/src/Api/Projects.php b/src/Api/Projects.php new file mode 100644 index 0000000..663f775 --- /dev/null +++ b/src/Api/Projects.php @@ -0,0 +1,58 @@ +get('/projects/'); + } + + public function show($projectId) + { + return $this->get(sprintf('/projects/%s/', $projectId)); + } + + public function create($name) + { + return $this->post('/projects/', ['name' => $name]); + } + + public function remove($projectId) + { + return $this->delete(sprintf('/projects/%s/', $projectId)); + } + + public function listTeams($projectsId) + { + return $this->get(sprintf('/projects/%s/teams/', $projectsId)); + } + + public function addOrUpdateTeams($projectId, array $teams) + { + foreach ($teams as $team) { + if (!isset($team['id'])) { + throw new InvalidArgumentException('Parameter "id" is required.'); + } + + if (!isset($team['permission'])) { + throw new InvalidArgumentException('Parameter "permission" is required.'); + } + } + + return $this->post(sprintf('/projects/%s/teams/', $projectId), $teams); + } + + public function removeTeam($projectId, $teamId) + { + return $this->delete(sprintf('/projects/%s/teams/%s/', $projectId, $teamId)); + } + + public function listPackages($projectsId) + { + return $this->get(sprintf('/projects/%s/packages/', $projectsId)); + } +} diff --git a/src/Api/Teams.php b/src/Api/Teams.php new file mode 100644 index 0000000..44cd674 --- /dev/null +++ b/src/Api/Teams.php @@ -0,0 +1,11 @@ +get('/teams/'); + } +} diff --git a/src/Client.php b/src/Client.php index eab7967..5e266bd 100644 --- a/src/Client.php +++ b/src/Client.php @@ -48,11 +48,21 @@ public function credentials() return new Api\Credentials($this, $this->responseMediator); } + public function teams() + { + return new Api\Teams($this, $this->responseMediator); + } + public function customers() { return new Api\Customers($this, $this->responseMediator); } + public function projects() + { + return new Api\Projects($this, $this->responseMediator); + } + public function organization() { return new Api\Organization($this, $this->responseMediator); diff --git a/tests/Api/CustomersTest.php b/tests/Api/CustomersTest.php index aa8bf2c..4ef4de7 100644 --- a/tests/Api/CustomersTest.php +++ b/tests/Api/CustomersTest.php @@ -122,7 +122,7 @@ public function testAddOrUpdatePackages() /** * @expectedException \PrivatePackagist\ApiClient\Exception\InvalidArgumentException - * @expectedExceptionMessage Parameter "name" is requried. + * @expectedExceptionMessage Parameter "name" is required. */ public function testAddOrUpdatePackagesMissingName() { diff --git a/tests/Api/ProjectsTest.php b/tests/Api/ProjectsTest.php new file mode 100644 index 0000000..1e0e527 --- /dev/null +++ b/tests/Api/ProjectsTest.php @@ -0,0 +1,177 @@ +getProjectDefinition(), + ]; + + /** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */ + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with($this->equalTo('/projects/')) + ->will($this->returnValue($expected)); + + $this->assertSame($expected, $api->all()); + } + + public function testShow() + { + $expected = $this->getProjectDefinition(); + + $projectId = 1; + + /** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */ + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with($this->equalTo('/projects/1/')) + ->will($this->returnValue($expected)); + + $this->assertSame($expected, $api->show($projectId)); + } + + public function testCreate() + { + $expected = $this->getProjectDefinition(); + + /** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */ + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('post') + ->with($this->equalTo('/projects/'), $this->equalTo(['name' => 'ACME Websites'])) + ->will($this->returnValue($expected)); + + $this->assertSame($expected, $api->create('ACME Websites')); + } + + public function testRemove() + { + $expected = ''; + + /** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */ + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('delete') + ->with($this->equalTo('/projects/1/')) + ->will($this->returnValue($expected)); + + $this->assertSame($expected, $api->remove(1)); + } + + public function testListTeams() + { + $expected = [ + [ + 'id' => 42, + 'name' => 'Owners', + 'permission' => 'owner', + 'members' => [], + 'projects' => [], + ], + ]; + + /** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */ + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with($this->equalTo('/projects/1/teams/')) + ->will($this->returnValue($expected)); + + $this->assertSame($expected, $api->listTeams(1)); + } + + public function testAddOrUpdateTeam() + { + $expected = [ + [ + 'id' => 42, + 'name' => 'Owners', + 'permission' => 'owner', + 'members' => [], + 'projects' => [], + ], + ]; + + $teams = [['id' => 42, 'permission' => 'owner']]; + + /** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */ + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('post') + ->with($this->equalTo('/projects/1/teams/'), $this->equalTo($teams)) + ->will($this->returnValue($expected)); + + $this->assertSame($expected, $api->addOrUpdateTeams(1, $teams)); + } + + public function testRemoveTeam() + { + $expected = ''; + + /** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */ + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('delete') + ->with($this->equalTo('/projects/1/teams/42/')) + ->will($this->returnValue($expected)); + + $this->assertSame($expected, $api->removeTeam(1, 42)); + } + + public function testListPackages() + { + $expected = [ + [ + 'name' => 'composer/composer', + 'origin' => 'private', + 'versionConstraint' => null, + 'expirationDate' => null, + ], + ]; + + /** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */ + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with($this->equalTo('/projects/1/packages/')) + ->will($this->returnValue($expected)); + + $this->assertSame($expected, $api->listPackages(1)); + } + + private function getProjectDefinition() + { + return [ + 'id' => 1, + 'name' => 'ACME Websites', + 'urlName' => 'acme-websites', + 'teams' => [ + [ + 'id' => 1, + 'name' => 'Owners', + 'permission' => 'owner', + 'members' => [ + [ + 'id' => 12, + 'username' => 'username' + ] + ], + ] + ] + ]; + } + + /** + * @return string + */ + protected function getApiClass() + { + return Projects::class; + } +} diff --git a/tests/Api/TeamsTest.php b/tests/Api/TeamsTest.php new file mode 100644 index 0000000..73a8e60 --- /dev/null +++ b/tests/Api/TeamsTest.php @@ -0,0 +1,42 @@ + 1, + 'name' => 'Owners', + 'permission' => 'owner', + 'members' => [ + [ + 'id' => 12, + 'username' => 'username' + ] + ], + 'projects' => [ + ], + ] + ]; + + /** @var Customers&\PHPUnit_Framework_MockObject_MockObject $api */ + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with($this->equalTo('/teams/')) + ->will($this->returnValue($expected)); + + $this->assertSame($expected, $api->all()); + } + + /** + * @return string + */ + protected function getApiClass() + { + return Teams::class; + } +}