Skip to content
Permalink
Browse files

Added status and source build fields validation.

  • Loading branch information...
corpsee committed Mar 12, 2018
1 parent 10d41db commit d3a390d3f89b5fb062f2f8ac8b5859a799a425ee
@@ -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">
@@ -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,13 +155,21 @@ public function getStatus()
/**
* @param integer $value
*
* @throws ValidationException
*
* @return boolean
*/
public function setStatus($value)
{
$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 @@ public function getSource()
/**
* @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;
}
@@ -80,6 +80,9 @@ public function testStatus()
$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 @@ public function testSource()
$result = $build->setSource(Build::SOURCE_WEBHOOK_PULL_REQUEST);
self::assertEquals(false, $result);
self::expectException('\PHPCensor\Exception\HttpException\ValidationException');
$build->setSource(20);
}
public function testUserId()
@@ -137,8 +137,10 @@ public function testType()
$result = $project->setType('git');
self::assertEquals(false, $result);
}
self::expectException('\PHPCensor\Exception\HttpException\ValidationException');
$project->setType('invalid-type');
}
public function testAccessInformation()
{
@@ -2,6 +2,7 @@
namespace Tests\PHPCensor\Model;
use PHPCensor\Exception\HttpException\ValidationException;
use PHPCensor\Model\Project;
use PHPCensor\Model;
@@ -16,6 +17,15 @@ public function testExecute_TestIsAValidModel()
{
$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()

0 comments on commit d3a390d

Please sign in to comment.
You can’t perform that action at this time.