MessageTest.php 11 KB

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