Skip to content

Commit cadc605

Browse files
committed
also made the parameters optional for updateVariable(), refactored tests for addVariable(), added tests for updateVariable()
1 parent 2771dc9 commit cadc605

File tree

2 files changed

+88
-30
lines changed

2 files changed

+88
-30
lines changed

lib/Gitlab/Api/Projects.php

+13-5
Original file line numberDiff line numberDiff line change
@@ -594,13 +594,21 @@ public function addVariable($project_id, $key, $value, $protected = null, $envir
594594
* @param string $environment_scope
595595
* @return mixed
596596
*/
597-
public function updateVariable($project_id, $key, $value, $protected = false, $environment_scope ="*")
597+
public function updateVariable($project_id, $key, $value, $protected = null, $environment_scope = null)
598598
{
599-
return $this->put($this->getProjectPath($project_id, 'variables/'.$this->encodePath($key)), array(
599+
$payload = array(
600600
'value' => $value,
601-
// 'protected' => $protected,
602-
// 'environment_scope' => $environment_scope,
603-
));
601+
);
602+
603+
if ($protected) {
604+
$payload['protected'] = $protected;
605+
}
606+
607+
if ($environment_scope) {
608+
$payload['environment_scope'] = $environment_scope;
609+
}
610+
611+
return $this->put($this->getProjectPath($project_id, 'variables/'.$this->encodePath($key)), $payload);
604612
}
605613

606614
/**

test/Gitlab/Tests/Api/ProjectsTest.php

+75-25
Original file line numberDiff line numberDiff line change
@@ -870,13 +870,9 @@ public function shouldAddVariable()
870870
*/
871871
public function shouldAddVariableWithProtected()
872872
{
873-
$expectedKey = 'ftp_port';
874-
$expectedValue = '21';
875-
$expectedProtection = true;
876-
877873
$expectedArray = array(
878-
'key' => $expectedKey,
879-
'value' => $expectedValue,
874+
'key' => 'DEPLOY_SERVER',
875+
'value' => 'stage.example.com',
880876
'protected' => true,
881877
);
882878

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

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

893889
/**
894890
* @test
895891
*/
896892
public function shouldAddVariableWithEnvironment()
897893
{
898-
$expectedKey = 'ftp_port';
899-
$expectedValue = '21';
900-
$expectedProtection = null;
901-
$expectedEnvironment = 'production';
902-
903894
$expectedArray = array(
904-
'key' => $expectedKey,
905-
'value' => $expectedValue,
906-
'environment_scope' => $expectedEnvironment,
895+
'key' => 'DEPLOY_SERVER',
896+
'value' => 'stage.example.com',
897+
'environment_scope' => 'staging',
907898
);
908899

909900
$api = $this->getApiMock();
@@ -913,24 +904,19 @@ public function shouldAddVariableWithEnvironment()
913904
->will($this->returnValue($expectedArray))
914905
;
915906

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

919910
/**
920911
* @test
921912
*/
922913
public function shouldAddVariableWithProtectionAndEnvironment()
923914
{
924-
$expectedKey = 'ftp_port';
925-
$expectedValue = '21';
926-
$expectedProtection = true;
927-
$expectedEnvironment = 'production';
928-
929915
$expectedArray = array(
930-
'key' => $expectedKey,
931-
'value' => $expectedValue,
916+
'key' => 'DEPLOY_SERVER',
917+
'value' => 'stage.example.com',
932918
'protected' => true,
933-
'environment_scope' => $expectedEnvironment,
919+
'environment_scope' => 'staging',
934920
);
935921

936922
$api = $this->getApiMock();
@@ -940,7 +926,7 @@ public function shouldAddVariableWithProtectionAndEnvironment()
940926
->will($this->returnValue($expectedArray))
941927
;
942928

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

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

955+
/**
956+
* @test
957+
*/
958+
public function shouldUpdateVariableWithProtected()
959+
{
960+
$expectedArray = array(
961+
'key' => 'DEPLOY_SERVER',
962+
'value' => 'stage.example.com',
963+
'protected' => true,
964+
);
965+
966+
$api = $this->getApiMock();
967+
$api->expects($this->once())
968+
->method('put')
969+
->with('projects/1/variables/DEPLOY_SERVER', array('value' => 'stage.example.com', 'protected' => true))
970+
->will($this->returnValue($expectedArray))
971+
;
972+
973+
$this->assertEquals($expectedArray, $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true));
974+
}
975+
976+
/**
977+
* @test
978+
*/
979+
public function shouldUpdateVariableWithEnvironment()
980+
{
981+
$expectedArray = array(
982+
'key' => 'DEPLOY_SERVER',
983+
'value' => 'stage.example.com',
984+
'environment_scope' => 'staging',
985+
);
986+
987+
$api = $this->getApiMock();
988+
$api->expects($this->once())
989+
->method('put')
990+
->with('projects/1/variables/DEPLOY_SERVER', array('value' => 'stage.example.com', 'environment_scope' => 'staging'))
991+
->will($this->returnValue($expectedArray))
992+
;
993+
994+
$this->assertEquals($expectedArray, $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', null, 'staging'));
995+
}
996+
997+
/**
998+
* @test
999+
*/
1000+
public function shouldUpdateVariableWithProtectedAndEnvironment()
1001+
{
1002+
$expectedArray = array(
1003+
'key' => 'DEPLOY_SERVER',
1004+
'value' => 'stage.example.com',
1005+
'protected' => true,
1006+
'environment_scope' => 'staging',
1007+
);
1008+
1009+
$api = $this->getApiMock();
1010+
$api->expects($this->once())
1011+
->method('put')
1012+
->with('projects/1/variables/DEPLOY_SERVER', array('value' => 'stage.example.com', 'protected' => true, 'environment_scope' => 'staging'))
1013+
->will($this->returnValue($expectedArray))
1014+
;
1015+
1016+
$this->assertEquals($expectedArray, $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true, 'staging'));
1017+
}
1018+
9691019
/**
9701020
* @test
9711021
*/

0 commit comments

Comments
 (0)