summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/ScanAndScrollTest.php
blob: 9f06f9e61bf66e10e63ec386c47123615a3a2c61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace Elastica\Test;

use Elastica\Document;
use Elastica\Query;
use Elastica\ResultSet;
use Elastica\ScanAndScroll;
use Elastica\Search;
use Elastica\Test\Base as BaseTest;

class ScanAndScrollTest extends BaseTest
{
    /**
     * Full foreach test.
     *
     * @gropu functional
     */
    public function testForeach()
    {
        $scanAndScroll = new ScanAndScroll($this->_prepareSearch(), '1m', 2);
        $docCount = 0;

        /** @var ResultSet $resultSet */
        foreach ($scanAndScroll as $scrollId => $resultSet) {
            $docCount += $resultSet->count();
        }

        /*
         * number of loops and documents per iteration may fluctuate
         * => only test end results
         */
        $this->assertEquals(12, $docCount);
    }

    /**
     * query size revert options.
     *
     * @group functional
     */
    public function testQuerySizeRevert()
    {
        $search = $this->_prepareSearch();
        $search->getQuery()->setSize(9);

        $scanAndScroll = new ScanAndScroll($search);

        $scanAndScroll->rewind();
        $this->assertEquals(9, $search->getQuery()->getParam('size'));

        $scanAndScroll->next();
        $this->assertEquals(9, $search->getQuery()->getParam('size'));
    }

    /**
     * index: 12 docs, 2 shards.
     *
     * @return Search
     */
    private function _prepareSearch()
    {
        $index = $this->_createIndex('', true, 2);
        $index->refresh();

        $docs = array();
        for ($x = 1; $x <= 12; $x++) {
            $docs[] = new Document($x, array('id' => $x, 'key' => 'value'));
        }

        $type = $index->getType('scanAndScrollTest');
        $type->addDocuments($docs);
        $index->refresh();

        $search = new Search($this->_getClient());
        $search->addIndex($index)->addType($type);

        return $search;
    }
}