Completed
Push — master ( 0d45ed...ea3502 )
by Nicolas
02:38
created

tests/ScrollTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Elastica\Test;
4
5
use Elastica\Client;
6
use Elastica\Document;
7
use Elastica\ResultSet;
8
use Elastica\Scroll;
9
use Elastica\Search;
10
11
/**
12
 * @internal
13
 */
14
class ScrollTest extends Base
15
{
16
    /**
17
     * Full foreach test.
18
     *
19
     * @group functional
20
     */
21
    public function testForeach(): void
22
    {
23
        $search = $this->_prepareSearch();
24
        $scroll = new Scroll($search);
25
        $count = 1;
26
27
        $this->_assertOpenSearchContexts($search->getClient(), 0);
28
29
        /** @var ResultSet $resultSet */
30 View Code Duplication
        foreach ($scroll as $scrollId => $resultSet) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
            $this->assertNotEmpty($scrollId);
32
            $this->_assertOpenSearchContexts($search->getClient(), 1);
33
34
            $results = $resultSet->getResults();
35
            switch (true) {
36
                case 1 === $count:
37
                    // hits: 1 - 5
38
                    $this->assertEquals(5, $resultSet->count());
39
                    $this->assertEquals('1', $results[0]->getId());
40
                    $this->assertEquals('5', $results[4]->getId());
41
                    break;
42
                case 2 === $count:
43
                    // hits: 6 - 10
44
                    $this->assertEquals(5, $resultSet->count());
45
                    $this->assertEquals('6', $results[0]->getId());
46
                    $this->assertEquals('10', $results[4]->getId());
47
                    break;
48
                case 3 === $count:
49
                    // hit: 11
50
                    $this->assertEquals(1, $resultSet->count());
51
                    $this->assertEquals('11', $results[0]->getId());
52
                    break;
53
                default:
54
                    $this->fail('too many iterations');
55
            }
56
57
            ++$count;
58
        }
59
60
        $this->_assertOpenSearchContexts($search->getClient(), 0);
61
    }
62
63
    /**
64
     * Scroll must not overwrite options.
65
     *
66
     * @group functional
67
     */
68
    public function testSearchRevert(): void
69
    {
70
        $search = $this->_prepareSearch();
71
72
        $search->setOption(Search::OPTION_SCROLL, 'must');
73
        $search->setOption(Search::OPTION_SCROLL_ID, 'not');
74
        $old = $search->getOptions();
75
76
        $scroll = new Scroll($search);
77
78
        $scroll->rewind();
79
        $this->assertEquals($old, $search->getOptions());
80
81
        $scroll->next();
82
        $this->assertEquals($old, $search->getOptions());
83
    }
84
85
    /**
86
     * Empty scroll (no results) must not run foreach.
87
     *
88
     * @group functional
89
     */
90
    public function testEmptyScroll(): void
91
    {
92
        $search = $this->_prepareSearch(0);
93
        $scroll = new Scroll($search);
94
95
        foreach ($scroll as $scrollId => $resultSet) {
96
            $this->fail("Empty scroll shouldn't run foreach.");
97
        }
98
99
        $this->assertEquals(0, $scroll->current()->count());
100
        $this->assertFalse($scroll->valid());
101
102
        $this->_assertOpenSearchContexts($search->getClient(), 0);
103
    }
104
105
    /**
106
     * Test with ignore_unavailable search option.
107
     *
108
     * @group functional
109
     */
110
    public function testScrollWithIgnoreUnavailable(): void
111
    {
112
        $search = $this->_prepareSearch();
113
        $search->addIndex('unavailable_index');
114
        $search->setOption($search::OPTION_SEARCH_IGNORE_UNAVAILABLE, 'true');
115
        $scroll = new Scroll($search);
116
        $count = 1;
117
118
        $this->_assertOpenSearchContexts($search->getClient(), 0);
119
120
        /** @var ResultSet $resultSet */
121 View Code Duplication
        foreach ($scroll as $scrollId => $resultSet) {
122
            $this->assertNotEmpty($scrollId);
123
            $this->_assertOpenSearchContexts($search->getClient(), 1);
124
125
            $results = $resultSet->getResults();
126
            switch (true) {
127
                case 1 === $count:
128
                    // hits: 1 - 5
129
                    $this->assertEquals(5, $resultSet->count());
130
                    $this->assertEquals('1', $results[0]->getId());
131
                    $this->assertEquals('5', $results[4]->getId());
132
                    break;
133
                case 2 === $count:
134
                    // hits: 6 - 10
135
                    $this->assertEquals(5, $resultSet->count());
136
                    $this->assertEquals('6', $results[0]->getId());
137
                    $this->assertEquals('10', $results[4]->getId());
138
                    break;
139
                case 3 === $count:
140
                    // hit: 11
141
                    $this->assertEquals(1, $resultSet->count());
142
                    $this->assertEquals('11', $results[0]->getId());
143
                    break;
144
                default:
145
                    $this->fail('too many iterations');
146
            }
147
148
            ++$count;
149
        }
150
151
        $this->_assertOpenSearchContexts($search->getClient(), 0);
152
    }
153
154
    /**
155
     * index: 11 docs default
156
     * query size: 5.
157
     */
158
    private function _prepareSearch(int $indexSize = 11): Search
159
    {
160
        $index = $this->_createIndex();
161
        $index->refresh();
162
163
        if ($indexSize > 0) {
164
            $docs = [];
165
            for ($x = 1; $x <= $indexSize; ++$x) {
166
                $docs[] = new Document($x, ['id' => $x, 'key' => 'value']);
167
            }
168
            $index->addDocuments($docs);
169
            $index->refresh();
170
        }
171
172
        $search = new Search($this->_getClient());
173
        $search->addIndex($index);
174
        $search->getQuery()->setSize(5);
175
176
        return $search;
177
    }
178
179
    /**
180
     * Tests the number of open search contexts on ES.
181
     */
182
    private function _assertOpenSearchContexts(Client $client, int $count): void
183
    {
184
        $stats = $client->getStatus()->getData();
185
        $this->assertSame($count, $stats['_all']['total']['search']['open_contexts'], 'Open search contexts should match');
186
    }
187
}
188