Refactored ValidationException (-> InvalidArgumentException).

This commit is contained in:
Dmitry Khomutov 2018-03-17 09:24:30 +07:00
parent cdc926c62a
commit 2a3adf25af
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
20 changed files with 79 additions and 91 deletions

View file

@ -2,6 +2,7 @@
namespace PHPCensor\Command;
use PHPCensor\Exception\InvalidArgumentException;
use PHPCensor\Service\UserService;
use PHPCensor\Store\UserStore;
use Symfony\Component\Console\Command\Command;
@ -58,7 +59,7 @@ class CreateAdminCommand extends Command
// Function to validate email address.
$mailValidator = function ($answer) {
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;

View file

@ -2,6 +2,7 @@
namespace PHPCensor\Command;
use PHPCensor\Exception\InvalidArgumentException;
use PHPCensor\Model\Build;
use PHPCensor\Service\BuildService;
use PHPCensor\Store\ProjectStore;
@ -71,7 +72,7 @@ class CreateBuildCommand extends Command
$project = $this->projectStore->getById($projectId);
if (empty($project) || $project->getArchived()) {
throw new \InvalidArgumentException('Project does not exist: ' . $projectId);
throw new InvalidArgumentException('Project does not exist: ' . $projectId);
}
try {

View file

@ -6,6 +6,7 @@ use Exception;
use PDO;
use PHPCensor\Config;
use PHPCensor\Exception\InvalidArgumentException;
use PHPCensor\Store\Factory;
use PHPCensor\Model\ProjectGroup;
use PHPCensor\Store\UserStore;
@ -185,7 +186,7 @@ class InstallCommand extends Command
// Function to validate email address.
$mailValidator = function ($answer) {
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;

View file

@ -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';
}

View file

@ -0,0 +1,7 @@
<?php
namespace PHPCensor\Exception;
class InvalidArgumentException extends \InvalidArgumentException
{
}

View file

@ -4,6 +4,7 @@ namespace PHPCensor\Http;
use PHPCensor\Application;
use PHPCensor\Config;
use PHPCensor\Exception\InvalidArgumentException;
class Router
{
@ -40,15 +41,16 @@ class Router
}
/**
* @param string $route Route definition
* @param array $options
* @param string $route Route definition
* @param array $options
* @param callable $callback
* @throws \InvalidArgumentException
*
* @throws InvalidArgumentException
*/
public function register($route, $options = [], $callback = null)
{
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]);

View file

@ -2,7 +2,7 @@
namespace PHPCensor;
use PHPCensor\Exception\HttpException\ValidationException;
use PHPCensor\Exception\InvalidArgumentException;
class Model
{
@ -29,7 +29,7 @@ class Model
if (is_array($initialData)) {
foreach ($initialData as $index => $item) {
if (!array_key_exists($index, $this->data)) {
throw new \InvalidArgumentException(sprintf(
throw new InvalidArgumentException(sprintf(
'Model "%s" doesn\'t have field "%s"',
get_called_class(),
$index
@ -81,12 +81,12 @@ class Model
* @param string $name
* @param mixed $value
*
* @throws ValidationException
* @throws InvalidArgumentException
*/
protected function validateString($name, $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 mixed $value
*
* @throws ValidationException
* @throws InvalidArgumentException
*/
protected function validateInt($name, $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 mixed $value
*
* @throws ValidationException
* @throws InvalidArgumentException
*/
protected function validateBoolean($name, $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 mixed $value
*
* @throws ValidationException
* @throws InvalidArgumentException
*/
protected function validateNotNull($name, $value)
{
if (is_null($value)) {
throw new ValidationException('Column "' . $name . '" must not be null.');
throw new InvalidArgumentException('Column "' . $name . '" must not be null.');
}
}
}

View file

@ -2,7 +2,7 @@
namespace PHPCensor\Model\Base;
use PHPCensor\Exception\HttpException\ValidationException;
use PHPCensor\Exception\InvalidArgumentException;
use PHPCensor\Model;
class Build extends Model
@ -155,7 +155,7 @@ class Build extends Model
/**
* @param integer $value
*
* @throws ValidationException
* @throws InvalidArgumentException
*
* @return boolean
*/
@ -165,7 +165,7 @@ class Build extends Model
$this->validateInt('status', $value);
if (!in_array($value, $this->allowedStatuses, true)) {
throw new ValidationException(
throw new InvalidArgumentException(
'Column "status" must be one of: ' . join(', ', $this->allowedStatuses) . '.'
);
}
@ -474,7 +474,7 @@ class Build extends Model
/**
* @param integer $value
*
* @throws ValidationException
* @throws InvalidArgumentException
*
* @return boolean
*/
@ -483,7 +483,7 @@ class Build extends Model
$this->validateInt('source', $value);
if (!in_array($value, $this->allowedSources, true)) {
throw new ValidationException(
throw new InvalidArgumentException(
'Column "source" must be one of: ' . join(', ', $this->allowedSources) . '.'
);
}

View file

@ -2,7 +2,7 @@
namespace PHPCensor\Model\Base;
use PHPCensor\Exception\HttpException\ValidationException;
use PHPCensor\Exception\InvalidArgumentException;
use PHPCensor\Model;
class Project extends Model
@ -274,7 +274,7 @@ class Project extends Model
*
* @return boolean
*
* @throws ValidationException
* @throws InvalidArgumentException
*/
public function setType($value)
{
@ -282,7 +282,7 @@ class Project extends Model
$this->validateString('type', $value);
if (!in_array($value, $this->allowedTypes, true)) {
throw new ValidationException(
throw new InvalidArgumentException(
'Column "type" must be one of: ' . join(', ', $this->allowedTypes) . '.'
);
}

View file

@ -2,6 +2,7 @@
namespace PHPCensor\Plugin\Util;
use PHPCensor\Exception\InvalidArgumentException;
use PHPCensor\Plugin;
use Pimple\Container;
@ -68,11 +69,9 @@ class Factory
* Builds an instance of plugin of class $className. $options will
* be passed along with any resources registered with the factory.
*
* @param $className
* @param string $className
* @param array|null $options
*
* @throws \InvalidArgumentException if $className doesn't represent a valid plugin
*
* @return \PHPCensor\Plugin
*/
public function buildPlugin($className, $options = [])
@ -103,10 +102,12 @@ class Factory
}
/**
* @param callable $loader
* @param callable $loader
* @param string|null $name
* @param string|null $type
* @throws \InvalidArgumentException
*
* @throws InvalidArgumentException
*
* @internal param mixed $resource
*/
public function registerResource(
@ -115,13 +116,13 @@ class Factory
$type = null
) {
if ($name === null && $type === null) {
throw new \InvalidArgumentException(
throw new InvalidArgumentException(
"Type or Name must be specified"
);
}
if (!($loader instanceof \Closure)) {
throw new \InvalidArgumentException(
throw new InvalidArgumentException(
'$loader is expected to be a function'
);
}

View file

@ -2,6 +2,8 @@
namespace PHPCensor;
use PHPCensor\Exception\InvalidArgumentException;
abstract class Store
{
/**
@ -110,15 +112,14 @@ abstract class Store
* @param Model $obj
* @param boolean $saveAllColumns
*
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*
* @return Model|null
*/
public function save(Model $obj, $saveAllColumns = false)
{
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();
@ -207,15 +208,14 @@ abstract class Store
/**
* @param Model $obj
*
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*
* @return boolean
*/
public function delete(Model $obj)
{
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();
@ -230,14 +230,14 @@ abstract class Store
/**
* @param string $field
*
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*
* @return string
*/
protected function fieldCheck($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) {

View file

@ -3,6 +3,7 @@
namespace Tests\PHPCensor\Command;
use PHPCensor\Command\CreateBuildCommand;
use PHPCensor\Exception\InvalidArgumentException;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
@ -68,7 +69,7 @@ class CreateBuildCommandTest extends \PHPUnit\Framework\TestCase
}
/**
* @expectedException \InvalidArgumentException
* @expectedException InvalidArgumentException
*/
public function testExecuteWithUnknownProjectId()
{

View file

@ -73,14 +73,4 @@ class HttpExceptionTest extends \PHPUnit\Framework\TestCase
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');
}
}
}

View file

@ -81,7 +81,7 @@ class BuildTest extends TestCase
$result = $build->setStatus(Build::STATUS_FAILED);
self::assertEquals(false, $result);
self::expectException('\PHPCensor\Exception\HttpException\ValidationException');
self::expectException('\PHPCensor\Exception\InvalidArgumentException');
$build->setStatus(10);
}
@ -230,7 +230,7 @@ class BuildTest extends TestCase
$result = $build->setSource(Build::SOURCE_WEBHOOK_PULL_REQUEST);
self::assertEquals(false, $result);
self::expectException('\PHPCensor\Exception\HttpException\ValidationException');
self::expectException('\PHPCensor\Exception\InvalidArgumentException');
$build->setSource(20);
}

View file

@ -138,7 +138,7 @@ class ProjectTest extends TestCase
$result = $project->setType('git');
self::assertEquals(false, $result);
self::expectException('\PHPCensor\Exception\HttpException\ValidationException');
self::expectException('\PHPCensor\Exception\InvalidArgumentException');
$project->setType('invalid-type');
}

View file

@ -2,7 +2,7 @@
namespace Tests\PHPCensor\Model;
use PHPCensor\Exception\HttpException\ValidationException;
use PHPCensor\Exception\InvalidArgumentException;
use PHPCensor\Model\Build;
/**
@ -49,7 +49,7 @@ class BuildTest extends \PHPUnit\Framework\TestCase
'branch' => 'dev',
'unknown' => 'unknown',
]);
} catch (\InvalidArgumentException $e) {
} catch (InvalidArgumentException $e) {
self::assertEquals(
'Model "PHPCensor\Model\Build" doesn\'t have field "unknown"',
$e->getMessage()
@ -65,7 +65,7 @@ class BuildTest extends \PHPUnit\Framework\TestCase
try {
$build->setLog([]);
} catch (ValidationException $e) {
} catch (InvalidArgumentException $e) {
self::assertEquals(
'Column "log" must be a string.',
$e->getMessage()
@ -77,7 +77,7 @@ class BuildTest extends \PHPUnit\Framework\TestCase
try {
$build->setSource('5');
} catch (ValidationException $e) {
} catch (InvalidArgumentException $e) {
self::assertEquals(
'Column "source" must be an integer.',
$e->getMessage()
@ -86,7 +86,7 @@ class BuildTest extends \PHPUnit\Framework\TestCase
try {
$build->setId(null);
} catch (ValidationException $e) {
} catch (InvalidArgumentException $e) {
self::assertEquals(
'Column "id" must not be null.',
$e->getMessage()

View file

@ -2,7 +2,7 @@
namespace Tests\PHPCensor\Model;
use PHPCensor\Exception\HttpException\ValidationException;
use PHPCensor\Exception\InvalidArgumentException;
use PHPCensor\Model\Project;
use PHPCensor\Model;
@ -20,7 +20,7 @@ class ProjectTest extends \PHPUnit\Framework\TestCase
try {
$project->setArchived('true');
} catch (ValidationException $e) {
} catch (InvalidArgumentException $e) {
self::assertEquals(
'Column "archived" must be a boolean.',
$e->getMessage()

View file

@ -34,13 +34,15 @@ class FactoryTest extends \PHPUnit\Framework\TestCase {
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);
}
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");
}
@ -53,10 +55,8 @@ class FactoryTest extends \PHPUnit\Framework\TestCase {
public function testBuildPluginThrowsExceptionIfMissingResourcesForRequiredArg()
{
$this->setExpectedException(
'DomainException',
'Unsatisfied dependency: requiredArgument'
);
self::expectException('\DomainException');
self::expectExceptionMessage('Unsatisfied dependency: requiredArgument');
$pluginClass = $this->getFakePluginClassName('ExamplePluginWithSingleRequiredArg');
$plugin = $this->testedFactory->buildPlugin($pluginClass);

View file

@ -4,6 +4,7 @@ namespace Tests\PHPCensor;
use PHPCensor\Config;
use PHPCensor\Database;
use PHPCensor\Exception\InvalidArgumentException;
use PHPCensor\Store\Factory;
use PHPCensor\Model\Project;
use PHPCensor\Model\ProjectGroup;
@ -177,7 +178,7 @@ class StoreMysqlTest extends \PHPUnit_Extensions_Database_TestCase
try {
$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());
}
@ -233,7 +234,7 @@ class StoreMysqlTest extends \PHPUnit_Extensions_Database_TestCase
$model->setId(1);
$testStore->save($model);
} catch (\InvalidArgumentException $e) {
} catch (InvalidArgumentException $e) {
self::assertEquals(
'PHPCensor\Model\Project is an invalid model type for this store.',
$e->getMessage()
@ -258,7 +259,7 @@ class StoreMysqlTest extends \PHPUnit_Extensions_Database_TestCase
$model->setId(1);
$testStore->delete($model);
} catch (\InvalidArgumentException $e) {
} catch (InvalidArgumentException $e) {
self::assertEquals(
'PHPCensor\Model\Project is an invalid model type for this store.',
$e->getMessage()

View file

@ -4,6 +4,7 @@ namespace Tests\PHPCensor;
use PHPCensor\Config;
use PHPCensor\Database;
use PHPCensor\Exception\InvalidArgumentException;
use PHPCensor\Store\Factory;
use PHPCensor\Model\Project;
use PHPCensor\Model\ProjectGroup;
@ -164,7 +165,7 @@ class StorePostgresqlTest extends \PHPUnit_Extensions_Database_TestCase
try {
$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());
}
@ -228,7 +229,7 @@ class StorePostgresqlTest extends \PHPUnit_Extensions_Database_TestCase
$model->setId(1);
$testStore->save($model);
} catch (\InvalidArgumentException $e) {
} catch (InvalidArgumentException $e) {
self::assertEquals(
'PHPCensor\Model\Project is an invalid model type for this store.',
$e->getMessage()
@ -253,7 +254,7 @@ class StorePostgresqlTest extends \PHPUnit_Extensions_Database_TestCase
$model->setId(1);
$testStore->delete($model);
} catch (\InvalidArgumentException $e) {
} catch (InvalidArgumentException $e) {
self::assertEquals(
'PHPCensor\Model\Project is an invalid model type for this store.',
$e->getMessage()