IdenticalTest.php 4.5 KB

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