diff --git a/src/Api/PersonalAccessTokens.php b/src/Api/PersonalAccessTokens.php index 82490e43..d42cbaac 100644 --- a/src/Api/PersonalAccessTokens.php +++ b/src/Api/PersonalAccessTokens.php @@ -42,10 +42,10 @@ public function all(array $parameters = []) $booleanNormalizer = function (Options $resolver, $value): string { return $value ? 'true' : 'false'; }; - + $resolver->setDefined('search'); $resolver->setDefined('state') - ->setAllowedValues('state', ['active', 'inactive']); + ->setAllowedValues('state', ['all', 'active', 'inactive']); $resolver->setDefined('user_id') ->setAllowedTypes('user_id', 'int') ->setAllowedValues('user_id', function ($value): bool { @@ -72,10 +72,10 @@ public function all(array $parameters = []) ->setAllowedTypes('revoked', 'bool') ->setNormalizer('revoked', $booleanNormalizer); ; - + return $this->get('personal_access_tokens', $resolver->resolve($parameters)); } - + /** * @param int $id * @@ -85,7 +85,7 @@ public function show(int $id) { return $this->get('personal_access_tokens/'.self::encodePath($id)); } - + /** * @return mixed */ @@ -93,11 +93,11 @@ public function current() { return $this->get('personal_access_tokens/self'); } - - + + /** * @param int $id - * + * * @param array $params * * @return mixed @@ -114,7 +114,7 @@ public function rotate(int $id, array $params = []) ; return $this->post('personal_access_tokens/'.self::encodePath($id).'/rotate', $resolver->resolve($params)); } - + /** * @param array $params * @@ -132,7 +132,7 @@ public function rotateCurrent(array $params = []) ; return $this->post('personal_access_tokens/self/rotate', $resolver->resolve($params)); } - + /** * @param int $id * @@ -142,7 +142,7 @@ public function remove(int $id) { return $this->delete('personal_access_tokens/'.self::encodePath($id)); } - + /** * @return mixed */ diff --git a/tests/Api/PersonalAccessTokensTest.php b/tests/Api/PersonalAccessTokensTest.php new file mode 100644 index 00000000..b3bde36b --- /dev/null +++ b/tests/Api/PersonalAccessTokensTest.php @@ -0,0 +1,167 @@ + + * (c) Graham Campbell + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Gitlab\Tests\Api; + +use Gitlab\Api\PersonalAccessTokens; + +class PersonalAccessTokensTest extends TestCase +{ + protected function getApiClass() + { + return PersonalAccessTokens::class; + } + + /** + * @test + */ + public function shouldGetAllTokens(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'Token 1', 'state' => 'active'], + ['id' => 2, 'name' => 'Token 2', 'state' => 'revoked'], + ]; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('personal_access_tokens', []) + ->will($this->returnValue($expectedArray)) + ; + + $this->assertEquals($expectedArray, $api->all()); + } + + /** + * @test + */ + public function shouldGetActiveTokens(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'Token 1', 'state' => 'active'], + ]; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('personal_access_tokens', ['state' => 'active']) + ->will($this->returnValue($expectedArray)) + ; + + $this->assertEquals($expectedArray, $api->all(['state' => 'active'])); + } + + /** + * @test + */ + public function shouldShowToken(): void + { + $expectedArray = ['id' => 1, 'name' => 'Token 1', 'state' => 'active']; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('personal_access_tokens/1') + ->will($this->returnValue($expectedArray)) + ; + + $this->assertEquals($expectedArray, $api->show(1)); + } + + /** + * @test + */ + public function shouldShowCurrent(): void + { + $expectedArray = ['id' => 1, 'name' => 'Token 1', 'state' => 'active']; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('personal_access_tokens/self') + ->will($this->returnValue($expectedArray)) + ; + + $this->assertEquals($expectedArray, $api->current()); + } + + /** + * @test + */ + public function shouldRotate(): void + { + $expectedArray = ['id' => 4, 'name' => 'Token 4']; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('post') + ->with('personal_access_tokens/3/rotate') + ->will($this->returnValue($expectedArray)) + ; + + $this->assertEquals($expectedArray, $api->rotate(3)); + } + + /** + * @test + */ + public function shouldRotateCurrent(): void + { + $expectedArray = ['id' => 4, 'name' => 'Token 4']; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('post') + ->with('personal_access_tokens/self/rotate') + ->will($this->returnValue($expectedArray)) + ; + + $this->assertEquals($expectedArray, $api->rotateCurrent()); + } + + /** + * @test + */ + public function shouldRemoveToken(): void + { + $expectedBool = true; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('delete') + ->with('personal_access_tokens/1') + ->will($this->returnValue($expectedBool)) + ; + + $this->assertEquals($expectedBool, $api->remove(1)); + } + + /** + * @test + */ + public function shouldRemoveCurrentToken(): void + { + $expectedBool = true; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('delete') + ->with('personal_access_tokens/self') + ->will($this->returnValue($expectedBool)) + ; + + $this->assertEquals($expectedBool, $api->removeCurrent()); + } + +}