Skip to content

Commit

Permalink
Added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
fjgarlin committed Feb 21, 2024
1 parent 0f3cd2c commit 12326ca
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/Api/PersonalAccessTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
*
Expand All @@ -85,19 +85,19 @@ public function show(int $id)
{
return $this->get('personal_access_tokens/'.self::encodePath($id));
}

/**
* @return mixed
*/
public function current()
{
return $this->get('personal_access_tokens/self');
}


/**
* @param int $id
*
*
* @param array $params
*
* @return mixed
Expand All @@ -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
*
Expand All @@ -132,7 +132,7 @@ public function rotateCurrent(array $params = [])
;
return $this->post('personal_access_tokens/self/rotate', $resolver->resolve($params));
}

/**
* @param int $id
*
Expand All @@ -142,7 +142,7 @@ public function remove(int $id)
{
return $this->delete('personal_access_tokens/'.self::encodePath($id));
}

/**
* @return mixed
*/
Expand Down
167 changes: 167 additions & 0 deletions tests/Api/PersonalAccessTokensTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Gitlab API library.
*
* (c) Matt Humphrey <matth@windsor-telecom.co.uk>
* (c) Graham Campbell <hello@gjcampbell.co.uk>
*
* 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());
}

}

0 comments on commit 12326ca

Please sign in to comment.