Skip to content

Commit 5c0edc6

Browse files
committed
adding test cases
1 parent 80c262f commit 5c0edc6

20 files changed

+1072
-38
lines changed

tests/ESQueryTest.php renamed to ESQueryTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ESQueryTest extends PHPUnit_Framework_TestCase
1717
protected function defaultQueryTemplate() {
1818
return [
1919
'index' => $this->index,
20-
'type' => $this->type,
20+
2121
'body' => [
2222
'_source' => [],
2323
'query' => [
@@ -33,7 +33,8 @@ protected function defaultQueryTemplate() {
3333
'size' => 10,
3434
'client' => [
3535
'ignore' => []
36-
]
36+
],
37+
'type' => $this->type,
3738
];
3839
}
3940

File renamed without changes.

composer.json

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,49 @@
11
{
22
"name": "basemkhirat/elasticsearch",
3-
43
"description": "Laravel, Lumen and Native php elasticseach query builder to build complex queries using an elegant syntax",
5-
6-
"keywords": ["elasticsearch", "php", "laravel", "lumen", "scout", "fulltext", "indexing", "builder"],
7-
4+
"keywords": [
5+
"elasticsearch",
6+
"php",
7+
"laravel",
8+
"lumen",
9+
"scout",
10+
"fulltext",
11+
"indexing",
12+
"builder"
13+
],
814
"license": "MIT",
9-
1015
"type": "package",
11-
1216
"homepage": "http://basemkhirat.com",
13-
1417
"support": {
1518
"issues": "https://github.com/basemkhirat/elasticsearch/issues",
1619
"source": "http://basemkhirat.com/indexing/2016/01/07/laravel-elasticseach-query-builder.html"
1720
},
18-
1921
"authors": [
2022
{
2123
"name": "basemkhirat",
2224
"email": "basemkhirat@gmail.com"
2325
}
2426
],
25-
2627
"autoload": {
27-
28-
"psr-4" : {
28+
"psr-4": {
2929
"Basemkhirat\\Elasticsearch\\": "src/"
3030
},
31-
3231
"files": [
3332
"src/helpers.php"
3433
]
3534
},
36-
35+
"autoload-dev": {
36+
"psr-4": {
37+
"Basemkhirat\\Elasticsearch\\Tests\\": "tests/"
38+
}
39+
},
3740
"require": {
3841
"php": ">=5.6.6",
3942
"elasticsearch/elasticsearch": "^5.0",
4043
"illuminate/pagination": "*",
4144
"illuminate/support": "*"
4245
},
43-
4446
"require-dev": {
4547
"phpunit/phpunit": "*"
4648
}
47-
4849
}

phpunit.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
>
1313
<testsuites>
1414
<testsuite name="Package Test Suite">
15-
<directory suffix=".php">./tests/</directory>
15+
<directory suffix=".php">./tests</directory>
1616
</testsuite>
1717
</testsuites>
18-
</phpunit>
18+
</phpunit>

src/Query.php

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -350,33 +350,37 @@ protected function getSkip()
350350
protected function getBody()
351351
{
352352

353-
if (count($this->must) || count($this->must_not) || count($this->filter) || count($this->sort) || count($this->_source)) {
353+
$body = [];
354354

355-
$this->body = [
356-
357-
"_source" => $this->_source,
358-
359-
"query" => [
360-
361-
"bool" => [
362-
363-
"must" => $this->must,
355+
if (count($this->_source)) {
356+
$body["_source"] = $this->_source;
357+
}
364358

365-
"must_not" => $this->must_not,
359+
$bool = [];
366360

367-
"filter" => $this->filter
361+
if (count($this->must)) {
362+
$bool["must"] = $this->must;
363+
}
368364

369-
]
370-
],
365+
if (count($this->must_not)) {
366+
$bool["must_not"] = $this->must_not;
367+
}
371368

372-
"sort" => $this->sort,
369+
if (count($this->filter)) {
370+
$bool["filter"] = $this->filter;
371+
}
373372

374-
];
373+
if (count($bool)) {
374+
$body["query"]["bool"] = $bool;
375+
}
375376

377+
if (count($this->sort)) {
378+
$body["sort"] = $this->sort;
376379
}
377380

378-
return $this->body;
381+
$this->body = $body;
379382

383+
return $body;
380384
}
381385

382386

@@ -577,7 +581,7 @@ public function whereNot($name, $operator = "=", $value = NULL)
577581
* @param $last_value
578582
* @return $this
579583
*/
580-
public function whereBetween($name, $first_value, $last_value)
584+
public function whereBetween($name, $first_value, $last_value = null)
581585
{
582586

583587
if (is_array($first_value) && count($first_value) == 2) {
@@ -598,7 +602,7 @@ public function whereBetween($name, $first_value, $last_value)
598602
* @param $last_value
599603
* @return $this
600604
*/
601-
public function whereNotBetween($name, $first_value, $last_value)
605+
public function whereNotBetween($name, $first_value, $last_value = null)
602606
{
603607

604608
if (is_array($first_value) && count($first_value) == 2) {

tests/DistanceTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Basemkhirat\Elasticsearch\Tests;
4+
5+
use Basemkhirat\Elasticsearch\Tests\Traits\ESQueryTrait;
6+
7+
class DistanceTest extends \PHPUnit_Framework_TestCase
8+
{
9+
10+
use ESQueryTrait;
11+
12+
/**
13+
* Test the distance() method.
14+
* @return void
15+
*/
16+
public function testDistanceMethod()
17+
{
18+
$this->assertEquals(
19+
$this->getExpected("location", ['lat' => -33.8688197, 'lon' => 151.20929550000005], "10km"),
20+
$this->getActual("location", ['lat' => -33.8688197, 'lon' => 151.20929550000005], "10km")
21+
);
22+
23+
$this->assertNotEquals(
24+
$this->getExpected("location", ['lat' => -33.8688197, 'lon' => 151.20929550000005], "10km"),
25+
$this->getActual("location", ['lat' => -33.8688197, 'lon' => 151.20929550000005], "15km")
26+
);
27+
}
28+
29+
30+
/**
31+
* Get The expected results.
32+
* @param $field
33+
* @param $geo_point
34+
* @param $distance
35+
* @return array
36+
*/
37+
protected function getExpected($field, $geo_point, $distance)
38+
{
39+
$query = $this->getQueryArray();
40+
41+
$query['body']['query']['bool']['filter'][] = [
42+
'geo_distance' => [
43+
$field => $geo_point,
44+
'distance' => $distance
45+
]
46+
];
47+
48+
return $query;
49+
}
50+
51+
52+
/**
53+
* Get The actual results.
54+
* @param $field
55+
* @param $geo_point
56+
* @param $distance
57+
* @return mixed
58+
*/
59+
protected function getActual($field, $geo_point, $distance)
60+
{
61+
return $this->getQueryObject()->distance($field, $geo_point, $distance)->query();
62+
}
63+
}

tests/IgnoreTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Basemkhirat\Elasticsearch\Tests;
4+
5+
use Basemkhirat\Elasticsearch\Tests\Traits\ESQueryTrait;
6+
7+
class IgnoreTest extends \PHPUnit_Framework_TestCase
8+
{
9+
10+
use ESQueryTrait;
11+
12+
/**
13+
* Test the ignore() method.
14+
* @return void
15+
*/
16+
public function testIgnoreMethod()
17+
{
18+
$this->assertEquals($this->getExpected(404), $this->getActual(404));
19+
$this->assertEquals($this->getExpected(500, 404), $this->getActual(500, 404));
20+
}
21+
22+
/**
23+
* Get The expected results.
24+
* @return array
25+
*/
26+
protected function getExpected()
27+
{
28+
$query = $this->getQueryArray();
29+
30+
$query["client"]["ignore"] = func_get_args();
31+
32+
return $query;
33+
}
34+
35+
/**
36+
* Get The actual results.
37+
* @return mixed
38+
*/
39+
protected function getActual()
40+
{
41+
return $this->getQueryObject()->ignore(func_get_args())->query();
42+
}
43+
}

tests/IndexTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Basemkhirat\Elasticsearch\Tests;
4+
5+
use Basemkhirat\Elasticsearch\Tests\Traits\ESQueryTrait;
6+
7+
class IndexTest extends \PHPUnit_Framework_TestCase
8+
{
9+
10+
use ESQueryTrait;
11+
12+
/**
13+
* Test the index() method.
14+
* @return void
15+
*/
16+
public function testIndexMethod()
17+
{
18+
$this->assertEquals($this->getExpected("index_1"), $this->getActual("index_1"));
19+
}
20+
21+
22+
/**
23+
* Get The expected results.
24+
* @param $index
25+
* @return array
26+
*/
27+
protected function getExpected($index)
28+
{
29+
$query = $this->getQueryArray();
30+
31+
$query["index"] = $index;
32+
33+
return $query;
34+
}
35+
36+
37+
/**
38+
* Get The actual results.
39+
* @param $index
40+
* @return mixed
41+
*/
42+
protected function getActual($index)
43+
{
44+
return $this->getQueryObject()->index($index)->query();
45+
}
46+
}

tests/OrderTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Basemkhirat\Elasticsearch\Tests;
4+
5+
use Basemkhirat\Elasticsearch\Tests\Traits\ESQueryTrait;
6+
7+
class OrderTest extends \PHPUnit_Framework_TestCase
8+
{
9+
10+
use ESQueryTrait;
11+
12+
/**
13+
* Test the orderBy() method.
14+
* @return void
15+
*/
16+
public function testOrderByMethod()
17+
{
18+
$this->assertEquals($this->getExpected("created_at", "asc"), $this->getActual("created_at", "asc"));
19+
$this->assertEquals($this->getExpected("_score"), $this->getActual("_score"));
20+
}
21+
22+
23+
/**
24+
* Get The expected results.
25+
* @param $field
26+
* @param $direction
27+
* @return array
28+
*/
29+
protected function getExpected($field, $direction = "desc")
30+
{
31+
$query = $this->getQueryArray();
32+
33+
$query["body"]["sort"][] = [$field => $direction];
34+
35+
return $query;
36+
}
37+
38+
39+
/**
40+
* Get The actual results.
41+
* @param $field
42+
* @param $direction
43+
* @return mixed
44+
*/
45+
protected function getActual($field, $direction = "desc")
46+
{
47+
return $this->getQueryObject()->orderBy($field, $direction)->query();
48+
}
49+
}

0 commit comments

Comments
 (0)