Completed
Pull Request — master (#1894)
by
unknown
11:00 queued 02:11
created

WildcardTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 124
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstruct() 0 10 1
B testSearchWithAnalyzer() 0 56 1
A testToArray() 0 17 1
A testCaseInsensitive() 0 23 2
1
<?php
2
3
namespace Elastica\Test\Query;
4
5
use Elastica\Document;
6
use Elastica\Mapping;
7
use Elastica\Query\Wildcard;
8
use Elastica\Test\Base as BaseTest;
9
10
/**
11
 * @internal
12
 */
13
class WildcardTest extends BaseTest
14
{
15
    /**
16
     * @group unit
17
     */
18
    public function testConstruct(): void
19
    {
20
        $wildcard = new Wildcard('name', 'aaa*');
21
22
        $data = $wildcard->getParam('name');
23
        $this->assertIsArray($data);
24
25
        $this->assertSame('aaa*', $data['value']);
26
        $this->assertSame(1.0, $data['boost']);
27
    }
28
29
    /**
30
     * @group unit
31
     */
32
    public function testToArray(): void
33
    {
34
        $wildcard = new Wildcard('name', 'value*', 2.0);
35
        $wildcard->setRewrite(Wildcard::REWRITE_SCORING_BOOLEAN);
36
37
        $expectedArray = [
38
            'wildcard' => [
39
                'name' => [
40
                    'value' => 'value*',
41
                    'boost' => 2.0,
42
                    'rewrite' => 'scoring_boolean',
43
                ],
44
            ],
45
        ];
46
47
        $this->assertEquals($expectedArray, $wildcard->toArray());
48
    }
49
50
    /**
51
     * @group functional
52
     */
53
    public function testSearchWithAnalyzer(): void
54
    {
55
        $client = $this->_getClient();
56
        $index = $client->getIndex('test');
57
58
        $indexParams = [
59
            'settings' => [
60
                'analysis' => [
61
                    'analyzer' => [
62
                        'lw' => [
63
                            'type' => 'custom',
64
                            'tokenizer' => 'keyword',
65
                            'filter' => ['lowercase'],
66
                        ],
67
                    ],
68
                ],
69
            ],
70
        ];
71
72
        $index->create($indexParams, ['recreate' => true]);
73
74
        $mapping = new Mapping([
75
            'name' => ['type' => 'text', 'analyzer' => 'lw'],
76
        ]);
77
        $index->setMapping($mapping);
78
79
        $index->addDocuments([
80
            new Document(1, ['name' => 'Basel-Stadt']),
81
            new Document(2, ['name' => 'New York']),
82
            new Document(3, ['name' => 'Baden']),
83
            new Document(4, ['name' => 'Baden Baden']),
84
            new Document(5, ['name' => 'New Orleans']),
85
        ]);
86
87
        $index->refresh();
88
89
        $query = new Wildcard('name', 'ba*');
90
        $resultSet = $index->search($query);
0 ignored issues
show
Documentation introduced by
$query is of type object<Elastica\Query\Wildcard>, but the function expects a string|array|object<Elastica\Query>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91
92
        $this->assertEquals(3, $resultSet->count());
93
94
        $query = new Wildcard('name', 'baden*');
95
        $resultSet = $index->search($query);
0 ignored issues
show
Documentation introduced by
$query is of type object<Elastica\Query\Wildcard>, but the function expects a string|array|object<Elastica\Query>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
97
        $this->assertEquals(2, $resultSet->count());
98
99
        $query = new Wildcard('name', 'baden b*');
100
        $resultSet = $index->search($query);
0 ignored issues
show
Documentation introduced by
$query is of type object<Elastica\Query\Wildcard>, but the function expects a string|array|object<Elastica\Query>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101
102
        $this->assertEquals(1, $resultSet->count());
103
104
        $query = new Wildcard('name', 'baden bas*');
105
        $resultSet = $index->search($query);
0 ignored issues
show
Documentation introduced by
$query is of type object<Elastica\Query\Wildcard>, but the function expects a string|array|object<Elastica\Query>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
107
        $this->assertEquals(0, $resultSet->count());
108
    }
109
110
    /**
111
     * @group functional
112
     */
113
    public function testCaseInsensitive(): void
114
    {
115
        // feature doesn't exist on version prior 7.10;
116
        $this->_checkVersion('7.10');
117
118
        foreach ([true, false] as $expected) {
119
            $expectedArray = [
120
                'wildcard' => [
121
                    'name' => [
122
                        'value' => 'exampl*',
123
                        'boost' => 1.0,
124
                        'case_insensitive' => $expected,
125
                    ],
126
                ],
127
            ];
128
129
            $query = new Wildcard('name', 'exampl*', 1.0);
130
            $this->assertEquals($expectedArray, $query->toArray());
131
132
            $query->setCaseInsensitive($expected);
133
            $this->assertEquals($expectedArray, $query->toArray());
134
        }
135
    }
136
}
137