/tests/Zend/Validator/RegexTest.php

https://github.com/Exercise/zf2 · PHP · 119 lines · 44 code · 9 blank · 66 comment · 0 complexity · 3c00a3d4110b80baf30183d7add67e79 MD5 · raw file

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