summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/Filter/TermsTest.php
blob: bb37ba969fdfb6e33b17e754408cc3e5f6ed6821 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace Elastica\Test\Filter;

use Elastica\Document;
use Elastica\Filter\Terms;
use Elastica\Query;
use Elastica\Test\Base as BaseTest;

class TermsTest extends BaseTest
{
    /**
     * @group functional
     */
    public function testLookup()
    {
        $index = $this->_createIndex();
        $type1 = $index->getType('musicians');
        $type2 = $index->getType('bands');

        //index some test data
        $type1->addDocuments(array(
            new Document(1, array('name' => 'robert', 'lastName' => 'plant')),
            new Document(2, array('name' => 'jimmy', 'lastName' => 'page')),
            new Document(3, array('name' => 'john paul', 'lastName' => 'jones')),
            new Document(4, array('name' => 'john', 'lastName' => 'bonham')),
            new Document(5, array('name' => 'jimi', 'lastName' => 'hendrix')),
        ));

        $type2->addDocument(new Document('led zeppelin', array('members' => array('plant', 'page', 'jones', 'bonham'))));
        $index->refresh();

        //use the terms lookup feature to query for some data
        $termsFilter = new Terms();
        $termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', null);
        $query = new Query();
        $query->setPostFilter($termsFilter);
        $results = $index->search($query);
        $this->assertEquals($results->count(), 4, 'Terms lookup with null index');

        $termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', $index);
        $query->setPostFilter($termsFilter);
        $results = $index->search($query);
        $this->assertEquals($results->count(), 4, 'Terms lookup with index as object');

        //Query with index given as string
        $termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', $index->getName());
        $query->setPostFilter($termsFilter);
        $results = $index->search($query);
        $this->assertEquals($results->count(), 4, 'Terms lookup with index as string');

        //Query with array of options
        $termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', array('index' => $index, 'cache' => false));
        $query->setPostFilter($termsFilter);
        $results = $index->search($query);
        $this->assertEquals($results->count(), 4, 'Terms lookup with options array');

        $index->delete();
    }

    /**
     * @group unit
     */
    public function testSetExecution()
    {
        $filter = new Terms('color', array('blue', 'green'));

        $filter->setExecution('bool');
        $this->assertEquals('bool', $filter->getParam('execution'));

        $returnValue = $filter->setExecution('bool');
        $this->assertInstanceOf('Elastica\Filter\Terms', $returnValue);
    }

    /**
     * @group unit
     */
    public function testSetTerms()
    {
        $field = 'color';
        $terms = array('blue', 'green');

        $filter = new Terms();
        $filter->setTerms($field, $terms);
        $expected = array('terms' => array($field => $terms));
        $this->assertEquals($expected, $filter->toArray());

        $returnValue = $filter->setTerms($field, $terms);
        $this->assertInstanceOf('Elastica\Filter\Terms', $returnValue);
    }

    /**
     * @group unit
     */
    public function testAddTerm()
    {
        $filter = new Terms('color', array('blue'));

        $filter->addTerm('green');
        $expected = array('terms' => array('color' => array('blue', 'green')));
        $this->assertEquals($expected, $filter->toArray());

        $returnValue = $filter->addTerm('cyan');
        $this->assertInstanceOf('Elastica\Filter\Terms', $returnValue);
    }

    /**
     * @group unit
     */
    public function testToArray()
    {
        $filter = new Terms('color', array());
        $expected = array('terms' => array('color' => array()));
        $this->assertEquals($expected, $filter->toArray());

        $filter = new Terms('color', array('cyan'));
        $expected = array('terms' => array('color' => array('cyan')));
        $this->assertEquals($expected, $filter->toArray());
    }

    /**
     * @group unit
     * @expectedException \Elastica\Exception\InvalidException
     */
    public function testToArrayInvalidException()
    {
        $filter = new Terms();
        $filter->toArray();
    }
}