PostCodeTest.php 5.2 KB

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