/test/lib/Elastica/ClientTest.php

https://github.com/jonathanbecker/Elastica · PHP · 340 lines · 165 code · 64 blank · 111 comment · 0 complexity · d3a3f7479beafe615b79d32c83d8c707 MD5 · raw file

  1. <?php
  2. require_once dirname(__FILE__) . '/../../bootstrap.php';
  3. class Elastica_ClientTest extends PHPUnit_Framework_TestCase
  4. {
  5. public function setUp() {
  6. }
  7. public function tearDown() {
  8. }
  9. public function testConstruct() {
  10. $host = 'ruflin.com';
  11. $port = 9300;
  12. $client = new Elastica_Client(array('host' => $host, 'port' => $port));
  13. $this->assertEquals($host, $client->getHost());
  14. $this->assertEquals($port, $client->getPort());
  15. }
  16. public function testDefaults() {
  17. $client = new Elastica_Client();
  18. $this->assertEquals(Elastica_Client::DEFAULT_HOST, 'localhost');
  19. $this->assertEquals(Elastica_Client::DEFAULT_PORT, 9200);
  20. $this->assertEquals(Elastica_Client::DEFAULT_TRANSPORT, 'Http');
  21. $this->assertEquals(Elastica_Client::DEFAULT_HOST, $client->getHost());
  22. $this->assertEquals(Elastica_Client::DEFAULT_PORT, $client->getPort());
  23. $this->assertEquals(Elastica_Client::DEFAULT_TRANSPORT, $client->getTransport());
  24. }
  25. public function testServersArray() {
  26. // Creates a new index 'xodoa' and a type 'user' inside this index
  27. $client = new Elastica_Client(array('servers' => array(array('host' => 'localhost', 'port' => 9200))));
  28. $index = $client->getIndex('test1');
  29. $index->create(array(), true);
  30. $type = $index->getType('user');
  31. // Adds 1 document to the index
  32. $doc1 = new Elastica_Document(1,
  33. array('username' => 'hans', 'test' => array('2', '3', '5'))
  34. );
  35. $type->addDocument($doc1);
  36. // Adds a list of documents with _bulk upload to the index
  37. $docs = array();
  38. $docs[] = new Elastica_Document(2,
  39. array('username' => 'john', 'test' => array('1', '3', '6'))
  40. );
  41. $docs[] = new Elastica_Document(3,
  42. array('username' => 'rolf', 'test' => array('2', '3', '7'))
  43. );
  44. $type->addDocuments($docs);
  45. // Refresh index
  46. $index->refresh();
  47. $resultSet = $type->search('rolf');
  48. }
  49. public function testBulk() {
  50. $client = new Elastica_Client();
  51. $params = array(
  52. array('index' => array('_index' => 'test', '_type' => 'user', '_id' => '1')),
  53. array('user' => array('name' => 'hans')),
  54. array('index' => array('_index' => 'test', '_type' => 'user', '_id' => '2')),
  55. array('user' => array('name' => 'peter')),
  56. );
  57. $client->bulk($params);
  58. }
  59. public function testOptimizeAll() {
  60. $client = new Elastica_Client();
  61. $response = $client->optimizeAll();
  62. $this->assertFalse($response->hasError());
  63. }
  64. public function testAddDocumentsEmpty() {
  65. $client = new Elastica_Client();
  66. try {
  67. $client->addDocuments(array());
  68. $this->fail('Should throw exception');
  69. } catch(Elastica_Exception_Invalid $e) {
  70. $this->assertTrue(true);
  71. }
  72. }
  73. /**
  74. * Test deleteIds method using string parameters
  75. *
  76. * This test ensures that the deleteIds method of
  77. * the Elastica_Client can properly accept and use
  78. * an $index parameter and $type parameter that are
  79. * strings
  80. *
  81. * This test is a bit more verbose than just sending the
  82. * values to deleteIds and checking for exceptions or
  83. * warnings.
  84. *
  85. * It will add a document, search for it, then delete it
  86. * using the parameter types we are interested in, and then
  87. * re-search to verify that they have been deleted
  88. */
  89. public function testDeleteIdsIdxStringTypeString() {
  90. $client = new Elastica_Client();
  91. $data = array('username' => 'hans');
  92. $userSearch = 'username:hans';
  93. $index = $client->getIndex('test1');
  94. // Create the index, deleting it first if it already exists
  95. $index->create(array(), true);
  96. $type = $index->getType('user');
  97. // Adds 1 document to the index
  98. $doc = new Elastica_Document(null, $data);
  99. $result = $type->addDocument($doc);
  100. // Refresh index
  101. $index->refresh();
  102. $resultData = $result->getData();
  103. $ids = array($resultData['_id']);
  104. // Check to make sure the document is in the index
  105. $resultSet = $type->search($userSearch);
  106. $totalHits = $resultSet->getTotalHits();
  107. $this->assertEquals(1, $totalHits);
  108. // And verify that the variables we are doing to send to
  109. // deleteIds are the type we are testing for
  110. $idxString = $index->getName();
  111. $typeString = $type->getName();
  112. $this->assertEquals(true, is_string($idxString));
  113. $this->assertEquals(true, is_string($typeString));
  114. // Using the existing $index and $type variables which
  115. // are Elastica_Index and Elastica_Type objects respectively
  116. $resp = $client->deleteIds($ids, $index, $type);
  117. // Refresh the index to clear out deleted ID information
  118. $index->refresh();
  119. // Research the index to verify that the items have been deleted
  120. $resultSet = $type->search($userSearch);
  121. $totalHits = $resultSet->getTotalHits();
  122. $this->assertEquals(0, $totalHits);
  123. }
  124. /**
  125. * Test deleteIds method using string parameter for $index
  126. * and object parameter for $type
  127. *
  128. * This test ensures that the deleteIds method of
  129. * the Elastica_Client can properly accept and use
  130. * an $index parameter that is a string and a $type
  131. * parameter that is of type Elastica_Type
  132. *
  133. * This test is a bit more verbose than just sending the
  134. * values to deleteIds and checking for exceptions or
  135. * warnings.
  136. *
  137. * It will add a document, search for it, then delete it
  138. * using the parameter types we are interested in, and then
  139. * re-search to verify that they have been deleted
  140. */
  141. public function testDeleteIdsIdxStringTypeObject() {
  142. $client = new Elastica_Client();
  143. $data = array('username' => 'hans');
  144. $userSearch = 'username:hans';
  145. $index = $client->getIndex('test1');
  146. // Create the index, deleting it first if it already exists
  147. $index->create(array(), true);
  148. $type = $index->getType('user');
  149. // Adds 1 document to the index
  150. $doc = new Elastica_Document(null, $data);
  151. $result = $type->addDocument($doc);
  152. // Refresh index
  153. $index->refresh();
  154. $resultData = $result->getData();
  155. $ids = array($resultData['_id']);
  156. // Check to make sure the document is in the index
  157. $resultSet = $type->search($userSearch);
  158. $totalHits = $resultSet->getTotalHits();
  159. $this->assertEquals(1, $totalHits);
  160. // And verify that the variables we are doing to send to
  161. // deleteIds are the type we are testing for
  162. $idxString = $index->getName();
  163. $this->assertEquals(true, is_string($idxString));
  164. $this->assertEquals(true, ($type instanceof Elastica_Type));
  165. // Using the existing $index and $type variables which
  166. // are Elastica_Index and Elastica_Type objects respectively
  167. $resp = $client->deleteIds($ids, $index, $type);
  168. // Refresh the index to clear out deleted ID information
  169. $index->refresh();
  170. // Research the index to verify that the items have been deleted
  171. $resultSet = $type->search($userSearch);
  172. $totalHits = $resultSet->getTotalHits();
  173. $this->assertEquals(0, $totalHits);
  174. }
  175. /**
  176. * Test deleteIds method using object parameter for $index
  177. * and string parameter for $type
  178. *
  179. * This test ensures that the deleteIds method of
  180. * the Elastica_Client can properly accept and use
  181. * an $index parameter that is of type Elasitca_Index
  182. * and a $type parameter that is a string
  183. *
  184. * This test is a bit more verbose than just sending the
  185. * values to deleteIds and checking for exceptions or
  186. * warnings.
  187. *
  188. * It will add a document, search for it, then delete it
  189. * using the parameter types we are interested in, and then
  190. * re-search to verify that they have been deleted
  191. */
  192. public function testDeleteIdsIdxObjectTypeString() {
  193. $client = new Elastica_Client();
  194. $data = array('username' => 'hans');
  195. $userSearch = 'username:hans';
  196. $index = $client->getIndex('test1');
  197. // Create the index, deleting it first if it already exists
  198. $index->create(array(), true);
  199. $type = $index->getType('user');
  200. // Adds 1 document to the index
  201. $doc = new Elastica_Document(null, $data);
  202. $result = $type->addDocument($doc);
  203. // Refresh index
  204. $index->refresh();
  205. $resultData = $result->getData();
  206. $ids = array($resultData['_id']);
  207. // Check to make sure the document is in the index
  208. $resultSet = $type->search($userSearch);
  209. $totalHits = $resultSet->getTotalHits();
  210. $this->assertEquals(1, $totalHits);
  211. // And verify that the variables we are doing to send to
  212. // deleteIds are the type we are testing for
  213. $typeString = $type->getName();
  214. $this->assertEquals(true, ($index instanceof Elastica_Index));
  215. $this->assertEquals(true, is_string($typeString));
  216. // Using the existing $index and $type variables which
  217. // are Elastica_Index and Elastica_Type objects respectively
  218. $resp = $client->deleteIds($ids, $index, $type);
  219. // Refresh the index to clear out deleted ID information
  220. $index->refresh();
  221. // Research the index to verify that the items have been deleted
  222. $resultSet = $type->search($userSearch);
  223. $totalHits = $resultSet->getTotalHits();
  224. $this->assertEquals(0, $totalHits);
  225. }
  226. /**
  227. * Test deleteIds method using object parameter for $index
  228. * and object parameter for $type
  229. *
  230. * This test ensures that the deleteIds method of
  231. * the Elastica_Client can properly accept and use
  232. * an $index parameter that is an object and a $type
  233. * parameter that is of type Elastica_Type
  234. *
  235. * This test is a bit more verbose than just sending the
  236. * values to deleteIds and checking for exceptions or
  237. * warnings.
  238. *
  239. * It will add a document, search for it, then delete it
  240. * using the parameter types we are interested in, and then
  241. * re-search to verify that they have been deleted
  242. */
  243. public function testDeleteIdsIdxObjectTypeObject() {
  244. $client = new Elastica_Client();
  245. $data = array('username' => 'hans');
  246. $userSearch = 'username:hans';
  247. $index = $client->getIndex('test1');
  248. // Create the index, deleting it first if it already exists
  249. $index->create(array(), true);
  250. $type = $index->getType('user');
  251. // Adds 1 document to the index
  252. $doc = new Elastica_Document(null, $data);
  253. $result = $type->addDocument($doc);
  254. // Refresh index
  255. $index->refresh();
  256. $resultData = $result->getData();
  257. $ids = array($resultData['_id']);
  258. // Check to make sure the document is in the index
  259. $resultSet = $type->search($userSearch);
  260. $totalHits = $resultSet->getTotalHits();
  261. $this->assertEquals(1, $totalHits);
  262. // And verify that the variables we are doing to send to
  263. // deleteIds are the type we are testing for
  264. $this->assertEquals(true, ($index instanceof Elastica_Index));
  265. $this->assertEquals(true, ($type instanceof Elastica_Type));
  266. // Using the existing $index and $type variables which
  267. // are Elastica_Index and Elastica_Type objects respectively
  268. $resp = $client->deleteIds($ids, $index, $type);
  269. // Refresh the index to clear out deleted ID information
  270. $index->refresh();
  271. // Research the index to verify that the items have been deleted
  272. $resultSet = $type->search($userSearch);
  273. $totalHits = $resultSet->getTotalHits();
  274. $this->assertEquals(0, $totalHits);
  275. }
  276. }