diff --git a/phpmd.xml b/phpmd.xml index e7d9aa7c..b4e6ef61 100644 --- a/phpmd.xml +++ b/phpmd.xml @@ -21,7 +21,7 @@ - + diff --git a/src/Model/Base/Build.php b/src/Model/Base/Build.php index c1fc0cab..c356aa2b 100644 --- a/src/Model/Base/Build.php +++ b/src/Model/Base/Build.php @@ -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; } diff --git a/tests/src/Model/Base/BuildTest.php b/tests/src/Model/Base/BuildTest.php index 41785c88..db000c94 100644 --- a/tests/src/Model/Base/BuildTest.php +++ b/tests/src/Model/Base/BuildTest.php @@ -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() diff --git a/tests/src/Model/Base/ProjectTest.php b/tests/src/Model/Base/ProjectTest.php index 8231afd2..de1ebc9b 100644 --- a/tests/src/Model/Base/ProjectTest.php +++ b/tests/src/Model/Base/ProjectTest.php @@ -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() { diff --git a/tests/src/Model/ProjectTest.php b/tests/src/Model/ProjectTest.php index 267c058c..ef9eda58 100644 --- a/tests/src/Model/ProjectTest.php +++ b/tests/src/Model/ProjectTest.php @@ -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()