2019-07-09 21:43:18 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Http\Validation;
|
|
|
|
|
|
|
|
use Engelsystem\Http\Validation\Validator;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
class ValidatorTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Validation\Validator::validate
|
|
|
|
* @covers \Engelsystem\Http\Validation\Validator::getData
|
|
|
|
* @covers \Engelsystem\Http\Validation\Validator::getErrors
|
|
|
|
*/
|
|
|
|
public function testValidate()
|
|
|
|
{
|
2019-07-10 13:34:15 +02:00
|
|
|
$val = new Validator();
|
2019-07-16 01:39:54 +02:00
|
|
|
|
2019-07-09 21:43:18 +02:00
|
|
|
$this->assertTrue($val->validate(
|
2019-07-10 13:34:15 +02:00
|
|
|
['foo' => 'bar', 'lorem' => 'on', 'dolor' => 'bla'],
|
|
|
|
['lorem' => 'accepted']
|
2019-07-09 21:43:18 +02:00
|
|
|
));
|
2019-07-10 13:34:15 +02:00
|
|
|
$this->assertEquals(['lorem' => 'on'], $val->getData());
|
2019-07-09 21:43:18 +02:00
|
|
|
|
|
|
|
$this->assertFalse($val->validate(
|
|
|
|
[],
|
|
|
|
['lorem' => 'required|min:3']
|
|
|
|
));
|
|
|
|
$this->assertEquals(
|
|
|
|
['lorem' => ['validation.lorem.required', 'validation.lorem.min']],
|
|
|
|
$val->getErrors()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:39:54 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Validation\Validator::validate
|
|
|
|
*/
|
|
|
|
public function testValidateChaining()
|
|
|
|
{
|
|
|
|
$val = new Validator();
|
|
|
|
|
|
|
|
$this->assertTrue($val->validate(
|
|
|
|
['lorem' => 10],
|
|
|
|
['lorem' => 'required|min:3|max:10']
|
|
|
|
));
|
|
|
|
$this->assertTrue($val->validate(
|
|
|
|
['lorem' => 3],
|
|
|
|
['lorem' => 'required|min:3|max:10']
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse($val->validate(
|
2019-10-07 21:59:40 +02:00
|
|
|
['lorem' => 'OMG'],
|
|
|
|
['lorem' => 'required|min:4|max:10']
|
2019-07-16 01:39:54 +02:00
|
|
|
));
|
2019-10-07 21:59:40 +02:00
|
|
|
$this->assertEquals(['lorem' => ['validation.lorem.min']], $val->getErrors());
|
2019-07-16 01:39:54 +02:00
|
|
|
$this->assertFalse($val->validate(
|
|
|
|
['lorem' => 42],
|
|
|
|
['lorem' => 'required|min:3|max:10']
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2022-10-03 18:58:04 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Validation\Validator::validate
|
|
|
|
*/
|
|
|
|
public function testValidateMultipleParameters()
|
|
|
|
{
|
|
|
|
$val = new Validator();
|
|
|
|
|
|
|
|
$this->assertFalse($val->validate(
|
|
|
|
['lorem' => 'h'],
|
|
|
|
['lorem' => 'length:2:3']
|
|
|
|
));
|
|
|
|
$this->assertTrue($val->validate(
|
|
|
|
['lorem' => 'hey'],
|
|
|
|
['lorem' => 'length:2:3']
|
|
|
|
));
|
|
|
|
$this->assertFalse($val->validate(
|
|
|
|
['lorem' => 'heyy'],
|
|
|
|
['lorem' => 'length:2:3']
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2019-07-09 21:43:18 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Validation\Validator::validate
|
|
|
|
*/
|
|
|
|
public function testValidateNotImplemented()
|
|
|
|
{
|
2019-07-10 13:34:15 +02:00
|
|
|
$val = new Validator();
|
2019-07-16 01:39:54 +02:00
|
|
|
|
2019-07-09 21:43:18 +02:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
|
|
|
|
$val->validate(
|
|
|
|
['lorem' => 'bar'],
|
|
|
|
['foo' => 'never_implemented']
|
|
|
|
);
|
|
|
|
}
|
2019-07-10 13:34:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Validation\Validator::map
|
|
|
|
* @covers \Engelsystem\Http\Validation\Validator::mapBack
|
|
|
|
*/
|
|
|
|
public function testValidateMapping()
|
|
|
|
{
|
|
|
|
$val = new Validator();
|
2019-07-16 01:39:54 +02:00
|
|
|
|
2019-07-10 13:34:15 +02:00
|
|
|
$this->assertTrue($val->validate(
|
|
|
|
['foo' => 'bar'],
|
|
|
|
['foo' => 'required']
|
|
|
|
));
|
|
|
|
$this->assertTrue($val->validate(
|
|
|
|
['foo' => '0'],
|
|
|
|
['foo' => 'int']
|
|
|
|
));
|
2022-12-08 17:40:24 +01:00
|
|
|
$this->assertFalse($val->validate(
|
|
|
|
['foo' => '0.0'],
|
|
|
|
['foo' => 'int']
|
|
|
|
));
|
|
|
|
$this->assertTrue($val->validate(
|
|
|
|
['foo' => '0.0'],
|
|
|
|
['foo' => 'float']
|
|
|
|
));
|
2019-07-10 13:34:15 +02:00
|
|
|
$this->assertTrue($val->validate(
|
|
|
|
['foo' => 'on'],
|
|
|
|
['foo' => 'accepted']
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse($val->validate(
|
|
|
|
[],
|
|
|
|
['lorem' => 'required']
|
|
|
|
));
|
|
|
|
$this->assertEquals(
|
|
|
|
['lorem' => ['validation.lorem.required']],
|
|
|
|
$val->getErrors()
|
|
|
|
);
|
|
|
|
}
|
2019-07-16 01:39:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Validation\Validator::validate
|
|
|
|
*/
|
|
|
|
public function testValidateNesting()
|
|
|
|
{
|
|
|
|
$val = new Validator();
|
|
|
|
|
|
|
|
$this->assertTrue($val->validate(
|
|
|
|
[],
|
|
|
|
['foo' => 'not|required']
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertTrue($val->validate(
|
|
|
|
['foo' => 'foo'],
|
|
|
|
['foo' => 'not|int']
|
|
|
|
));
|
|
|
|
$this->assertFalse($val->validate(
|
|
|
|
['foo' => 1],
|
|
|
|
['foo' => 'not|int']
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertTrue($val->validate(
|
|
|
|
[],
|
|
|
|
['foo' => 'optional|int']
|
|
|
|
));
|
|
|
|
$this->assertTrue($val->validate(
|
|
|
|
['foo' => '33'],
|
|
|
|
['foo' => 'optional|int']
|
|
|
|
));
|
|
|
|
$this->assertFalse($val->validate(
|
|
|
|
['foo' => 'T'],
|
|
|
|
['foo' => 'optional|int']
|
|
|
|
));
|
|
|
|
}
|
2019-07-09 21:43:18 +02:00
|
|
|
}
|