ReCaptchaTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_Captcha
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2008 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_Captcha_ReCaptchaTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Captcha_ReCaptchaTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../TestHelper.php';
  27. require_once 'Zend/Form/Element/Captcha.php';
  28. require_once 'Zend/View.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Captcha
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Captcha_ReCaptchaTest extends PHPUnit_Framework_TestCase
  37. {
  38. /**
  39. * Runs the test methods of this class.
  40. *
  41. * @return void
  42. */
  43. public static function main()
  44. {
  45. $suite = new PHPUnit_Framework_TestSuite("Zend_Captcha_ReCaptchaTest");
  46. $result = PHPUnit_TextUI_TestRunner::run($suite);
  47. }
  48. /**
  49. * Sets up the fixture, for example, open a network connection.
  50. * This method is called before a test is executed.
  51. *
  52. * @return void
  53. */
  54. public function setUp()
  55. {
  56. if (isset($this->word)) {
  57. unset($this->word);
  58. }
  59. $this->element = new Zend_Form_Element_Captcha(
  60. 'captchaR',
  61. array(
  62. 'captcha' => array(
  63. 'ReCaptcha',
  64. 'sessionClass' => 'Zend_Captcha_ReCaptchaTest_SessionContainer'
  65. )
  66. )
  67. );
  68. $this->captcha = $this->element->getCaptcha();
  69. }
  70. /**
  71. * Tears down the fixture, for example, close a network connection.
  72. * This method is called after a test is executed.
  73. *
  74. * @return void
  75. */
  76. public function tearDown()
  77. {
  78. }
  79. public function testConstructorShouldSetOptions()
  80. {
  81. $options = array(
  82. 'privKey' => 'privateKey',
  83. 'pubKey' => 'publicKey',
  84. 'ssl' => true,
  85. 'xhtml' => true,
  86. );
  87. $captcha = new Zend_Captcha_ReCaptcha($options);
  88. $test = $captcha->getOptions();
  89. $compare = array('privKey' => $options['privKey'], 'pubKey' => $options['pubKey']);
  90. $this->assertEquals($compare, $test);
  91. $service = $captcha->getService();
  92. $test = $service->getParams();
  93. $compare = array('ssl' => $options['ssl'], 'xhtml' => $options['xhtml']);
  94. foreach ($compare as $key => $value) {
  95. $this->assertTrue(array_key_exists($key, $test));
  96. $this->assertSame($value, $test[$key]);
  97. }
  98. }
  99. public function testShouldAllowSpecifyingServiceObject()
  100. {
  101. $captcha = new Zend_Captcha_ReCaptcha();
  102. $try = new Zend_Service_ReCaptcha();
  103. $this->assertNotSame($captcha->getService(), $try);
  104. $captcha->setService($try);
  105. $this->assertSame($captcha->getService(), $try);
  106. }
  107. }
  108. class Zend_Captcha_ReCaptchaTest_SessionContainer
  109. {
  110. protected static $_word;
  111. public function __get($name)
  112. {
  113. if ('word' == $name) {
  114. return self::$_word;
  115. }
  116. return null;
  117. }
  118. public function __set($name, $value)
  119. {
  120. if ('word' == $name) {
  121. self::$_word = $value;
  122. } else {
  123. $this->$name = $value;
  124. }
  125. }
  126. public function __isset($name)
  127. {
  128. if (('word' == $name) && (null !== self::$_word)) {
  129. return true;
  130. }
  131. return false;
  132. }
  133. public function __call($method, $args)
  134. {
  135. switch ($method) {
  136. case 'setExpirationHops':
  137. case 'setExpirationSeconds':
  138. $this->$method = array_shift($args);
  139. break;
  140. default:
  141. }
  142. }
  143. }
  144. // Call Zend_Captcha_ReCaptchaTest::main() if this source file is executed directly.
  145. if (PHPUnit_MAIN_METHOD == "Zend_Captcha_ReCaptchaTest::main") {
  146. Zend_Captcha_ReCaptchaTest::main();
  147. }