Skip to content

Commit

Permalink
also made the parameters optional for updateVariable(), refactored …
Browse files Browse the repository at this point in the history
…tests for `addVariable()`, added tests for `updateVariable()`
  • Loading branch information
c33s committed Sep 20, 2017
1 parent 2771dc9 commit cadc605
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 30 deletions.
18 changes: 13 additions & 5 deletions lib/Gitlab/Api/Projects.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,13 +594,21 @@ public function addVariable($project_id, $key, $value, $protected = null, $envir
* @param string $environment_scope
* @return mixed
*/
public function updateVariable($project_id, $key, $value, $protected = false, $environment_scope ="*")
public function updateVariable($project_id, $key, $value, $protected = null, $environment_scope = null)
{
return $this->put($this->getProjectPath($project_id, 'variables/'.$this->encodePath($key)), array(
$payload = array(
'value' => $value,
// 'protected' => $protected,
// 'environment_scope' => $environment_scope,
));
);

if ($protected) {
$payload['protected'] = $protected;
}

if ($environment_scope) {
$payload['environment_scope'] = $environment_scope;
}

return $this->put($this->getProjectPath($project_id, 'variables/'.$this->encodePath($key)), $payload);
}

/**
Expand Down
100 changes: 75 additions & 25 deletions test/Gitlab/Tests/Api/ProjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -870,13 +870,9 @@ public function shouldAddVariable()
*/
public function shouldAddVariableWithProtected()
{
$expectedKey = 'ftp_port';
$expectedValue = '21';
$expectedProtection = true;

$expectedArray = array(
'key' => $expectedKey,
'value' => $expectedValue,
'key' => 'DEPLOY_SERVER',
'value' => 'stage.example.com',
'protected' => true,
);

Expand All @@ -887,23 +883,18 @@ public function shouldAddVariableWithProtected()
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->addVariable(1, $expectedKey, $expectedValue, $expectedProtection));
$this->assertEquals($expectedArray, $api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true));
}

/**
* @test
*/
public function shouldAddVariableWithEnvironment()
{
$expectedKey = 'ftp_port';
$expectedValue = '21';
$expectedProtection = null;
$expectedEnvironment = 'production';

$expectedArray = array(
'key' => $expectedKey,
'value' => $expectedValue,
'environment_scope' => $expectedEnvironment,
'key' => 'DEPLOY_SERVER',
'value' => 'stage.example.com',
'environment_scope' => 'staging',
);

$api = $this->getApiMock();
Expand All @@ -913,24 +904,19 @@ public function shouldAddVariableWithEnvironment()
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->addVariable(1, $expectedKey, $expectedValue, $expectedProtection, $expectedEnvironment));
$this->assertEquals($expectedArray, $api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', null, 'staging'));
}

/**
* @test
*/
public function shouldAddVariableWithProtectionAndEnvironment()
{
$expectedKey = 'ftp_port';
$expectedValue = '21';
$expectedProtection = true;
$expectedEnvironment = 'production';

$expectedArray = array(
'key' => $expectedKey,
'value' => $expectedValue,
'key' => 'DEPLOY_SERVER',
'value' => 'stage.example.com',
'protected' => true,
'environment_scope' => $expectedEnvironment,
'environment_scope' => 'staging',
);

$api = $this->getApiMock();
Expand All @@ -940,7 +926,7 @@ public function shouldAddVariableWithProtectionAndEnvironment()
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->addVariable(1, $expectedKey, $expectedValue, $expectedProtection, $expectedEnvironment));
$this->assertEquals($expectedArray, $api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true, 'staging'));
}

/**
Expand All @@ -966,6 +952,70 @@ public function shouldUpdateVariable()
$this->assertEquals($expectedArray, $api->updateVariable(1, $expectedKey, $expectedValue));
}

/**
* @test
*/
public function shouldUpdateVariableWithProtected()
{
$expectedArray = array(
'key' => 'DEPLOY_SERVER',
'value' => 'stage.example.com',
'protected' => true,
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('put')
->with('projects/1/variables/DEPLOY_SERVER', array('value' => 'stage.example.com', 'protected' => true))
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true));
}

/**
* @test
*/
public function shouldUpdateVariableWithEnvironment()
{
$expectedArray = array(
'key' => 'DEPLOY_SERVER',
'value' => 'stage.example.com',
'environment_scope' => 'staging',
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('put')
->with('projects/1/variables/DEPLOY_SERVER', array('value' => 'stage.example.com', 'environment_scope' => 'staging'))
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', null, 'staging'));
}

/**
* @test
*/
public function shouldUpdateVariableWithProtectedAndEnvironment()
{
$expectedArray = array(
'key' => 'DEPLOY_SERVER',
'value' => 'stage.example.com',
'protected' => true,
'environment_scope' => 'staging',
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('put')
->with('projects/1/variables/DEPLOY_SERVER', array('value' => 'stage.example.com', 'protected' => true, 'environment_scope' => 'staging'))
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true, 'staging'));
}

/**
* @test
*/
Expand Down

0 comments on commit cadc605

Please sign in to comment.