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

77 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.
*/
declare(strict_types=1);
2015-01-26 13:10:54 +01:00
namespace Respect\Validation\Rules;
2017-11-04 11:21:40 +01:00
use PHPUnit\Framework\TestCase;
2015-01-26 13:10:54 +01:00
/**
* @group rule
2017-02-04 14:01:14 +01:00
* @covers \Respect\Validation\Rules\FilterVar
* @covers \Respect\Validation\Exceptions\FilterVarException
2015-01-26 13:10:54 +01:00
*/
2017-11-04 11:21:40 +01:00
class FilterVarTest extends TestCase
2015-01-26 13:10:54 +01:00
{
/**
2017-02-04 14:01:14 +01:00
* @expectedException \Respect\Validation\Exceptions\ComponentException
2015-01-26 13:10:54 +01:00
* @expectedExceptionMessage Cannot validate without filter flag
*/
public function testShouldThrowsExceptionWhenFilterIsNotDefined(): void
2015-01-26 13:10:54 +01:00
{
new FilterVar();
}
/**
2017-02-04 14:01:14 +01:00
* @expectedException \Respect\Validation\Exceptions\ComponentException
2015-01-26 13:10:54 +01:00
* @expectedExceptionMessage Cannot accept the given filter
*/
public function testShouldThrowsExceptionWhenFilterIsNotValid(): void
2015-01-26 13:10:54 +01:00
{
new FilterVar(FILTER_SANITIZE_EMAIL);
}
public function testShouldDefineFilterOnConstructor(): void
2015-01-26 13:10:54 +01:00
{
$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
self::assertEquals($expectedArguments, $actualArguments);
2015-01-26 13:10:54 +01:00
}
public function testShouldDefineFilterOptionsOnConstructor(): void
2015-01-26 13:10:54 +01:00
{
$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
self::assertEquals($expectedArguments, $actualArguments);
2015-01-26 13:10:54 +01:00
}
public function testShouldUseDefineFilterToValidate(): void
2015-01-26 13:10:54 +01:00
{
$rule = new FilterVar(FILTER_VALIDATE_EMAIL);
self::assertTrue($rule->validate('henriquemoody@users.noreply.github.com'));
2015-01-26 13:10:54 +01:00
}
public function testShouldUseDefineFilterOptionsToValidate(): void
2015-01-26 13:10:54 +01:00
{
$rule = new FilterVar(FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED);
self::assertTrue($rule->validate('http://example.com?foo=bar'));
2015-01-26 13:10:54 +01:00
}
}