2. Sobre
• Biblioteca de validação criada por Alexandre Gaigalas (Alganet)
• PHP 5.3+ e HHVM 3.3+
• Interface fluente
• Mais de 100 regras de validação
• Mais de 175 mil instalações via Composer
• Média de 13 mil instalações por mês via Composer
11. Método check()
try {
v::stringType()->length(2, 15)->check(0);
} catch (ValidationExceptionInterface $exception) {
echo $exception->getMainMessage();
}
// Resultado:
//
// 0 must be a string
12. Método check()
try {
v::stringType()->length(2, 15)->check('A');
} catch (ValidationExceptionInterface $exception) {
echo $exception->getMainMessage();
}
// Resultado:
//
// "A" must have a length between 2 and 15
13. Método assert()
try {
v::stringType()->length(2, 15)->assert(0);
} catch (NestedValidationExceptionInterface $exception) {
echo $exception->getFullMessage();
}
// Resultado:
//
// -All of the required rules must pass for 0
// |-0 must be a string
// -0 must have a length between 2 and 15
15. Não utilizar estáticos
use RespectValidationRules;
use RespectValidationValidator;
$validator = new Validator();
$validator->addRule(new RulesKey('email', new RulesEmail()));
$validator->assert($_POST);
16. Reutilizar cadeia de validação
$validator = v::stringType()->length(2, 15);
$validator->assert($input1);
$validator->check($input2);
if ($validator->validate($input3)) {
// ...
}
if ($validator($input4)) {
// ...
}
17. Obter as mensagens em formato
de array
try {
v::stringType()->length(2, 15)->assert(0);
} catch (NestedValidationExceptionInterface $exception) {
print_r($exception->getMessages());
}
// Resultado:
//
// Array
// (
// [0] => 0 must be a string
// [1] => 0 must have a length between 2 and 15
// )
26. Teste unitário: IntTypeTest
namespace RespectValidationRules;
/**
* @group rule
* @covers RespectValidationRulesIntType
*/
class IntTypeTest extends PHPUnit_Framework_TestCase
{
}
27. Teste1: Sucesso
// ...
public function providerForValidIntType()
{
return array(
array(0),
array(123456),
array(PHP_INT_MAX),
array(PHP_INT_MAX * -1),
);
}
/**
* @dataProvider providerForValidIntType
*/
public function testShouldValidateInputWhenItIsAValidIntType($input)
{
$rule = new IntType();
$this->assertTrue($rule->validate($input));
}
// ...
28. Teste2: Falha
// ...
public function providerForInvalidIntType()
{
return array(
array('1'),
array(1.0),
array(PHP_INT_MAX + 1),
array(true),
);
}
/**
* @dataProvider providerForInvalidIntType
*/
public function testShouldInvalidateInputWhenItIsNotAValidIntType($input)
{
$rule = new IntType();
$this->assertFalse($rule->validate($input));
}
// ...
29. Exception: IntTypeException
namespace RespectValidationExceptions;
class IntTypeException extends ValidationException
{
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be an integer',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be an integer',
),
);
}
30. Teste de integração: PHPT
--FILE--
<?php
require 'vendor/autoload.php';
use RespectValidationValidator as v;
// ..
?>
--EXPECTF--
32. Teste2: intType_2.phpt
--FILE--
<?php
require 'vendor/autoload.php';
use RespectValidationValidator as v;
use RespectValidationExceptionsIntTypeException;
try {
v::intType()->check('42');
} catch (IntTypeException $exception) {
echo $exception->getMainMessage();
}
?>
--EXPECTF--
"42" must be an integer
33. Teste3: intType_3.phpt
--FILE--
<?php
require 'vendor/autoload.php';
use RespectValidationValidator as v;
use RespectValidationExceptionsAllOfException;
try {
v::intType()->assert('1984');
} catch (AllOfException $exception) {
echo $exception->getFullMessage();
}
?>
--EXPECTF--
-"1984" must be an integer
34. Teste4: intType_4.phpt
--FILE--
<?php
require 'vendor/autoload.php';
use RespectValidationValidator as v;
use RespectValidationExceptionsIntTypeException;
try {
v::not(v::intType())->check(42);
} catch (IntTypeException $exception) {
echo $exception->getMainMessage();
}
?>
--EXPECTF--
42 must not be an integer
35. Teste5: intType_5.phpt
--FILE--
<?php
require 'vendor/autoload.php';
use RespectValidationValidator as v;
use RespectValidationExceptionsAllOfException;
try {
v::not(v::intType())->assert(1984);
} catch (AllOfException $exception) {
echo $exception->getFullMessage();
}
?>
--EXPECTF--
-1984 must not be an integer