diff --git a/src/Command/CreateAdminCommand.php b/src/Command/CreateAdminCommand.php index 34335573..ae767ef2 100644 --- a/src/Command/CreateAdminCommand.php +++ b/src/Command/CreateAdminCommand.php @@ -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; diff --git a/src/Command/CreateBuildCommand.php b/src/Command/CreateBuildCommand.php index 237d85b3..14004134 100644 --- a/src/Command/CreateBuildCommand.php +++ b/src/Command/CreateBuildCommand.php @@ -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 { diff --git a/src/Command/InstallCommand.php b/src/Command/InstallCommand.php index dba9396b..326a383f 100644 --- a/src/Command/InstallCommand.php +++ b/src/Command/InstallCommand.php @@ -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; diff --git a/src/Exception/HttpException/ValidationException.php b/src/Exception/HttpException/ValidationException.php deleted file mode 100644 index 3bdda75c..00000000 --- a/src/Exception/HttpException/ValidationException.php +++ /dev/null @@ -1,18 +0,0 @@ -routes, ['route' => $route, 'callback' => $callback, 'defaults' => $options]); diff --git a/src/Model.php b/src/Model.php index 292610eb..6fe8dfe3 100644 --- a/src/Model.php +++ b/src/Model.php @@ -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.'); } } } diff --git a/src/Model/Base/Build.php b/src/Model/Base/Build.php index c356aa2b..908a3adf 100644 --- a/src/Model/Base/Build.php +++ b/src/Model/Base/Build.php @@ -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) . '.' ); } diff --git a/src/Model/Base/Project.php b/src/Model/Base/Project.php index 15e0c250..82565f14 100644 --- a/src/Model/Base/Project.php +++ b/src/Model/Base/Project.php @@ -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) . '.' ); } diff --git a/src/Plugin/Util/Factory.php b/src/Plugin/Util/Factory.php index ea55b1f8..4dd2ae2f 100644 --- a/src/Plugin/Util/Factory.php +++ b/src/Plugin/Util/Factory.php @@ -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' ); } diff --git a/src/Store.php b/src/Store.php index ce218f92..89a90087 100644 --- a/src/Store.php +++ b/src/Store.php @@ -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) { diff --git a/tests/src/Command/CreateBuildCommandTest.php b/tests/src/Command/CreateBuildCommandTest.php index 06cb7bd1..ff08008d 100644 --- a/tests/src/Command/CreateBuildCommandTest.php +++ b/tests/src/Command/CreateBuildCommandTest.php @@ -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() { diff --git a/tests/src/HttpExceptionTest.php b/tests/src/HttpExceptionTest.php index b328870a..53865e48 100755 --- a/tests/src/HttpExceptionTest.php +++ b/tests/src/HttpExceptionTest.php @@ -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'); - } - } } diff --git a/tests/src/Model/Base/BuildTest.php b/tests/src/Model/Base/BuildTest.php index db000c94..23b6e104 100644 --- a/tests/src/Model/Base/BuildTest.php +++ b/tests/src/Model/Base/BuildTest.php @@ -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); } diff --git a/tests/src/Model/Base/ProjectTest.php b/tests/src/Model/Base/ProjectTest.php index de1ebc9b..3792c966 100644 --- a/tests/src/Model/Base/ProjectTest.php +++ b/tests/src/Model/Base/ProjectTest.php @@ -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'); } diff --git a/tests/src/Model/BuildTest.php b/tests/src/Model/BuildTest.php index e34c2c31..56360d81 100644 --- a/tests/src/Model/BuildTest.php +++ b/tests/src/Model/BuildTest.php @@ -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() diff --git a/tests/src/Model/ProjectTest.php b/tests/src/Model/ProjectTest.php index ef9eda58..419e43c1 100644 --- a/tests/src/Model/ProjectTest.php +++ b/tests/src/Model/ProjectTest.php @@ -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() diff --git a/tests/src/Plugin/Util/FactoryTest.php b/tests/src/Plugin/Util/FactoryTest.php index b760b24a..e8e7f1c2 100644 --- a/tests/src/Plugin/Util/FactoryTest.php +++ b/tests/src/Plugin/Util/FactoryTest.php @@ -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); diff --git a/tests/src/StoreMysqlTest.php b/tests/src/StoreMysqlTest.php index 0bd3981f..80693b37 100755 --- a/tests/src/StoreMysqlTest.php +++ b/tests/src/StoreMysqlTest.php @@ -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() diff --git a/tests/src/StorePostgresqlTest.php b/tests/src/StorePostgresqlTest.php index 5f3bef70..1c6e17cd 100755 --- a/tests/src/StorePostgresqlTest.php +++ b/tests/src/StorePostgresqlTest.php @@ -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()