Completed
Push — master ( 9146c8...39ecb4 )
by Nicolas
04:52
created

QueryBuilderTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 4
dl 0
loc 42
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testCustomDSL() 0 19 2
A testFacade() 0 13 1
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');
0 ignored issues
show
Documentation Bug introduced by
The method custom does not exist on object<Elastica\QueryBuilder>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and 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 __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
28
29
        // test custom DSL exception message
30
        $exceptionMessage = '';
31
        try {
32
            $qb->invalid();
0 ignored issues
show
Documentation Bug introduced by
The method invalid does not exist on object<Elastica\QueryBuilder>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and 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 __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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());
0 ignored issues
show
Documentation Bug introduced by
The method match does not exist on object<Elastica\QueryBuilder\Facade>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and 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 __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
49
        $this->assertInstanceOf(AbstractAggregation::class, $qb->aggregation()->avg('name'));
0 ignored issues
show
Documentation Bug introduced by
The method avg does not exist on object<Elastica\QueryBuilder\Facade>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and 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 __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
50
        $this->assertInstanceOf(AbstractSuggest::class, $qb->suggest()->term('name', 'field'));
0 ignored issues
show
Documentation Bug introduced by
The method term does not exist on object<Elastica\QueryBuilder\Facade>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and 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 __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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());
0 ignored issues
show
Documentation Bug introduced by
The method inner_hits does not exist on object<Elastica\QueryBuilder\Facade>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and 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 __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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