1 | <?php |
||
22 | abstract class AbstractElasticsearchTestCase extends WebTestCase |
||
23 | { |
||
24 | /** |
||
25 | * @var Manager[] Holds used managers. |
||
26 | */ |
||
27 | private $managers = []; |
||
28 | |||
29 | /** |
||
30 | * @var ContainerInterface |
||
31 | */ |
||
32 | private $container; |
||
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | public function runTest() |
||
38 | { |
||
39 | if ($this->getNumberOfRetries() < 1) { |
||
40 | return parent::runTest(); |
||
41 | } |
||
42 | |||
43 | foreach (range(1, $this->getNumberOfRetries()) as $try) { |
||
44 | try { |
||
45 | return parent::runTest(); |
||
46 | } catch (\Exception $e) { |
||
47 | if (!($e instanceof ElasticsearchException)) { |
||
48 | throw $e; |
||
49 | } |
||
50 | // If error was from elasticsearch re-setup tests and retry. |
||
51 | if ($try !== $this->getNumberOfRetries()) { |
||
52 | $this->tearDown(); |
||
53 | $this->setUp(); |
||
54 | } |
||
55 | } |
||
56 | } |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | protected function setUp() |
||
63 | { |
||
64 | foreach ($this->getDataArray() as $manager => $data) { |
||
65 | // Create index and populate data |
||
66 | $this->getManager($manager); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Returns number of retries tests should execute. |
||
72 | * |
||
73 | * @return int |
||
74 | */ |
||
75 | protected function getNumberOfRetries() |
||
76 | { |
||
77 | return 3; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Can be overwritten in child class to populate elasticsearch index with the data. |
||
82 | * |
||
83 | * Example: |
||
84 | * "manager_name" => |
||
85 | * [ |
||
86 | * 'type_name' => [ |
||
87 | * [ |
||
88 | * '_id' => 1, |
||
89 | * 'title' => 'foo', |
||
90 | * ], |
||
91 | * [ |
||
92 | * '_id' => 2, |
||
93 | * 'title' => 'bar', |
||
94 | * ] |
||
95 | * ] |
||
96 | * ] |
||
97 | * |
||
98 | * @return array |
||
99 | */ |
||
100 | protected function getDataArray() |
||
101 | { |
||
102 | return []; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Ignores versions specified. |
||
107 | * |
||
108 | * Returns two dimensional array, first item in sub array is version to ignore, second is comparator, |
||
109 | * last test name. If no test name is provided it will be used on all test class. |
||
110 | * |
||
111 | * Comparator types can be found in `version_compare` documentation. |
||
112 | * |
||
113 | * Example: [ |
||
114 | * ['1.2.7', '<='], |
||
115 | * ['1.2.9', '==', 'testSmth'] |
||
116 | * ] |
||
117 | * |
||
118 | * @return array |
||
119 | */ |
||
120 | protected function getIgnoredVersions() |
||
121 | { |
||
122 | return []; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Ignores version specified. |
||
127 | * |
||
128 | * @param Manager $manager |
||
129 | */ |
||
130 | private function ignoreVersions(Manager $manager) |
||
131 | { |
||
132 | $currentVersion = $manager->getVersionNumber(); |
||
133 | $ignore = null; |
||
134 | |||
135 | foreach ($this->getIgnoredVersions() as $ignoredVersion) { |
||
136 | if (version_compare($currentVersion, $ignoredVersion[0], $ignoredVersion[1]) === true) { |
||
137 | $ignore = true; |
||
138 | if (isset($ignoredVersion[2])) { |
||
139 | if ($ignoredVersion[2] === $this->getName()) { |
||
140 | break; |
||
141 | } |
||
142 | $ignore = false; |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | |||
147 | if ($ignore === true) { |
||
148 | $this->markTestSkipped("Elasticsearch version {$currentVersion} not supported by this test."); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Removes manager from local cache and drops its index. |
||
154 | * |
||
155 | * @param string $name |
||
156 | */ |
||
157 | protected function removeManager($name) |
||
158 | { |
||
159 | if (isset($this->managers[$name])) { |
||
160 | $this->managers[$name]->dropIndex(); |
||
161 | unset($this->managers[$name]); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Populates elasticsearch with data. |
||
167 | * |
||
168 | * @param Manager $manager |
||
169 | * @param array $data |
||
170 | */ |
||
171 | private function populateElasticsearchWithData($manager, array $data) |
||
183 | |||
184 | /** |
||
185 | * {@inheritdoc} |
||
186 | */ |
||
187 | protected function tearDown() |
||
188 | { |
||
189 | parent::tearDown(); |
||
190 | |||
191 | foreach ($this->managers as $name => $manager) { |
||
192 | try { |
||
193 | $manager->dropIndex(); |
||
194 | } catch (\Exception $e) { |
||
195 | // Do nothing. |
||
196 | } |
||
197 | } |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Returns service container. |
||
202 | * |
||
203 | * @param bool $reinitialize Force kernel reinitialization. |
||
204 | * @param array $kernelOptions Options used passed to kernel if it needs to be initialized. |
||
205 | * |
||
206 | * @return ContainerInterface |
||
207 | */ |
||
208 | protected function getContainer($reinitialize = false, $kernelOptions = []) |
||
217 | |||
218 | /** |
||
219 | * Returns manager instance with injected connection if does not exist creates new one. |
||
220 | * |
||
221 | * @param string $name Manager name |
||
222 | * |
||
223 | * @return Manager |
||
224 | * |
||
225 | * @throws \LogicException |
||
226 | */ |
||
227 | protected function getManager($name = 'default') |
||
255 | } |
||
256 |