ResponseTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_Service_ReCaptcha
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 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. /** @see Zend_Service_ReCaptcha_Response */
  23. require_once 'Zend/Service/ReCaptcha/Response.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Service_ReCaptcha
  27. * @subpackage UnitTests
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @group Zend_Service
  31. * @group Zend_Service_ReCaptcha
  32. */
  33. class Zend_Service_ReCaptcha_ResponseTest extends PHPUnit_Framework_TestCase
  34. {
  35. protected $_response = null;
  36. public function setUp() {
  37. $this->_response = new Zend_Service_ReCaptcha_Response();
  38. }
  39. public function testSetAndGet() {
  40. /* Set and get status */
  41. $status = 'true';
  42. $this->_response->setStatus($status);
  43. $this->assertSame(true, $this->_response->getStatus());
  44. $status = 'false';
  45. $this->_response->setStatus($status);
  46. $this->assertSame(false, $this->_response->getStatus());
  47. /* Set and get the error code */
  48. $errorCode = 'foobar';
  49. $this->_response->setErrorCode($errorCode);
  50. $this->assertSame($errorCode, $this->_response->getErrorCode());
  51. }
  52. public function testIsValid() {
  53. $this->_response->setStatus('true');
  54. $this->assertSame(true, $this->_response->isValid());
  55. }
  56. public function testIsInvalid() {
  57. $this->_response->setStatus('false');
  58. $this->assertSame(false, $this->_response->isValid());
  59. }
  60. public function testSetFromHttpResponseWhenResponseContentIsMissing() {
  61. $responseBody = 'true';
  62. $httpResponse = new Zend_Http_Response(200, array('Content-Type' => 'text/html'), $responseBody);
  63. $this->_response->setFromHttpResponse($httpResponse);
  64. $this->assertTrue($this->_response->getStatus());
  65. $this->assertSame('', $this->_response->getErrorCode());
  66. }
  67. public function testSetFromHttpResponse() {
  68. $status = 'false';
  69. $errorCode = 'foobar';
  70. $responseBody = $status . "\n" . $errorCode;
  71. $httpResponse = new Zend_Http_Response(200, array('Content-Type' => 'text/html'), $responseBody);
  72. $this->_response->setFromHttpResponse($httpResponse);
  73. $this->assertSame(false, $this->_response->getStatus());
  74. $this->assertSame($errorCode, $this->_response->getErrorCode());
  75. }
  76. public function testSetFromHttpResponseWhenResponseHasSeveralLinesOfContent() {
  77. $status = 'false';
  78. $errorCode = 'foobar';
  79. $responseBody = $status . "\n" . $errorCode . "\nSome data\nEven more data";
  80. $httpResponse = new Zend_Http_Response(200, array('Content-Type' => 'text/html'), $responseBody);
  81. $this->_response->setFromHttpResponse($httpResponse);
  82. $this->assertSame(false, $this->_response->getStatus());
  83. $this->assertSame($errorCode, $this->_response->getErrorCode());
  84. }
  85. public function testConstructor() {
  86. $status = 'true';
  87. $errorCode = 'ok';
  88. $response = new Zend_Service_ReCaptcha_Response($status, $errorCode);
  89. $this->assertSame(true, $response->getStatus());
  90. $this->assertSame($errorCode, $response->getErrorCode());
  91. }
  92. public function testConstructorWithHttpResponse() {
  93. $status = 'false';
  94. $errorCode = 'foobar';
  95. $responseBody = $status . "\n" . $errorCode;
  96. $httpResponse = new Zend_Http_Response(200, array('Content-Type' => 'text/html'), $responseBody);
  97. $response = new Zend_Service_ReCaptcha_Response(null, null, $httpResponse);
  98. $this->assertSame(false, $response->getStatus());
  99. $this->assertSame($errorCode, $response->getErrorCode());
  100. }
  101. }