RegexTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Validate_Regex
  24. */
  25. require_once 'Zend/Validate/Regex.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Validate
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Validate
  33. */
  34. class Zend_Validate_RegexTest extends PHPUnit_Framework_TestCase
  35. {
  36. /**
  37. * Ensures that the validator follows expected behavior
  38. *
  39. * @return void
  40. */
  41. public function testBasic()
  42. {
  43. /**
  44. * The elements of each array are, in order:
  45. * - pattern
  46. * - expected validation result
  47. * - array of test input values
  48. */
  49. $valuesExpected = array(
  50. array('/[a-z]/', true, array('abc123', 'foo', 'a', 'z')),
  51. array('/[a-z]/', false, array('123', 'A'))
  52. );
  53. foreach ($valuesExpected as $element) {
  54. $validator = new Zend_Validate_Regex($element[0]);
  55. foreach ($element[2] as $input) {
  56. $this->assertEquals($element[1], $validator->isValid($input));
  57. }
  58. }
  59. }
  60. /**
  61. * Ensures that getMessages() returns expected default value
  62. *
  63. * @return void
  64. */
  65. public function testGetMessages()
  66. {
  67. $validator = new Zend_Validate_Regex('/./');
  68. $this->assertEquals(array(), $validator->getMessages());
  69. }
  70. /**
  71. * Ensures that getPattern() returns expected value
  72. *
  73. * @return void
  74. */
  75. public function testGetPattern()
  76. {
  77. $validator = new Zend_Validate_Regex('/./');
  78. $this->assertEquals('/./', $validator->getPattern());
  79. }
  80. /**
  81. * Ensures that a bad pattern results in a thrown exception upon isValid() call
  82. *
  83. * @return void
  84. */
  85. public function testBadPattern()
  86. {
  87. try {
  88. $validator = new Zend_Validate_Regex('/');
  89. $validator->isValid('anything');
  90. $this->fail('Expected Zend_Validate_Exception not thrown for bad pattern');
  91. } catch (Zend_Validate_Exception $e) {
  92. $this->assertContains('Internal error while', $e->getMessage());
  93. }
  94. }
  95. /**
  96. * @ZF-4352
  97. */
  98. public function testNonStringValidation()
  99. {
  100. $validator = new Zend_Validate_Regex('/./');
  101. $this->assertFalse($validator->isValid(array(1 => 1)));
  102. }
  103. }