Skip to content

Commit 46e92ac

Browse files
committed
Tests!
1 parent 05c3b6b commit 46e92ac

File tree

10 files changed

+493
-2
lines changed

10 files changed

+493
-2
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ matrix:
2626
include:
2727
- php: 7.3
2828
env: CODING_STANDARD=1 PHPSTAN=1
29+
allow_failures:
30+
- env: RUN_TESTS=1 ES_VERSION="7.0.0-linux-x86_64"
2931

3032
before_install:
3133
- travis_retry composer self-update
3234
- sudo apt-get update && sudo apt-get install oracle-java8-installer
3335
- java -version
3436
- sudo update-alternatives --set java /usr/lib/jvm/java-8-oracle/jre/bin/java
3537
- java -version
36-
- ./travis-elastic.sh
38+
- if [ "$ES_VERSION" != "" ]; then ./travis-elastic.sh; fi
3739

3840
install:
3941
- travis_retry composer update --no-interaction --no-suggest --no-progress --prefer-dist --prefer-stable

doc/02-query-objects.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,41 @@ Must and should collection of objects is also **LeafQueryInterface**, so you can
1414
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/Match.php)
1515
- [Sample usage](https://github.com/Spameri/ElasticQuery/blob/master/tests/SpameriTests/ElasticQuery/Query/Match.phpt#L8)
1616

17+
##### MatchPhrase Query
18+
- Class `\Spameri\ElasticQuery\Query\MatchPhrase`
19+
- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html)
20+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/MatchPhrase.php)
21+
- [Sample usage](https://github.com/Spameri/ElasticQuery/blob/master/tests/SpameriTests/ElasticQuery/Query/MatchPhrase.phpt#L8)
22+
1723
##### Fuzzy Query
1824
- Class `\Spameri\ElasticQuery\Query\Fuzzy`
1925
- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html)
2026
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/Fuzzy.php)
27+
- [Sample usage](https://github.com/Spameri/ElasticQuery/blob/master/tests/SpameriTests/ElasticQuery/Query/Fuzzy.phpt#L8)
2128

2229
##### Range Query
2330
- Class `\Spameri\ElasticQuery\Query\Range`
2431
- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html)
2532
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/Range.php)
33+
- [Sample usage](https://github.com/Spameri/ElasticQuery/blob/master/tests/SpameriTests/ElasticQuery/Query/Range.phpt#L8)
2634

2735
##### Term Query
2836
- Class `\Spameri\ElasticQuery\Query\Term`
2937
- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html)
3038
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/Term.php)
39+
- [Sample usage](https://github.com/Spameri/ElasticQuery/blob/master/tests/SpameriTests/ElasticQuery/Query/Term.phpt#L8)
3140

3241
##### Terms Query
3342
- Class `\Spameri\ElasticQuery\Query\Terms`
3443
- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html)
3544
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/Terms.php)
45+
- [Sample usage](https://github.com/Spameri/ElasticQuery/blob/master/tests/SpameriTests/ElasticQuery/Query/Terms.phpt#L8)
3646

3747
##### WildCard Query
3848
- Class `\Spameri\ElasticQuery\Query\WildCard`
3949
- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html)
4050
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/WildCard.php)
51+
- [Sample usage](https://github.com/Spameri/ElasticQuery/blob/master/tests/SpameriTests/ElasticQuery/Query/WildCard.phpt#L8)
4152

4253
##### QueryCollection Query
4354
- Class `\Spameri\ElasticQuery\Query\QueryCollection`

src/Query/Fuzzy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function toArray() : array
7373
'boost' => $this->boost,
7474
'fuzziness' => $this->fuzziness,
7575
'prefix_length' => $this->prefixLength,
76-
'max_expansion' => $this->maxExpansion,
76+
'max_expansions' => $this->maxExpansion,
7777
],
7878
],
7979
];

src/Query/MatchPhrase.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Query;
4+
5+
6+
/**
7+
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html
8+
*/
9+
class MatchPhrase implements LeafQueryInterface
10+
{
11+
12+
/**
13+
* @var string
14+
*/
15+
private $field;
16+
17+
/**
18+
* @var string
19+
*/
20+
private $query;
21+
22+
/**
23+
* @var float
24+
*/
25+
private $boost;
26+
27+
/**
28+
* @var null|string
29+
*/
30+
private $analyzer;
31+
32+
/**
33+
* @var int
34+
*/
35+
private $slop;
36+
37+
38+
public function __construct(
39+
string $field
40+
, $query
41+
, float $boost = 1.0
42+
, int $slop = 0
43+
, ?string $analyzer = NULL
44+
)
45+
{
46+
$this->field = $field;
47+
$this->query = $query;
48+
$this->boost = $boost;
49+
$this->analyzer = $analyzer;
50+
$this->slop = $slop;
51+
}
52+
53+
54+
public function key() : string
55+
{
56+
return 'match_phrase_' . $this->field . '_' . $this->query;
57+
}
58+
59+
60+
public function toArray() : array
61+
{
62+
$array = [
63+
'match_phrase' => [
64+
$this->field => [
65+
'query' => $this->query,
66+
'boost' => $this->boost,
67+
],
68+
],
69+
];
70+
71+
if ($this->analyzer) {
72+
$array['match_phrase'][$this->field]['analyzer'] = $this->analyzer;
73+
}
74+
75+
if ($this->slop) {
76+
$array['match_phrase'][$this->field]['slop'] = $this->slop;
77+
}
78+
79+
return $array;
80+
}
81+
82+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SpameriTests\ElasticQuery\Query;
4+
5+
require_once __DIR__ . '/../../bootstrap.php';
6+
7+
8+
class Fuzzy extends \Tester\TestCase
9+
{
10+
11+
public function testCreate() : void
12+
{
13+
$fuzzy = new \Spameri\ElasticQuery\Query\Fuzzy(
14+
'name',
15+
'Avengers',
16+
1.0,
17+
2,
18+
0,
19+
100
20+
);
21+
22+
$array = $fuzzy->toArray();
23+
24+
\Tester\Assert::true(isset($array['fuzzy']['name']['value']));
25+
\Tester\Assert::same('Avengers', $array['fuzzy']['name']['value']);
26+
\Tester\Assert::same(1.0, $array['fuzzy']['name']['boost']);
27+
\Tester\Assert::same(2, $array['fuzzy']['name']['fuzziness']);
28+
\Tester\Assert::same(0, $array['fuzzy']['name']['prefix_length']);
29+
\Tester\Assert::same(100, $array['fuzzy']['name']['max_expansions']);
30+
31+
$document = new \Spameri\ElasticQuery\Document(
32+
'spameri_video',
33+
new \Spameri\ElasticQuery\Document\Body\Plain(
34+
(
35+
new \Spameri\ElasticQuery\ElasticQuery(
36+
new \Spameri\ElasticQuery\Query\QueryCollection(
37+
new \Spameri\ElasticQuery\Query\MustCollection(
38+
$fuzzy
39+
)
40+
)
41+
)
42+
)->toArray()
43+
),
44+
'spameri_video'
45+
);
46+
47+
$ch = curl_init();
48+
curl_setopt($ch, CURLOPT_URL, 'localhost:9200/_search');
49+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
50+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
51+
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
52+
curl_setopt(
53+
$ch, CURLOPT_POSTFIELDS,
54+
\json_encode($document->toArray()['body'])
55+
);
56+
57+
\Tester\Assert::noError(static function () use ($ch) {
58+
$response = curl_exec($ch);
59+
$resultMapper = new \Spameri\ElasticQuery\Response\ResultMapper();
60+
/** @var \Spameri\ElasticQuery\Response\ResultSearch $result */
61+
$result = $resultMapper->map(\json_decode($response, TRUE));
62+
\Tester\Assert::type('int', $result->stats()->total());
63+
});
64+
65+
curl_close($ch);
66+
}
67+
68+
}
69+
70+
(new Fuzzy())->run();
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SpameriTests\ElasticQuery\Query;
4+
5+
require_once __DIR__ . '/../../bootstrap.php';
6+
7+
8+
class MatchPhrase extends \Tester\TestCase
9+
{
10+
11+
public function testCreate() : void
12+
{
13+
$match = new \Spameri\ElasticQuery\Query\MatchPhrase(
14+
'name',
15+
'Avengers',
16+
1.0,
17+
1,
18+
'standard'
19+
);
20+
21+
$array = $match->toArray();
22+
23+
\Tester\Assert::true(isset($array['match_phrase']['name']['query']));
24+
\Tester\Assert::same('Avengers', $array['match_phrase']['name']['query']);
25+
\Tester\Assert::same(1.0, $array['match_phrase']['name']['boost']);
26+
\Tester\Assert::same(1, $array['match_phrase']['name']['slop']);
27+
\Tester\Assert::same('standard', $array['match_phrase']['name']['analyzer']);
28+
29+
$document = new \Spameri\ElasticQuery\Document(
30+
'spameri_video',
31+
new \Spameri\ElasticQuery\Document\Body\Plain(
32+
(
33+
new \Spameri\ElasticQuery\ElasticQuery(
34+
new \Spameri\ElasticQuery\Query\QueryCollection(
35+
new \Spameri\ElasticQuery\Query\MustCollection(
36+
$match
37+
)
38+
)
39+
)
40+
)->toArray()
41+
),
42+
'spameri_video'
43+
);
44+
45+
$ch = curl_init();
46+
curl_setopt($ch, CURLOPT_URL, 'localhost:9200/_search');
47+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
48+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
49+
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
50+
curl_setopt(
51+
$ch, CURLOPT_POSTFIELDS,
52+
\json_encode($document->toArray()['body'])
53+
);
54+
55+
\Tester\Assert::noError(static function () use ($ch) {
56+
$response = curl_exec($ch);
57+
$resultMapper = new \Spameri\ElasticQuery\Response\ResultMapper();
58+
/** @var \Spameri\ElasticQuery\Response\ResultSearch $result */
59+
$result = $resultMapper->map(\json_decode($response, TRUE));
60+
\Tester\Assert::type('int', $result->stats()->total());
61+
});
62+
63+
curl_close($ch);
64+
}
65+
66+
}
67+
68+
(new MatchPhrase())->run();
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SpameriTests\ElasticQuery\Query;
4+
5+
require_once __DIR__ . '/../../bootstrap.php';
6+
7+
8+
class Range extends \Tester\TestCase
9+
{
10+
11+
public function testCreate() : void
12+
{
13+
$range = new \Spameri\ElasticQuery\Query\Range(
14+
'id',
15+
1,
16+
1000000,
17+
1.0
18+
);
19+
20+
$array = $range->toArray();
21+
22+
\Tester\Assert::true(isset($array['range']['id']));
23+
\Tester\Assert::same(1, $array['range']['id']['gte']);
24+
\Tester\Assert::same(1000000, $array['range']['id']['lte']);
25+
\Tester\Assert::same(1.0, $array['range']['id']['boost']);
26+
27+
$document = new \Spameri\ElasticQuery\Document(
28+
'spameri_video',
29+
new \Spameri\ElasticQuery\Document\Body\Plain(
30+
(
31+
new \Spameri\ElasticQuery\ElasticQuery(
32+
new \Spameri\ElasticQuery\Query\QueryCollection(
33+
new \Spameri\ElasticQuery\Query\MustCollection(
34+
$range
35+
)
36+
)
37+
)
38+
)->toArray()
39+
),
40+
'spameri_video'
41+
);
42+
43+
$ch = curl_init();
44+
curl_setopt($ch, CURLOPT_URL, 'localhost:9200/_search');
45+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
46+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
47+
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
48+
curl_setopt(
49+
$ch, CURLOPT_POSTFIELDS,
50+
\json_encode($document->toArray()['body'])
51+
);
52+
53+
\Tester\Assert::noError(static function () use ($ch) {
54+
$response = curl_exec($ch);
55+
$resultMapper = new \Spameri\ElasticQuery\Response\ResultMapper();
56+
/** @var \Spameri\ElasticQuery\Response\ResultSearch $result */
57+
$result = $resultMapper->map(\json_decode($response, TRUE));
58+
\Tester\Assert::type('int', $result->stats()->total());
59+
});
60+
61+
curl_close($ch);
62+
}
63+
64+
}
65+
66+
(new Range())->run();

0 commit comments

Comments
 (0)