Skip to content

Commit 6edab65

Browse files
authored
Functional tests (#166)
* Moved all tests to Unit folder * updated travis to install elasticsearch * updated composer to install elasticsearch * updated namespace in unit tests * added funtional tests * fix match all query composing When there are no parameters it has to return \stdClass. (cherry picked from commit 5970e92) * updated unit test for match all query * constants can only have underscore dash * fixed psr style issues * updated travis * exclude not compatible versions
1 parent 78bd3be commit 6edab65

File tree

105 files changed

+340
-105
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+340
-105
lines changed

Diff for: .travis.yml

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1-
sudo: false
1+
sudo: true
22
language: php
33
php:
44
- 5.4
55
- 5.5
66
- 5.6
77
- 7.0
88
- hhvm
9+
env:
10+
global:
11+
- JAVA_HOME="/usr/lib/jvm/java-8-oracle/jre"
12+
matrix:
13+
- ES_VERSION="5.0" ELASTICSEARH_PHP="~5.0"
14+
- ES_VERSION="2.4" ELASTICSEARH_PHP="~2.0"
915
matrix:
1016
allow_failures:
1117
- php: hhvm
18+
exclude:
19+
- php: 5.4
20+
env: ES_VERSION="5.0" ELASTICSEARH_PHP="~5.0"
21+
- php: 5.5
22+
env: ES_VERSION="5.0" ELASTICSEARH_PHP="~5.0"
23+
install:
24+
# Container based PHP image ues PHP 5.6.5, once it will be upgraded sudo will be not necessary
25+
- sudo apt-get install -y oracle-java8-set-default
26+
- ES_URL=$(curl -sS "https://esvm-props.kibana.rocks/builds" | jq -r ".branches[\"$ES_VERSION\"].zip")
27+
- curl -L -o elasticsearch.zip $ES_URL
28+
- unzip elasticsearch.zip
29+
- ./elasticsearch-*/bin/elasticsearch -d
1230
before_script:
31+
- composer require --no-update elasticsearch/elasticsearch:${ELASTICSEARH_PHP}
32+
- composer config -g github-oauth.github.com $GITHUB_COMPOSER_AUTH
1333
- composer install --no-interaction --prefer-dist
1434
script:
1535
- vendor/bin/phpunit --coverage-clover=coverage.clover

Diff for: composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"require-dev": {
1818
"phpunit/phpunit": "~4.4",
1919
"squizlabs/php_codesniffer": "~2.0",
20-
"satooshi/php-coveralls": "~0.7"
20+
"satooshi/php-coveralls": "~0.7",
21+
"elasticsearch/elasticsearch": "~5.0"
2122
},
2223
"autoload": {
2324
"psr-4": {

Diff for: src/Query/MatchAllQuery.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function getType()
4444
*/
4545
public function toArray()
4646
{
47-
return [$this->getType() => $this->getParameters()];
47+
$params = $this->getParameters();
48+
return [$this->getType() => !empty($params) ? $params : new \stdClass()];
4849
}
4950
}

Diff for: tests/Functional/AbstractElasticsearchTestCase.php

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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;
13+
14+
use Elasticsearch\Client;
15+
use Elasticsearch\ClientBuilder;
16+
use ONGR\ElasticsearchDSL\Search;
17+
18+
abstract class AbstractElasticsearchTestCase extends \PHPUnit_Framework_TestCase
19+
{
20+
/**
21+
* Test index name in the elasticsearch.
22+
*/
23+
const INDEX_NAME = 'elasticsaerch-dsl-test';
24+
25+
/**
26+
* @var Client
27+
*/
28+
private $client;
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
protected function setUp()
34+
{
35+
parent::setUp();
36+
37+
$this->client = ClientBuilder::create()->build();
38+
$this->deleteIndex();
39+
40+
$this->client->indices()->create(
41+
array_filter(
42+
[
43+
'index' => self::INDEX_NAME,
44+
'mapping' => $this->getMapping()
45+
]
46+
)
47+
);
48+
49+
$bulkBody = [];
50+
51+
foreach ($this->getDataArray() as $type => $documents) {
52+
foreach ($documents as $id => $document) {
53+
$bulkBody[] = [
54+
'index' => [
55+
'_index' => self::INDEX_NAME,
56+
'_type' => $type,
57+
'_id' => $id,
58+
]
59+
];
60+
$bulkBody[] = $document;
61+
}
62+
}
63+
64+
$this->client->bulk(
65+
[
66+
'body' => $bulkBody
67+
]
68+
);
69+
$this->client->indices()->refresh();
70+
}
71+
72+
private function deleteIndex()
73+
{
74+
try {
75+
$this->client->indices()->delete(['index' => self::INDEX_NAME]);
76+
} catch (\Exception $e) {
77+
// Do nothing.
78+
}
79+
}
80+
81+
/**
82+
* Defines index mapping for test index.
83+
* Override this function in your test case and return array with mapping body.
84+
* More info check here: https://goo.gl/zWBree
85+
*
86+
* @return array Mapping body
87+
*/
88+
protected function getMapping()
89+
{
90+
return [];
91+
}
92+
93+
/**
94+
* Can be overwritten in child class to populate elasticsearch index with the data.
95+
*
96+
* Example:
97+
* [
98+
* 'type_name' => [
99+
* 'custom_id' => [
100+
* 'title' => 'foo',
101+
* ],
102+
* 3 => [
103+
* '_id' => 2,
104+
* 'title' => 'bar',
105+
* ]
106+
* ]
107+
* ]
108+
* Document _id can be set as it's id.
109+
*
110+
* @return array
111+
*/
112+
protected function getDataArray()
113+
{
114+
return [];
115+
}
116+
117+
/**
118+
* {@inheritdoc}
119+
*/
120+
protected function tearDown()
121+
{
122+
parent::tearDown();
123+
$this->deleteIndex();
124+
}
125+
126+
protected function executeSearch(Search $search, $type = null, $returnRaw = false)
127+
{
128+
$response = $this->client->search(
129+
array_filter([
130+
'index' => self::INDEX_NAME,
131+
'type' => $type,
132+
'body' => $search->toArray(),
133+
])
134+
);
135+
136+
if ($returnRaw) {
137+
return $response;
138+
}
139+
140+
$documents = [];
141+
142+
try {
143+
foreach ($response['hits']['hits'] as $document) {
144+
$documents[$document['_id']] = $document['_source'];
145+
}
146+
} catch (\Exception $e) {
147+
return $documents;
148+
}
149+
150+
return $documents;
151+
}
152+
}

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

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\MatchAllQuery;
15+
use ONGR\ElasticsearchDSL\Search;
16+
use ONGR\ElasticsearchDSL\Tests\Functional\AbstractElasticsearchTestCase;
17+
18+
class MatchAllQueryTest extends AbstractElasticsearchTestCase
19+
{
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
protected function getDataArray()
24+
{
25+
return [
26+
'product' => [
27+
[
28+
'title' => 'acme',
29+
],
30+
[
31+
'title' => 'foo',
32+
],
33+
]
34+
];
35+
}
36+
37+
/**
38+
* Match all test
39+
*/
40+
public function testMatchAll()
41+
{
42+
$search = new Search();
43+
$matchAll = new MatchAllQuery();
44+
45+
$search->addQuery($matchAll);
46+
47+
$results = $this->executeSearch($search);
48+
49+
$this->assertEquals($this->getDataArray()['product'], $results);
50+
}
51+
}

Diff for: tests/Aggregation/CardinalityAggregationTest.php renamed to tests/Unit/Aggregation/CardinalityAggregationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace ONGR\ElasticsearchDSL\Tests\Aggregation;
12+
namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation;
1313

1414
use ONGR\ElasticsearchDSL\Aggregation\CardinalityAggregation;
1515

Diff for: tests/Aggregation/ChildrenAggregationTest.php renamed to tests/Unit/Aggregation/ChildrenAggregationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace ONGR\ElasticsearchDSL\Tests\Aggregation;
12+
namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation;
1313

1414
use ONGR\ElasticsearchDSL\Aggregation\ChildrenAggregation;
1515

Diff for: tests/Aggregation/DateHistogramAggregationTest.php renamed to tests/Unit/Aggregation/DateHistogramAggregationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace ONGR\ElasticsearchDSL\Tests\Aggregation;
12+
namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation;
1313

1414
use ONGR\ElasticsearchDSL\Aggregation\DateHistogramAggregation;
1515

Diff for: tests/Aggregation/DateRangeAggregationTest.php renamed to tests/Unit/Aggregation/DateRangeAggregationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace ONGR\ElasticsearchDSL\Tests\Aggregation;
12+
namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation;
1313

1414
use ONGR\ElasticsearchDSL\Aggregation\DateRangeAggregation;
1515

Diff for: tests/Aggregation/FilterAggregationTest.php renamed to tests/Unit/Aggregation/FilterAggregationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace ONGR\ElasticsearchDSL\Tests\Aggregation;
12+
namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation;
1313

1414
use ONGR\ElasticsearchDSL\Aggregation\FilterAggregation;
1515
use ONGR\ElasticsearchDSL\Aggregation\HistogramAggregation;

Diff for: tests/Aggregation/FiltersAggregationTest.php renamed to tests/Unit/Aggregation/FiltersAggregationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace ONGR\ElasticsearchDSL\Tests\Aggregation;
12+
namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation;
1313

1414
use ONGR\ElasticsearchDSL\Aggregation\FiltersAggregation;
1515
use ONGR\ElasticsearchDSL\BuilderInterface;

Diff for: tests/Aggregation/GeoBoundsAggregationTest.php renamed to tests/Unit/Aggregation/GeoBoundsAggregationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace ONGR\ElasticsearchDSL\Tests\Aggregation;
12+
namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation;
1313

1414
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
1515
use ONGR\ElasticsearchDSL\Aggregation\GeoBoundsAggregation;

Diff for: tests/Aggregation/GeoCentroidAggregationTest.php renamed to tests/Unit/Aggregation/GeoCentroidAggregationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace ONGR\ElasticsearchDSL\Tests\Aggregation;
12+
namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation;
1313

1414
use ONGR\ElasticsearchDSL\Aggregation\GeoCentroidAggregation;
1515

Diff for: tests/Aggregation/GeoDistanceAggregationTest.php renamed to tests/Unit/Aggregation/GeoDistanceAggregationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace ONGR\ElasticsearchDSL\Tests\Aggregation;
12+
namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation;
1313

1414
use ONGR\ElasticsearchDSL\Aggregation\GeoDistanceAggregation;
1515

Diff for: tests/Aggregation/GeoHashGridAggregationTest.php renamed to tests/Unit/Aggregation/GeoHashGridAggregationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace ONGR\ElasticsearchDSL\Tests\Aggregation;
12+
namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation;
1313

1414
use ONGR\ElasticsearchDSL\Aggregation\GeoHashGridAggregation;
1515

Diff for: tests/Aggregation/GlobalAggregationTest.php renamed to tests/Unit/Aggregation/GlobalAggregationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace ONGR\ElasticsearchDSL\Tests\Aggregation;
12+
namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation;
1313

1414
use ONGR\ElasticsearchDSL\Aggregation\GlobalAggregation;
1515

Diff for: tests/Aggregation/Ipv4RangeAggregationTest.php renamed to tests/Unit/Aggregation/Ipv4RangeAggregationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace ONGR\ElasticsearchDSL\Tests\Aggregation;
12+
namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation;
1313

1414
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
1515
use ONGR\ElasticsearchDSL\Aggregation\Ipv4RangeAggregation;

Diff for: tests/Aggregation/MissingAggregationTest.php renamed to tests/Unit/Aggregation/MissingAggregationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace ONGR\ElasticsearchDSL\Tests\Aggregation;
12+
namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation;
1313

1414
use ONGR\ElasticsearchDSL\Aggregation\MissingAggregation;
1515

Diff for: tests/Aggregation/NestedAggregationTest.php renamed to tests/Unit/Aggregation/NestedAggregationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace ONGR\ElasticsearchDSL\Tests\Aggregation;
12+
namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation;
1313

1414
use ONGR\ElasticsearchDSL\Aggregation\NestedAggregation;
1515
use ONGR\ElasticsearchDSL\Aggregation\TermsAggregation;

Diff for: tests/Aggregation/PercentileRanksAggregationTest.php renamed to tests/Unit/Aggregation/PercentileRanksAggregationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace ONGR\ElasticsearchDSL\Tests\Aggregation;
12+
namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation;
1313

1414
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
1515
use ONGR\ElasticsearchDSL\Aggregation\PercentileRanksAggregation;

0 commit comments

Comments
 (0)