Refactored ValidationException (-> InvalidArgumentException).
This commit is contained in:
parent
cdc926c62a
commit
2a3adf25af
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace PHPCensor\Command;
|
namespace PHPCensor\Command;
|
||||||
|
|
||||||
|
use PHPCensor\Exception\InvalidArgumentException;
|
||||||
use PHPCensor\Service\UserService;
|
use PHPCensor\Service\UserService;
|
||||||
use PHPCensor\Store\UserStore;
|
use PHPCensor\Store\UserStore;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
|
@ -58,7 +59,7 @@ class CreateAdminCommand extends Command
|
||||||
// Function to validate email address.
|
// Function to validate email address.
|
||||||
$mailValidator = function ($answer) {
|
$mailValidator = function ($answer) {
|
||||||
if (!filter_var($answer, FILTER_VALIDATE_EMAIL)) {
|
if (!filter_var($answer, FILTER_VALIDATE_EMAIL)) {
|
||||||
throw new \InvalidArgumentException('Must be a valid email address.');
|
throw new InvalidArgumentException('Must be a valid email address.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $answer;
|
return $answer;
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace PHPCensor\Command;
|
namespace PHPCensor\Command;
|
||||||
|
|
||||||
|
use PHPCensor\Exception\InvalidArgumentException;
|
||||||
use PHPCensor\Model\Build;
|
use PHPCensor\Model\Build;
|
||||||
use PHPCensor\Service\BuildService;
|
use PHPCensor\Service\BuildService;
|
||||||
use PHPCensor\Store\ProjectStore;
|
use PHPCensor\Store\ProjectStore;
|
||||||
|
@ -71,7 +72,7 @@ class CreateBuildCommand extends Command
|
||||||
|
|
||||||
$project = $this->projectStore->getById($projectId);
|
$project = $this->projectStore->getById($projectId);
|
||||||
if (empty($project) || $project->getArchived()) {
|
if (empty($project) || $project->getArchived()) {
|
||||||
throw new \InvalidArgumentException('Project does not exist: ' . $projectId);
|
throw new InvalidArgumentException('Project does not exist: ' . $projectId);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -6,6 +6,7 @@ use Exception;
|
||||||
use PDO;
|
use PDO;
|
||||||
|
|
||||||
use PHPCensor\Config;
|
use PHPCensor\Config;
|
||||||
|
use PHPCensor\Exception\InvalidArgumentException;
|
||||||
use PHPCensor\Store\Factory;
|
use PHPCensor\Store\Factory;
|
||||||
use PHPCensor\Model\ProjectGroup;
|
use PHPCensor\Model\ProjectGroup;
|
||||||
use PHPCensor\Store\UserStore;
|
use PHPCensor\Store\UserStore;
|
||||||
|
@ -185,7 +186,7 @@ class InstallCommand extends Command
|
||||||
// Function to validate email address.
|
// Function to validate email address.
|
||||||
$mailValidator = function ($answer) {
|
$mailValidator = function ($answer) {
|
||||||
if (!filter_var($answer, FILTER_VALIDATE_EMAIL)) {
|
if (!filter_var($answer, FILTER_VALIDATE_EMAIL)) {
|
||||||
throw new \InvalidArgumentException('Must be a valid email address.');
|
throw new InvalidArgumentException('Must be a valid email address.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $answer;
|
return $answer;
|
||||||
|
|
|
@ -1,18 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace PHPCensor\Exception\HttpException;
|
|
||||||
|
|
||||||
use PHPCensor\Exception\HttpException;
|
|
||||||
|
|
||||||
class ValidationException extends HttpException
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @var integer
|
|
||||||
*/
|
|
||||||
protected $errorCode = 400;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $statusMessage = 'Bad Request';
|
|
||||||
}
|
|
7
src/Exception/InvalidArgumentException.php
Normal file
7
src/Exception/InvalidArgumentException.php
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PHPCensor\Exception;
|
||||||
|
|
||||||
|
class InvalidArgumentException extends \InvalidArgumentException
|
||||||
|
{
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ namespace PHPCensor\Http;
|
||||||
|
|
||||||
use PHPCensor\Application;
|
use PHPCensor\Application;
|
||||||
use PHPCensor\Config;
|
use PHPCensor\Config;
|
||||||
|
use PHPCensor\Exception\InvalidArgumentException;
|
||||||
|
|
||||||
class Router
|
class Router
|
||||||
{
|
{
|
||||||
|
@ -40,15 +41,16 @@ class Router
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $route Route definition
|
* @param string $route Route definition
|
||||||
* @param array $options
|
* @param array $options
|
||||||
* @param callable $callback
|
* @param callable $callback
|
||||||
* @throws \InvalidArgumentException
|
*
|
||||||
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function register($route, $options = [], $callback = null)
|
public function register($route, $options = [], $callback = null)
|
||||||
{
|
{
|
||||||
if (!is_callable($callback)) {
|
if (!is_callable($callback)) {
|
||||||
throw new \InvalidArgumentException('$callback must be callable.');
|
throw new InvalidArgumentException('$callback must be callable.');
|
||||||
}
|
}
|
||||||
|
|
||||||
array_unshift($this->routes, ['route' => $route, 'callback' => $callback, 'defaults' => $options]);
|
array_unshift($this->routes, ['route' => $route, 'callback' => $callback, 'defaults' => $options]);
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
namespace PHPCensor;
|
namespace PHPCensor;
|
||||||
|
|
||||||
use PHPCensor\Exception\HttpException\ValidationException;
|
use PHPCensor\Exception\InvalidArgumentException;
|
||||||
|
|
||||||
class Model
|
class Model
|
||||||
{
|
{
|
||||||
|
@ -29,7 +29,7 @@ class Model
|
||||||
if (is_array($initialData)) {
|
if (is_array($initialData)) {
|
||||||
foreach ($initialData as $index => $item) {
|
foreach ($initialData as $index => $item) {
|
||||||
if (!array_key_exists($index, $this->data)) {
|
if (!array_key_exists($index, $this->data)) {
|
||||||
throw new \InvalidArgumentException(sprintf(
|
throw new InvalidArgumentException(sprintf(
|
||||||
'Model "%s" doesn\'t have field "%s"',
|
'Model "%s" doesn\'t have field "%s"',
|
||||||
get_called_class(),
|
get_called_class(),
|
||||||
$index
|
$index
|
||||||
|
@ -81,12 +81,12 @@ class Model
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
*
|
*
|
||||||
* @throws ValidationException
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
protected function validateString($name, $value)
|
protected function validateString($name, $value)
|
||||||
{
|
{
|
||||||
if (!is_string($value) && !is_null($value)) {
|
if (!is_string($value) && !is_null($value)) {
|
||||||
throw new ValidationException('Column "' . $name . '" must be a string.');
|
throw new InvalidArgumentException('Column "' . $name . '" must be a string.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,12 +94,12 @@ class Model
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
*
|
*
|
||||||
* @throws ValidationException
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
protected function validateInt($name, $value)
|
protected function validateInt($name, $value)
|
||||||
{
|
{
|
||||||
if (!is_integer($value) && !is_null($value)) {
|
if (!is_integer($value) && !is_null($value)) {
|
||||||
throw new ValidationException('Column "' . $name . '" must be an integer.');
|
throw new InvalidArgumentException('Column "' . $name . '" must be an integer.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,12 +107,12 @@ class Model
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
*
|
*
|
||||||
* @throws ValidationException
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
protected function validateBoolean($name, $value)
|
protected function validateBoolean($name, $value)
|
||||||
{
|
{
|
||||||
if (!is_bool($value) && !is_null($value)) {
|
if (!is_bool($value) && !is_null($value)) {
|
||||||
throw new ValidationException('Column "' . $name . '" must be a boolean.');
|
throw new InvalidArgumentException('Column "' . $name . '" must be a boolean.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,12 +120,12 @@ class Model
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
*
|
*
|
||||||
* @throws ValidationException
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
protected function validateNotNull($name, $value)
|
protected function validateNotNull($name, $value)
|
||||||
{
|
{
|
||||||
if (is_null($value)) {
|
if (is_null($value)) {
|
||||||
throw new ValidationException('Column "' . $name . '" must not be null.');
|
throw new InvalidArgumentException('Column "' . $name . '" must not be null.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
namespace PHPCensor\Model\Base;
|
namespace PHPCensor\Model\Base;
|
||||||
|
|
||||||
use PHPCensor\Exception\HttpException\ValidationException;
|
use PHPCensor\Exception\InvalidArgumentException;
|
||||||
use PHPCensor\Model;
|
use PHPCensor\Model;
|
||||||
|
|
||||||
class Build extends Model
|
class Build extends Model
|
||||||
|
@ -155,7 +155,7 @@ class Build extends Model
|
||||||
/**
|
/**
|
||||||
* @param integer $value
|
* @param integer $value
|
||||||
*
|
*
|
||||||
* @throws ValidationException
|
* @throws InvalidArgumentException
|
||||||
*
|
*
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
|
@ -165,7 +165,7 @@ class Build extends Model
|
||||||
$this->validateInt('status', $value);
|
$this->validateInt('status', $value);
|
||||||
|
|
||||||
if (!in_array($value, $this->allowedStatuses, true)) {
|
if (!in_array($value, $this->allowedStatuses, true)) {
|
||||||
throw new ValidationException(
|
throw new InvalidArgumentException(
|
||||||
'Column "status" must be one of: ' . join(', ', $this->allowedStatuses) . '.'
|
'Column "status" must be one of: ' . join(', ', $this->allowedStatuses) . '.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -474,7 +474,7 @@ class Build extends Model
|
||||||
/**
|
/**
|
||||||
* @param integer $value
|
* @param integer $value
|
||||||
*
|
*
|
||||||
* @throws ValidationException
|
* @throws InvalidArgumentException
|
||||||
*
|
*
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
|
@ -483,7 +483,7 @@ class Build extends Model
|
||||||
$this->validateInt('source', $value);
|
$this->validateInt('source', $value);
|
||||||
|
|
||||||
if (!in_array($value, $this->allowedSources, true)) {
|
if (!in_array($value, $this->allowedSources, true)) {
|
||||||
throw new ValidationException(
|
throw new InvalidArgumentException(
|
||||||
'Column "source" must be one of: ' . join(', ', $this->allowedSources) . '.'
|
'Column "source" must be one of: ' . join(', ', $this->allowedSources) . '.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
namespace PHPCensor\Model\Base;
|
namespace PHPCensor\Model\Base;
|
||||||
|
|
||||||
use PHPCensor\Exception\HttpException\ValidationException;
|
use PHPCensor\Exception\InvalidArgumentException;
|
||||||
use PHPCensor\Model;
|
use PHPCensor\Model;
|
||||||
|
|
||||||
class Project extends Model
|
class Project extends Model
|
||||||
|
@ -274,7 +274,7 @@ class Project extends Model
|
||||||
*
|
*
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*
|
*
|
||||||
* @throws ValidationException
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function setType($value)
|
public function setType($value)
|
||||||
{
|
{
|
||||||
|
@ -282,7 +282,7 @@ class Project extends Model
|
||||||
$this->validateString('type', $value);
|
$this->validateString('type', $value);
|
||||||
|
|
||||||
if (!in_array($value, $this->allowedTypes, true)) {
|
if (!in_array($value, $this->allowedTypes, true)) {
|
||||||
throw new ValidationException(
|
throw new InvalidArgumentException(
|
||||||
'Column "type" must be one of: ' . join(', ', $this->allowedTypes) . '.'
|
'Column "type" must be one of: ' . join(', ', $this->allowedTypes) . '.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace PHPCensor\Plugin\Util;
|
namespace PHPCensor\Plugin\Util;
|
||||||
|
|
||||||
|
use PHPCensor\Exception\InvalidArgumentException;
|
||||||
use PHPCensor\Plugin;
|
use PHPCensor\Plugin;
|
||||||
use Pimple\Container;
|
use Pimple\Container;
|
||||||
|
|
||||||
|
@ -68,11 +69,9 @@ class Factory
|
||||||
* Builds an instance of plugin of class $className. $options will
|
* Builds an instance of plugin of class $className. $options will
|
||||||
* be passed along with any resources registered with the factory.
|
* be passed along with any resources registered with the factory.
|
||||||
*
|
*
|
||||||
* @param $className
|
* @param string $className
|
||||||
* @param array|null $options
|
* @param array|null $options
|
||||||
*
|
*
|
||||||
* @throws \InvalidArgumentException if $className doesn't represent a valid plugin
|
|
||||||
*
|
|
||||||
* @return \PHPCensor\Plugin
|
* @return \PHPCensor\Plugin
|
||||||
*/
|
*/
|
||||||
public function buildPlugin($className, $options = [])
|
public function buildPlugin($className, $options = [])
|
||||||
|
@ -103,10 +102,12 @@ class Factory
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param callable $loader
|
* @param callable $loader
|
||||||
* @param string|null $name
|
* @param string|null $name
|
||||||
* @param string|null $type
|
* @param string|null $type
|
||||||
* @throws \InvalidArgumentException
|
*
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
*
|
||||||
* @internal param mixed $resource
|
* @internal param mixed $resource
|
||||||
*/
|
*/
|
||||||
public function registerResource(
|
public function registerResource(
|
||||||
|
@ -115,13 +116,13 @@ class Factory
|
||||||
$type = null
|
$type = null
|
||||||
) {
|
) {
|
||||||
if ($name === null && $type === null) {
|
if ($name === null && $type === null) {
|
||||||
throw new \InvalidArgumentException(
|
throw new InvalidArgumentException(
|
||||||
"Type or Name must be specified"
|
"Type or Name must be specified"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!($loader instanceof \Closure)) {
|
if (!($loader instanceof \Closure)) {
|
||||||
throw new \InvalidArgumentException(
|
throw new InvalidArgumentException(
|
||||||
'$loader is expected to be a function'
|
'$loader is expected to be a function'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
namespace PHPCensor;
|
namespace PHPCensor;
|
||||||
|
|
||||||
|
use PHPCensor\Exception\InvalidArgumentException;
|
||||||
|
|
||||||
abstract class Store
|
abstract class Store
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
@ -110,15 +112,14 @@ abstract class Store
|
||||||
* @param Model $obj
|
* @param Model $obj
|
||||||
* @param boolean $saveAllColumns
|
* @param boolean $saveAllColumns
|
||||||
*
|
*
|
||||||
* @throws \RuntimeException
|
* @throws InvalidArgumentException
|
||||||
* @throws \InvalidArgumentException
|
|
||||||
*
|
*
|
||||||
* @return Model|null
|
* @return Model|null
|
||||||
*/
|
*/
|
||||||
public function save(Model $obj, $saveAllColumns = false)
|
public function save(Model $obj, $saveAllColumns = false)
|
||||||
{
|
{
|
||||||
if (!($obj instanceof $this->modelName)) {
|
if (!($obj instanceof $this->modelName)) {
|
||||||
throw new \InvalidArgumentException(get_class($obj) . ' is an invalid model type for this store.');
|
throw new InvalidArgumentException(get_class($obj) . ' is an invalid model type for this store.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = $obj->getDataArray();
|
$data = $obj->getDataArray();
|
||||||
|
@ -207,15 +208,14 @@ abstract class Store
|
||||||
/**
|
/**
|
||||||
* @param Model $obj
|
* @param Model $obj
|
||||||
*
|
*
|
||||||
* @throws \RuntimeException
|
* @throws InvalidArgumentException
|
||||||
* @throws \InvalidArgumentException
|
|
||||||
*
|
*
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function delete(Model $obj)
|
public function delete(Model $obj)
|
||||||
{
|
{
|
||||||
if (!($obj instanceof $this->modelName)) {
|
if (!($obj instanceof $this->modelName)) {
|
||||||
throw new \InvalidArgumentException(get_class($obj) . ' is an invalid model type for this store.');
|
throw new InvalidArgumentException(get_class($obj) . ' is an invalid model type for this store.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = $obj->getDataArray();
|
$data = $obj->getDataArray();
|
||||||
|
@ -230,14 +230,14 @@ abstract class Store
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
* @param string $field
|
||||||
*
|
*
|
||||||
* @throws \InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function fieldCheck($field)
|
protected function fieldCheck($field)
|
||||||
{
|
{
|
||||||
if (empty($field)) {
|
if (empty($field)) {
|
||||||
throw new \InvalidArgumentException('You cannot have an empty field name.');
|
throw new InvalidArgumentException('You cannot have an empty field name.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strpos($field, '.') === false) {
|
if (strpos($field, '.') === false) {
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
namespace Tests\PHPCensor\Command;
|
namespace Tests\PHPCensor\Command;
|
||||||
|
|
||||||
use PHPCensor\Command\CreateBuildCommand;
|
use PHPCensor\Command\CreateBuildCommand;
|
||||||
|
use PHPCensor\Exception\InvalidArgumentException;
|
||||||
use Symfony\Component\Console\Application;
|
use Symfony\Component\Console\Application;
|
||||||
use Symfony\Component\Console\Tester\CommandTester;
|
use Symfony\Component\Console\Tester\CommandTester;
|
||||||
|
|
||||||
|
@ -68,7 +69,7 @@ class CreateBuildCommandTest extends \PHPUnit\Framework\TestCase
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function testExecuteWithUnknownProjectId()
|
public function testExecuteWithUnknownProjectId()
|
||||||
{
|
{
|
||||||
|
|
|
@ -73,14 +73,4 @@ class HttpExceptionTest extends \PHPUnit\Framework\TestCase
|
||||||
self::assertTrue($ex->getStatusMessage() == 'Internal Server Error');
|
self::assertTrue($ex->getStatusMessage() == 'Internal Server Error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testValidationException()
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
throw new HttpException\ValidationException('Test');
|
|
||||||
} catch (HttpException $ex) {
|
|
||||||
self::assertTrue($ex->getErrorCode() == 400);
|
|
||||||
self::assertTrue($ex->getStatusMessage() == 'Bad Request');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,7 +81,7 @@ class BuildTest extends TestCase
|
||||||
$result = $build->setStatus(Build::STATUS_FAILED);
|
$result = $build->setStatus(Build::STATUS_FAILED);
|
||||||
self::assertEquals(false, $result);
|
self::assertEquals(false, $result);
|
||||||
|
|
||||||
self::expectException('\PHPCensor\Exception\HttpException\ValidationException');
|
self::expectException('\PHPCensor\Exception\InvalidArgumentException');
|
||||||
$build->setStatus(10);
|
$build->setStatus(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -230,7 +230,7 @@ class BuildTest extends TestCase
|
||||||
$result = $build->setSource(Build::SOURCE_WEBHOOK_PULL_REQUEST);
|
$result = $build->setSource(Build::SOURCE_WEBHOOK_PULL_REQUEST);
|
||||||
self::assertEquals(false, $result);
|
self::assertEquals(false, $result);
|
||||||
|
|
||||||
self::expectException('\PHPCensor\Exception\HttpException\ValidationException');
|
self::expectException('\PHPCensor\Exception\InvalidArgumentException');
|
||||||
$build->setSource(20);
|
$build->setSource(20);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -138,7 +138,7 @@ class ProjectTest extends TestCase
|
||||||
$result = $project->setType('git');
|
$result = $project->setType('git');
|
||||||
self::assertEquals(false, $result);
|
self::assertEquals(false, $result);
|
||||||
|
|
||||||
self::expectException('\PHPCensor\Exception\HttpException\ValidationException');
|
self::expectException('\PHPCensor\Exception\InvalidArgumentException');
|
||||||
$project->setType('invalid-type');
|
$project->setType('invalid-type');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
namespace Tests\PHPCensor\Model;
|
namespace Tests\PHPCensor\Model;
|
||||||
|
|
||||||
use PHPCensor\Exception\HttpException\ValidationException;
|
use PHPCensor\Exception\InvalidArgumentException;
|
||||||
use PHPCensor\Model\Build;
|
use PHPCensor\Model\Build;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -49,7 +49,7 @@ class BuildTest extends \PHPUnit\Framework\TestCase
|
||||||
'branch' => 'dev',
|
'branch' => 'dev',
|
||||||
'unknown' => 'unknown',
|
'unknown' => 'unknown',
|
||||||
]);
|
]);
|
||||||
} catch (\InvalidArgumentException $e) {
|
} catch (InvalidArgumentException $e) {
|
||||||
self::assertEquals(
|
self::assertEquals(
|
||||||
'Model "PHPCensor\Model\Build" doesn\'t have field "unknown"',
|
'Model "PHPCensor\Model\Build" doesn\'t have field "unknown"',
|
||||||
$e->getMessage()
|
$e->getMessage()
|
||||||
|
@ -65,7 +65,7 @@ class BuildTest extends \PHPUnit\Framework\TestCase
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$build->setLog([]);
|
$build->setLog([]);
|
||||||
} catch (ValidationException $e) {
|
} catch (InvalidArgumentException $e) {
|
||||||
self::assertEquals(
|
self::assertEquals(
|
||||||
'Column "log" must be a string.',
|
'Column "log" must be a string.',
|
||||||
$e->getMessage()
|
$e->getMessage()
|
||||||
|
@ -77,7 +77,7 @@ class BuildTest extends \PHPUnit\Framework\TestCase
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$build->setSource('5');
|
$build->setSource('5');
|
||||||
} catch (ValidationException $e) {
|
} catch (InvalidArgumentException $e) {
|
||||||
self::assertEquals(
|
self::assertEquals(
|
||||||
'Column "source" must be an integer.',
|
'Column "source" must be an integer.',
|
||||||
$e->getMessage()
|
$e->getMessage()
|
||||||
|
@ -86,7 +86,7 @@ class BuildTest extends \PHPUnit\Framework\TestCase
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$build->setId(null);
|
$build->setId(null);
|
||||||
} catch (ValidationException $e) {
|
} catch (InvalidArgumentException $e) {
|
||||||
self::assertEquals(
|
self::assertEquals(
|
||||||
'Column "id" must not be null.',
|
'Column "id" must not be null.',
|
||||||
$e->getMessage()
|
$e->getMessage()
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
namespace Tests\PHPCensor\Model;
|
namespace Tests\PHPCensor\Model;
|
||||||
|
|
||||||
use PHPCensor\Exception\HttpException\ValidationException;
|
use PHPCensor\Exception\InvalidArgumentException;
|
||||||
use PHPCensor\Model\Project;
|
use PHPCensor\Model\Project;
|
||||||
use PHPCensor\Model;
|
use PHPCensor\Model;
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ class ProjectTest extends \PHPUnit\Framework\TestCase
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$project->setArchived('true');
|
$project->setArchived('true');
|
||||||
} catch (ValidationException $e) {
|
} catch (InvalidArgumentException $e) {
|
||||||
self::assertEquals(
|
self::assertEquals(
|
||||||
'Column "archived" must be a boolean.',
|
'Column "archived" must be a boolean.',
|
||||||
$e->getMessage()
|
$e->getMessage()
|
||||||
|
|
|
@ -34,13 +34,15 @@ class FactoryTest extends \PHPUnit\Framework\TestCase {
|
||||||
|
|
||||||
public function testRegisterResourceThrowsExceptionWithoutTypeAndName()
|
public function testRegisterResourceThrowsExceptionWithoutTypeAndName()
|
||||||
{
|
{
|
||||||
$this->setExpectedException('InvalidArgumentException', 'Type or Name must be specified');
|
self::expectException('\PHPCensor\Exception\InvalidArgumentException');
|
||||||
|
self::expectExceptionMessage('Type or Name must be specified');
|
||||||
$this->testedFactory->registerResource($this->resourceLoader, null, null);
|
$this->testedFactory->registerResource($this->resourceLoader, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRegisterResourceThrowsExceptionIfLoaderIsntFunction()
|
public function testRegisterResourceThrowsExceptionIfLoaderIsntFunction()
|
||||||
{
|
{
|
||||||
$this->setExpectedException('InvalidArgumentException', '$loader is expected to be a function');
|
self::expectException('\PHPCensor\Exception\InvalidArgumentException');
|
||||||
|
self::expectExceptionMessage('$loader is expected to be a function');
|
||||||
$this->testedFactory->registerResource(["dummy"], "TestName", "TestClass");
|
$this->testedFactory->registerResource(["dummy"], "TestName", "TestClass");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,10 +55,8 @@ class FactoryTest extends \PHPUnit\Framework\TestCase {
|
||||||
|
|
||||||
public function testBuildPluginThrowsExceptionIfMissingResourcesForRequiredArg()
|
public function testBuildPluginThrowsExceptionIfMissingResourcesForRequiredArg()
|
||||||
{
|
{
|
||||||
$this->setExpectedException(
|
self::expectException('\DomainException');
|
||||||
'DomainException',
|
self::expectExceptionMessage('Unsatisfied dependency: requiredArgument');
|
||||||
'Unsatisfied dependency: requiredArgument'
|
|
||||||
);
|
|
||||||
|
|
||||||
$pluginClass = $this->getFakePluginClassName('ExamplePluginWithSingleRequiredArg');
|
$pluginClass = $this->getFakePluginClassName('ExamplePluginWithSingleRequiredArg');
|
||||||
$plugin = $this->testedFactory->buildPlugin($pluginClass);
|
$plugin = $this->testedFactory->buildPlugin($pluginClass);
|
||||||
|
|
|
@ -4,6 +4,7 @@ namespace Tests\PHPCensor;
|
||||||
|
|
||||||
use PHPCensor\Config;
|
use PHPCensor\Config;
|
||||||
use PHPCensor\Database;
|
use PHPCensor\Database;
|
||||||
|
use PHPCensor\Exception\InvalidArgumentException;
|
||||||
use PHPCensor\Store\Factory;
|
use PHPCensor\Store\Factory;
|
||||||
use PHPCensor\Model\Project;
|
use PHPCensor\Model\Project;
|
||||||
use PHPCensor\Model\ProjectGroup;
|
use PHPCensor\Model\ProjectGroup;
|
||||||
|
@ -177,7 +178,7 @@ class StoreMysqlTest extends \PHPUnit_Extensions_Database_TestCase
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$data = $testStore->getWhere(['' => 0], 100, 0, ['id' => 'ASC']);
|
$data = $testStore->getWhere(['' => 0], 100, 0, ['id' => 'ASC']);
|
||||||
} catch (\InvalidArgumentException $e) {
|
} catch (InvalidArgumentException $e) {
|
||||||
self::assertEquals('You cannot have an empty field name.', $e->getMessage());
|
self::assertEquals('You cannot have an empty field name.', $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,7 +234,7 @@ class StoreMysqlTest extends \PHPUnit_Extensions_Database_TestCase
|
||||||
$model->setId(1);
|
$model->setId(1);
|
||||||
|
|
||||||
$testStore->save($model);
|
$testStore->save($model);
|
||||||
} catch (\InvalidArgumentException $e) {
|
} catch (InvalidArgumentException $e) {
|
||||||
self::assertEquals(
|
self::assertEquals(
|
||||||
'PHPCensor\Model\Project is an invalid model type for this store.',
|
'PHPCensor\Model\Project is an invalid model type for this store.',
|
||||||
$e->getMessage()
|
$e->getMessage()
|
||||||
|
@ -258,7 +259,7 @@ class StoreMysqlTest extends \PHPUnit_Extensions_Database_TestCase
|
||||||
$model->setId(1);
|
$model->setId(1);
|
||||||
|
|
||||||
$testStore->delete($model);
|
$testStore->delete($model);
|
||||||
} catch (\InvalidArgumentException $e) {
|
} catch (InvalidArgumentException $e) {
|
||||||
self::assertEquals(
|
self::assertEquals(
|
||||||
'PHPCensor\Model\Project is an invalid model type for this store.',
|
'PHPCensor\Model\Project is an invalid model type for this store.',
|
||||||
$e->getMessage()
|
$e->getMessage()
|
||||||
|
|
|
@ -4,6 +4,7 @@ namespace Tests\PHPCensor;
|
||||||
|
|
||||||
use PHPCensor\Config;
|
use PHPCensor\Config;
|
||||||
use PHPCensor\Database;
|
use PHPCensor\Database;
|
||||||
|
use PHPCensor\Exception\InvalidArgumentException;
|
||||||
use PHPCensor\Store\Factory;
|
use PHPCensor\Store\Factory;
|
||||||
use PHPCensor\Model\Project;
|
use PHPCensor\Model\Project;
|
||||||
use PHPCensor\Model\ProjectGroup;
|
use PHPCensor\Model\ProjectGroup;
|
||||||
|
@ -164,7 +165,7 @@ class StorePostgresqlTest extends \PHPUnit_Extensions_Database_TestCase
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$data = $testStore->getWhere(['' => 0], 100, 0, ['id' => 'ASC']);
|
$data = $testStore->getWhere(['' => 0], 100, 0, ['id' => 'ASC']);
|
||||||
} catch (\InvalidArgumentException $e) {
|
} catch (InvalidArgumentException $e) {
|
||||||
self::assertEquals('You cannot have an empty field name.', $e->getMessage());
|
self::assertEquals('You cannot have an empty field name.', $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -228,7 +229,7 @@ class StorePostgresqlTest extends \PHPUnit_Extensions_Database_TestCase
|
||||||
$model->setId(1);
|
$model->setId(1);
|
||||||
|
|
||||||
$testStore->save($model);
|
$testStore->save($model);
|
||||||
} catch (\InvalidArgumentException $e) {
|
} catch (InvalidArgumentException $e) {
|
||||||
self::assertEquals(
|
self::assertEquals(
|
||||||
'PHPCensor\Model\Project is an invalid model type for this store.',
|
'PHPCensor\Model\Project is an invalid model type for this store.',
|
||||||
$e->getMessage()
|
$e->getMessage()
|
||||||
|
@ -253,7 +254,7 @@ class StorePostgresqlTest extends \PHPUnit_Extensions_Database_TestCase
|
||||||
$model->setId(1);
|
$model->setId(1);
|
||||||
|
|
||||||
$testStore->delete($model);
|
$testStore->delete($model);
|
||||||
} catch (\InvalidArgumentException $e) {
|
} catch (InvalidArgumentException $e) {
|
||||||
self::assertEquals(
|
self::assertEquals(
|
||||||
'PHPCensor\Model\Project is an invalid model type for this store.',
|
'PHPCensor\Model\Project is an invalid model type for this store.',
|
||||||
$e->getMessage()
|
$e->getMessage()
|
||||||
|
|
Loading…
Reference in a new issue