More work on ProjectService and its tests

This commit is contained in:
Dan Cryer 2014-07-14 14:37:51 +01:00
parent af4cdd90b6
commit 0a0c911356
4 changed files with 64 additions and 33 deletions

View file

@ -119,7 +119,7 @@ class ProjectController extends \PHPCI\Controller
}
$project = $this->projectStore->getById($projectId);
$this->projectStore->delete($project);
$this->projectService->delete($project);
header('Location: '.PHPCI_URL);
exit;
@ -197,7 +197,7 @@ class ProjectController extends \PHPCI\Controller
'allow_public_status' => $this->getParam('allow_public_status', 0),
);
$project = $this->projectService->createProject($title, $reference, $type, $options);
$project = $this->projectService->createProject($title, $type, $reference, $options);
header('Location: '.PHPCI_URL.'project/view/' . $project->getId());
die;
}
@ -247,21 +247,18 @@ class ProjectController extends \PHPCI\Controller
return $view->render();
}
$values = $form->getValues();
$values['ssh_private_key'] = $values['key'];
$values['ssh_public_key'] = $values['pubkey'];
$title = $this->getParam('title', 'New Project');
$reference = $this->getParam('reference', null);
$type = $this->getParam('type', null);
if ($values['type'] == "gitlab") {
preg_match('`^(.*)@(.*):(.*)/(.*)\.git`', $values['reference'], $matches);
$info = array();
$info["user"] = $matches[1];
$info["domain"] = $matches[2];
$values['access_information'] = serialize($info);
$values['reference'] = $matches[3] . "/" . $matches[4];
}
$options = array(
'ssh_private_key' => $this->getParam('key', null),
'ssh_public_key' => $this->getParam('pubkey', null),
'build_config' => $this->getParam('build_config', null),
'allow_public_status' => $this->getParam('allow_public_status', 0),
);
$project->setValues($values);
$project = $this->projectStore->save($project);
$project = $this->projectService->updateProject($project, $title, $type, $reference, $options);
header('Location: '.PHPCI_URL.'project/view/' . $project->getId());
die;
@ -332,7 +329,7 @@ class ProjectController extends \PHPCI\Controller
$field = Form\Element\Checkbox::create('allow_public_status', $label, false);
$field->setContainerClass('form-group');
$field->setCheckedValue(1);
$field->setValue(1);
$field->setValue(0);
$form->addField($field);
$field = new Form\Element\Submit();

View file

@ -57,6 +57,7 @@ class ProjectService
$project->setTitle($title);
$project->setType($type);
$project->setReference($reference);
$project->setAllowPublicStatus(0);
// Handle extra project options:
if (array_key_exists('ssh_private_key', $options)) {
@ -82,6 +83,16 @@ class ProjectService
return $this->projectStore->save($project);
}
/**
* Delete a given project.
* @param Project $project
* @return bool
*/
public function deleteProject(Project $project)
{
return $this->projectStore->delete($project);
}
/**
* In circumstances where it is necessary, populate access information based on other project properties.
* @see ProjectService::createProject()

View file

@ -1,15 +0,0 @@
<?php
namespace Tests\PHPCI\Service;
use b8\Model;
use PHPCI\Store\ProjectStore;
class MockProjectStore extends ProjectStore
{
public function save(Model $project)
{
$project->setId(1);
return $project;
}
}

View file

@ -11,7 +11,6 @@ namespace PHPCI\Service\Tests;
use PHPCI\Model\Project;
use PHPCI\Service\ProjectService;
use Tests\PHPCI\Service\MockProjectStore;
/**
* Unit tests for the ProjectService class.
@ -32,7 +31,11 @@ class ProjectServiceTest extends \PHPUnit_Framework_TestCase
public function setUp()
{
$this->mockProjectStore = new MockProjectStore();
$this->mockProjectStore = $this->getMock('PHPCI\Store\ProjectStore');
$this->mockProjectStore->expects($this->any())
->method('save')
->will($this->returnArgument(0));
$this->testedService = new ProjectService($this->mockProjectStore);
}
@ -98,4 +101,39 @@ class ProjectServiceTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('After Reference', $returnValue->getReference());
$this->assertEquals('bitbucket', $returnValue->getType());
}
/**
* @covers PHPUnit::execute
*/
public function testExecute_EmptyPublicStatus()
{
$project = new Project();
$project->setAllowPublicStatus(1);
$options = array(
'ssh_private_key' => 'private',
'ssh_public_key' => 'public',
'build_config' => 'config',
);
$returnValue = $this->testedService->updateProject($project, 'Test Project', 'github', 'block8/phpci', $options);
$this->assertEquals(0, $returnValue->getAllowPublicStatus());
}
/**
* @covers PHPUnit::execute
*/
public function testExecute_DeleteProject()
{
$store = $this->getMock('PHPCI\Store\ProjectStore');
$store->expects($this->once())
->method('delete')
->will($this->returnValue(true));
$service = new ProjectService($store);
$project = new Project();
$this->assertEquals(true, $service->deleteProject($project));
}
}