engelsystem/tests/Unit/Http/Validation/ValidatorTest.php

165 lines
4.2 KiB
PHP
Raw Normal View History

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()
{
$val = new Validator();
2019-07-16 01:39:54 +02:00
2019-07-09 21:43:18 +02:00
$this->assertTrue($val->validate(
['foo' => 'bar', 'lorem' => 'on', 'dolor' => 'bla'],
['lorem' => 'accepted']
2019-07-09 21:43:18 +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(
['lorem' => 'OMG'],
['lorem' => 'required|min:4|max:10']
2019-07-16 01:39:54 +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']
));
}
/**
* @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()
{
$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']
);
}
/**
* @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
$this->assertTrue($val->validate(
['foo' => 'bar'],
['foo' => 'required']
));
$this->assertTrue($val->validate(
['foo' => '0'],
['foo' => 'int']
));
$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
}