2014-07-14 17:02:36 +02:00
|
|
|
<?php
|
2015-03-03 17:18:17 +01:00
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace Tests\PHPCensor\Service;
|
2014-07-14 17:02:36 +02:00
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
use PHPCensor\Model\Build;
|
|
|
|
use PHPCensor\Model\Project;
|
|
|
|
use PHPCensor\Service\BuildService;
|
2014-07-14 17:02:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for the ProjectService class.
|
2017-03-04 16:39:56 +01:00
|
|
|
*
|
2014-07-14 17:02:36 +02:00
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
|
|
|
*/
|
|
|
|
class BuildServiceTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var BuildService $testedService
|
|
|
|
*/
|
|
|
|
protected $testedService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \ $mockBuildStore
|
|
|
|
*/
|
|
|
|
protected $mockBuildStore;
|
|
|
|
|
2017-03-25 23:02:27 +01:00
|
|
|
/**
|
|
|
|
* @var \ $mockEnvironmentStore
|
|
|
|
*/
|
|
|
|
protected $mockEnvironmentStore;
|
|
|
|
|
2014-07-14 17:02:36 +02:00
|
|
|
public function setUp()
|
|
|
|
{
|
2017-01-09 17:23:29 +01:00
|
|
|
$this->mockBuildStore = $this->getMockBuilder('PHPCensor\Store\BuildStore')->getMock();
|
2014-07-14 17:02:36 +02:00
|
|
|
$this->mockBuildStore->expects($this->any())
|
|
|
|
->method('save')
|
|
|
|
->will($this->returnArgument(0));
|
|
|
|
|
2017-03-25 23:02:27 +01:00
|
|
|
$this->mockEnvironmentStore = $this->getMockBuilder('PHPCensor\Store\EnvironmentStore')->getMock();
|
|
|
|
$this->mockEnvironmentStore->expects($this->any())
|
|
|
|
->method('getByProjectId')
|
|
|
|
->will($this->returnValue(['items' => [], 'count' => 0]));
|
|
|
|
|
2014-07-14 17:02:36 +02:00
|
|
|
$this->testedService = new BuildService($this->mockBuildStore);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExecute_CreateBasicBuild()
|
|
|
|
{
|
2017-03-25 23:02:27 +01:00
|
|
|
$project = $this
|
|
|
|
->getMockBuilder('PHPCensor\Model\Project')
|
|
|
|
->setMethods(['getEnvironmentStore'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$project->expects($this->any())
|
|
|
|
->method('getEnvironmentStore')
|
|
|
|
->will($this->returnValue($this->mockEnvironmentStore));
|
|
|
|
|
2014-07-14 17:02:36 +02:00
|
|
|
$project->setType('github');
|
|
|
|
$project->setId(101);
|
|
|
|
|
2017-03-23 13:53:24 +01:00
|
|
|
$returnValue = $this->testedService->createBuild($project, null);
|
2014-07-14 17:02:36 +02:00
|
|
|
|
|
|
|
$this->assertEquals(101, $returnValue->getProjectId());
|
2017-02-01 15:53:31 +01:00
|
|
|
$this->assertEquals(Build::STATUS_PENDING, $returnValue->getStatus());
|
2014-07-14 17:02:36 +02:00
|
|
|
$this->assertNull($returnValue->getStarted());
|
|
|
|
$this->assertNull($returnValue->getFinished());
|
|
|
|
$this->assertNull($returnValue->getLog());
|
2017-02-09 16:15:30 +01:00
|
|
|
$this->assertEquals('Manual', $returnValue->getCommitMessage());
|
2014-07-14 17:02:36 +02:00
|
|
|
$this->assertNull($returnValue->getCommitterEmail());
|
2017-03-26 12:48:34 +02:00
|
|
|
$this->assertEquals(['branches' => []], $returnValue->getExtra());
|
2014-07-14 17:02:36 +02:00
|
|
|
$this->assertEquals('master', $returnValue->getBranch());
|
|
|
|
$this->assertInstanceOf('DateTime', $returnValue->getCreated());
|
|
|
|
$this->assertEquals('Manual', $returnValue->getCommitId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExecute_CreateBuildWithOptions()
|
|
|
|
{
|
2017-03-25 23:02:27 +01:00
|
|
|
$project = $this
|
|
|
|
->getMockBuilder('PHPCensor\Model\Project')
|
|
|
|
->setMethods(['getEnvironmentStore'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$project->expects($this->any())
|
|
|
|
->method('getEnvironmentStore')
|
|
|
|
->will($this->returnValue($this->mockEnvironmentStore));
|
|
|
|
|
2014-07-14 17:02:36 +02:00
|
|
|
$project->setType('hg');
|
|
|
|
$project->setId(101);
|
|
|
|
|
2017-03-23 13:53:24 +01:00
|
|
|
$returnValue = $this->testedService->createBuild($project, null, '123', 'testbranch', 'test@example.com', 'test');
|
2014-07-14 17:02:36 +02:00
|
|
|
|
|
|
|
$this->assertEquals('testbranch', $returnValue->getBranch());
|
|
|
|
$this->assertEquals('123', $returnValue->getCommitId());
|
|
|
|
$this->assertEquals('test', $returnValue->getCommitMessage());
|
|
|
|
$this->assertEquals('test@example.com', $returnValue->getCommitterEmail());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExecute_CreateBuildWithExtra()
|
|
|
|
{
|
2017-03-25 23:02:27 +01:00
|
|
|
$project = $this
|
|
|
|
->getMockBuilder('PHPCensor\Model\Project')
|
|
|
|
->setMethods(['getEnvironmentStore'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$project->expects($this->any())
|
|
|
|
->method('getEnvironmentStore')
|
|
|
|
->will($this->returnValue($this->mockEnvironmentStore));
|
|
|
|
|
2014-07-14 17:02:36 +02:00
|
|
|
$project->setType('bitbucket');
|
|
|
|
$project->setId(101);
|
|
|
|
|
2017-03-23 13:53:24 +01:00
|
|
|
$returnValue = $this->testedService->createBuild($project, null, null, null, null, null, ['item1' => 1001]);
|
2014-07-14 17:02:36 +02:00
|
|
|
|
|
|
|
$this->assertEquals(1001, $returnValue->getExtra('item1'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExecute_CreateDuplicateBuild()
|
|
|
|
{
|
|
|
|
$build = new Build();
|
|
|
|
$build->setId(1);
|
2015-10-09 11:37:50 +02:00
|
|
|
$build->setProject(101);
|
2014-07-14 17:02:36 +02:00
|
|
|
$build->setCommitId('abcde');
|
|
|
|
$build->setStatus(Build::STATUS_FAILED);
|
|
|
|
$build->setLog('Test');
|
|
|
|
$build->setBranch('example_branch');
|
|
|
|
$build->setStarted(new \DateTime());
|
|
|
|
$build->setFinished(new \DateTime());
|
|
|
|
$build->setCommitMessage('test');
|
|
|
|
$build->setCommitterEmail('test@example.com');
|
2016-04-21 09:46:38 +02:00
|
|
|
$build->setExtra(json_encode(['item1' => 1001]));
|
2014-07-14 17:02:36 +02:00
|
|
|
|
|
|
|
$returnValue = $this->testedService->createDuplicateBuild($build);
|
|
|
|
|
|
|
|
$this->assertNotEquals($build->getId(), $returnValue->getId());
|
|
|
|
$this->assertEquals($build->getProjectId(), $returnValue->getProjectId());
|
|
|
|
$this->assertEquals($build->getCommitId(), $returnValue->getCommitId());
|
|
|
|
$this->assertNotEquals($build->getStatus(), $returnValue->getStatus());
|
2017-02-01 15:53:31 +01:00
|
|
|
$this->assertEquals(Build::STATUS_PENDING, $returnValue->getStatus());
|
2014-07-14 17:02:36 +02:00
|
|
|
$this->assertNull($returnValue->getLog());
|
|
|
|
$this->assertEquals($build->getBranch(), $returnValue->getBranch());
|
|
|
|
$this->assertNotEquals($build->getCreated(), $returnValue->getCreated());
|
|
|
|
$this->assertNull($returnValue->getStarted());
|
|
|
|
$this->assertNull($returnValue->getFinished());
|
|
|
|
$this->assertEquals('test', $returnValue->getCommitMessage());
|
|
|
|
$this->assertEquals('test@example.com', $returnValue->getCommitterEmail());
|
|
|
|
$this->assertEquals($build->getExtra('item1'), $returnValue->getExtra('item1'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExecute_DeleteBuild()
|
|
|
|
{
|
2017-01-09 17:23:29 +01:00
|
|
|
$store = $this->getMockBuilder('PHPCensor\Store\BuildStore')->getMock();
|
2014-07-14 17:02:36 +02:00
|
|
|
$store->expects($this->once())
|
|
|
|
->method('delete')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$service = new BuildService($store);
|
|
|
|
$build = new Build();
|
|
|
|
|
|
|
|
$this->assertEquals(true, $service->deleteBuild($build));
|
|
|
|
}
|
|
|
|
}
|