diff --git a/README.md b/README.md index 48ff7a3..b66ac8f 100644 --- a/README.md +++ b/README.md @@ -125,8 +125,8 @@ Returns an array of projects. ##### Show a project ```php -$projectId = 42; -$project = $client->projects()->show($projectId); +$projectName = 'project'; +$project = $client->projects()->show($projectName); ``` Returns a single project. @@ -138,45 +138,81 @@ Returns the project. ##### Delete a project ```php -$projectId = 42; -$client->projects()->remove($projectId); +$projectName = 'project'; +$client->projects()->remove($projectName); ``` ##### List a projects's teams ```php -$projectId = 42; -$teams = $client->projects()->listTeams($projectId); +$projectName = 'project'; +$teams = $client->projects()->listTeams($projectName); ``` Returns an array of projects teams. ##### Add a team to a project or update the permission ```php -$projectId = 42; +$projectName = 'project'; $teams = [ [ 'id' => 12, 'permission' => 'owner', ], ]; -$teams = $client->customers()->addOrUpdateTeams($projectId, $teams); +$teams = $client->customers()->addOrUpdateTeams($projectName, $teams); ``` Returns an array of added project teams. ##### Remove a team from a customer ```php -$projectId = 42; +$projectName = 'project'; $teamId = 12; -$client->customers()->removeTeam($projectId, $teamId); +$client->customers()->removeTeam($projectName, $teamId); ``` ##### List a projects's packages ```php -$projectId = 42; -$packages = $client->projects()->listPackages($projectId); +$projectName = 'project'; +$packages = $client->projects()->listPackages($projectName); ``` Returns an array of projects packages. +##### List a projects's authentication tokens +```php +$projectName = 'project'; +$tokens = $client->projects()->listTokens($projectName); +``` +Returns an array of authentication tokens. + +##### Create a project authentication token +```php +$projectName = 'project'; +$data = [ + 'description' => 'Project Token', + 'access' => 'read', +]; +$token = $client->projects()->createToken($projectName, $data); +``` +Returns the authentication token. + +##### Delete a project authentication token +```php +$projectName = 'project'; +$tokenId = 33; +$client->projects()->removeToken($projectName, $tokenId); +``` + +##### Regenerate a project authentication token +```php +$projectName = 'project'; +$tokenId = 33; +$confirmation = [ + 'IConfirmOldTokenWillStopWorkingImmediately' => true, +]; +$token = $client->projects()->regenerateToken($projectName, $confirmation); +``` +Returns the authentication token. + #### Package ##### List an organization's packages diff --git a/src/Api/Projects.php b/src/Api/Projects.php index 663f775..c4240c2 100644 --- a/src/Api/Projects.php +++ b/src/Api/Projects.php @@ -11,9 +11,9 @@ public function all() return $this->get('/projects/'); } - public function show($projectId) + public function show($projectName) { - return $this->get(sprintf('/projects/%s/', $projectId)); + return $this->get(sprintf('/projects/%s/', $projectName)); } public function create($name) @@ -21,17 +21,17 @@ public function create($name) return $this->post('/projects/', ['name' => $name]); } - public function remove($projectId) + public function remove($projectName) { - return $this->delete(sprintf('/projects/%s/', $projectId)); + return $this->delete(sprintf('/projects/%s/', $projectName)); } - public function listTeams($projectsId) + public function listTeams($projectsName) { - return $this->get(sprintf('/projects/%s/teams/', $projectsId)); + return $this->get(sprintf('/projects/%s/teams/', $projectsName)); } - public function addOrUpdateTeams($projectId, array $teams) + public function addOrUpdateTeams($projectName, array $teams) { foreach ($teams as $team) { if (!isset($team['id'])) { @@ -43,16 +43,40 @@ public function addOrUpdateTeams($projectId, array $teams) } } - return $this->post(sprintf('/projects/%s/teams/', $projectId), $teams); + return $this->post(sprintf('/projects/%s/teams/', $projectName), $teams); } - public function removeTeam($projectId, $teamId) + public function removeTeam($projectName, $teamId) { - return $this->delete(sprintf('/projects/%s/teams/%s/', $projectId, $teamId)); + return $this->delete(sprintf('/projects/%s/teams/%s/', $projectName, $teamId)); } - public function listPackages($projectsId) + public function listPackages($projectName) { - return $this->get(sprintf('/projects/%s/packages/', $projectsId)); + return $this->get(sprintf('/projects/%s/packages/', $projectName)); + } + + public function listTokens($projectName) + { + return $this->get(sprintf('/projects/%s/tokens/', $projectName)); + } + + public function createToken($projectName, array $tokenData) + { + return $this->post(sprintf('/projects/%s/tokens/', $projectName), $tokenData); + } + + public function removeToken($projectName, $tokenId) + { + return $this->delete(sprintf('/projects/%s/tokens/%s/', $projectName, $tokenId)); + } + + public function regenerateToken($projectName, $tokenId, array $confirmation) + { + if (!isset($confirmation['IConfirmOldTokenWillStopWorkingImmediately'])) { + throw new InvalidArgumentException('Confirmation is required to regenerate the Composer repository token.'); + } + + return $this->post(sprintf('/projects/%s/tokens/%s/regenerate', $projectName, $tokenId), $confirmation); } } diff --git a/tests/Api/ProjectsTest.php b/tests/Api/ProjectsTest.php index 1e0e527..d8fef03 100644 --- a/tests/Api/ProjectsTest.php +++ b/tests/Api/ProjectsTest.php @@ -24,16 +24,16 @@ public function testShow() { $expected = $this->getProjectDefinition(); - $projectId = 1; + $projectName = 'project'; /** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */ $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with($this->equalTo('/projects/1/')) + ->with($this->equalTo('/projects/project/')) ->will($this->returnValue($expected)); - $this->assertSame($expected, $api->show($projectId)); + $this->assertSame($expected, $api->show($projectName)); } public function testCreate() @@ -58,10 +58,10 @@ public function testRemove() $api = $this->getApiMock(); $api->expects($this->once()) ->method('delete') - ->with($this->equalTo('/projects/1/')) + ->with($this->equalTo('/projects/project/')) ->will($this->returnValue($expected)); - $this->assertSame($expected, $api->remove(1)); + $this->assertSame($expected, $api->remove('project')); } public function testListTeams() @@ -80,10 +80,10 @@ public function testListTeams() $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with($this->equalTo('/projects/1/teams/')) + ->with($this->equalTo('/projects/project/teams/')) ->will($this->returnValue($expected)); - $this->assertSame($expected, $api->listTeams(1)); + $this->assertSame($expected, $api->listTeams('project')); } public function testAddOrUpdateTeam() @@ -104,10 +104,10 @@ public function testAddOrUpdateTeam() $api = $this->getApiMock(); $api->expects($this->once()) ->method('post') - ->with($this->equalTo('/projects/1/teams/'), $this->equalTo($teams)) + ->with($this->equalTo('/projects/project/teams/'), $this->equalTo($teams)) ->will($this->returnValue($expected)); - $this->assertSame($expected, $api->addOrUpdateTeams(1, $teams)); + $this->assertSame($expected, $api->addOrUpdateTeams('project', $teams)); } public function testRemoveTeam() @@ -118,10 +118,10 @@ public function testRemoveTeam() $api = $this->getApiMock(); $api->expects($this->once()) ->method('delete') - ->with($this->equalTo('/projects/1/teams/42/')) + ->with($this->equalTo('/projects/project/teams/42/')) ->will($this->returnValue($expected)); - $this->assertSame($expected, $api->removeTeam(1, 42)); + $this->assertSame($expected, $api->removeTeam('project', 42)); } public function testListPackages() @@ -139,10 +139,88 @@ public function testListPackages() $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with($this->equalTo('/projects/1/packages/')) + ->with($this->equalTo('/projects/project/packages/')) ->will($this->returnValue($expected)); - $this->assertSame($expected, $api->listPackages(1)); + $this->assertSame($expected, $api->listPackages('project')); + } + + public function testListTokens() + { + $expected = [ + [ + 'description' => 'Generated Client Token', + 'access' => 'read', + 'url' => 'https://vendor-org.repo.packagist.com/acme-websites/', + 'user' => 'token', + 'token' => 'password', + 'lastUsed' => '2018-03-14T11:36:00+00:00' + ], + ]; + + /** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */ + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with($this->equalTo('/projects/project/tokens/')) + ->will($this->returnValue($expected)); + + $this->assertSame($expected, $api->listTokens('project')); + } + + public function testCreateToken() + { + $expected = [ + 'description' => 'Project Token', + 'access' => 'read', + 'url' => 'https://vendor-org.repo.packagist.com/acme-websites/', + 'user' => 'token', + 'token' => 'password', + 'lastUsed' => '2018-03-14T11:36:00+00:00' + ]; + + /** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */ + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('post') + ->with($this->equalTo('/projects/project/tokens/'), $this->equalTo([ + 'description' => 'Project Token', + 'access' => 'read', + ])) + ->will($this->returnValue($expected)); + + $this->assertSame($expected, $api->createToken('project', [ + 'description' => 'Project Token', + 'access' => 'read', + ])); + } + + public function testRemoveToken() + { + $expected = []; + + /** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */ + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('delete') + ->with($this->equalTo('/projects/project/tokens/1/')) + ->will($this->returnValue($expected)); + + $this->assertSame($expected, $api->removeToken('project', 1)); + } + + public function testRegenerateToken() + { + $expected = []; + + /** @var Projects&\PHPUnit_Framework_MockObject_MockObject $api */ + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('post') + ->with($this->equalTo('/projects/project/tokens/1/regenerate'), $this->equalTo(['IConfirmOldTokenWillStopWorkingImmediately' => true])) + ->will($this->returnValue($expected)); + + $this->assertSame($expected, $api->regenerateToken('project', 1, ['IConfirmOldTokenWillStopWorkingImmediately' => true])); } private function getProjectDefinition()