View source
<?php
namespace Constraints;
use Symfony\Component\Validator\Constraints\Regex;
class RegexTest extends \PHPUnit_Framework_TestCase {
public function testConstraintGetDefaultOption() {
$constraint = new Regex('/^[0-9]+$/');
$this
->assertSame('/^[0-9]+$/', $constraint->pattern);
}
public function provideHtmlPatterns() {
return array(
array(
'/^[0-9]+$/',
'[0-9]+',
),
array(
'/[0-9]+$/',
'.*[0-9]+',
),
array(
'/^[0-9]+/',
'[0-9]+.*',
),
array(
'/[0-9]+/',
'.*[0-9]+.*',
),
array(
'/[0-9]$|[a-z]+/',
'.*([0-9]$|[a-z]+).*',
),
array(
'/[0-9]$|^[a-z]+/',
'.*([0-9]$|^[a-z]+).*',
),
array(
'/^[0-9]|[a-z]+$/',
'.*(^[0-9]|[a-z]+$).*',
),
array(
'/^[0-9]+\\/$/',
'[0-9]+/',
),
array(
'#^[0-9]+\\#$#',
'[0-9]+#',
),
array(
'/^[0-9]+$/i',
null,
),
array(
'/^[0-9]+$/',
'((?!^[0-9]+$).)*',
false,
),
array(
'/[0-9]+$/',
'((?![0-9]+$).)*',
false,
),
array(
'/^[0-9]+/',
'((?!^[0-9]+).)*',
false,
),
array(
'/[0-9]+/',
'((?![0-9]+).)*',
false,
),
array(
'/[0-9]$|[a-z]+/',
'((?![0-9]$|[a-z]+).)*',
false,
),
array(
'/[0-9]$|^[a-z]+/',
'((?![0-9]$|^[a-z]+).)*',
false,
),
array(
'/^[0-9]|[a-z]+$/',
'((?!^[0-9]|[a-z]+$).)*',
false,
),
array(
'/^[0-9]+\\/$/',
'((?!^[0-9]+/$).)*',
false,
),
array(
'#^[0-9]+\\#$#',
'((?!^[0-9]+#$).)*',
false,
),
array(
'/^[0-9]+$/i',
null,
false,
),
);
}
public function testGetHtmlPattern($pattern, $htmlPattern, $match = true) {
$constraint = new Regex(array(
'pattern' => $pattern,
'match' => $match,
));
$this
->assertSame($pattern, $constraint->pattern);
$this
->assertSame($htmlPattern, $constraint
->getHtmlPattern());
}
public function testGetCustomHtmlPattern() {
$constraint = new Regex(array(
'pattern' => '((?![0-9]$|[a-z]+).)*',
'htmlPattern' => 'foobar',
));
$this
->assertSame('((?![0-9]$|[a-z]+).)*', $constraint->pattern);
$this
->assertSame('foobar', $constraint
->getHtmlPattern());
}
}