2
0

MessageTest.php 10.0 KB

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