ResponseTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * Copyright (c) 2007-2011, Servigistics, Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * - Neither the name of Servigistics, Inc. nor the names of
  15. * its contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * @copyright Copyright 2007-2011 Servigistics, Inc. (http://servigistics.com)
  31. * @license http://solr-php-client.googlecode.com/svn/trunk/COPYING New BSD
  32. *
  33. * @package Apache
  34. * @subpackage Solr
  35. * @author Donovan Jimenez <djimenez@conduit-it.com>
  36. */
  37. /**
  38. * Apache_Solr_Response Unit Test
  39. */
  40. class Apache_Solr_ResponseTest extends PHPUnit_Framework_TestCase
  41. {
  42. static public function get0Response($createDocuments = true, $collapseSingleValueArrays = true)
  43. {
  44. return new Apache_Solr_Response(Apache_Solr_HttpTransport_ResponseTest::get0Response(), $createDocuments, $collapseSingleValueArrays);
  45. }
  46. static public function get200Response($createDocuments = true, $collapseSingleValueArrays = true)
  47. {
  48. return new Apache_Solr_Response(Apache_Solr_HttpTransport_ResponseTest::get200Response(), $createDocuments, $collapseSingleValueArrays);
  49. }
  50. static public function get200ResponseWithDocuments($createDocuments = true, $collapseSingleValueArrays = true)
  51. {
  52. return new Apache_Solr_Response(Apache_Solr_HttpTransport_ResponseTest::get200ResponseWithDocuments(), $createDocuments, $collapseSingleValueArrays);
  53. }
  54. static public function get400Response($createDocuments = true, $collapseSingleValueArrays = true)
  55. {
  56. return new Apache_Solr_Response(Apache_Solr_HttpTransport_ResponseTest::get400Response(), $createDocuments, $collapseSingleValueArrays);
  57. }
  58. static public function get404Response($createDocuments = true, $collapseSingleValueArrays = true)
  59. {
  60. return new Apache_Solr_Response(Apache_Solr_HttpTransport_ResponseTest::get404Response(), $createDocuments, $collapseSingleValueArrays);
  61. }
  62. public function testConstuctorWithValidBodyAndHeaders()
  63. {
  64. $fixture = self::get200Response();
  65. // check that we parsed the HTTP status correctly
  66. $this->assertEquals(Apache_Solr_HttpTransport_ResponseTest::STATUS_CODE_200, $fixture->getHttpStatus());
  67. // check that we received the body correctly
  68. $this->assertEquals(Apache_Solr_HttpTransport_ResponseTest::BODY_200, $fixture->getRawResponse());
  69. // check that our defaults are correct
  70. $this->assertEquals(Apache_Solr_HttpTransport_ResponseTest::ENCODING_200, $fixture->getEncoding());
  71. $this->assertEquals(Apache_Solr_HttpTransport_ResponseTest::MIME_TYPE_200, $fixture->getType());
  72. }
  73. public function testConstructorWithBadBodyAndHeaders()
  74. {
  75. $fixture = self::get0Response();
  76. // check that our defaults are correct
  77. $this->assertEquals(0, $fixture->getHttpStatus());
  78. $this->assertEquals("UTF-8", $fixture->getEncoding());
  79. $this->assertEquals("text/plain", $fixture->getType());
  80. }
  81. public function testMagicGetWithValidBodyAndHeaders()
  82. {
  83. $fixture = self::get200Response();
  84. // test top level gets
  85. $this->assertType('stdClass', $fixture->responseHeader);
  86. $this->assertEquals(0, $fixture->responseHeader->status);
  87. $this->assertEquals(0, $fixture->responseHeader->QTime);
  88. $this->assertType('stdClass', $fixture->response);
  89. $this->assertEquals(0, $fixture->response->numFound);
  90. $this->assertTrue(is_array($fixture->response->docs));
  91. $this->assertEquals(0, count($fixture->response->docs));
  92. }
  93. /**
  94. * @expectedException Apache_Solr_ParserException
  95. */
  96. public function testMagicGetWith0Response()
  97. {
  98. $fixture = self::get0Response();
  99. // attempting to magic get a part of the response
  100. // should throw a ParserException
  101. $fixture->responseHeader;
  102. $this->fail("Expected Apache_Solr_ParserException was not raised");
  103. }
  104. /**
  105. * @expectedException Apache_Solr_ParserException
  106. */
  107. public function testMagicGetWith400Response()
  108. {
  109. $fixture = self::get400Response();
  110. // attempting to magic get a part of the response
  111. // should throw a ParserException
  112. $fixture->responseHeader;
  113. $this->fail("Expected Apache_Solr_ParserException was not raised");
  114. }
  115. /**
  116. * @expectedException Apache_Solr_ParserException
  117. */
  118. public function testMagicGetWith404Response()
  119. {
  120. $fixture = self::get404Response();
  121. // attempting to magic get a part of the response
  122. // should throw a ParserException
  123. $fixture->responseHeader;
  124. $this->fail("Expected Apache_Solr_ParserException was not raised");
  125. }
  126. public function testCreateDocuments()
  127. {
  128. $fixture = self::get200ResponseWithDocuments();
  129. $this->assertTrue(count($fixture->response->docs) > 0, 'There are not 1 or more documents, cannot test');
  130. $this->assertType('Apache_Solr_Document', $fixture->response->docs[0], 'The first document is not of type Apache_Solr_Document');
  131. }
  132. public function testDontCreateDocuments()
  133. {
  134. $fixture = self::get200ResponseWithDocuments(false);
  135. $this->assertTrue(count($fixture->response->docs) > 0, 'There are not 1 or more documents, cannot test');
  136. $this->assertType('stdClass', $fixture->response->docs[0], 'The first document is not of type stdClass');
  137. }
  138. public function testGetHttpStatusMessage()
  139. {
  140. $fixture = self::get200Response();
  141. $this->assertEquals("OK", $fixture->getHttpStatusMessage());
  142. }
  143. public function testMagicGetReturnsNullForUndefinedData()
  144. {
  145. $fixture = self::get200Response();
  146. $this->assertNull($fixture->doesnotexist);
  147. }
  148. public function testMagicIssetForDefinedProperty()
  149. {
  150. $fixture = self::get200Response();
  151. $this->assertTrue(isset($fixture->responseHeader));
  152. }
  153. public function testMagicIssetForUndefinedProperty()
  154. {
  155. $fixture = self::get200Response();
  156. $this->assertFalse(isset($fixture->doesnotexist));
  157. }
  158. }