Skip to content

Commit 2771dc9

Browse files
committed
refactored add and updateVariable code and added tests
1 parent 7bbfbd2 commit 2771dc9

File tree

2 files changed

+93
-7
lines changed

2 files changed

+93
-7
lines changed

lib/Gitlab/Api/Projects.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -568,14 +568,22 @@ public function variable($project_id, $key)
568568
* @param string $environment_scope
569569
* @return mixed
570570
*/
571-
public function addVariable($project_id, $key, $value, $protected = false, $environment_scope ="*")
571+
public function addVariable($project_id, $key, $value, $protected = null, $environment_scope = null)
572572
{
573-
return $this->post($this->getProjectPath($project_id, 'variables'), array(
573+
$payload = array(
574574
'key' => $key,
575575
'value' => $value,
576-
'protected' => $protected,
577-
'environment_scope' => $environment_scope,
578-
));
576+
);
577+
578+
if ($protected) {
579+
$payload['protected'] = $protected;
580+
}
581+
582+
if ($environment_scope) {
583+
$payload['environment_scope'] = $environment_scope;
584+
}
585+
586+
return $this->post($this->getProjectPath($project_id, 'variables'), $payload);
579587
}
580588

581589
/**
@@ -590,8 +598,8 @@ public function updateVariable($project_id, $key, $value, $protected = false, $e
590598
{
591599
return $this->put($this->getProjectPath($project_id, 'variables/'.$this->encodePath($key)), array(
592600
'value' => $value,
593-
'protected' => $protected,
594-
'environment_scope' => $environment_scope,
601+
// 'protected' => $protected,
602+
// 'environment_scope' => $environment_scope,
595603
));
596604
}
597605

test/Gitlab/Tests/Api/ProjectsTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,84 @@ public function shouldAddVariable()
865865
$this->assertEquals($expectedArray, $api->addVariable(1, $expectedKey, $expectedValue));
866866
}
867867

868+
/**
869+
* @test
870+
*/
871+
public function shouldAddVariableWithProtected()
872+
{
873+
$expectedKey = 'ftp_port';
874+
$expectedValue = '21';
875+
$expectedProtection = true;
876+
877+
$expectedArray = array(
878+
'key' => $expectedKey,
879+
'value' => $expectedValue,
880+
'protected' => true,
881+
);
882+
883+
$api = $this->getApiMock();
884+
$api->expects($this->once())
885+
->method('post')
886+
->with('projects/1/variables', $expectedArray)
887+
->will($this->returnValue($expectedArray))
888+
;
889+
890+
$this->assertEquals($expectedArray, $api->addVariable(1, $expectedKey, $expectedValue, $expectedProtection));
891+
}
892+
893+
/**
894+
* @test
895+
*/
896+
public function shouldAddVariableWithEnvironment()
897+
{
898+
$expectedKey = 'ftp_port';
899+
$expectedValue = '21';
900+
$expectedProtection = null;
901+
$expectedEnvironment = 'production';
902+
903+
$expectedArray = array(
904+
'key' => $expectedKey,
905+
'value' => $expectedValue,
906+
'environment_scope' => $expectedEnvironment,
907+
);
908+
909+
$api = $this->getApiMock();
910+
$api->expects($this->once())
911+
->method('post')
912+
->with('projects/1/variables', $expectedArray)
913+
->will($this->returnValue($expectedArray))
914+
;
915+
916+
$this->assertEquals($expectedArray, $api->addVariable(1, $expectedKey, $expectedValue, $expectedProtection, $expectedEnvironment));
917+
}
918+
919+
/**
920+
* @test
921+
*/
922+
public function shouldAddVariableWithProtectionAndEnvironment()
923+
{
924+
$expectedKey = 'ftp_port';
925+
$expectedValue = '21';
926+
$expectedProtection = true;
927+
$expectedEnvironment = 'production';
928+
929+
$expectedArray = array(
930+
'key' => $expectedKey,
931+
'value' => $expectedValue,
932+
'protected' => true,
933+
'environment_scope' => $expectedEnvironment,
934+
);
935+
936+
$api = $this->getApiMock();
937+
$api->expects($this->once())
938+
->method('post')
939+
->with('projects/1/variables', $expectedArray)
940+
->will($this->returnValue($expectedArray))
941+
;
942+
943+
$this->assertEquals($expectedArray, $api->addVariable(1, $expectedKey, $expectedValue, $expectedProtection, $expectedEnvironment));
944+
}
945+
868946
/**
869947
* @test
870948
*/

0 commit comments

Comments
 (0)