respect-validation/tests/unit/Rules/FilterVarTest.php

73 lines
2.1 KiB
PHP
Raw Normal View History

2015-01-26 13:10:54 +01:00
<?php
2015-06-08 16:47:14 +02:00
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
2015-01-26 13:10:54 +01:00
namespace Respect\Validation\Rules;
/**
* @group rule
2015-01-26 13:10:54 +01:00
* @covers Respect\Validation\Rules\FilterVar
* @covers Respect\Validation\Exceptions\FilterVarException
2015-01-26 13:10:54 +01:00
*/
class FilterVarTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException Respect\Validation\Exceptions\ComponentException
* @expectedExceptionMessage Cannot validate without filter flag
*/
public function testShouldThrowsExceptionWhenFilterIsNotDefined()
{
new FilterVar();
}
/**
* @expectedException Respect\Validation\Exceptions\ComponentException
* @expectedExceptionMessage Cannot accept the given filter
*/
public function testShouldThrowsExceptionWhenFilterIsNotValid()
{
new FilterVar(FILTER_SANITIZE_EMAIL);
}
public function testShouldDefineFilterOnConstructor()
{
$rule = new FilterVar(FILTER_VALIDATE_REGEXP);
$actualArguments = $rule->arguments;
2015-10-18 03:44:47 +02:00
$expectedArguments = [FILTER_VALIDATE_REGEXP];
2015-01-26 13:10:54 +01:00
$this->assertEquals($expectedArguments, $actualArguments);
}
public function testShouldDefineFilterOptionsOnConstructor()
{
$rule = new FilterVar(FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED);
$actualArguments = $rule->arguments;
2015-10-18 03:44:47 +02:00
$expectedArguments = [FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED];
2015-01-26 13:10:54 +01:00
$this->assertEquals($expectedArguments, $actualArguments);
}
public function testShouldUseDefineFilterToValidate()
{
$rule = new FilterVar(FILTER_VALIDATE_EMAIL);
$this->assertTrue($rule->validate('henriquemoody@users.noreply.github.com'));
}
public function testShouldUseDefineFilterOptionsToValidate()
{
$rule = new FilterVar(FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED);
$this->assertTrue($rule->validate('http://example.com?foo=bar'));
}
}