PageRenderTime 30ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MappingTest.php

https://bitbucket.org/openplacement/sherlock
PHP | 306 lines | 192 code | 65 blank | 49 comment | 0 complexity | 47055018c8e52566c334849f2ed3609a MD5 | raw file
  1. <?php
  2. namespace Sherlock\tests;
  3. use Sherlock\Sherlock;
  4. /**
  5. * Generated by PHPUnit_SkeletonGenerator 1.2.0 on 2013-02-07 at 03:12:53.
  6. */
  7. class MappingTest extends \PHPUnit_Framework_TestCase
  8. {
  9. /**
  10. * @var Sherlock
  11. */
  12. protected $object;
  13. /**
  14. * Sets up the fixture, for example, opens a network connection.
  15. * This method is called before a test is executed.
  16. */
  17. protected function setUp()
  18. {
  19. $this->object = new Sherlock();
  20. $this->object->addNode('localhost', '9200');
  21. }
  22. /**
  23. * Tears down the fixture, for example, closes a network connection.
  24. * This method is called after a test is executed.
  25. */
  26. protected function tearDown()
  27. {
  28. /*
  29. try {
  30. $this->object->index('testmapping')->delete();
  31. } catch (\Exception $e) {
  32. }
  33. */
  34. }
  35. public function assertThrowsException($exception_name, $code)
  36. {
  37. $e = null;
  38. try {
  39. $code();
  40. } catch (\Exception $e) {
  41. // No more code, we only want to catch the exception in $e
  42. }
  43. $this->assertInstanceOf($exception_name, $e);
  44. }
  45. public function testStringMapping()
  46. {
  47. $sherlock = $this->object;
  48. //Set the index
  49. $index = $sherlock->index('test123');
  50. //no type, no field, expect error
  51. $this->assertThrowsException(
  52. '\sherlock\common\exceptions\BadMethodCallException',
  53. function () {
  54. $mapping = Sherlock::mappingBuilder()->String();
  55. }
  56. );
  57. //type, but no field, expect error
  58. $mapping = Sherlock::mappingBuilder('testField')->String();
  59. $this->assertThrowsException(
  60. '\sherlock\common\exceptions\RuntimeException',
  61. function () use ($mapping) {
  62. $data = $mapping->toJSON();
  63. }
  64. );
  65. //no type, field, expect error
  66. $this->assertThrowsException(
  67. '\sherlock\common\exceptions\BadMethodCallException',
  68. function () {
  69. $mapping = Sherlock::mappingBuilder()->String()->field('testField');
  70. }
  71. );
  72. //type, field
  73. $mapping = Sherlock::mappingBuilder('testType')->String()->field('testField');
  74. $data = $mapping->toJSON();
  75. $this->assertEquals("testType", $mapping->getType());
  76. $expected = '{"testField":{"type":"string"}}';
  77. $this->assertEquals($expected, $data);
  78. //two fields, single type
  79. $mapping = Sherlock::mappingBuilder('testType')->String()->field('testField');
  80. $mapping2 = Sherlock::mappingBuilder('testType')->String()->field('testField2');
  81. $index = $sherlock->index('index')->mappings($mapping, $mapping2);
  82. //no type, hashmap of properties
  83. $hash = array("field" => 'testField');
  84. $this->assertThrowsException(
  85. '\sherlock\common\exceptions\BadMethodCallException',
  86. function () use ($hash) {
  87. $mapping = Sherlock::mappingBuilder()->String($hash);
  88. }
  89. );
  90. //type, hashmap of properties
  91. $hash = array("field" => 'testField');
  92. $mapping = Sherlock::mappingBuilder('testType')->String($hash);
  93. $data = $mapping->toJSON();
  94. $expected = '{"testField":{"type":"string"}}';
  95. $this->assertEquals($expected, $data);
  96. //type, hashmap of properties, but override the hashmap with a new value
  97. $hash = array("field" => 'testField');
  98. $mapping = Sherlock::mappingBuilder('testType')->String($hash)->field("testFieldNew");
  99. $data = $mapping->toJSON();
  100. $expected = '{"testFieldNew":{"type":"string"}}';
  101. $this->assertEquals($expected, $data);
  102. //type, hashmap of properties, but override the hashmap with a new value, add a boost
  103. $hash = array("field" => 'testField');
  104. $mapping = Sherlock::mappingBuilder('testType')->String($hash)->field("testFieldNew")->boost(0.2);
  105. $data = $mapping->toJSON();
  106. $expected = '{"testFieldNew":{"type":"string","boost":0.2}}';
  107. $this->assertEquals($expected, $data);
  108. }
  109. public function testNumberMapping()
  110. {
  111. $sherlock = $this->object;
  112. //Set the index
  113. $index = $sherlock->index('test123');
  114. //no field, expect error
  115. $this->assertThrowsException(
  116. '\sherlock\common\exceptions\BadMethodCallException',
  117. function () {
  118. $mapping = Sherlock::mappingBuilder()->Number();
  119. }
  120. );
  121. //type, but no field, expect error
  122. $mapping = Sherlock::mappingBuilder('testType')->Number();
  123. $this->assertThrowsException(
  124. '\sherlock\common\exceptions\RuntimeException',
  125. function () use ($mapping) {
  126. $data = $mapping->toJSON();
  127. }
  128. );
  129. //type, field, but no number-type, expect error
  130. $mapping = Sherlock::mappingBuilder('testType')->Number()->field("testField");
  131. $this->assertThrowsException(
  132. '\sherlock\common\exceptions\RuntimeException',
  133. function () use ($mapping) {
  134. $data = $mapping->toJSON();
  135. }
  136. );
  137. //type, field, number-type
  138. $mapping = Sherlock::mappingBuilder('testType')->Number()->field('testField')->type("float");
  139. $data = $mapping->toJSON();
  140. $expected = '{"testField":{"type":"float"}}';
  141. $this->assertEquals($expected, $data);
  142. }
  143. public function testDateMapping()
  144. {
  145. $sherlock = $this->object;
  146. //Set the index
  147. $index = $sherlock->index('test123');
  148. //no field, expect error
  149. $this->assertThrowsException(
  150. '\sherlock\common\exceptions\BadMethodCallException',
  151. function () {
  152. $mapping = Sherlock::mappingBuilder()->Date();
  153. }
  154. );
  155. //type, but no field, expect error
  156. $mapping = Sherlock::mappingBuilder('testType')->Date();
  157. $this->assertThrowsException(
  158. '\sherlock\common\exceptions\RuntimeException',
  159. function () use ($mapping) {
  160. $data = $mapping->toJSON();
  161. }
  162. );
  163. //type, field, format
  164. $mapping = Sherlock::mappingBuilder('testType')->Date()->field('testField')->format("YYYY-MM-dd");
  165. $data = $mapping->toJSON();
  166. $expected = '{"testField":{"type":"date","format":"YYYY-MM-dd"}}';
  167. $this->assertEquals($expected, $data);
  168. }
  169. public function testMultiMapping()
  170. {
  171. $sherlock = $this->object;
  172. //Set the index
  173. $index = $sherlock->index('testmultimapping');
  174. $mapping1 = Sherlock::mappingBuilder('testType')->Date()->field('testField')->format("YYYY-MM-dd");
  175. $mapping2 = Sherlock::mappingBuilder('testType2')->String()->field('testField2');
  176. //add both mappings and create the index
  177. $index->mappings($mapping1, $mapping2);
  178. $response = $index->create();
  179. $this->assertEquals(true, $response->ok);
  180. //try to update the index
  181. $index->type("testType")->mappings($mapping1);
  182. $response = $index->updateMapping();
  183. $this->assertEquals(true, $response->ok);
  184. //try to update with two mappings, should error
  185. $index->type("testType")->mappings($mapping1, $mapping2);
  186. $this->assertThrowsException(
  187. '\sherlock\common\exceptions\RuntimeException',
  188. function () use ($index) {
  189. $response = $index->updateMapping();
  190. }
  191. );
  192. $response = $index->delete();
  193. $this->assertEquals(true, $response->ok);
  194. }
  195. public function testObjectMapping()
  196. {
  197. $sherlock = $this->object;
  198. //Set the index
  199. $index = $sherlock->index('test123');
  200. $mapping1 = Sherlock::mappingBuilder('testType')->Date()->field('testField')->format("YYYY-MM-dd");
  201. $mapping2 = Sherlock::mappingBuilder('testType2')->Object()->field("testField2")->object(($mapping1));
  202. $data = $mapping2->toJSON();
  203. $expected = '{"testField2":{"properties":{"testField":{"type":"date","format":"YYYY-MM-dd"}},"type":"object"}}';
  204. $this->assertEquals($expected, $data);
  205. $mapping2->dynamic(true);
  206. $data = $mapping2->toJSON();
  207. $expected = '{"testField2":{"properties":{"testField":{"type":"date","format":"YYYY-MM-dd"}},"type":"object","dynamic":true}}';
  208. $this->assertEquals($expected, $data);
  209. }
  210. public function testAnalyzerMapping()
  211. {
  212. $sherlock = $this->object;
  213. //Set the index
  214. $index = $sherlock->index('testanalyzermapping');
  215. //no path, expect error
  216. $this->assertThrowsException(
  217. '\sherlock\common\exceptions\BadMethodCallException',
  218. function () {
  219. $mapping = Sherlock::mappingBuilder()->Analyzer();
  220. }
  221. );
  222. //type, field, format
  223. $mapping = Sherlock::mappingBuilder('testType')->Analyzer()->path('testField');
  224. $data = $mapping->toJSON();
  225. $expected = '{"_analyzer":{"path":"testField"}}';
  226. $this->assertEquals($expected, $data);
  227. $type = 'data';
  228. $index->mappings(
  229. Sherlock::mappingBuilder($type)->String()->field('parents')->analyzer('keyword'),
  230. Sherlock::mappingBuilder($type)->String()->field('ancestors')->analyzer('keyword'),
  231. Sherlock::mappingBuilder($type)->String()->field('tags')->analyzer('keyword'),
  232. Sherlock::mappingBuilder($type)->String()->field('type')->analyzer('keyword'),
  233. Sherlock::mappingBuilder($type)->String()->field('slug')->analyzer('keyword'),
  234. Sherlock::mappingBuilder($type)->Analyzer()->path("contentanalyzer")
  235. );
  236. $index->create();
  237. $response = $index->delete();
  238. $this->assertEquals(true, $response->ok);
  239. }
  240. }