Skip to content

Commit e9f486f

Browse files
authored
Add functional score test (#189)
* add functional score test * update tests changed deprecated getMock to getMockBuilder * updated travis
1 parent 12a601d commit e9f486f

16 files changed

+108
-17
lines changed

Diff for: .travis.yml

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,21 @@ php:
88
- hhvm
99
env:
1010
global:
11-
- JAVA_HOME="/usr/lib/jvm/java-8-oracle/jre" ES_VERSION="2.4" ELASTICSEARH_PHP="~2.0"
11+
- JAVA_HOME="/usr/lib/jvm/java-8-oracle/jre" ELASTICSEARH_PHP="~2.0"
12+
- ELASRICSEARCH_HOST="127.0.0.1:9200"
13+
- ES_URL="https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/zip/elasticsearch/2.4.4/elasticsearch-2.4.4.zip"
1214
matrix:
1315
allow_failures:
1416
- php: hhvm
1517
install:
1618
# Container based PHP image ues PHP 5.6.5, once it will be upgraded sudo will be not necessary
1719
- sudo apt-get install -y oracle-java8-set-default
18-
- ES_URL=$(curl -sS "https://esvm-props.kibana.rocks/builds" | jq -r ".branches[\"$ES_VERSION\"].zip")
1920
- curl -L -o elasticsearch.zip $ES_URL
2021
- unzip elasticsearch.zip
21-
- ./elasticsearch-*/bin/elasticsearch -d
22+
- ./elasticsearch-*/bin/elasticsearch -d --script.inline true --script.stored true
23+
- sleep 10
24+
- curl 127.0.0.1:9200/_nodes/plugins
25+
- curl 127.0.0.1:9200/_nodes
2226
before_script:
2327
- composer require --no-update elasticsearch/elasticsearch:${ELASTICSEARH_PHP}
2428
- composer config -g github-oauth.github.com $GITHUB_COMPOSER_AUTH

Diff for: tests/Functional/Query/FunctionScoreQueryTest.php

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchDSL\Tests\Functional\Query;
13+
14+
use ONGR\ElasticsearchDSL\Query\FunctionScoreQuery;
15+
use ONGR\ElasticsearchDSL\Query\MatchAllQuery;
16+
use ONGR\ElasticsearchDSL\Search;
17+
use ONGR\ElasticsearchDSL\Tests\Functional\AbstractElasticsearchTestCase;
18+
19+
class FunctionScoreQueryTest extends AbstractElasticsearchTestCase
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
protected function getDataArray()
25+
{
26+
return [
27+
'product' => [
28+
[
29+
'title' => 'acme',
30+
'price' => 10,
31+
],
32+
[
33+
'title' => 'foo',
34+
'price' => 20,
35+
],
36+
[
37+
'title' => 'bar',
38+
'price' => 30,
39+
],
40+
]
41+
];
42+
}
43+
44+
/**
45+
* Match all test
46+
*/
47+
public function testRandomScore()
48+
{
49+
$fquery = new FunctionScoreQuery(new MatchAllQuery());
50+
$fquery->addRandomFunction();
51+
$fquery->addParameter('boost_mode', 'multiply');
52+
53+
$search = new Search();
54+
$search->addQuery($fquery);
55+
$results = $this->executeSearch($search);
56+
57+
$this->assertEquals(count($this->getDataArray()['product']), count($results));
58+
}
59+
60+
public function testScriptScore()
61+
{
62+
$fquery = new FunctionScoreQuery(new MatchAllQuery());
63+
$fquery->addScriptScoreFunction(
64+
'price = doc[\'price\'].value; margin = doc[\'margin\'].value;
65+
if (price > target) { return price * (1 - discount); };
66+
return price;',
67+
[
68+
'target' => 20,
69+
'discount' => 0.9,
70+
],
71+
[
72+
'lang' => 'groovy',
73+
]
74+
);
75+
76+
$search = new Search();
77+
$search->addQuery($fquery);
78+
$results = $this->executeSearch($search);
79+
80+
foreach ($results as $document) {
81+
$this->assertLessThanOrEqual(20, $document['price']);
82+
}
83+
}
84+
}

Diff for: tests/Unit/BuilderBagTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ public function testGet()
8383
*/
8484
private function getBuilder($name)
8585
{
86-
$friendlyBuilderMock = $this->getMock('ONGR\ElasticsearchDSL\BuilderInterface');
86+
$friendlyBuilderMock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')
87+
->setMethods(['getName', 'toArray', 'getType'])
88+
->disableOriginalConstructor()
89+
->getMock();
8790

8891
$friendlyBuilderMock
8992
->expects($this->any())

Diff for: tests/Unit/Query/BoostingQueryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class BoostingQueryTest extends \PHPUnit_Framework_TestCase
2020
*/
2121
public function testToArray()
2222
{
23-
$mock = $this->getMock('ONGR\ElasticsearchDSL\BuilderInterface');
23+
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock();
2424
$mock
2525
->expects($this->any())
2626
->method('toArray')

Diff for: tests/Unit/Query/ConstantScoreQueryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ConstantScoreQueryTest extends \PHPUnit_Framework_TestCase
2020
*/
2121
public function testToArray()
2222
{
23-
$mock = $this->getMock('ONGR\ElasticsearchDSL\BuilderInterface');
23+
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock();
2424
$mock
2525
->expects($this->any())
2626
->method('toArray')

Diff for: tests/Unit/Query/DisMaxQueryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class DisMaxQueryTest extends \PHPUnit_Framework_TestCase
2020
*/
2121
public function testToArray()
2222
{
23-
$mock = $this->getMock('ONGR\ElasticsearchDSL\BuilderInterface');
23+
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock();
2424
$mock
2525
->expects($this->any())
2626
->method('toArray')

Diff for: tests/Unit/Query/FunctionScoreQueryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function addRandomFunctionProvider()
6767
public function testAddRandomFunction($seed, $expectedArray)
6868
{
6969
/** @var MatchAllQuery|MockObject $matchAllQuery */
70-
$matchAllQuery = $this->getMock('ONGR\ElasticsearchDSL\Query\MatchAllQuery');
70+
$matchAllQuery = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\MatchAllQuery')->getMock();
7171

7272
$functionScoreQuery = new FunctionScoreQuery($matchAllQuery);
7373
$functionScoreQuery->addRandomFunction($seed);

Diff for: tests/Unit/Query/HasChildQueryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class HasChildQueryTest extends \PHPUnit_Framework_TestCase
2020
*/
2121
public function testConstructor()
2222
{
23-
$parentQuery = $this->getMock('ONGR\ElasticsearchDSL\BuilderInterface');
23+
$parentQuery = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock();
2424
$query = new HasChildQuery('test_type', $parentQuery, ['test_parameter1']);
2525
$this->assertEquals(['test_parameter1'], $query->getParameters());
2626
}

Diff for: tests/Unit/Query/HasParentQueryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class HasParentQueryTest extends \PHPUnit_Framework_TestCase
2020
*/
2121
public function testConstructor()
2222
{
23-
$parentQuery = $this->getMock('ONGR\ElasticsearchDSL\BuilderInterface');
23+
$parentQuery = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock();
2424
$query = new HasParentQuery('test_type', $parentQuery, ['test_parameter1']);
2525
$this->assertEquals(['test_parameter1'], $query->getParameters());
2626
}

Diff for: tests/Unit/Query/Span/SpanContainingQueryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testToArray()
4747
*/
4848
private function getSpanQueryMock($value)
4949
{
50-
$mock = $this->getMock('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface');
50+
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock();
5151
$mock
5252
->expects($this->once())
5353
->method('toArray')

Diff for: tests/Unit/Query/Span/SpanFirstQueryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class SpanFirstQueryTest extends \PHPUnit_Framework_TestCase
2323
*/
2424
public function testToArray()
2525
{
26-
$mock = $this->getMock('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface');
26+
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock();
2727
$mock
2828
->expects($this->once())
2929
->method('toArray')

Diff for: tests/Unit/Query/Span/SpanMultiTermQueryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class SpanMultiTermQueryTest extends \PHPUnit_Framework_TestCase
2323
*/
2424
public function testToArray()
2525
{
26-
$mock = $this->getMock('ONGR\ElasticsearchDSL\BuilderInterface');
26+
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock();
2727
$mock
2828
->expects($this->once())
2929
->method('toArray')

Diff for: tests/Unit/Query/Span/SpanNearQueryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class SpanNearQueryTest extends \PHPUnit_Framework_TestCase
2323
*/
2424
public function testToArray()
2525
{
26-
$mock = $this->getMock('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface');
26+
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock();
2727
$mock
2828
->expects($this->once())
2929
->method('toArray')

Diff for: tests/Unit/Query/Span/SpanNotQueryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class SpanNotQueryTest extends \PHPUnit_Framework_TestCase
2323
*/
2424
public function testSpanNotQueryToArray()
2525
{
26-
$mock = $this->getMock('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface');
26+
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock();
2727
$mock
2828
->expects($this->exactly(2))
2929
->method('toArray')

Diff for: tests/Unit/Query/Span/SpanOrQueryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class SpanOrQueryTest extends \PHPUnit_Framework_TestCase
2323
*/
2424
public function testToArray()
2525
{
26-
$mock = $this->getMock('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface');
26+
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock();
2727
$mock
2828
->expects($this->once())
2929
->method('toArray')

Diff for: tests/Unit/Query/Span/SpanWithinQueryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testToArray()
4747
*/
4848
private function getSpanQueryMock($value)
4949
{
50-
$mock = $this->getMock('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface');
50+
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock();
5151
$mock
5252
->expects($this->once())
5353
->method('toArray')

0 commit comments

Comments
 (0)