Added status and source build fields validation.

This commit is contained in:
Dmitry Khomutov 2018-03-12 19:51:41 +07:00
parent 10d41dba81
commit d3a390d3f8
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
5 changed files with 59 additions and 2 deletions

View file

@ -21,7 +21,7 @@
<rule ref="rulesets/naming.xml/ShortVariable">
<properties>
<property name="exceptions" value="db,dn,id,i,j" />
<property name="exceptions" value="db,dn,id,i,j,e,ex" />
</properties>
</rule>
<rule ref="rulesets/naming.xml/ShortMethodName">

View file

@ -2,6 +2,7 @@
namespace PHPCensor\Model\Base;
use PHPCensor\Exception\HttpException\ValidationException;
use PHPCensor\Model;
class Build extends Model
@ -40,6 +41,28 @@ class Build extends Model
'user_id' => 0,
];
/**
* @var array
*/
protected $allowedStatuses = [
self::STATUS_PENDING,
self::STATUS_RUNNING,
self::STATUS_SUCCESS,
self::STATUS_FAILED,
];
/**
* @var array
*/
protected $allowedSources = [
self::SOURCE_UNKNOWN,
self::SOURCE_MANUAL_WEB,
self::SOURCE_MANUAL_CONSOLE,
self::SOURCE_PERIODICAL,
self::SOURCE_WEBHOOK,
self::SOURCE_WEBHOOK_PULL_REQUEST,
];
/**
* @return integer
*/
@ -132,6 +155,8 @@ class Build extends Model
/**
* @param integer $value
*
* @throws ValidationException
*
* @return boolean
*/
public function setStatus($value)
@ -139,6 +164,12 @@ class Build extends Model
$this->validateNotNull('status', $value);
$this->validateInt('status', $value);
if (!in_array($value, $this->allowedStatuses, true)) {
throw new ValidationException(
'Column "status" must be one of: ' . join(', ', $this->allowedStatuses) . '.'
);
}
if ($this->data['status'] === $value) {
return false;
}
@ -443,12 +474,20 @@ class Build extends Model
/**
* @param integer $value
*
* @throws ValidationException
*
* @return boolean
*/
public function setSource($value)
{
$this->validateInt('source', $value);
if (!in_array($value, $this->allowedSources, true)) {
throw new ValidationException(
'Column "source" must be one of: ' . join(', ', $this->allowedSources) . '.'
);
}
if ($this->data['source'] === $value) {
return false;
}

View file

@ -80,6 +80,9 @@ class BuildTest extends TestCase
$result = $build->setStatus(Build::STATUS_FAILED);
self::assertEquals(false, $result);
self::expectException('\PHPCensor\Exception\HttpException\ValidationException');
$build->setStatus(10);
}
public function testLog()
@ -226,6 +229,9 @@ class BuildTest extends TestCase
$result = $build->setSource(Build::SOURCE_WEBHOOK_PULL_REQUEST);
self::assertEquals(false, $result);
self::expectException('\PHPCensor\Exception\HttpException\ValidationException');
$build->setSource(20);
}
public function testUserId()

View file

@ -137,8 +137,10 @@ class ProjectTest extends TestCase
$result = $project->setType('git');
self::assertEquals(false, $result);
}
self::expectException('\PHPCensor\Exception\HttpException\ValidationException');
$project->setType('invalid-type');
}
public function testAccessInformation()
{

View file

@ -2,6 +2,7 @@
namespace Tests\PHPCensor\Model;
use PHPCensor\Exception\HttpException\ValidationException;
use PHPCensor\Model\Project;
use PHPCensor\Model;
@ -16,6 +17,15 @@ class ProjectTest extends \PHPUnit\Framework\TestCase
{
$project = new Project();
self::assertTrue($project instanceof Model);
try {
$project->setArchived('true');
} catch (ValidationException $e) {
self::assertEquals(
'Column "archived" must be a boolean.',
$e->getMessage()
);
}
}
public function testExecute_TestGitDefaultBranch()