2
0

MessageTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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$
  21. */
  22. // Call Zend_Validate_MessageTest::main() if this source file is executed directly.
  23. if (!defined('PHPUnit_MAIN_METHOD')) {
  24. define('PHPUnit_MAIN_METHOD', 'Zend_Validate_MessageTest::main');
  25. }
  26. /**
  27. * Test helper
  28. */
  29. require_once dirname(__FILE__) . '/../../TestHelper.php';
  30. /**
  31. * @see Zend_Validate_StringLength
  32. */
  33. require_once 'Zend/Validate/StringLength.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Validate
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class Zend_Validate_MessageTest extends PHPUnit_Framework_TestCase
  42. {
  43. /**
  44. * Default instance created for all test methods
  45. *
  46. * @var Zend_Validate_StringLength
  47. */
  48. protected $_validator;
  49. public static function main()
  50. {
  51. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  52. $result = PHPUnit_TextUI_TestRunner::run($suite);
  53. }
  54. /**
  55. * Creates a new Zend_Validate_StringLength object for each test method
  56. *
  57. * @return void
  58. */
  59. public function setUp()
  60. {
  61. $this->_validator = new Zend_Validate_StringLength(4, 8);
  62. }
  63. /**
  64. * Ensures that we can change a specified message template by its key
  65. * and that this message is returned when the input is invalid.
  66. *
  67. * @return void
  68. */
  69. public function testSetMessage()
  70. {
  71. $inputInvalid = 'abcdefghij';
  72. $this->assertFalse($this->_validator->isValid($inputInvalid));
  73. $messages = $this->_validator->getMessages();
  74. $this->assertEquals("'$inputInvalid' is greater than 8 characters long", current($messages));
  75. $this->_validator->setMessage(
  76. 'Your value is too long',
  77. Zend_Validate_StringLength::TOO_LONG
  78. );
  79. $this->assertFalse($this->_validator->isValid('abcdefghij'));
  80. $messages = $this->_validator->getMessages();
  81. $this->assertEquals('Your value is too long', current($messages));
  82. }
  83. /**
  84. * Ensures that if we don't specify the message key, it uses
  85. * the first one in the list of message templates.
  86. * In the case of Zend_Validate_StringLength, TOO_SHORT is
  87. * the one we should expect to change.
  88. *
  89. * @return void
  90. */
  91. public function testSetMessageDefaultKey()
  92. {
  93. $this->_validator->setMessage(
  94. 'Your value is too short', Zend_Validate_StringLength::TOO_SHORT
  95. );
  96. $this->assertFalse($this->_validator->isValid('abc'));
  97. $messages = $this->_validator->getMessages();
  98. $this->assertEquals('Your value is too short', current($messages));
  99. $errors = $this->_validator->getErrors();
  100. $this->assertEquals(Zend_Validate_StringLength::TOO_SHORT, current($errors));
  101. }
  102. /**
  103. * Ensures that we can include the %value% parameter in the message,
  104. * and that it is substituted with the value we are validating.
  105. *
  106. * @return void
  107. */
  108. public function testSetMessageWithValueParam()
  109. {
  110. $this->_validator->setMessage(
  111. "Your value '%value%' is too long",
  112. Zend_Validate_StringLength::TOO_LONG
  113. );
  114. $inputInvalid = 'abcdefghij';
  115. $this->assertFalse($this->_validator->isValid($inputInvalid));
  116. $messages = $this->_validator->getMessages();
  117. $this->assertEquals("Your value '$inputInvalid' is too long", current($messages));
  118. }
  119. /**
  120. * Ensures that we can include another parameter, defined on a
  121. * class-by-class basis, in the message string.
  122. * In the case of Zend_Validate_StringLength, one such parameter
  123. * is %max%.
  124. *
  125. * @return void
  126. */
  127. public function testSetMessageWithOtherParam()
  128. {
  129. $this->_validator->setMessage(
  130. 'Your value is too long, it should be no longer than %max%',
  131. Zend_Validate_StringLength::TOO_LONG
  132. );
  133. $inputInvalid = 'abcdefghij';
  134. $this->assertFalse($this->_validator->isValid($inputInvalid));
  135. $messages = $this->_validator->getMessages();
  136. $this->assertEquals('Your value is too long, it should be no longer than 8', current($messages));
  137. }
  138. /**
  139. * Ensures that if we set a parameter in the message that is not
  140. * known to the validator class, it is not changed; %shazam% is
  141. * left as literal text in the message.
  142. *
  143. * @return void
  144. */
  145. public function testSetMessageWithUnknownParam()
  146. {
  147. $this->_validator->setMessage(
  148. 'Your value is too long, and btw, %shazam%!',
  149. Zend_Validate_StringLength::TOO_LONG
  150. );
  151. $inputInvalid = 'abcdefghij';
  152. $this->assertFalse($this->_validator->isValid($inputInvalid));
  153. $messages = $this->_validator->getMessages();
  154. $this->assertEquals('Your value is too long, and btw, %shazam%!', current($messages));
  155. }
  156. /**
  157. * Ensures that the validator throws an exception when we
  158. * try to set a message for a key that is unknown to the class.
  159. *
  160. * @return void
  161. */
  162. public function testSetMessageExceptionInvalidKey()
  163. {
  164. $keyInvalid = 'invalidKey';
  165. try {
  166. $this->_validator->setMessage(
  167. 'Your value is too long',
  168. $keyInvalid
  169. );
  170. $this->fail('Expected to catch Zend_Validate_Exception');
  171. } catch (Zend_Exception $e) {
  172. $this->assertType('Zend_Validate_Exception', $e,
  173. 'Expected exception of type Zend_Validate_Exception, got ' . get_class($e));
  174. $this->assertEquals("No message template exists for key '$keyInvalid'", $e->getMessage());
  175. }
  176. }
  177. /**
  178. * Ensures that we can set more than one message at a time,
  179. * by passing an array of key/message pairs. Both messages
  180. * should be defined.
  181. *
  182. * @return void
  183. */
  184. public function testSetMessages()
  185. {
  186. $this->_validator->setMessages(
  187. array(
  188. Zend_Validate_StringLength::TOO_LONG => 'Your value is too long',
  189. Zend_Validate_StringLength::TOO_SHORT => 'Your value is too short'
  190. )
  191. );
  192. $this->assertFalse($this->_validator->isValid('abcdefghij'));
  193. $messages = $this->_validator->getMessages();
  194. $this->assertEquals('Your value is too long', current($messages));
  195. $this->assertFalse($this->_validator->isValid('abc'));
  196. $messages = $this->_validator->getMessages();
  197. $this->assertEquals('Your value is too short', current($messages));
  198. }
  199. /**
  200. * Ensures that the magic getter gives us access to properties
  201. * that are permitted to be substituted in the message string.
  202. * The access is by the parameter name, not by the protected
  203. * property variable name.
  204. *
  205. * @return void
  206. */
  207. public function testGetProperty()
  208. {
  209. $this->_validator->setMessage(
  210. 'Your value is too long',
  211. Zend_Validate_StringLength::TOO_LONG
  212. );
  213. $inputInvalid = 'abcdefghij';
  214. $this->assertFalse($this->_validator->isValid($inputInvalid));
  215. $messages = $this->_validator->getMessages();
  216. $this->assertEquals('Your value is too long', current($messages));
  217. $this->assertEquals($inputInvalid, $this->_validator->value);
  218. $this->assertEquals(8, $this->_validator->max);
  219. $this->assertEquals(4, $this->_validator->min);
  220. }
  221. /**
  222. * Ensures that the class throws an exception when we try to
  223. * access a property that doesn't exist as a parameter.
  224. *
  225. * @return void
  226. */
  227. public function testGetPropertyException()
  228. {
  229. $this->_validator->setMessage(
  230. 'Your value is too long',
  231. Zend_Validate_StringLength::TOO_LONG
  232. );
  233. $this->assertFalse($this->_validator->isValid('abcdefghij'));
  234. $messages = $this->_validator->getMessages();
  235. $this->assertEquals('Your value is too long', current($messages));
  236. try {
  237. $property = $this->_validator->unknownProperty;
  238. $this->fail('Expected to catch Zend_Validate_Exception');
  239. } catch (Zend_Exception $e) {
  240. $this->assertType('Zend_Validate_Exception', $e,
  241. 'Expected exception of type Zend_Validate_Exception, got ' . get_class($e));
  242. $this->assertEquals("No property exists by the name 'unknownProperty'", $e->getMessage());
  243. }
  244. }
  245. /**
  246. * Ensures that the getError() function returns an array of
  247. * message key values corresponding to the messages.
  248. *
  249. * @return void
  250. */
  251. public function testGetErrors()
  252. {
  253. $inputInvalid = 'abcdefghij';
  254. $this->assertFalse($this->_validator->isValid($inputInvalid));
  255. $messages = $this->_validator->getMessages();
  256. $this->assertEquals("'$inputInvalid' is greater than 8 characters long", current($messages));
  257. $errors = $this->_validator->getErrors();
  258. $this->assertEquals(Zend_Validate_StringLength::TOO_LONG, current($errors));
  259. }
  260. /**
  261. * Ensures that getMessageVariables() returns an array of
  262. * strings and that these strings that can be used as variables
  263. * in a message.
  264. */
  265. public function testGetMessageVariables()
  266. {
  267. $vars = $this->_validator->getMessageVariables();
  268. $this->assertType('array', $vars);
  269. $this->assertEquals(array('min', 'max'), $vars);
  270. $message = 'variables: %notvar% ';
  271. foreach ($vars as $var) {
  272. $message .= "%$var% ";
  273. }
  274. $this->_validator->setMessage($message, Zend_Validate_StringLength::TOO_SHORT);
  275. $this->assertFalse($this->_validator->isValid('abc'));
  276. $messages = $this->_validator->getMessages();
  277. $this->assertEquals('variables: %notvar% 4 8 ', current($messages));
  278. }
  279. }
  280. // Call Zend_Validate_MessageTest::main() if this source file is executed directly.
  281. if (PHPUnit_MAIN_METHOD == 'Zend_Validate_MessageTest::main') {
  282. Zend_Validate_MessageTest::main();
  283. }