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