[ZF-10060] Unittests:
[zend/radio.git] / tests / Zend / Validate / MessageTest.php
blob6c91c4ab2a4f4c6ec126445ca7bdcc8118429228
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
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$
23 // Call Zend_Validate_MessageTest::main() if this source file is executed directly.
24 if (!defined('PHPUnit_MAIN_METHOD')) {
25 define('PHPUnit_MAIN_METHOD', 'Zend_Validate_MessageTest::main');
28 /**
29 * Test helper
31 require_once dirname(__FILE__) . '/../../TestHelper.php';
33 /**
34 * @see Zend_Validate_StringLength
36 require_once 'Zend/Validate/StringLength.php';
39 /**
40 * @category Zend
41 * @package Zend_Validate
42 * @subpackage UnitTests
43 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
44 * @license http://framework.zend.com/license/new-bsd New BSD License
45 * @group Zend_Validate
47 class Zend_Validate_MessageTest extends PHPUnit_Framework_TestCase
49 /**
50 * Default instance created for all test methods
52 * @var Zend_Validate_StringLength
54 protected $_validator;
56 public static function main()
58 $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
59 $result = PHPUnit_TextUI_TestRunner::run($suite);
62 /**
63 * Creates a new Zend_Validate_StringLength object for each test method
65 * @return void
67 public function setUp()
69 $this->_validator = new Zend_Validate_StringLength(4, 8);
72 /**
73 * Ensures that we can change a specified message template by its key
74 * and that this message is returned when the input is invalid.
76 * @return void
78 public function testSetMessage()
80 $inputInvalid = 'abcdefghij';
81 $this->assertFalse($this->_validator->isValid($inputInvalid));
82 $messages = $this->_validator->getMessages();
83 $this->assertEquals("'$inputInvalid' is more than 8 characters long", current($messages));
85 $this->_validator->setMessage(
86 'Your value is too long',
87 Zend_Validate_StringLength::TOO_LONG
90 $this->assertFalse($this->_validator->isValid('abcdefghij'));
91 $messages = $this->_validator->getMessages();
92 $this->assertEquals('Your value is too long', current($messages));
95 /**
96 * Ensures that if we don't specify the message key, it uses
97 * the first one in the list of message templates.
98 * In the case of Zend_Validate_StringLength, TOO_SHORT is
99 * the one we should expect to change.
101 * @return void
103 public function testSetMessageDefaultKey()
105 $this->_validator->setMessage(
106 'Your value is too short', Zend_Validate_StringLength::TOO_SHORT
109 $this->assertFalse($this->_validator->isValid('abc'));
110 $messages = $this->_validator->getMessages();
111 $this->assertEquals('Your value is too short', current($messages));
112 $errors = $this->_validator->getErrors();
113 $this->assertEquals(Zend_Validate_StringLength::TOO_SHORT, current($errors));
117 * Ensures that we can include the %value% parameter in the message,
118 * and that it is substituted with the value we are validating.
120 * @return void
122 public function testSetMessageWithValueParam()
124 $this->_validator->setMessage(
125 "Your value '%value%' is too long",
126 Zend_Validate_StringLength::TOO_LONG
129 $inputInvalid = 'abcdefghij';
130 $this->assertFalse($this->_validator->isValid($inputInvalid));
131 $messages = $this->_validator->getMessages();
132 $this->assertEquals("Your value '$inputInvalid' is too long", current($messages));
136 * Ensures that we can include another parameter, defined on a
137 * class-by-class basis, in the message string.
138 * In the case of Zend_Validate_StringLength, one such parameter
139 * is %max%.
141 * @return void
143 public function testSetMessageWithOtherParam()
145 $this->_validator->setMessage(
146 'Your value is too long, it should be no longer than %max%',
147 Zend_Validate_StringLength::TOO_LONG
150 $inputInvalid = 'abcdefghij';
151 $this->assertFalse($this->_validator->isValid($inputInvalid));
152 $messages = $this->_validator->getMessages();
153 $this->assertEquals('Your value is too long, it should be no longer than 8', current($messages));
157 * Ensures that if we set a parameter in the message that is not
158 * known to the validator class, it is not changed; %shazam% is
159 * left as literal text in the message.
161 * @return void
163 public function testSetMessageWithUnknownParam()
165 $this->_validator->setMessage(
166 'Your value is too long, and btw, %shazam%!',
167 Zend_Validate_StringLength::TOO_LONG
170 $inputInvalid = 'abcdefghij';
171 $this->assertFalse($this->_validator->isValid($inputInvalid));
172 $messages = $this->_validator->getMessages();
173 $this->assertEquals('Your value is too long, and btw, %shazam%!', current($messages));
177 * Ensures that the validator throws an exception when we
178 * try to set a message for a key that is unknown to the class.
180 * @return void
182 public function testSetMessageExceptionInvalidKey()
184 $keyInvalid = 'invalidKey';
185 try {
186 $this->_validator->setMessage(
187 'Your value is too long',
188 $keyInvalid
190 $this->fail('Expected to catch Zend_Validate_Exception');
191 } catch (Zend_Exception $e) {
192 $this->assertType('Zend_Validate_Exception', $e,
193 'Expected exception of type Zend_Validate_Exception, got ' . get_class($e));
194 $this->assertEquals("No message template exists for key '$keyInvalid'", $e->getMessage());
199 * Ensures that we can set more than one message at a time,
200 * by passing an array of key/message pairs. Both messages
201 * should be defined.
203 * @return void
205 public function testSetMessages()
207 $this->_validator->setMessages(
208 array(
209 Zend_Validate_StringLength::TOO_LONG => 'Your value is too long',
210 Zend_Validate_StringLength::TOO_SHORT => 'Your value is too short'
214 $this->assertFalse($this->_validator->isValid('abcdefghij'));
215 $messages = $this->_validator->getMessages();
216 $this->assertEquals('Your value is too long', current($messages));
218 $this->assertFalse($this->_validator->isValid('abc'));
219 $messages = $this->_validator->getMessages();
220 $this->assertEquals('Your value is too short', current($messages));
224 * Ensures that the magic getter gives us access to properties
225 * that are permitted to be substituted in the message string.
226 * The access is by the parameter name, not by the protected
227 * property variable name.
229 * @return void
231 public function testGetProperty()
233 $this->_validator->setMessage(
234 'Your value is too long',
235 Zend_Validate_StringLength::TOO_LONG
238 $inputInvalid = 'abcdefghij';
240 $this->assertFalse($this->_validator->isValid($inputInvalid));
241 $messages = $this->_validator->getMessages();
242 $this->assertEquals('Your value is too long', current($messages));
244 $this->assertEquals($inputInvalid, $this->_validator->value);
245 $this->assertEquals(8, $this->_validator->max);
246 $this->assertEquals(4, $this->_validator->min);
250 * Ensures that the class throws an exception when we try to
251 * access a property that doesn't exist as a parameter.
253 * @return void
255 public function testGetPropertyException()
257 $this->_validator->setMessage(
258 'Your value is too long',
259 Zend_Validate_StringLength::TOO_LONG
262 $this->assertFalse($this->_validator->isValid('abcdefghij'));
263 $messages = $this->_validator->getMessages();
264 $this->assertEquals('Your value is too long', current($messages));
266 try {
267 $property = $this->_validator->unknownProperty;
268 $this->fail('Expected to catch Zend_Validate_Exception');
269 } catch (Zend_Exception $e) {
270 $this->assertType('Zend_Validate_Exception', $e,
271 'Expected exception of type Zend_Validate_Exception, got ' . get_class($e));
272 $this->assertEquals("No property exists by the name 'unknownProperty'", $e->getMessage());
277 * Ensures that the getError() function returns an array of
278 * message key values corresponding to the messages.
280 * @return void
282 public function testGetErrors()
284 $inputInvalid = 'abcdefghij';
285 $this->assertFalse($this->_validator->isValid($inputInvalid));
287 $messages = $this->_validator->getMessages();
288 $this->assertEquals("'$inputInvalid' is more than 8 characters long", current($messages));
290 $errors = $this->_validator->getErrors();
291 $this->assertEquals(Zend_Validate_StringLength::TOO_LONG, current($errors));
295 * Ensures that getMessageVariables() returns an array of
296 * strings and that these strings that can be used as variables
297 * in a message.
299 public function testGetMessageVariables()
301 $vars = $this->_validator->getMessageVariables();
303 $this->assertType('array', $vars);
304 $this->assertEquals(array('min', 'max'), $vars);
305 $message = 'variables: %notvar% ';
306 foreach ($vars as $var) {
307 $message .= "%$var% ";
309 $this->_validator->setMessage($message, Zend_Validate_StringLength::TOO_SHORT);
311 $this->assertFalse($this->_validator->isValid('abc'));
312 $messages = $this->_validator->getMessages();
313 $this->assertEquals('variables: %notvar% 4 8 ', current($messages));
318 // Call Zend_Validate_MessageTest::main() if this source file is executed directly.
319 if (PHPUnit_MAIN_METHOD == 'Zend_Validate_MessageTest::main') {
320 Zend_Validate_MessageTest::main();