From c096a9513729da18242853a8643c37fee0fe10bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simonas=20=C5=A0erlinskas?= Date: Fri, 16 Sep 2016 10:41:38 +0300 Subject: [PATCH] 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 --- Result/ArrayIterator.php | 34 ++++++++++++++++++- Service/Repository.php | 16 ++++++++- .../Profiler/ElasticsearchProfilerTest.php | 4 +-- .../Result/AggregationIteratorFindTest.php | 2 +- .../Result/DocumentIteratorTest.php | 6 ++-- .../Result/DocumentScanIteratorTest.php | 2 ++ .../Result/DocumentWithMultipleFieldsTest.php | 6 ++-- .../Functional/Result/GetDocumentSortTest.php | 2 +- .../Functional/Result/ObjectIteratorTest.php | 2 +- Tests/Functional/Service/ManagerTest.php | 24 +++++++------ Tests/Functional/Service/RepositoryTest.php | 2 +- 11 files changed, 75 insertions(+), 25 deletions(-) diff --git a/Result/ArrayIterator.php b/Result/ArrayIterator.php index 74b7d0e7..9d0ec699 100644 --- a/Result/ArrayIterator.php +++ b/Result/ArrayIterator.php @@ -17,8 +17,40 @@ /** * Class DocumentIterator. */ -class ArrayIterator extends AbstractResultsIterator +class ArrayIterator extends AbstractResultsIterator implements \ArrayAccess { + /** + * {@inheritdoc} + */ + public function offsetExists($offset) + { + return $this->documentExists($offset); + } + + /** + * {@inheritdoc} + */ + public function offsetGet($offset) + { + return $this->getDocument($offset); + } + + /** + * {@inheritdoc} + */ + public function offsetSet($offset, $value) + { + $this->documents[$offset] = $value; + } + + /** + * {@inheritdoc} + */ + public function offsetUnset($offset) + { + unset($this->documents[$offset]); + } + /** * {@inheritdoc} */ diff --git a/Service/Repository.php b/Service/Repository.php index 80e505dc..894a4c28 100644 --- a/Service/Repository.php +++ b/Service/Repository.php @@ -232,11 +232,25 @@ private function getScrollConfiguration($raw, $scrollDuration) /** * Returns DocumentIterator with composed Document objects from array response. * - * @param Search $search + * @deprecated Miss type in the function name, use findDocuments() instead. Will remove in 3.0 * + * @param Search $search * @return DocumentIterator */ public function findDocument(Search $search) + { + return $this->findDocuments($search); + } + + + /** + * Returns DocumentIterator with composed Document objects from array response. + * + * @param Search $search + * + * @return DocumentIterator + */ + public function findDocuments(Search $search) { $results = $this->executeSearch($search); diff --git a/Tests/Functional/Profiler/ElasticsearchProfilerTest.php b/Tests/Functional/Profiler/ElasticsearchProfilerTest.php index 086477b7..4cde5cdd 100644 --- a/Tests/Functional/Profiler/ElasticsearchProfilerTest.php +++ b/Tests/Functional/Profiler/ElasticsearchProfilerTest.php @@ -112,7 +112,7 @@ public function testGetTermQuery() $search = $repository ->createSearch() ->addQuery(new TermQuery('title', 'pizza')); - $result = $repository->execute($search); + $repository->findDocuments($search); $queries = $this->getCollector()->getQueries(); $lastQuery = end($queries[ElasticsearchProfiler::UNDEFINED_ROUTE]); @@ -172,7 +172,7 @@ public function testMatchAllQuery() $search = $repository ->createSearch() ->addAggregation(new GlobalAggregation('g')); - $repository->execute($search); + $repository->findDocuments($search); $queries = $this->getCollector()->getQueries(); $lastQuery = end($queries[ElasticsearchProfiler::UNDEFINED_ROUTE]); diff --git a/Tests/Functional/Result/AggregationIteratorFindTest.php b/Tests/Functional/Result/AggregationIteratorFindTest.php index 6203df12..fb2f2cc3 100644 --- a/Tests/Functional/Result/AggregationIteratorFindTest.php +++ b/Tests/Functional/Result/AggregationIteratorFindTest.php @@ -72,7 +72,7 @@ public function testIteration() $search = $repository ->createSearch() ->addAggregation($this->buildAggregation()); - $results = $repository->execute($search); + $results = $repository->findDocuments($search); $agg = $results->getAggregation('terms'); $this->assertInstanceOf('ONGR\ElasticsearchBundle\Result\Aggregation\AggregationValue', $agg); diff --git a/Tests/Functional/Result/DocumentIteratorTest.php b/Tests/Functional/Result/DocumentIteratorTest.php index 4e5ad5a5..466c206e 100644 --- a/Tests/Functional/Result/DocumentIteratorTest.php +++ b/Tests/Functional/Result/DocumentIteratorTest.php @@ -60,7 +60,7 @@ public function testIteration() $repo = $this->getManager()->getRepository('AcmeBarBundle:Product'); $match = new MatchAllQuery(); $search = $repo->createSearch()->addQuery($match); - $iterator = $repo->execute($search); + $iterator = $repo->findDocuments($search); $this->assertInstanceOf('ONGR\ElasticsearchBundle\Result\DocumentIterator', $iterator); @@ -91,7 +91,7 @@ public function testCurrentWithEmptyIterator() $search = $repo ->createSearch() ->addQuery(new MatchAllQuery()); - $result = $repo->execute($search); + $result = $repo->findDocuments($search); $this->assertNull($result->current()); } @@ -105,7 +105,7 @@ public function testIteratorFirst() $search = $repo ->createSearch() ->addQuery(new MatchAllQuery()); - $document = $repo->execute($search)->first(); + $document = $repo->findDocuments($search)->first(); $this->assertEquals('Foo Product', $document->getTitle()); } diff --git a/Tests/Functional/Result/DocumentScanIteratorTest.php b/Tests/Functional/Result/DocumentScanIteratorTest.php index a77642a3..99e2ef9d 100644 --- a/Tests/Functional/Result/DocumentScanIteratorTest.php +++ b/Tests/Functional/Result/DocumentScanIteratorTest.php @@ -80,6 +80,8 @@ public function getIterationData() /** * Iteration test. * + * @deprecated Tested function will be removed in 3.0 + * * @param Search $search * @param bool $isSorted * diff --git a/Tests/Functional/Result/DocumentWithMultipleFieldsTest.php b/Tests/Functional/Result/DocumentWithMultipleFieldsTest.php index 4f2eda45..bf356237 100644 --- a/Tests/Functional/Result/DocumentWithMultipleFieldsTest.php +++ b/Tests/Functional/Result/DocumentWithMultipleFieldsTest.php @@ -61,7 +61,7 @@ public function testMultipleFields() $search = $repo->createSearch(); $search->addQuery($query); - $result = $repo->execute($search); + $result = $repo->findDocuments($search); $this->assertEquals(2, count($result)); @@ -69,7 +69,7 @@ public function testMultipleFields() $search = $repo->createSearch(); $search->addQuery($query); - $result = $repo->execute($search); + $result = $repo->findDocuments($search); $this->assertEquals(0, count($result)); @@ -77,7 +77,7 @@ public function testMultipleFields() $search = $repo->createSearch(); $search->addQuery($query); - $result = $repo->execute($search); + $result = $repo->findDocuments($search); $this->assertEquals(1, count($result)); } diff --git a/Tests/Functional/Result/GetDocumentSortTest.php b/Tests/Functional/Result/GetDocumentSortTest.php index 747406dc..6ff8310e 100644 --- a/Tests/Functional/Result/GetDocumentSortTest.php +++ b/Tests/Functional/Result/GetDocumentSortTest.php @@ -57,7 +57,7 @@ public function testGetDocumentSort() $sort = new FieldSort('price', 'asc'); $search = $repo->createSearch()->addQuery($match); $search->addSort($sort); - $results = $repo->execute($search); + $results = $repo->findDocuments($search); $sort_result = []; $expected = [1.95, 5, 8.33]; diff --git a/Tests/Functional/Result/ObjectIteratorTest.php b/Tests/Functional/Result/ObjectIteratorTest.php index 1fa55936..d94a31f3 100644 --- a/Tests/Functional/Result/ObjectIteratorTest.php +++ b/Tests/Functional/Result/ObjectIteratorTest.php @@ -61,7 +61,7 @@ public function testIteration() $repo = $this->getManager()->getRepository('AcmeBarBundle:Product'); $match = new MatchAllQuery(); $search = $repo->createSearch()->addQuery($match); - $iterator = $repo->execute($search); + $iterator = $repo->findDocuments($search); $this->assertInstanceOf('ONGR\ElasticsearchBundle\Result\DocumentIterator', $iterator); diff --git a/Tests/Functional/Service/ManagerTest.php b/Tests/Functional/Service/ManagerTest.php index 466f62cd..5f338227 100644 --- a/Tests/Functional/Service/ManagerTest.php +++ b/Tests/Functional/Service/ManagerTest.php @@ -225,16 +225,16 @@ public function testPersistTokenCountField() $product->setTokenPiecesCount('t e s t'); $manager->persist($product); $manager->commit(); - + $repo = $manager->getRepository('AcmeBarBundle:Product'); // Analyzer is whitespace, so there are four tokens. $search = new Search(); $search->addQuery(new TermQuery('pieces_count.count', '4')); - $this->assertEquals(1, $manager->execute(['AcmeBarBundle:Product'], $search)->count()); + $this->assertEquals(1, $repo->findDocuments($search)->count()); // Test with invalid count. $search = new Search(); $search->addQuery(new TermQuery('pieces_count.count', '6')); - $this->assertEquals(0, $manager->execute(['AcmeBarBundle:Product'], $search)->count()); + $this->assertEquals(0, $repo->findDocuments($search)->count()); } /** @@ -277,16 +277,16 @@ public function testGetRepositoryException() } /** - * Test if execute() works with multiple types. + * Test if search() works with multiple types. */ public function testExecuteQueryOnMultipleTypes() { - $result = $this->getManager('foo')->execute( - ['AcmeBarBundle:Product', 'AcmeFooBundle:Customer'], - new Search() + $result = $this->getManager('foo')->search( + ['product', 'customer'], + (new Search())->toArray() ); - $this->assertCount(5, $result); + $this->assertCount(5, $result['hits']['hits']); } /** @@ -328,19 +328,19 @@ public function testParseResultsWithDifferentResultTypes() $repo = $fooManager->getRepository('AcmeBarBundle:Product'); $search = $repo->createSearch(); $search->addQuery(new MatchAllQuery()); - $products = $repo->execute($search, 'array'); + $products = $repo->findArray($search); $this->assertArrayHasKey(0, $products); $repo = $defaultManager->getRepository('AcmeBarBundle:Product'); $search = $repo->createSearch(); $search->addQuery(new MatchAllQuery()); - $products = $repo->execute($search, 'array'); + $products = $repo->findArray($search); $this->assertArrayNotHasKey(0, $products); $repo = $fooManager->getRepository('AcmeBarBundle:Product'); $search = $repo->createSearch(); $search->addQuery(new MatchAllQuery()); - $products = $repo->execute($search, 'raw_iterator'); + $products = $repo->findRaw($search); $this->assertInstanceOf('ONGR\ElasticsearchBundle\Result\RawIterator', $products); } @@ -348,6 +348,8 @@ public function testParseResultsWithDifferentResultTypes() * Tests exception that is thrown by parseResults() * when a bad result type is provided * + * @deprecated Function will be removed in 3.0 + * * @expectedException \Exception */ public function testParseResultsException() diff --git a/Tests/Functional/Service/RepositoryTest.php b/Tests/Functional/Service/RepositoryTest.php index 0b90bba0..154690cb 100644 --- a/Tests/Functional/Service/RepositoryTest.php +++ b/Tests/Functional/Service/RepositoryTest.php @@ -338,7 +338,7 @@ public function testRepositoryExecuteWhenZeroResult() ->createSearch() ->addFilter(new PrefixQuery('title', 'dummy')); - $searchResult = $repository->execute($search); + $searchResult = $repository->findDocuments($search); $this->assertInstanceOf( '\ONGR\ElasticsearchBundle\Result\DocumentIterator', $searchResult