Skip to content

Commit

Permalink
Add functional score test (ongr-io#189)
Browse files Browse the repository at this point in the history
* add functional score test

* update tests

changed deprecated getMock to getMockBuilder

* updated travis
  • Loading branch information
saimaz authored Jan 26, 2017
1 parent 12a601d commit e9f486f
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 17 deletions.
10 changes: 7 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ php:
- hhvm
env:
global:
- JAVA_HOME="/usr/lib/jvm/java-8-oracle/jre" ES_VERSION="2.4" ELASTICSEARH_PHP="~2.0"
- JAVA_HOME="/usr/lib/jvm/java-8-oracle/jre" ELASTICSEARH_PHP="~2.0"
- ELASRICSEARCH_HOST="127.0.0.1:9200"
- ES_URL="https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/zip/elasticsearch/2.4.4/elasticsearch-2.4.4.zip"
matrix:
allow_failures:
- php: hhvm
install:
# Container based PHP image ues PHP 5.6.5, once it will be upgraded sudo will be not necessary
- sudo apt-get install -y oracle-java8-set-default
- ES_URL=$(curl -sS "https://esvm-props.kibana.rocks/builds" | jq -r ".branches[\"$ES_VERSION\"].zip")
- curl -L -o elasticsearch.zip $ES_URL
- unzip elasticsearch.zip
- ./elasticsearch-*/bin/elasticsearch -d
- ./elasticsearch-*/bin/elasticsearch -d --script.inline true --script.stored true
- sleep 10
- curl 127.0.0.1:9200/_nodes/plugins
- curl 127.0.0.1:9200/_nodes
before_script:
- composer require --no-update elasticsearch/elasticsearch:${ELASTICSEARH_PHP}
- composer config -g github-oauth.github.com $GITHUB_COMPOSER_AUTH
Expand Down
84 changes: 84 additions & 0 deletions tests/Functional/Query/FunctionScoreQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ElasticsearchDSL\Tests\Functional\Query;

use ONGR\ElasticsearchDSL\Query\FunctionScoreQuery;
use ONGR\ElasticsearchDSL\Query\MatchAllQuery;
use ONGR\ElasticsearchDSL\Search;
use ONGR\ElasticsearchDSL\Tests\Functional\AbstractElasticsearchTestCase;

class FunctionScoreQueryTest extends AbstractElasticsearchTestCase
{
/**
* {@inheritdoc}
*/
protected function getDataArray()
{
return [
'product' => [
[
'title' => 'acme',
'price' => 10,
],
[
'title' => 'foo',
'price' => 20,
],
[
'title' => 'bar',
'price' => 30,
],
]
];
}

/**
* Match all test
*/
public function testRandomScore()
{
$fquery = new FunctionScoreQuery(new MatchAllQuery());
$fquery->addRandomFunction();
$fquery->addParameter('boost_mode', 'multiply');

$search = new Search();
$search->addQuery($fquery);
$results = $this->executeSearch($search);

$this->assertEquals(count($this->getDataArray()['product']), count($results));
}

public function testScriptScore()
{
$fquery = new FunctionScoreQuery(new MatchAllQuery());
$fquery->addScriptScoreFunction(
'price = doc[\'price\'].value; margin = doc[\'margin\'].value;
if (price > target) { return price * (1 - discount); };
return price;',
[
'target' => 20,
'discount' => 0.9,
],
[
'lang' => 'groovy',
]
);

$search = new Search();
$search->addQuery($fquery);
$results = $this->executeSearch($search);

foreach ($results as $document) {
$this->assertLessThanOrEqual(20, $document['price']);
}
}
}
5 changes: 4 additions & 1 deletion tests/Unit/BuilderBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ public function testGet()
*/
private function getBuilder($name)
{
$friendlyBuilderMock = $this->getMock('ONGR\ElasticsearchDSL\BuilderInterface');
$friendlyBuilderMock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')
->setMethods(['getName', 'toArray', 'getType'])
->disableOriginalConstructor()
->getMock();

$friendlyBuilderMock
->expects($this->any())
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Query/BoostingQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BoostingQueryTest extends \PHPUnit_Framework_TestCase
*/
public function testToArray()
{
$mock = $this->getMock('ONGR\ElasticsearchDSL\BuilderInterface');
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock();
$mock
->expects($this->any())
->method('toArray')
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Query/ConstantScoreQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ConstantScoreQueryTest extends \PHPUnit_Framework_TestCase
*/
public function testToArray()
{
$mock = $this->getMock('ONGR\ElasticsearchDSL\BuilderInterface');
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock();
$mock
->expects($this->any())
->method('toArray')
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Query/DisMaxQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DisMaxQueryTest extends \PHPUnit_Framework_TestCase
*/
public function testToArray()
{
$mock = $this->getMock('ONGR\ElasticsearchDSL\BuilderInterface');
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock();
$mock
->expects($this->any())
->method('toArray')
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Query/FunctionScoreQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function addRandomFunctionProvider()
public function testAddRandomFunction($seed, $expectedArray)
{
/** @var MatchAllQuery|MockObject $matchAllQuery */
$matchAllQuery = $this->getMock('ONGR\ElasticsearchDSL\Query\MatchAllQuery');
$matchAllQuery = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\MatchAllQuery')->getMock();

$functionScoreQuery = new FunctionScoreQuery($matchAllQuery);
$functionScoreQuery->addRandomFunction($seed);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Query/HasChildQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class HasChildQueryTest extends \PHPUnit_Framework_TestCase
*/
public function testConstructor()
{
$parentQuery = $this->getMock('ONGR\ElasticsearchDSL\BuilderInterface');
$parentQuery = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock();
$query = new HasChildQuery('test_type', $parentQuery, ['test_parameter1']);
$this->assertEquals(['test_parameter1'], $query->getParameters());
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Query/HasParentQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class HasParentQueryTest extends \PHPUnit_Framework_TestCase
*/
public function testConstructor()
{
$parentQuery = $this->getMock('ONGR\ElasticsearchDSL\BuilderInterface');
$parentQuery = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock();
$query = new HasParentQuery('test_type', $parentQuery, ['test_parameter1']);
$this->assertEquals(['test_parameter1'], $query->getParameters());
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Query/Span/SpanContainingQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testToArray()
*/
private function getSpanQueryMock($value)
{
$mock = $this->getMock('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface');
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock();
$mock
->expects($this->once())
->method('toArray')
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Query/Span/SpanFirstQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SpanFirstQueryTest extends \PHPUnit_Framework_TestCase
*/
public function testToArray()
{
$mock = $this->getMock('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface');
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock();
$mock
->expects($this->once())
->method('toArray')
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Query/Span/SpanMultiTermQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SpanMultiTermQueryTest extends \PHPUnit_Framework_TestCase
*/
public function testToArray()
{
$mock = $this->getMock('ONGR\ElasticsearchDSL\BuilderInterface');
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock();
$mock
->expects($this->once())
->method('toArray')
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Query/Span/SpanNearQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SpanNearQueryTest extends \PHPUnit_Framework_TestCase
*/
public function testToArray()
{
$mock = $this->getMock('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface');
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock();
$mock
->expects($this->once())
->method('toArray')
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Query/Span/SpanNotQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SpanNotQueryTest extends \PHPUnit_Framework_TestCase
*/
public function testSpanNotQueryToArray()
{
$mock = $this->getMock('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface');
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock();
$mock
->expects($this->exactly(2))
->method('toArray')
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Query/Span/SpanOrQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SpanOrQueryTest extends \PHPUnit_Framework_TestCase
*/
public function testToArray()
{
$mock = $this->getMock('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface');
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock();
$mock
->expects($this->once())
->method('toArray')
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Query/Span/SpanWithinQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testToArray()
*/
private function getSpanQueryMock($value)
{
$mock = $this->getMock('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface');
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock();
$mock
->expects($this->once())
->method('toArray')
Expand Down

0 comments on commit e9f486f

Please sign in to comment.