Skip to content

Commit

Permalink
增加DB验证器 (swoft-cloud/swoft-component#146)
Browse files Browse the repository at this point in the history
* 增加DB验证器

* bigint validate

* 增加bigint、tinyint、smallint Validator的单测

* 完善bigint Validator 以及对应单测

* 完善单测
  • Loading branch information
limingxinleo authored and huangzhhui committed Jul 31, 2018
1 parent a10b862 commit b3ec760
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 9 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ before_install:
PRIMARY KEY (`id`)
) ENGINE = INNODB DEFAULT CHARSET = utf8 ;
CREATE TABLE `user2` (
`id` INT (11) NOT NULL AUTO_INCREMENT,
`id` BIGINT (20) NOT NULL AUTO_INCREMENT,
`oid` BIGINT (20) NOT NULL DEFAULT '0',
`name` VARCHAR (20) DEFAULT NULL,
`sex` INT (1) NOT NULL DEFAULT '0',
`age` INT (1) NOT NULL DEFAULT '0',
`sex` TINYINT (1) NOT NULL DEFAULT '0',
`age` SMALLINT (1) NOT NULL DEFAULT '0',
`description` VARCHAR (240) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE = INNODB DEFAULT CHARSET = utf8 ;
Expand Down
46 changes: 46 additions & 0 deletions src/Validator/BigintValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* This file is part of Swoft.
*
* @link https://swoft.org
* @document https://doc.swoft.org
* @contact [email protected]
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/
namespace Swoft\Db\Validator;

use Swoft\Bean\Annotation\Bean;
use Swoft\Exception\ValidatorException;

/**
* @Bean("DbBigintValidator")
*/
class BigintValidator implements ValidatorInterface
{
/**
* @param string $column Colunm name
* @param mixed $value Column value
* @param array ...$params Other parameters
* @throws ValidatorException When validation failures, will throw an Exception
* @return bool When validation successful
*/
public function validate(string $column, $value, ...$params): bool
{
$min = '-9223372036854775808';
$max = '18446744073709551615';

if (!preg_match("/^-?[1-9][0-9]*$/", $value)) {
throw new ValidatorException("数据库字段值验证失败,当前值 {$value} 不为有效整数,column={$column}");
}

if (bccomp($value, $min) === -1) {
throw new ValidatorException("数据库字段值验证失败,当前值小于{$min},column={$column}");
}

if(bccomp($value, $max) === 1){
throw new ValidatorException("数据库字段值验证失败,当前值大于{$max},column={$column}");
}

return true;
}
}
37 changes: 37 additions & 0 deletions src/Validator/SmallintValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* This file is part of Swoft.
*
* @link https://swoft.org
* @document https://doc.swoft.org
* @contact [email protected]
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/
namespace Swoft\Db\Validator;

use Swoft\Bean\Annotation\Bean;
use Swoft\Exception\ValidatorException;

/**
* @Bean("DbSmallintValidator")
*/
class SmallintValidator implements ValidatorInterface
{
/**
* @param string $column Colunm name
* @param mixed $value Column value
* @param array ...$params Other parameters
* @throws ValidatorException When validation failures, will throw an Exception
* @return bool When validation successful
*/
public function validate(string $column, $value, ...$params): bool
{
if (!\is_int($value)) {
throw new ValidatorException('数据库字段值验证失败,不是int类型,column=' . $column);
}
if ($value > 65535 || $value < -32768) {
throw new ValidatorException('数据库字段值验证失败,字段超过smallint大小范围,column=' . $column);
}
return true;
}
}
37 changes: 37 additions & 0 deletions src/Validator/TinyintValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* This file is part of Swoft.
*
* @link https://swoft.org
* @document https://doc.swoft.org
* @contact [email protected]
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/
namespace Swoft\Db\Validator;

use Swoft\Bean\Annotation\Bean;
use Swoft\Exception\ValidatorException;

/**
* @Bean("DbTinyintValidator")
*/
class TinyintValidator implements ValidatorInterface
{
/**
* @param string $column Colunm name
* @param mixed $value Column value
* @param array ...$params Other parameters
* @throws ValidatorException When validation failures, will throw an Exception
* @return bool When validation successful
*/
public function validate(string $column, $value, ...$params): bool
{
if (!\is_int($value)) {
throw new ValidatorException('数据库字段值验证失败,不是int类型,column=' . $column);
}
if ($value > 255 || $value < -128) {
throw new ValidatorException('数据库字段值验证失败,字段超过tinyint大小范围,column=' . $column);
}
return true;
}
}
92 changes: 90 additions & 2 deletions test/Cases/Mysql/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Swoft\Db\Query;
use Swoft\Db\QueryBuilder;
use Swoft\Exception\ValidatorException;
use SwoftTest\Db\Cases\AbstractMysqlCase;
use SwoftTest\Db\Testing\Entity\User;
use SwoftTest\Db\Testing\Entity\User2;
Expand All @@ -34,6 +35,13 @@ public function testSave()
$this->assertTrue($reuslt);
}

public function testSaveByCo()
{
go(function () {
$this->testSave();
});
}

public function testUpdateWhenNameIsNull()
{
$user = new User2();
Expand All @@ -51,10 +59,90 @@ public function testUpdateWhenNameIsNull()
$this->assertTrue($reuslt);
}

public function testSaveByCo()
public function testUpdateWhenNameIsNullByCo()
{
go(function () {
$this->testSave();
$this->testUpdateWhenNameIsNull();
});
}

public function testValidator()
{
$user = new User2();
$user->setName('limx');
$user->setSex(1);
$user->setOid(144);
$user->setDesc('hello world');
$user->setAge(27);

$id = $user->save()->getResult();
$reuslt = $id > 0;
$this->assertTrue($reuslt);

$user->setOid('18446744073709551615');
$row = $user->update()->getResult();
$this->assertEquals(1, $row);

try {
$user->setOid('18446744073709551616');
$user->update();
throw new \Exception('validate error');
} catch (\Exception $ex) {
$this->assertInstanceOf(ValidatorException::class, $ex);
}

try {
$user->setOid('xxx');
$user->update();
throw new \Exception('validate error');
} catch (\Exception $ex) {
$this->assertInstanceOf(ValidatorException::class, $ex);
}

try {
$user->setOid('-9223372036854775809');
$user->update();
throw new \Exception('validate error');
} catch (\Exception $ex) {
$this->assertInstanceOf(ValidatorException::class, $ex);
}

// tinyint Validator
try {
$user = new User2();
$user->setName('limx');
$user->setSex(256);
$user->setDesc('hello world');
$user->setAge(27);
$user->save()->getResult();
throw new \Exception('validate error');
} catch (\Exception $ex) {
$this->assertInstanceOf(ValidatorException::class, $ex);
}

// smallint Validator
try {
$user = new User2();
$user->setName('limx');
$user->setSex(1);
$user->setDesc('hello world');
$user->setAge(65536);

$user->save()->getResult();
throw new \Exception('validate error');
} catch (\Exception $ex) {
$this->assertInstanceOf(ValidatorException::class, $ex);
}

/** @var User2 $user */
$user = User2::findById($id)->getResult();
$this->assertEquals('18446744073709551615', $user->getOid());
}

public function testValidatorByCo()
{
go(function () {
$this->testValidator();
});
}

Expand Down
30 changes: 26 additions & 4 deletions test/Testing/Entity/User2.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,39 @@

/**
* @Entity()
* @Table(name="user")
* @Table(name="user2")
*/
class User2 extends Model
{
/**
* @Id()
* @Column(name="id", type=Types::INT)
* @Column(name="id", type="bigint")
* @var null|int
*/
private $id;

/**
* @Column(name="oid", type="bigint")
* @var int
*/
private $oid;

/**
* @Column(name="name", type=Types::STRING, length=20)
* @var null|string
*/
private $name;

/**
* @Column(name="age", type=Types::INT)
* @Column(name="age", type="smallint")
* @var int
*/
private $age;

/**
* 性别
*
* @Column(name="sex", type="int")
* @Column(name="sex", type="tinyint")
* @var int
*/
private $sex;
Expand Down Expand Up @@ -77,6 +83,22 @@ public function setId($id)
$this->id = $id;
}

/**
* @return int|null
*/
public function getOid()
{
return $this->oid;
}

/**
* @param int|null $oid
*/
public function setOid($oid)
{
$this->oid = $oid;
}

/**
* @return null|string
*/
Expand Down

0 comments on commit b3ec760

Please sign in to comment.