Update the code to use same statuses as the rest of the application

This commit is contained in:
Robert Boloc 2015-10-14 21:21:04 +01:00
parent fdefd909d1
commit 14f23eb21a
2 changed files with 39 additions and 47 deletions

View file

@ -50,7 +50,7 @@ class BuildStatusController extends \PHPCI\Controller
$branch = $this->getParam('branch', 'master');
try {
$project = $this->projectStore->getById($projectId);
$status = 'unknown';
$status = Build::STATUS_NEW;
if (!$project->getAllowPublicStatus()) {
return null;
@ -60,24 +60,11 @@ class BuildStatusController extends \PHPCI\Controller
$build = $project->getLatestBuild($branch);
if (isset($build) && $build instanceof Build) {
switch ($build->getStatus()) {
case Build::STATUS_NEW:
$status = 'unknown';
break;
case Build::STATUS_RUNNING:
$status = 'running';
break;
case Build::STATUS_FAILED:
$status = 'failed';
break;
case Build::STATUS_SUCCESS:
$status = 'passing';
break;
}
return $build->getStatus();
}
}
} catch (\Exception $e) {
$status = 'error';
$status = Build::STATUS_FAILED;
}
return $status;
@ -155,11 +142,13 @@ class BuildStatusController extends \PHPCI\Controller
return $response;
}
list($text, $color) = $this->getImageTextAndColorFromStatus($status);
$image = file_get_contents(sprintf(
'http://img.shields.io/badge/%s-%s-%s.svg?style=%s',
$label,
$status,
$this->getImageColorFromStatus($status),
$text,
$color,
$style
));
@ -216,25 +205,23 @@ class BuildStatusController extends \PHPCI\Controller
}
/**
* Return the color of the right side of the status badge, as a string,
* based on the status type.
* Return the image text and color based on the build
* status.
*
* @param string $status
* @return string
* @param $status
* @return array
*/
protected function getImageColorFromStatus($status)
protected function getImageTextAndColorFromStatus($status)
{
switch ($status) {
case 'passing':
return 'green';
case 'running':
return 'orange';
case 'failed':
case 'error':
return 'red';
case 'unknown':
default:
return 'lightgrey';
case Build::STATUS_NEW:
return array('pending', 'blue');
case Build::STATUS_RUNNING:
return array('running', 'yellow');
case Build::STATUS_SUCCESS:
return array('success', 'green');
case Build::STATUS_FAILED:
return array('failed', 'red');
}
}
}

View file

@ -10,16 +10,20 @@
namespace Tests\PHPCI\Controller;
use PHPCI\Model\Build;
class BuildStatusControllerTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider getImageColorFromStatusDataProvider
* @dataProvider getImageTextAndColorFromStatusDataProvider
*
* @param string $status
* @param string $expectedText
* @param string $expectedColor
*/
public function testGetImageColorFromStatusReturnsTheCorrectColorForEachStatus(
public function testGetImageTextAndColorFromStatusReturnsTheCorrectTextAndColorForEachStatus(
$status,
$expectedText,
$expectedColor
) {
$buildStatusControllerMock =
@ -32,24 +36,25 @@ class BuildStatusControllerTest extends \PHPUnit_Framework_TestCase
new \ReflectionClass('PHPCI\Controller\BuildStatusController');
$getImageColorFromStatusReflection =
$buildStatusControllerReflection->getMethod('getImageColorFromStatus');
$buildStatusControllerReflection->getMethod('getImageTextAndColorFromStatus');
$getImageColorFromStatusReflection->setAccessible(true);
$this->assertEquals(
$expectedColor,
$getImageColorFromStatusReflection->invoke($buildStatusControllerMock, $status)
);
list($text, $color) = $getImageColorFromStatusReflection->invoke($buildStatusControllerMock, $status);
$this->assertEquals($expectedText, $text);
$this->assertEquals($expectedColor, $color);
}
public function getImageColorFromStatusDataProvider()
/**
* @return array
*/
public function getImageTextAndColorFromStatusDataProvider()
{
return array(
array('passing', 'green'),
array('running', 'orange'),
array('failed', 'red'),
array('error', 'red'),
array('unknown', 'lightgrey'),
array('test the default case', 'lightgrey'),
array(Build::STATUS_NEW, 'pending', 'blue'),
array(Build::STATUS_RUNNING, 'running', 'yellow'),
array(Build::STATUS_SUCCESS, 'success', 'green'),
array(Build::STATUS_FAILED, 'failed', 'red'),
);
}
}