|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the ONGR package. |
| 5 | + * |
| 6 | + * (c) NFQ Technologies UAB <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace ONGR\ElasticsearchDSL\Tests\Functional; |
| 13 | + |
| 14 | +use Elasticsearch\Client; |
| 15 | +use Elasticsearch\ClientBuilder; |
| 16 | +use ONGR\ElasticsearchDSL\Search; |
| 17 | + |
| 18 | +abstract class AbstractElasticsearchTestCase extends \PHPUnit_Framework_TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Test index name in the elasticsearch. |
| 22 | + */ |
| 23 | + const INDEX_NAME = 'elasticsaerch-dsl-test'; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var Client |
| 27 | + */ |
| 28 | + private $client; |
| 29 | + |
| 30 | + /** |
| 31 | + * {@inheritdoc} |
| 32 | + */ |
| 33 | + protected function setUp() |
| 34 | + { |
| 35 | + parent::setUp(); |
| 36 | + |
| 37 | + $this->client = ClientBuilder::create()->build(); |
| 38 | + $this->deleteIndex(); |
| 39 | + |
| 40 | + $this->client->indices()->create( |
| 41 | + array_filter( |
| 42 | + [ |
| 43 | + 'index' => self::INDEX_NAME, |
| 44 | + 'mapping' => $this->getMapping() |
| 45 | + ] |
| 46 | + ) |
| 47 | + ); |
| 48 | + |
| 49 | + $bulkBody = []; |
| 50 | + |
| 51 | + foreach ($this->getDataArray() as $type => $documents) { |
| 52 | + foreach ($documents as $id => $document) { |
| 53 | + $bulkBody[] = [ |
| 54 | + 'index' => [ |
| 55 | + '_index' => self::INDEX_NAME, |
| 56 | + '_type' => $type, |
| 57 | + '_id' => $id, |
| 58 | + ] |
| 59 | + ]; |
| 60 | + $bulkBody[] = $document; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + $this->client->bulk( |
| 65 | + [ |
| 66 | + 'body' => $bulkBody |
| 67 | + ] |
| 68 | + ); |
| 69 | + $this->client->indices()->refresh(); |
| 70 | + } |
| 71 | + |
| 72 | + private function deleteIndex() |
| 73 | + { |
| 74 | + try { |
| 75 | + $this->client->indices()->delete(['index' => self::INDEX_NAME]); |
| 76 | + } catch (\Exception $e) { |
| 77 | + // Do nothing. |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Defines index mapping for test index. |
| 83 | + * Override this function in your test case and return array with mapping body. |
| 84 | + * More info check here: https://goo.gl/zWBree |
| 85 | + * |
| 86 | + * @return array Mapping body |
| 87 | + */ |
| 88 | + protected function getMapping() |
| 89 | + { |
| 90 | + return []; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Can be overwritten in child class to populate elasticsearch index with the data. |
| 95 | + * |
| 96 | + * Example: |
| 97 | + * [ |
| 98 | + * 'type_name' => [ |
| 99 | + * 'custom_id' => [ |
| 100 | + * 'title' => 'foo', |
| 101 | + * ], |
| 102 | + * 3 => [ |
| 103 | + * '_id' => 2, |
| 104 | + * 'title' => 'bar', |
| 105 | + * ] |
| 106 | + * ] |
| 107 | + * ] |
| 108 | + * Document _id can be set as it's id. |
| 109 | + * |
| 110 | + * @return array |
| 111 | + */ |
| 112 | + protected function getDataArray() |
| 113 | + { |
| 114 | + return []; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * {@inheritdoc} |
| 119 | + */ |
| 120 | + protected function tearDown() |
| 121 | + { |
| 122 | + parent::tearDown(); |
| 123 | + $this->deleteIndex(); |
| 124 | + } |
| 125 | + |
| 126 | + protected function executeSearch(Search $search, $type = null, $returnRaw = false) |
| 127 | + { |
| 128 | + $response = $this->client->search( |
| 129 | + array_filter([ |
| 130 | + 'index' => self::INDEX_NAME, |
| 131 | + 'type' => $type, |
| 132 | + 'body' => $search->toArray(), |
| 133 | + ]) |
| 134 | + ); |
| 135 | + |
| 136 | + if ($returnRaw) { |
| 137 | + return $response; |
| 138 | + } |
| 139 | + |
| 140 | + $documents = []; |
| 141 | + |
| 142 | + try { |
| 143 | + foreach ($response['hits']['hits'] as $document) { |
| 144 | + $documents[$document['_id']] = $document['_source']; |
| 145 | + } |
| 146 | + } catch (\Exception $e) { |
| 147 | + return $documents; |
| 148 | + } |
| 149 | + |
| 150 | + return $documents; |
| 151 | + } |
| 152 | +} |
0 commit comments