Completed
Pull Request — master (#1851)
by
unknown
02:27
created

ParentIdTest::testParentId()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 87

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 87
rs 8.2836
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Elastica\Test\Query;
4
5
use Elastica\Mapping;
6
use Elastica\Query\ParentId;
7
use Elastica\QueryBuilder\DSL\Query;
8
use Elastica\Search;
9
use Elastica\Test\Base as BaseTest;
10
11
/**
12
 * @internal
13
 */
14
class ParentIdTest extends BaseTest
15
{
16
    /**
17
     * @group unit
18
     */
19
    public function testToArray(): void
20
    {
21
        $query = new ParentId('join', '1');
22
        $expectedArray = [
23
            'parent_id' => [
24
                'type' => 'join',
25
                'id' => 1,
26
                'ignore_unmapped' => false,
27
            ],
28
        ];
29
30
        $this->assertEquals($expectedArray, $query->toArray());
31
    }
32
33
    /**
34
     * @group functional
35
     */
36
    public function testParentId(): void
37
    {
38
        $client = $this->_getClient();
39
        $index = $client->getIndex('testparentid');
40
        $index->create([], true);
41
42
        $mapping = new Mapping([
43
            'firstname' => ['type' => 'text', 'store' => true],
44
            'lastname' => ['type' => 'text'],
45
            'my_join_field' => [
46
                'type' => 'join',
47
                'relations' => [
48
                    'question' => 'answer',
49
                ],
50
            ],
51
        ]);
52
53
        $index->setMapping($mapping);
54
55
        $expected = [
56
            'properties' => [
57
                'firstname' => ['type' => 'text', 'store' => true],
58
                'lastname' => ['type' => 'text'],
59
                'my_join_field' => [
60
                    'type' => 'join',
61
                    'relations' => [
62
                        'question' => 'answer',
63
                    ],
64
                ],
65
            ],
66
        ];
67
68
        $this->assertEquals($expected, $mapping->toArray());
69
        $index->refresh();
70
71
        $doc1 = $index->createDocument('1', [
72
            'text' => 'this is the 1st question',
73
            'my_join_field' => [
74
                'name' => 'question',
75
            ],
76
        ]);
77
        $doc2 = $index->createDocument('2', [
78
            'text' => 'this is the 2nd question',
79
            'my_join_field' => [
80
                'name' => 'question',
81
            ],
82
        ]);
83
        $index->addDocuments([$doc1, $doc2]);
84
85
        $doc3 = $index->createDocument('3', [
86
            'text' => 'this is an answer, the 1st',
87
            'my_join_field' => [
88
                'name' => 'answer',
89
                'parent' => 1,
90
            ],
91
        ]);
92
        $doc4 = $index->createDocument('4', [
93
            'text' => 'this is an answer, the 2nd',
94
            'my_join_field' => [
95
                'name' => 'answer',
96
                'parent' => '2',
97
            ],
98
        ]);
99
        $doc5 = $index->createDocument('5', [
100
            'text' => 'this is an answer, the 3rd',
101
            'my_join_field' => [
102
                'name' => 'answer',
103
                'parent' => '2',
104
            ],
105
        ]);
106
        $this->_getClient()->addDocuments([$doc3, $doc4, $doc5], ['routing' => 1]);
107
        $index->refresh();
108
109
        $parentQuery = new ParentId('answer', 1, true);
110
        $search = new Search($index->getClient());
111
        $results = $search->search($parentQuery);
0 ignored issues
show
Documentation introduced by
$parentQuery is of type object<Elastica\Query\ParentId>, 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...
112
        $this->assertEquals(1, $results->count());
113
114
        $result = $results->current();
115
        $data = $result->getData();
116
        $this->assertEquals($data['text'], 'this is an answer, the 1st');
117
118
        $parentQuery = new ParentId('answer', '2', true);
119
        $search = new Search($index->getClient());
120
        $results = $search->search($parentQuery);
0 ignored issues
show
Documentation introduced by
$parentQuery is of type object<Elastica\Query\ParentId>, 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...
121
        $this->assertEquals(2, $results->count());
122
    }
123
124
    /**
125
     * @group unit
126
     */
127
    public function testQueryBuilderParentId(): void
128
    {
129
        $client = $this->_getClient();
130
        $index = $client->getIndex('testparentid');
131
        $index->create([], true);
132
133
        $mapping = new Mapping([
134
            'firstname' => ['type' => 'text', 'store' => true],
135
            'lastname' => ['type' => 'text'],
136
            'my_join_field' => [
137
                'type' => 'join',
138
                'relations' => [
139
                    'question' => 'answer',
140
                ],
141
            ],
142
        ]);
143
144
        $index->setMapping($mapping);
145
        $index->refresh();
146
147
        $doc1 = $index->createDocument('1', [
148
            'text' => 'this is the 1st question',
149
            'my_join_field' => [
150
                'name' => 'question',
151
            ],
152
        ]);
153
154
        $doc2 = $index->createDocument('2', [
155
            'text' => 'this is the 2nd question',
156
            'my_join_field' => [
157
                'name' => 'question',
158
            ],
159
        ]);
160
        $index->addDocuments([$doc1, $doc2]);
161
162
        $doc3 = $index->createDocument('3', [
163
            'text' => 'this is an answer, the 1st',
164
            'my_join_field' => [
165
                'name' => 'answer',
166
                'parent' => 1,
167
            ],
168
        ]);
169
        $doc4 = $index->createDocument('4', [
170
            'text' => 'this is an answer, the 2nd',
171
            'my_join_field' => [
172
                'name' => 'answer',
173
                'parent' => 2,
174
            ],
175
        ]);
176
        $doc5 = $index->createDocument('5', [
177
            'text' => 'this is an answer, the 3rd',
178
            'my_join_field' => [
179
                'name' => 'answer',
180
                'parent' => 2,
181
            ],
182
        ]);
183
        $this->_getClient()->addDocuments([$doc3, $doc4, $doc5], ['routing' => 1]);
184
        $index->refresh();
185
186
        $queryDSL = new Query();
187
        $parentId = $queryDSL->parent_id('answer', 1, true);
188
        $search = new Search($index->getClient());
189
        $results = $search->search($parentId);
0 ignored issues
show
Documentation introduced by
$parentId is of type object<Elastica\Query\ParentId>, 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...
190
191
        $this->assertEquals(1, $results->count());
192
193
        $result = $results->current();
194
        $data = $result->getData();
195
        $this->assertEquals($data['text'], 'this is an answer, the 1st');
196
197
        $parentId = $queryDSL->parent_id('answer', 2, true);
198
        $search = new Search($index->getClient());
199
        $results = $search->search($parentId);
0 ignored issues
show
Documentation introduced by
$parentId is of type object<Elastica\Query\ParentId>, 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...
200
201
        $this->assertEquals(2, $results->count());
202
    }
203
}
204