|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Elastica\Test; |
|
4
|
|
|
|
|
5
|
|
|
use Elastica\Aggregation\AbstractAggregation; |
|
6
|
|
|
use Elastica\Collapse; |
|
7
|
|
|
use Elastica\Exception\QueryBuilderException; |
|
8
|
|
|
use Elastica\Query\AbstractQuery; |
|
9
|
|
|
use Elastica\QueryBuilder; |
|
10
|
|
|
use Elastica\Suggest\AbstractSuggest; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @internal |
|
14
|
|
|
*/ |
|
15
|
|
|
class QueryBuilderTest extends Base |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @group unit |
|
19
|
|
|
*/ |
|
20
|
|
|
public function testCustomDSL(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$qb = new QueryBuilder(); |
|
23
|
|
|
|
|
24
|
|
|
// test custom DSL |
|
25
|
|
|
$qb->addDSL(new CustomDSL()); |
|
26
|
|
|
|
|
27
|
|
|
$this->assertTrue($qb->custom()->custom_method(), 'custom DSL execution failed'); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
// test custom DSL exception message |
|
30
|
|
|
$exceptionMessage = ''; |
|
31
|
|
|
try { |
|
32
|
|
|
$qb->invalid(); |
|
|
|
|
|
|
33
|
|
|
} catch (QueryBuilderException $exception) { |
|
34
|
|
|
$exceptionMessage = $exception->getMessage(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$this->assertEquals('DSL "invalid" not supported', $exceptionMessage); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @group unit |
|
42
|
|
|
*/ |
|
43
|
|
|
public function testFacade(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$qb = new QueryBuilder(); |
|
46
|
|
|
|
|
47
|
|
|
// test one example QueryBuilder flow for each default DSL type |
|
48
|
|
|
$this->assertInstanceOf(AbstractQuery::class, $qb->query()->match()); |
|
|
|
|
|
|
49
|
|
|
$this->assertInstanceOf(AbstractAggregation::class, $qb->aggregation()->avg('name')); |
|
|
|
|
|
|
50
|
|
|
$this->assertInstanceOf(AbstractSuggest::class, $qb->suggest()->term('name', 'field')); |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
// Collapse is a special case of the above; it doesn't have an abstract base class for individual parts right |
|
53
|
|
|
// now because 'inner_hits' is the only thing that can be set besides field and concurrency. |
|
54
|
|
|
$this->assertInstanceOf(Collapse\InnerHits::class, $qb->collapse()->inner_hits()); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
class CustomDSL implements QueryBuilder\DSL |
|
59
|
|
|
{ |
|
60
|
|
|
public function getType(): string |
|
61
|
|
|
{ |
|
62
|
|
|
return 'custom'; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function custom_method(): bool |
|
66
|
|
|
{ |
|
67
|
|
|
return true; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: