/test/RegexTest.php

https://github.com/zendframework/zend-validator · PHP · 182 lines · 114 code · 17 blank · 51 comment · 0 complexity · b764e91706f5c163fec9cf357cd3270c MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\Validator;
  10. use PHPUnit\Framework\TestCase;
  11. use Zend\Validator\Regex;
  12. use ReflectionProperty;
  13. use Zend\Validator\Exception\InvalidArgumentException;
  14. /**
  15. * @group Zend_Validator
  16. */
  17. class RegexTest extends TestCase
  18. {
  19. /**
  20. * Ensures that the validator follows expected behavior
  21. *
  22. * @return void
  23. */
  24. public function testBasic()
  25. {
  26. /**
  27. * The elements of each array are, in order:
  28. * - pattern
  29. * - expected validation result
  30. * - array of test input values
  31. */
  32. $valuesExpected = [
  33. ['/[a-z]/', true, ['abc123', 'foo', 'a', 'z']],
  34. ['/[a-z]/', false, ['123', 'A']]
  35. ];
  36. foreach ($valuesExpected as $element) {
  37. $validator = new Regex($element[0]);
  38. foreach ($element[2] as $input) {
  39. $this->assertEquals($element[1], $validator->isValid($input));
  40. }
  41. }
  42. }
  43. /**
  44. * Ensures that getMessages() returns expected default value
  45. *
  46. * @return void
  47. */
  48. public function testGetMessages()
  49. {
  50. $validator = new Regex('/./');
  51. $this->assertEquals([], $validator->getMessages());
  52. }
  53. /**
  54. * Ensures that getPattern() returns expected value
  55. *
  56. * @return void
  57. */
  58. public function testGetPattern()
  59. {
  60. $validator = new Regex('/./');
  61. $this->assertEquals('/./', $validator->getPattern());
  62. }
  63. /**
  64. * Ensures that a bad pattern results in a thrown exception upon isValid() call
  65. *
  66. * @return void
  67. */
  68. public function testBadPattern()
  69. {
  70. $this->expectException(InvalidArgumentException::class);
  71. $this->expectExceptionMessage('Internal error parsing');
  72. $validator = new Regex('/');
  73. }
  74. /**
  75. * @ZF-4352
  76. */
  77. public function testNonStringValidation()
  78. {
  79. $validator = new Regex('/./');
  80. $this->assertFalse($validator->isValid([1 => 1]));
  81. }
  82. /**
  83. * @ZF-11863
  84. * @dataProvider specialCharValidationProvider
  85. */
  86. public function testSpecialCharValidation($expected, $input)
  87. {
  88. $validator = new Regex('/^[[:alpha:]\']+$/iu');
  89. $this->assertEquals(
  90. $expected,
  91. $validator->isValid($input),
  92. 'Reason: ' . implode('', $validator->getMessages())
  93. );
  94. }
  95. /**
  96. * The elements of each array are, in order:
  97. * - expected validation result
  98. * - test input value
  99. */
  100. public function specialCharValidationProvider()
  101. {
  102. return [
  103. [true, 'test'],
  104. [true, 'òèùtestòò'],
  105. [true, 'testà'],
  106. [true, 'teààst'],
  107. [true, 'ààòòìùéé'],
  108. [true, 'èùòìiieeà'],
  109. [false, 'test99'],
  110. ];
  111. }
  112. public function testEqualsMessageTemplates()
  113. {
  114. $validator = new Regex('//');
  115. $this->assertAttributeEquals(
  116. $validator->getOption('messageTemplates'),
  117. 'messageTemplates',
  118. $validator
  119. );
  120. }
  121. public function testEqualsMessageVariables()
  122. {
  123. $validator = new Regex('//');
  124. $this->assertAttributeEquals(
  125. $validator->getOption('messageVariables'),
  126. 'messageVariables',
  127. $validator
  128. );
  129. }
  130. public function invalidConstructorArgumentsProvider()
  131. {
  132. return [
  133. 'true' => [true],
  134. 'false' => [false],
  135. 'zero' => [0],
  136. 'int' => [1],
  137. 'zero-float' => [0.0],
  138. 'float' => [1.0],
  139. 'object' => [(object) []],
  140. ];
  141. }
  142. /**
  143. * @dataProvider invalidConstructorArgumentsProvider
  144. */
  145. public function testConstructorRaisesExceptionWhenProvidedInvalidArguments($options)
  146. {
  147. $this->expectException(InvalidArgumentException::class);
  148. $validator = new Regex($options);
  149. }
  150. public function testConstructorRaisesExceptionWhenProvidedWithInvalidOptionsArray()
  151. {
  152. $options = ['foo' => 'bar'];
  153. $this->expectException(InvalidArgumentException::class);
  154. $validator = new Regex($options);
  155. }
  156. public function testIsValidShouldReturnFalseWhenRegexPatternIsInvalid()
  157. {
  158. $validator = new Regex('//');
  159. $pattern = '/';
  160. $r = new ReflectionProperty($validator, 'pattern');
  161. $r->setAccessible(true);
  162. $r->setValue($validator, $pattern);
  163. $this->assertFalse($validator->isValid('test'));
  164. }
  165. }