PageRenderTime 31ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/test/lib/Elastica/Test/Filter/IdsTest.php

http://github.com/ruflin/Elastica
PHP | 253 lines | 149 code | 50 blank | 54 comment | 2 complexity | 64439b91db3d3cd632764e7c6c395470 MD5 | raw file
  1. <?php
  2. namespace Elastica\Test\Filter;
  3. use Elastica\Document;
  4. use Elastica\Filter\Ids;
  5. use Elastica\Filter\Type;
  6. use Elastica\Query;
  7. use Elastica\Test\DeprecatedClassBase as BaseTest;
  8. class IdsTest extends BaseTest
  9. {
  10. /**
  11. * @group unit
  12. */
  13. public function testDeprecated()
  14. {
  15. $reflection = new \ReflectionClass(new Ids());
  16. $this->assertFileDeprecated($reflection->getFileName(), 'Deprecated: Filters are deprecated. Use queries in filter context. See https://www.elastic.co/guide/en/elasticsearch/reference/2.0/query-dsl-filters.html');
  17. }
  18. protected function _getIndexForTest()
  19. {
  20. $index = $this->_createIndex();
  21. // Add documents to first type
  22. $docs = [];
  23. for ($i = 1; $i < 100; ++$i) {
  24. $docs[] = new Document($i, ['name' => 'ruflin']);
  25. }
  26. $index->getType('helloworld1')->addDocuments($docs);
  27. // Add documents to second type
  28. $docs = [];
  29. for ($i = 1; $i < 100; ++$i) {
  30. $docs[] = new Document($i, ['name' => 'ruflin']);
  31. }
  32. // This is a special id that will only be in the second type
  33. $docs[] = new Document(101, ['name' => 'ruflin']);
  34. $index->getType('helloworld2')->addDocuments($docs);
  35. $index->optimize();
  36. $index->refresh();
  37. return $index;
  38. }
  39. protected function _getTypeForTest()
  40. {
  41. return $this->_getIndexForTest()->getType('helloworld1');
  42. }
  43. /**
  44. * @group functional
  45. */
  46. public function testSetIdsSearchSingle()
  47. {
  48. $filter = new Ids();
  49. $filter->setIds('1');
  50. $query = Query::create($filter);
  51. $resultSet = $this->_getTypeForTest()->search($query);
  52. $this->assertEquals(1, $resultSet->count());
  53. }
  54. /**
  55. * @group functional
  56. */
  57. public function testSetIdsSearchArray()
  58. {
  59. $filter = new Ids();
  60. $filter->setIds([1, 7, 13]);
  61. $query = Query::create($filter);
  62. $resultSet = $this->_getTypeForTest()->search($query);
  63. $this->assertEquals(3, $resultSet->count());
  64. }
  65. /**
  66. * @group functional
  67. */
  68. public function testAddIdsSearchSingle()
  69. {
  70. $filter = new Ids();
  71. $filter->addId('39');
  72. $query = Query::create($filter);
  73. $resultSet = $this->_getTypeForTest()->search($query);
  74. $this->assertEquals(1, $resultSet->count());
  75. }
  76. /**
  77. * @group functional
  78. */
  79. public function testAddIdsSearchSingleNotInType()
  80. {
  81. $filter = new Ids();
  82. $filter->addId('39');
  83. // Add an ID that is not in the index
  84. $filter->addId(104);
  85. $query = Query::create($filter);
  86. $resultSet = $this->_getTypeForTest()->search($query);
  87. $this->assertEquals(1, $resultSet->count());
  88. }
  89. /**
  90. * @group functional
  91. */
  92. public function testComboIdsSearchArray()
  93. {
  94. $filter = new Ids();
  95. $filter->setIds([1, 7, 13]);
  96. $filter->addId('39');
  97. $query = Query::create($filter);
  98. $resultSet = $this->_getTypeForTest()->search($query);
  99. $this->assertEquals(4, $resultSet->count());
  100. }
  101. /**
  102. * @group functional
  103. */
  104. public function testSetTypeSingleSearchSingle()
  105. {
  106. $filter = new Ids();
  107. $filter->setIds('1');
  108. $filter->setType('helloworld1');
  109. $query = Query::create($filter);
  110. $resultSet = $this->_getIndexForTest()->search($query);
  111. $this->assertEquals(1, $resultSet->count());
  112. }
  113. /**
  114. * @group functional
  115. */
  116. public function testSetTypeSingleSearchArray()
  117. {
  118. $filter = new Ids();
  119. $filter->setIds(['1', '2']);
  120. $filter->setType('helloworld1');
  121. $query = Query::create($filter);
  122. $resultSet = $this->_getIndexForTest()->search($query);
  123. $this->assertEquals(2, $resultSet->count());
  124. }
  125. /**
  126. * @group functional
  127. */
  128. public function testSetTypeSingleSearchSingleDocInOtherType()
  129. {
  130. $filter = new Ids();
  131. // Doc 4 is in the second type...
  132. $filter->setIds('101');
  133. $filter->setType('helloworld1');
  134. $query = Query::create($filter);
  135. $resultSet = $this->_getTypeForTest()->search($query);
  136. // ...therefore 0 results should be returned
  137. $this->assertEquals(0, $resultSet->count());
  138. }
  139. /**
  140. * @group functional
  141. */
  142. public function testSetTypeSingleSearchArrayDocInOtherType()
  143. {
  144. $filter = new Ids();
  145. // Doc 4 is in the second type...
  146. $filter->setIds(['1', '101']);
  147. $filter->setType('helloworld1');
  148. $query = Query::create($filter);
  149. $resultSet = $this->_getTypeForTest()->search($query);
  150. // ...therefore only 1 result should be returned
  151. $this->assertEquals(1, $resultSet->count());
  152. }
  153. /**
  154. * @group functional
  155. */
  156. public function testSetTypeArraySearchArray()
  157. {
  158. $filter = new Ids();
  159. $filter->setIds(['1', '4']);
  160. $filter->setType(['helloworld1', 'helloworld2']);
  161. $query = Query::create($filter);
  162. $resultSet = $this->_getIndexForTest()->search($query);
  163. $this->assertEquals(4, $resultSet->count());
  164. }
  165. /**
  166. * @group functional
  167. */
  168. public function testSetTypeArraySearchSingle()
  169. {
  170. $filter = new Ids();
  171. $filter->setIds('4');
  172. $filter->setType(['helloworld1', 'helloworld2']);
  173. $query = Query::create($filter);
  174. $resultSet = $this->_getIndexForTest()->search($query);
  175. $this->assertEquals(2, $resultSet->count());
  176. }
  177. /**
  178. * @group unit
  179. */
  180. public function testFilterTypeAndTypeCollision()
  181. {
  182. // This test ensures that Elastica\Type and Elastica\Filter\Type
  183. // do not collide when used together, which at one point
  184. // happened because of a use statement in Elastica\Filter\Ids
  185. // Test goal is to make sure a Fatal Error is not triggered
  186. $filterType = new Type();
  187. $filter = new Ids();
  188. }
  189. /**
  190. * @group unit
  191. */
  192. public function testAddType()
  193. {
  194. $type = $this->_getClient()->getIndex('indexname')->getType('typename');
  195. $filter = new Ids();
  196. $filter->addType('foo');
  197. $this->assertEquals(['foo'], $filter->getParam('type'));
  198. $filter->addType($type);
  199. $this->assertEquals(['foo', $type->getName()], $filter->getParam('type'));
  200. $returnValue = $filter->addType('bar');
  201. $this->assertInstanceOf('Elastica\Filter\Ids', $returnValue);
  202. }
  203. }