Skip to content

Commit c096a95

Browse files
authored
Refactor tests by eliminating deprecated execute function (#688)
* added findDocuments() instead of findDocument() * implemented array access in ArrayIterator * refactored tests by eliminating deprecated execute() function
1 parent 8e5e6e7 commit c096a95

11 files changed

+75
-25
lines changed

Result/ArrayIterator.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,40 @@
1717
/**
1818
* Class DocumentIterator.
1919
*/
20-
class ArrayIterator extends AbstractResultsIterator
20+
class ArrayIterator extends AbstractResultsIterator implements \ArrayAccess
2121
{
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function offsetExists($offset)
26+
{
27+
return $this->documentExists($offset);
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function offsetGet($offset)
34+
{
35+
return $this->getDocument($offset);
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function offsetSet($offset, $value)
42+
{
43+
$this->documents[$offset] = $value;
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function offsetUnset($offset)
50+
{
51+
unset($this->documents[$offset]);
52+
}
53+
2254
/**
2355
* {@inheritdoc}
2456
*/

Service/Repository.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,25 @@ private function getScrollConfiguration($raw, $scrollDuration)
232232
/**
233233
* Returns DocumentIterator with composed Document objects from array response.
234234
*
235-
* @param Search $search
235+
* @deprecated Miss type in the function name, use findDocuments() instead. Will remove in 3.0
236236
*
237+
* @param Search $search
237238
* @return DocumentIterator
238239
*/
239240
public function findDocument(Search $search)
241+
{
242+
return $this->findDocuments($search);
243+
}
244+
245+
246+
/**
247+
* Returns DocumentIterator with composed Document objects from array response.
248+
*
249+
* @param Search $search
250+
*
251+
* @return DocumentIterator
252+
*/
253+
public function findDocuments(Search $search)
240254
{
241255
$results = $this->executeSearch($search);
242256

Tests/Functional/Profiler/ElasticsearchProfilerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function testGetTermQuery()
112112
$search = $repository
113113
->createSearch()
114114
->addQuery(new TermQuery('title', 'pizza'));
115-
$result = $repository->execute($search);
115+
$repository->findDocuments($search);
116116

117117
$queries = $this->getCollector()->getQueries();
118118
$lastQuery = end($queries[ElasticsearchProfiler::UNDEFINED_ROUTE]);
@@ -172,7 +172,7 @@ public function testMatchAllQuery()
172172
$search = $repository
173173
->createSearch()
174174
->addAggregation(new GlobalAggregation('g'));
175-
$repository->execute($search);
175+
$repository->findDocuments($search);
176176

177177
$queries = $this->getCollector()->getQueries();
178178
$lastQuery = end($queries[ElasticsearchProfiler::UNDEFINED_ROUTE]);

Tests/Functional/Result/AggregationIteratorFindTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function testIteration()
7272
$search = $repository
7373
->createSearch()
7474
->addAggregation($this->buildAggregation());
75-
$results = $repository->execute($search);
75+
$results = $repository->findDocuments($search);
7676
$agg = $results->getAggregation('terms');
7777

7878
$this->assertInstanceOf('ONGR\ElasticsearchBundle\Result\Aggregation\AggregationValue', $agg);

Tests/Functional/Result/DocumentIteratorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testIteration()
6060
$repo = $this->getManager()->getRepository('AcmeBarBundle:Product');
6161
$match = new MatchAllQuery();
6262
$search = $repo->createSearch()->addQuery($match);
63-
$iterator = $repo->execute($search);
63+
$iterator = $repo->findDocuments($search);
6464

6565
$this->assertInstanceOf('ONGR\ElasticsearchBundle\Result\DocumentIterator', $iterator);
6666

@@ -91,7 +91,7 @@ public function testCurrentWithEmptyIterator()
9191
$search = $repo
9292
->createSearch()
9393
->addQuery(new MatchAllQuery());
94-
$result = $repo->execute($search);
94+
$result = $repo->findDocuments($search);
9595

9696
$this->assertNull($result->current());
9797
}
@@ -105,7 +105,7 @@ public function testIteratorFirst()
105105
$search = $repo
106106
->createSearch()
107107
->addQuery(new MatchAllQuery());
108-
$document = $repo->execute($search)->first();
108+
$document = $repo->findDocuments($search)->first();
109109

110110
$this->assertEquals('Foo Product', $document->getTitle());
111111
}

Tests/Functional/Result/DocumentScanIteratorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public function getIterationData()
8080
/**
8181
* Iteration test.
8282
*
83+
* @deprecated Tested function will be removed in 3.0
84+
*
8385
* @param Search $search
8486
* @param bool $isSorted
8587
*

Tests/Functional/Result/DocumentWithMultipleFieldsTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,23 @@ public function testMultipleFields()
6161
$search = $repo->createSearch();
6262
$search->addQuery($query);
6363

64-
$result = $repo->execute($search);
64+
$result = $repo->findDocuments($search);
6565

6666
$this->assertEquals(2, count($result));
6767

6868
$query = new MatchQuery('title.raw', 'Bar');
6969
$search = $repo->createSearch();
7070
$search->addQuery($query);
7171

72-
$result = $repo->execute($search);
72+
$result = $repo->findDocuments($search);
7373

7474
$this->assertEquals(0, count($result));
7575

7676
$query = new TermQuery('title.raw', 'Foo Product');
7777
$search = $repo->createSearch();
7878
$search->addQuery($query);
7979

80-
$result = $repo->execute($search);
80+
$result = $repo->findDocuments($search);
8181

8282
$this->assertEquals(1, count($result));
8383
}

Tests/Functional/Result/GetDocumentSortTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function testGetDocumentSort()
5757
$sort = new FieldSort('price', 'asc');
5858
$search = $repo->createSearch()->addQuery($match);
5959
$search->addSort($sort);
60-
$results = $repo->execute($search);
60+
$results = $repo->findDocuments($search);
6161
$sort_result = [];
6262
$expected = [1.95, 5, 8.33];
6363

Tests/Functional/Result/ObjectIteratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function testIteration()
6161
$repo = $this->getManager()->getRepository('AcmeBarBundle:Product');
6262
$match = new MatchAllQuery();
6363
$search = $repo->createSearch()->addQuery($match);
64-
$iterator = $repo->execute($search);
64+
$iterator = $repo->findDocuments($search);
6565

6666
$this->assertInstanceOf('ONGR\ElasticsearchBundle\Result\DocumentIterator', $iterator);
6767

Tests/Functional/Service/ManagerTest.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,16 +225,16 @@ public function testPersistTokenCountField()
225225
$product->setTokenPiecesCount('t e s t');
226226
$manager->persist($product);
227227
$manager->commit();
228-
228+
$repo = $manager->getRepository('AcmeBarBundle:Product');
229229
// Analyzer is whitespace, so there are four tokens.
230230
$search = new Search();
231231
$search->addQuery(new TermQuery('pieces_count.count', '4'));
232-
$this->assertEquals(1, $manager->execute(['AcmeBarBundle:Product'], $search)->count());
232+
$this->assertEquals(1, $repo->findDocuments($search)->count());
233233

234234
// Test with invalid count.
235235
$search = new Search();
236236
$search->addQuery(new TermQuery('pieces_count.count', '6'));
237-
$this->assertEquals(0, $manager->execute(['AcmeBarBundle:Product'], $search)->count());
237+
$this->assertEquals(0, $repo->findDocuments($search)->count());
238238
}
239239

240240
/**
@@ -277,16 +277,16 @@ public function testGetRepositoryException()
277277
}
278278

279279
/**
280-
* Test if execute() works with multiple types.
280+
* Test if search() works with multiple types.
281281
*/
282282
public function testExecuteQueryOnMultipleTypes()
283283
{
284-
$result = $this->getManager('foo')->execute(
285-
['AcmeBarBundle:Product', 'AcmeFooBundle:Customer'],
286-
new Search()
284+
$result = $this->getManager('foo')->search(
285+
['product', 'customer'],
286+
(new Search())->toArray()
287287
);
288288

289-
$this->assertCount(5, $result);
289+
$this->assertCount(5, $result['hits']['hits']);
290290
}
291291

292292
/**
@@ -328,26 +328,28 @@ public function testParseResultsWithDifferentResultTypes()
328328
$repo = $fooManager->getRepository('AcmeBarBundle:Product');
329329
$search = $repo->createSearch();
330330
$search->addQuery(new MatchAllQuery());
331-
$products = $repo->execute($search, 'array');
331+
$products = $repo->findArray($search);
332332
$this->assertArrayHasKey(0, $products);
333333

334334
$repo = $defaultManager->getRepository('AcmeBarBundle:Product');
335335
$search = $repo->createSearch();
336336
$search->addQuery(new MatchAllQuery());
337-
$products = $repo->execute($search, 'array');
337+
$products = $repo->findArray($search);
338338
$this->assertArrayNotHasKey(0, $products);
339339

340340
$repo = $fooManager->getRepository('AcmeBarBundle:Product');
341341
$search = $repo->createSearch();
342342
$search->addQuery(new MatchAllQuery());
343-
$products = $repo->execute($search, 'raw_iterator');
343+
$products = $repo->findRaw($search);
344344
$this->assertInstanceOf('ONGR\ElasticsearchBundle\Result\RawIterator', $products);
345345
}
346346

347347
/**
348348
* Tests exception that is thrown by parseResults()
349349
* when a bad result type is provided
350350
*
351+
* @deprecated Function will be removed in 3.0
352+
*
351353
* @expectedException \Exception
352354
*/
353355
public function testParseResultsException()

0 commit comments

Comments
 (0)