Skip to content

Commit

Permalink
regex and ipv4 serialize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TomK committed Aug 21, 2020
1 parent 3fc4257 commit 26aa7b0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
use Packaged\Validate\Validators\EmailValidator;
use Packaged\Validate\Validators\EnumValidator;
use Packaged\Validate\Validators\IntegerValidator;
use Packaged\Validate\Validators\IPv4AddressValidator;
use Packaged\Validate\Validators\MultiValidator;
use Packaged\Validate\Validators\RegexValidator;
use Packaged\Validate\Validators\SchemaValidator;
use Packaged\Validate\Validators\StringValidator;

class Validation
Expand All @@ -25,24 +28,24 @@ public static function bind()
if(empty(static::$_validators))
{
static::$_validators = $_validators = [
ArrayKeysValidator::serializeType() => ArrayKeysValidator::class,
ArrayValidator::serializeType() => ArrayValidator::class,
BoolValidator::serializeType() => BoolValidator::class,
ConstEnumValidator::serializeType() => ConstEnumValidator::class,
//DecimalValidator::serializeType() => DecimalValidator::class,
EmailValidator::serializeType() => EmailValidator::class,
EnumValidator::serializeType() => EnumValidator::class,
//EqualValidator::serializeType() => EqualValidator::class,
IntegerValidator::serializeType() => IntegerValidator::class,
//IPv4ValidatorTest::serializeType() => IPv4ValidatorTest::class,
MultiValidator::serializeType() => MultiValidator::class,
//NullableValidator::serializeType() => NullableValidator::class,
//NumberValidator::serializeType() => NumberValidator::class,
//OptionalValidator::serializeType() => OptionalValidator::class,
//PropertiesValidator::serializeType() => PropertiesValidator::class,
//RegexValidator::serializeType() => RegexValidator::class,
//SchemaValidator::serializeType() => SchemaValidator::class,
StringValidator::serializeType() => StringValidator::class,
ArrayKeysValidator::serializeType() => ArrayKeysValidator::class,
ArrayValidator::serializeType() => ArrayValidator::class,
BoolValidator::serializeType() => BoolValidator::class,
ConstEnumValidator::serializeType() => ConstEnumValidator::class,
// DecimalValidator::serializeType() => DecimalValidator::class,
EmailValidator::serializeType() => EmailValidator::class,
EnumValidator::serializeType() => EnumValidator::class,
// EqualValidator::serializeType() => EqualValidator::class,
IntegerValidator::serializeType() => IntegerValidator::class,
IPv4AddressValidator::serializeType() => IPv4AddressValidator::class,
MultiValidator::serializeType() => MultiValidator::class,
// NullableValidator::serializeType() => NullableValidator::class,
// NumberValidator::serializeType() => NumberValidator::class,
// OptionalValidator::serializeType() => OptionalValidator::class,
// PropertiesValidator::serializeType() => PropertiesValidator::class,
RegexValidator::serializeType() => RegexValidator::class,
SchemaValidator::serializeType() => SchemaValidator::class,
StringValidator::serializeType() => StringValidator::class,
];
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/IPv4ValidatorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Packaged\Validate\Tests;

use Packaged\Validate\Validation;
use Packaged\Validate\Validators\IPv4AddressValidator;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -40,4 +41,16 @@ public function testIpv4Validator($address, $isValid)
$this->assertNotEmpty($errors);
}
}

public function testSerialize()
{
$validator = new IPv4AddressValidator();
$this->assertTrue($validator->isValid('255.255.255.255'));

$jsn = json_encode($validator);
$unsValidator = Validation::fromJsonObject(json_decode($jsn));
$this->assertInstanceOf(get_class($validator), $unsValidator);
$this->assertTrue($validator->isValid('255.255.255.255'));
$this->assertEquals(json_encode($validator), json_encode($unsValidator));
}
}
13 changes: 13 additions & 0 deletions tests/RegexValidatorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Packaged\Validate\Tests;

use Packaged\Validate\Validation;
use Packaged\Validate\Validators\RegexValidator;
use PHPUnit\Framework\TestCase;

Expand All @@ -17,4 +18,16 @@ public function testRegexValidatorMessage()
$this->assertNotEmpty($v2err);
$this->assertEquals('test failure message', $v2err[0]->getMessage());
}

public function testSerialize()
{
$validator = new RegexValidator('/^[0-9]{6}$/');
$this->assertTrue($validator->isValid('123456'));

$jsn = json_encode($validator);
$unsValidator = Validation::fromJsonObject(json_decode($jsn));
$this->assertInstanceOf(get_class($validator), $unsValidator);
$this->assertTrue($validator->isValid('123456'));
$this->assertEquals(json_encode($validator), json_encode($unsValidator));
}
}

0 comments on commit 26aa7b0

Please sign in to comment.