PostCodeTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: PostCodeTest.php 17798 2009-08-24 20:07:53Z thomas $
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Validate_PostCodeTest::main');
  24. }
  25. /**
  26. * Test helper
  27. */
  28. require_once dirname(__FILE__) . '/../../TestHelper.php';
  29. /**
  30. * @see Zend_Validate_PostCode
  31. */
  32. require_once 'Zend/Validate/PostCode.php';
  33. /**
  34. * @category Zend
  35. * @package Zend_Validate
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2009 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 Zend_Validate_PostCodeTest extends PHPUnit_Framework_TestCase
  42. {
  43. /**
  44. * Zend_Validate_PostCode object
  45. *
  46. * @var Zend_Validate_PostCode
  47. */
  48. protected $_validator;
  49. /**
  50. * Runs this test suite
  51. *
  52. * @return void
  53. */
  54. public static function main()
  55. {
  56. $suite = new PHPUnit_Framework_TestSuite('Zend_Validate_PostCodeTest');
  57. $result = PHPUnit_TextUI_TestRunner::run($suite);
  58. }
  59. /**
  60. * Creates a new Zend_Validate_PostCode object for each test method
  61. *
  62. * @return void
  63. */
  64. public function setUp()
  65. {
  66. $this->_validator = new Zend_Validate_PostCode('de_AT');
  67. }
  68. /**
  69. * Ensures that the validator follows expected behavior
  70. *
  71. * @return void
  72. */
  73. public function testBasic()
  74. {
  75. $valuesExpected = array(
  76. array('2292', true),
  77. array('1000', true),
  78. array('0000', true),
  79. array('12345', false),
  80. array(1234, true),
  81. array(9821, true),
  82. array('21A4', false),
  83. array('ABCD', false),
  84. array(true, false),
  85. array('AT-2292', false),
  86. array(1.56, false)
  87. );
  88. foreach ($valuesExpected as $element) {
  89. $this->assertEquals($element[1], $this->_validator->isValid($element[0]),
  90. 'Test failed with ' . var_export($element, 1));
  91. }
  92. }
  93. /**
  94. * Ensures that getMessages() returns expected default value
  95. *
  96. * @return void
  97. */
  98. public function testGetMessages()
  99. {
  100. $this->assertEquals(array(), $this->_validator->getMessages());
  101. }
  102. /**
  103. * Ensures that a region is available
  104. */
  105. public function testSettingLocalesWithoutRegion()
  106. {
  107. try {
  108. $this->_validator->setLocale('de');
  109. $this->fail();
  110. } catch (Zend_Validate_Exception $e) {
  111. $this->assertContains('Unable to detect a region', $e->getMessage());
  112. }
  113. }
  114. /**
  115. * Ensures that the region contains postal codes
  116. */
  117. public function testSettingLocalesWithoutPostalCodes()
  118. {
  119. try {
  120. $this->_validator->setLocale('gez_ER');
  121. $this->fail();
  122. } catch (Zend_Validate_Exception $e) {
  123. $this->assertContains('Unable to detect a format', $e->getMessage());
  124. }
  125. }
  126. /**
  127. * Ensures locales can be retrieved
  128. */
  129. public function testGettingLocale()
  130. {
  131. $this->assertEquals('de_AT', $this->_validator->getLocale());
  132. }
  133. /**
  134. * Ensures format can be set and retrieved
  135. */
  136. public function testSetGetFormat()
  137. {
  138. $this->_validator->setFormat('\d{1}');
  139. $this->assertEquals('/^\d{1}$/', $this->_validator->getFormat());
  140. $this->_validator->setFormat('/^\d{1}');
  141. $this->assertEquals('/^\d{1}$/', $this->_validator->getFormat());
  142. $this->_validator->setFormat('/^\d{1}$/');
  143. $this->assertEquals('/^\d{1}$/', $this->_validator->getFormat());
  144. $this->_validator->setFormat('\d{1}$/');
  145. $this->assertEquals('/^\d{1}$/', $this->_validator->getFormat());
  146. try {
  147. $this->_validator->setFormat(null);
  148. $this->fail();
  149. } catch (Zend_Validate_Exception $e) {
  150. $this->assertContains('a not empty string', $e->getMessage());
  151. }
  152. try {
  153. $this->_validator->setFormat('');
  154. $this->fail();
  155. } catch (Zend_Validate_Exception $e) {
  156. $this->assertContains('a not empty string', $e->getMessage());
  157. }
  158. }
  159. }
  160. if (PHPUnit_MAIN_METHOD == 'Zend_Validate_PostCodeTest::main') {
  161. Zend_Validate_PostCodeTest::main();
  162. }