Added BuildStatus::ccxml tests.

This commit is contained in:
Marco Vito Moscaritolo 2015-05-31 16:32:50 +02:00
parent 180bdcf4fe
commit e977360083
2 changed files with 82 additions and 9 deletions

View file

@ -11,11 +11,13 @@ namespace PHPCI\Controller;
use b8;
use b8\Exception\HttpException\NotFoundException;
use b8\Exception\HttpException\NotAuthorizedException;
use b8\Store;
use b8\Http\Request;
use b8\Http\Response;
use PHPCI\Config;
use PHPCI\BuildFactory;
use PHPCI\Helper\Lang;
use PHPCI\Model\Project;
use PHPCI\Model\Build;
use PHPCI\Service\BuildStatusService;
@ -50,8 +52,6 @@ class BuildStatusController extends \PHPCI\Controller
{
parent::__construct($config, $request, $response);
$this->response->disableLayout();
$this->buildStore = $buildStore;
$this->projectStore = $projectStore;
}
@ -98,12 +98,16 @@ class BuildStatusController extends \PHPCI\Controller
{
/* @var Project $project */
$project = $this->projectStore->getById($projectId);
$xml = new \SimpleXMLElement('<Projects/>');
if (!$project instanceof Project || !$project->getAllowPublicStatus()) {
return $this->renderXml($xml);
if (!$project instanceof Project) {
throw new NotFoundException(Lang::get('project_x_not_found', $projectId));
}
if (!$project->getAllowPublicStatus()) {
throw new NotAuthorizedException();
}
$xml = new \SimpleXMLElement('<Projects/>');
try {
$branchList = $this->buildStore->getBuildBranches($projectId);
@ -120,7 +124,6 @@ class BuildStatusController extends \PHPCI\Controller
}
}
}
} catch (\Exception $e) {
$xml = new \SimpleXMLElement('<projects/>');
}
@ -134,12 +137,11 @@ class BuildStatusController extends \PHPCI\Controller
*/
protected function renderXml(\SimpleXMLElement $xml = null)
{
$this->response->disableLayout();
$this->response->setHeader('Content-Type', 'text/xml');
$this->response->setContent($xml->asXML());
$this->response->flush();
echo $xml->asXML();
return true;
return $this->response;
}
/**
@ -170,6 +172,7 @@ class BuildStatusController extends \PHPCI\Controller
$this->response->disableLayout();
$this->response->setHeader('Content-Type', 'image/svg+xml');
$this->response->setContent($image);
return $this->response;
}

View file

@ -0,0 +1,70 @@
<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2014, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link https://www.phptesting.org/
*/
namespace Tests\PHPCI\Controller;
use PHPCI\Controller\BuildStatusController;
use PHPCI\Model\Project;
use b8\Exception\HttpException\NotAuthorizedException;
use Prophecy\Argument;
class BuildStatusControllerTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \b8\Exception\HttpException\NotAuthorizedException
*/
public function test_ccxml_hidden_for_non_public_project()
{
$buildStore = $this->prophesize('PHPCI\Store\BuildStore');
$projectStore = $this->prophesize('PHPCI\Store\ProjectStore');
$project = new Project();
$project->setAllowPublicStatus(false);
$projectStore->getById(Argument::any())->willReturn($project);
$webController = new BuildStatusController(
$this->prophesize('PHPCI\Config')->reveal(),
$this->prophesize('b8\Http\Request')->reveal(),
new \b8\Http\Response(),
$buildStore->reveal(),
$projectStore->reveal()
);
$result = $webController->handleAction('ccxml', [1]);
}
public function test_ccxml_visible_for_public_project()
{
$buildStore = $this->prophesize('PHPCI\Store\BuildStore');
$projectStore = $this->prophesize('PHPCI\Store\ProjectStore');
$project = new Project();
$project->setId(1);
$project->setBranch('test');
$project->setAllowPublicStatus(true);
$projectStore->getById(1)->willReturn($project);
$webController = new BuildStatusController(
$this->prophesize('PHPCI\Config')->reveal(),
$this->prophesize('b8\Http\Request')->reveal(),
new \b8\Http\Response(),
$buildStore->reveal(),
$projectStore->reveal()
);
$result = $webController->handleAction('ccxml', [1]);
$this->assertInstanceOf('b8\Http\Response', $result);
$responseData = $result->getData();
$this->assertEquals('text/xml', $responseData['headers']['Content-Type']);
$this->assertXmlStringEqualsXmlString('<Projects/>', $responseData['body']);
}
}