RegexTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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-2010 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. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. /**
  27. * @see Zend_Validate_Regex
  28. */
  29. require_once 'Zend/Validate/Regex.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Validate
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Validate
  37. */
  38. class Zend_Validate_RegexTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * Ensures that the validator follows expected behavior
  42. *
  43. * @return void
  44. */
  45. public function testBasic()
  46. {
  47. /**
  48. * The elements of each array are, in order:
  49. * - pattern
  50. * - expected validation result
  51. * - array of test input values
  52. */
  53. $valuesExpected = array(
  54. array('/[a-z]/', true, array('abc123', 'foo', 'a', 'z')),
  55. array('/[a-z]/', false, array('123', 'A'))
  56. );
  57. foreach ($valuesExpected as $element) {
  58. $validator = new Zend_Validate_Regex($element[0]);
  59. foreach ($element[2] as $input) {
  60. $this->assertEquals($element[1], $validator->isValid($input));
  61. }
  62. }
  63. }
  64. /**
  65. * Ensures that getMessages() returns expected default value
  66. *
  67. * @return void
  68. */
  69. public function testGetMessages()
  70. {
  71. $validator = new Zend_Validate_Regex('/./');
  72. $this->assertEquals(array(), $validator->getMessages());
  73. }
  74. /**
  75. * Ensures that getPattern() returns expected value
  76. *
  77. * @return void
  78. */
  79. public function testGetPattern()
  80. {
  81. $validator = new Zend_Validate_Regex('/./');
  82. $this->assertEquals('/./', $validator->getPattern());
  83. }
  84. /**
  85. * Ensures that a bad pattern results in a thrown exception upon isValid() call
  86. *
  87. * @return void
  88. */
  89. public function testBadPattern()
  90. {
  91. $validator = new Zend_Validate_Regex('/');
  92. try {
  93. $validator->isValid('anything');
  94. $this->fail('Expected Zend_Validate_Exception not thrown for bad pattern');
  95. } catch (Zend_Validate_Exception $e) {
  96. $this->assertContains('Internal error matching pattern', $e->getMessage());
  97. }
  98. }
  99. /**
  100. * @ZF-4352
  101. */
  102. public function testNonStringValidation()
  103. {
  104. $validator = new Zend_Validate_Regex('/');
  105. $this->assertFalse($validator->isValid(array(1 => 1)));
  106. }
  107. }