Skip to content

Commit

Permalink
Refactor tests by eliminating deprecated execute function (#688)
Browse files Browse the repository at this point in the history
* added findDocuments() instead of findDocument()

* implemented array access in ArrayIterator

* refactored tests by eliminating deprecated execute() function
  • Loading branch information
saimaz authored Sep 16, 2016
1 parent 8e5e6e7 commit c096a95
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 25 deletions.
34 changes: 33 additions & 1 deletion Result/ArrayIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down
16 changes: 15 additions & 1 deletion Service/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions Tests/Functional/Profiler/ElasticsearchProfilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/Result/AggregationIteratorFindTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions Tests/Functional/Result/DocumentIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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());
}
Expand All @@ -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());
}
Expand Down
2 changes: 2 additions & 0 deletions Tests/Functional/Result/DocumentScanIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public function getIterationData()
/**
* Iteration test.
*
* @deprecated Tested function will be removed in 3.0
*
* @param Search $search
* @param bool $isSorted
*
Expand Down
6 changes: 3 additions & 3 deletions Tests/Functional/Result/DocumentWithMultipleFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,23 @@ public function testMultipleFields()
$search = $repo->createSearch();
$search->addQuery($query);

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

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

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

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

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

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

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

$this->assertEquals(1, count($result));
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/Result/GetDocumentSortTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/Result/ObjectIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
24 changes: 13 additions & 11 deletions Tests/Functional/Service/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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']);
}

/**
Expand Down Expand Up @@ -328,26 +328,28 @@ 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);
}

/**
* 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()
Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/Service/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c096a95

Please sign in to comment.