2
0

IdenticalTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 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_IdenticalTest::main() if this source file is executed directly.
  23. if (!defined('PHPUnit_MAIN_METHOD')) {
  24. define('PHPUnit_MAIN_METHOD', 'Zend_Validate_IdenticalTest::main');
  25. }
  26. require_once dirname(__FILE__) . '/../../TestHelper.php';
  27. /** Zend_Validate_Identical */
  28. require_once 'Zend/Validate/Identical.php';
  29. /**
  30. * Zend_Validate_Identical
  31. *
  32. * @category Zend
  33. * @package Zend
  34. * @subpackage UnitTests
  35. * @uses Zend_Validate_Identical
  36. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @group Zend_Validate
  39. */
  40. class Zend_Validate_IdenticalTest extends PHPUnit_Framework_TestCase
  41. {
  42. public static function main()
  43. {
  44. require_once "PHPUnit/TextUI/TestRunner.php";
  45. $suite = new PHPUnit_Framework_TestSuite('Zend_Validate_IdenticalTest');
  46. $result = PHPUnit_TextUI_TestRunner::run($suite);
  47. }
  48. public function setUp()
  49. {
  50. $this->validator = new Zend_Validate_Identical;
  51. }
  52. public function testTokenInitiallyNull()
  53. {
  54. $this->assertNull($this->validator->getToken());
  55. }
  56. public function testCanSetToken()
  57. {
  58. $this->testTokenInitiallyNull();
  59. $this->validator->setToken('foo');
  60. $this->assertEquals('foo', $this->validator->getToken());
  61. }
  62. public function testCanSetTokenViaConstructor()
  63. {
  64. $validator = new Zend_Validate_Identical('foo');
  65. $this->assertEquals('foo', $validator->getToken());
  66. }
  67. public function testValidatingWhenTokenNullReturnsFalse()
  68. {
  69. $this->assertFalse($this->validator->isValid('foo'));
  70. }
  71. public function testValidatingWhenTokenNullSetsMissingTokenMessage()
  72. {
  73. $this->testValidatingWhenTokenNullReturnsFalse();
  74. $messages = $this->validator->getMessages();
  75. $this->assertTrue(array_key_exists('missingToken', $messages));
  76. }
  77. public function testValidatingAgainstTokenWithNonMatchingValueReturnsFalse()
  78. {
  79. $this->validator->setToken('foo');
  80. $this->assertFalse($this->validator->isValid('bar'));
  81. }
  82. public function testValidatingAgainstTokenWithNonMatchingValueSetsNotSameMessage()
  83. {
  84. $this->testValidatingAgainstTokenWithNonMatchingValueReturnsFalse();
  85. $messages = $this->validator->getMessages();
  86. $this->assertTrue(array_key_exists('notSame', $messages));
  87. }
  88. public function testValidatingAgainstTokenWithMatchingValueReturnsTrue()
  89. {
  90. $this->validator->setToken('foo');
  91. $this->assertTrue($this->validator->isValid('foo'));
  92. }
  93. /**
  94. * @see ZF-6511
  95. */
  96. public function testNotSameMessageContainsTokenAndValue()
  97. {
  98. $this->testValidatingAgainstTokenWithNonMatchingValueReturnsFalse();
  99. $messages = $this->validator->getMessages();
  100. $this->assertContains("The token 'foo'", $messages['notSame']);
  101. $this->assertContains('bar', $messages['notSame']);
  102. $this->assertContains('does not match', $messages['notSame']);
  103. }
  104. /**
  105. * @see ZF-6953
  106. */
  107. public function testValidatingAgainstEmptyToken()
  108. {
  109. $this->validator->setToken('');
  110. $this->assertTrue($this->validator->isValid(''));
  111. }
  112. /**
  113. * @see ZF-7128
  114. */
  115. public function testValidatingAgainstNonStrings()
  116. {
  117. $this->validator->setToken(true);
  118. $this->assertTrue($this->validator->isValid(true));
  119. $this->assertFalse($this->validator->isValid(1));
  120. $this->validator->setToken(array('one' => 'two', 'three'));
  121. $this->assertTrue($this->validator->isValid(array('one' => 'two', 'three')));
  122. $this->assertFalse($this->validator->isValid(array()));
  123. }
  124. public function testValidatingTokenArray()
  125. {
  126. $validator = new Zend_Validate_Identical(array('token' => 123));
  127. $this->assertTrue($validator->isValid(123));
  128. $this->assertFalse($validator->isValid(array('token' => 123)));
  129. }
  130. }
  131. // Call Zend_Validate_IdenticalTest::main() if this source file is executed directly.
  132. if (PHPUnit_MAIN_METHOD == 'Zend_Validate_IdenticalTest::main') {
  133. Zend_Validate_IdenticalTest::main();
  134. }