RegexTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Tests\Constraints;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Validator\Constraints\Regex;
  13. /**
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. */
  16. class RegexTest extends TestCase
  17. {
  18. public function testConstraintGetDefaultOption()
  19. {
  20. $constraint = new Regex('/^[0-9]+$/');
  21. $this->assertSame('/^[0-9]+$/', $constraint->pattern);
  22. }
  23. public function provideHtmlPatterns()
  24. {
  25. return array(
  26. // HTML5 wraps the pattern in ^(?:pattern)$
  27. array('/^[0-9]+$/', '[0-9]+'),
  28. array('/[0-9]+$/', '.*[0-9]+'),
  29. array('/^[0-9]+/', '[0-9]+.*'),
  30. array('/[0-9]+/', '.*[0-9]+.*'),
  31. // We need a smart way to allow matching of patterns that contain
  32. // ^ and $ at various sub-clauses of an or-clause
  33. // .*(pattern).* seems to work correctly
  34. array('/[0-9]$|[a-z]+/', '.*([0-9]$|[a-z]+).*'),
  35. array('/[0-9]$|^[a-z]+/', '.*([0-9]$|^[a-z]+).*'),
  36. array('/^[0-9]|[a-z]+$/', '.*(^[0-9]|[a-z]+$).*'),
  37. // Unescape escaped delimiters
  38. array('/^[0-9]+\/$/', '[0-9]+/'),
  39. array('#^[0-9]+\#$#', '[0-9]+#'),
  40. // Cannot be converted
  41. array('/^[0-9]+$/i', null),
  42. // Inverse matches are simple, just wrap in
  43. // ((?!pattern).)*
  44. array('/^[0-9]+$/', '((?!^[0-9]+$).)*', false),
  45. array('/[0-9]+$/', '((?![0-9]+$).)*', false),
  46. array('/^[0-9]+/', '((?!^[0-9]+).)*', false),
  47. array('/[0-9]+/', '((?![0-9]+).)*', false),
  48. array('/[0-9]$|[a-z]+/', '((?![0-9]$|[a-z]+).)*', false),
  49. array('/[0-9]$|^[a-z]+/', '((?![0-9]$|^[a-z]+).)*', false),
  50. array('/^[0-9]|[a-z]+$/', '((?!^[0-9]|[a-z]+$).)*', false),
  51. array('/^[0-9]+\/$/', '((?!^[0-9]+/$).)*', false),
  52. array('#^[0-9]+\#$#', '((?!^[0-9]+#$).)*', false),
  53. array('/^[0-9]+$/i', null, false),
  54. );
  55. }
  56. /**
  57. * @dataProvider provideHtmlPatterns
  58. */
  59. public function testGetHtmlPattern($pattern, $htmlPattern, $match = true)
  60. {
  61. $constraint = new Regex(array(
  62. 'pattern' => $pattern,
  63. 'match' => $match,
  64. ));
  65. $this->assertSame($pattern, $constraint->pattern);
  66. $this->assertSame($htmlPattern, $constraint->getHtmlPattern());
  67. }
  68. public function testGetCustomHtmlPattern()
  69. {
  70. $constraint = new Regex(array(
  71. 'pattern' => '((?![0-9]$|[a-z]+).)*',
  72. 'htmlPattern' => 'foobar',
  73. ));
  74. $this->assertSame('((?![0-9]$|[a-z]+).)*', $constraint->pattern);
  75. $this->assertSame('foobar', $constraint->getHtmlPattern());
  76. }
  77. }