| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Validate
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- // Call Zend_Validate_MessageTest::main() if this source file is executed directly.
- if (!defined('PHPUnit_MAIN_METHOD')) {
- define('PHPUnit_MAIN_METHOD', 'Zend_Validate_MessageTest::main');
- }
- /**
- * @see Zend_Validate_StringLength
- */
- require_once 'Zend/Validate/StringLength.php';
- /**
- * @category Zend
- * @package Zend_Validate
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_Validate
- */
- class Zend_Validate_MessageTest extends PHPUnit_Framework_TestCase
- {
- /**
- * Default instance created for all test methods
- *
- * @var Zend_Validate_StringLength
- */
- protected $_validator;
- public static function main()
- {
- $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
- $result = PHPUnit_TextUI_TestRunner::run($suite);
- }
- /**
- * Creates a new Zend_Validate_StringLength object for each test method
- *
- * @return void
- */
- public function setUp()
- {
- $this->_validator = new Zend_Validate_StringLength(4, 8);
- }
- /**
- * Ensures that we can change a specified message template by its key
- * and that this message is returned when the input is invalid.
- *
- * @return void
- */
- public function testSetMessage()
- {
- $inputInvalid = 'abcdefghij';
- $this->assertFalse($this->_validator->isValid($inputInvalid));
- $messages = $this->_validator->getMessages();
- $this->assertEquals("'$inputInvalid' is more than 8 characters long", current($messages));
- $this->_validator->setMessage(
- 'Your value is too long',
- Zend_Validate_StringLength::TOO_LONG
- );
- $this->assertFalse($this->_validator->isValid('abcdefghij'));
- $messages = $this->_validator->getMessages();
- $this->assertEquals('Your value is too long', current($messages));
- }
- /**
- * Ensures that if we don't specify the message key, it uses
- * the first one in the list of message templates.
- * In the case of Zend_Validate_StringLength, TOO_SHORT is
- * the one we should expect to change.
- *
- * @return void
- */
- public function testSetMessageDefaultKey()
- {
- $this->_validator->setMessage(
- 'Your value is too short', Zend_Validate_StringLength::TOO_SHORT
- );
- $this->assertFalse($this->_validator->isValid('abc'));
- $messages = $this->_validator->getMessages();
- $this->assertEquals('Your value is too short', current($messages));
- $errors = $this->_validator->getErrors();
- $this->assertEquals(Zend_Validate_StringLength::TOO_SHORT, current($errors));
- }
- /**
- * Ensures that we can include the %value% parameter in the message,
- * and that it is substituted with the value we are validating.
- *
- * @return void
- */
- public function testSetMessageWithValueParam()
- {
- $this->_validator->setMessage(
- "Your value '%value%' is too long",
- Zend_Validate_StringLength::TOO_LONG
- );
- $inputInvalid = 'abcdefghij';
- $this->assertFalse($this->_validator->isValid($inputInvalid));
- $messages = $this->_validator->getMessages();
- $this->assertEquals("Your value '$inputInvalid' is too long", current($messages));
- }
- /**
- * Ensures that we can include another parameter, defined on a
- * class-by-class basis, in the message string.
- * In the case of Zend_Validate_StringLength, one such parameter
- * is %max%.
- *
- * @return void
- */
- public function testSetMessageWithOtherParam()
- {
- $this->_validator->setMessage(
- 'Your value is too long, it should be no longer than %max%',
- Zend_Validate_StringLength::TOO_LONG
- );
- $inputInvalid = 'abcdefghij';
- $this->assertFalse($this->_validator->isValid($inputInvalid));
- $messages = $this->_validator->getMessages();
- $this->assertEquals('Your value is too long, it should be no longer than 8', current($messages));
- }
- /**
- * Ensures that if we set a parameter in the message that is not
- * known to the validator class, it is not changed; %shazam% is
- * left as literal text in the message.
- *
- * @return void
- */
- public function testSetMessageWithUnknownParam()
- {
- $this->_validator->setMessage(
- 'Your value is too long, and btw, %shazam%!',
- Zend_Validate_StringLength::TOO_LONG
- );
- $inputInvalid = 'abcdefghij';
- $this->assertFalse($this->_validator->isValid($inputInvalid));
- $messages = $this->_validator->getMessages();
- $this->assertEquals('Your value is too long, and btw, %shazam%!', current($messages));
- }
- /**
- * Ensures that the validator throws an exception when we
- * try to set a message for a key that is unknown to the class.
- *
- * @return void
- */
- public function testSetMessageExceptionInvalidKey()
- {
- $keyInvalid = 'invalidKey';
- try {
- $this->_validator->setMessage(
- 'Your value is too long',
- $keyInvalid
- );
- $this->fail('Expected to catch Zend_Validate_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Validate_Exception,
- 'Expected exception of type Zend_Validate_Exception, got ' . get_class($e));
- $this->assertEquals("No message template exists for key '$keyInvalid'", $e->getMessage());
- }
- }
- /**
- * Ensures that we can set more than one message at a time,
- * by passing an array of key/message pairs. Both messages
- * should be defined.
- *
- * @return void
- */
- public function testSetMessages()
- {
- $this->_validator->setMessages(
- array(
- Zend_Validate_StringLength::TOO_LONG => 'Your value is too long',
- Zend_Validate_StringLength::TOO_SHORT => 'Your value is too short'
- )
- );
- $this->assertFalse($this->_validator->isValid('abcdefghij'));
- $messages = $this->_validator->getMessages();
- $this->assertEquals('Your value is too long', current($messages));
- $this->assertFalse($this->_validator->isValid('abc'));
- $messages = $this->_validator->getMessages();
- $this->assertEquals('Your value is too short', current($messages));
- }
- /**
- * Ensures that the magic getter gives us access to properties
- * that are permitted to be substituted in the message string.
- * The access is by the parameter name, not by the protected
- * property variable name.
- *
- * @return void
- */
- public function testGetProperty()
- {
- $this->_validator->setMessage(
- 'Your value is too long',
- Zend_Validate_StringLength::TOO_LONG
- );
- $inputInvalid = 'abcdefghij';
- $this->assertFalse($this->_validator->isValid($inputInvalid));
- $messages = $this->_validator->getMessages();
- $this->assertEquals('Your value is too long', current($messages));
- $this->assertEquals($inputInvalid, $this->_validator->value);
- $this->assertEquals(8, $this->_validator->max);
- $this->assertEquals(4, $this->_validator->min);
- }
- /**
- * Ensures that the class throws an exception when we try to
- * access a property that doesn't exist as a parameter.
- *
- * @return void
- */
- public function testGetPropertyException()
- {
- $this->_validator->setMessage(
- 'Your value is too long',
- Zend_Validate_StringLength::TOO_LONG
- );
- $this->assertFalse($this->_validator->isValid('abcdefghij'));
- $messages = $this->_validator->getMessages();
- $this->assertEquals('Your value is too long', current($messages));
- try {
- $property = $this->_validator->unknownProperty;
- $this->fail('Expected to catch Zend_Validate_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Validate_Exception,
- 'Expected exception of type Zend_Validate_Exception, got ' . get_class($e));
- $this->assertEquals("No property exists by the name 'unknownProperty'", $e->getMessage());
- }
- }
- /**
- * Ensures that the getError() function returns an array of
- * message key values corresponding to the messages.
- *
- * @return void
- */
- public function testGetErrors()
- {
- $inputInvalid = 'abcdefghij';
- $this->assertFalse($this->_validator->isValid($inputInvalid));
- $messages = $this->_validator->getMessages();
- $this->assertEquals("'$inputInvalid' is more than 8 characters long", current($messages));
- $errors = $this->_validator->getErrors();
- $this->assertEquals(Zend_Validate_StringLength::TOO_LONG, current($errors));
- }
- /**
- * Ensures that getMessageVariables() returns an array of
- * strings and that these strings that can be used as variables
- * in a message.
- */
- public function testGetMessageVariables()
- {
- $vars = $this->_validator->getMessageVariables();
- $this->assertTrue(is_array($vars));
- $this->assertEquals(array('min', 'max'), $vars);
- $message = 'variables: %notvar% ';
- foreach ($vars as $var) {
- $message .= "%$var% ";
- }
- $this->_validator->setMessage($message, Zend_Validate_StringLength::TOO_SHORT);
- $this->assertFalse($this->_validator->isValid('abc'));
- $messages = $this->_validator->getMessages();
- $this->assertEquals('variables: %notvar% 4 8 ', current($messages));
- }
- }
- // Call Zend_Validate_MessageTest::main() if this source file is executed directly.
- if (PHPUnit_MAIN_METHOD == 'Zend_Validate_MessageTest::main') {
- Zend_Validate_MessageTest::main();
- }
|