Completed
Pull Request — master (#1894)
by
unknown
10:26
created

WildcardTest::testCaseInsensitive()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 2
nc 2
nop 0
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
                    'case_insensitive' => false,
44
                ],
45
            ],
46
        ];
47
48
        $this->assertEquals($expectedArray, $wildcard->toArray());
49
    }
50
51
    /**
52
     * @group functional
53
     */
54
    public function testSearchWithAnalyzer(): void
55
    {
56
        $client = $this->_getClient();
57
        $index = $client->getIndex('test');
58
59
        $indexParams = [
60
            'settings' => [
61
                'analysis' => [
62
                    'analyzer' => [
63
                        'lw' => [
64
                            'type' => 'custom',
65
                            'tokenizer' => 'keyword',
66
                            'filter' => ['lowercase'],
67
                        ],
68
                    ],
69
                ],
70
            ],
71
        ];
72
73
        $index->create($indexParams, ['recreate' => true]);
74
75
        $mapping = new Mapping([
76
            'name' => ['type' => 'text', 'analyzer' => 'lw'],
77
        ]);
78
        $index->setMapping($mapping);
79
80
        $index->addDocuments([
81
            new Document(1, ['name' => 'Basel-Stadt']),
82
            new Document(2, ['name' => 'New York']),
83
            new Document(3, ['name' => 'Baden']),
84
            new Document(4, ['name' => 'Baden Baden']),
85
            new Document(5, ['name' => 'New Orleans']),
86
        ]);
87
88
        $index->refresh();
89
90
        $query = new Wildcard('name', 'ba*');
91
        $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...
92
93
        $this->assertEquals(3, $resultSet->count());
94
95
        $query = new Wildcard('name', 'baden*');
96
        $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...
97
98
        $this->assertEquals(2, $resultSet->count());
99
100
        $query = new Wildcard('name', 'baden b*');
101
        $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...
102
103
        $this->assertEquals(1, $resultSet->count());
104
105
        $query = new Wildcard('name', 'baden bas*');
106
        $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...
107
108
        $this->assertEquals(0, $resultSet->count());
109
    }
110
111
    /**
112
     * @group functional
113
     */
114
    public function testCaseInsensitive(): void
115
    {
116
        foreach ([true, false] as $expected) {
117
            $expectedArray = [
118
                'wildcard' => [
119
                    'name' => [
120
                        'value' => 'exampl*',
121
                        'boost' => 1.0,
122
                        'case_insensitive' => $expected,
123
                    ],
124
                ],
125
            ];
126
127
            $query = new Wildcard('name', 'exampl*', 1.0, true);
0 ignored issues
show
Unused Code introduced by
The call to Wildcard::__construct() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
128
            $this->assertEquals($expectedArray, $query->toArray());
129
130
            $query->setCaseInsensitive($expected);
131
            $this->assertEquals($expectedArray, $query->toArray());
132
        }
133
    }
134
}
135