RegexTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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-2008 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Validate_RegexTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Ensures that the validator follows expected behavior
  41. *
  42. * @return void
  43. */
  44. public function testBasic()
  45. {
  46. /**
  47. * The elements of each array are, in order:
  48. * - pattern
  49. * - expected validation result
  50. * - array of test input values
  51. */
  52. $valuesExpected = array(
  53. array('/[a-z]/', true, array('abc123', 'foo', 'a', 'z')),
  54. array('/[a-z]/', false, array('123', 'A'))
  55. );
  56. foreach ($valuesExpected as $element) {
  57. $validator = new Zend_Validate_Regex($element[0]);
  58. foreach ($element[2] as $input) {
  59. $this->assertEquals($element[1], $validator->isValid($input));
  60. }
  61. }
  62. }
  63. /**
  64. * Ensures that getMessages() returns expected default value
  65. *
  66. * @return void
  67. */
  68. public function testGetMessages()
  69. {
  70. $validator = new Zend_Validate_Regex('/./');
  71. $this->assertEquals(array(), $validator->getMessages());
  72. }
  73. /**
  74. * Ensures that getPattern() returns expected value
  75. *
  76. * @return void
  77. */
  78. public function testGetPattern()
  79. {
  80. $validator = new Zend_Validate_Regex('/./');
  81. $this->assertEquals('/./', $validator->getPattern());
  82. }
  83. /**
  84. * Ensures that a bad pattern results in a thrown exception upon isValid() call
  85. *
  86. * @return void
  87. */
  88. public function testBadPattern()
  89. {
  90. $validator = new Zend_Validate_Regex('/');
  91. try {
  92. $validator->isValid('anything');
  93. $this->fail('Expected Zend_Validate_Exception not thrown for bad pattern');
  94. } catch (Zend_Validate_Exception $e) {
  95. $this->assertContains('Internal error matching pattern', $e->getMessage());
  96. }
  97. }
  98. }