summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/PostFilterTest.php
blob: 16b7e07b7fd76daa38f2a812febd40d4d0ae66e6 (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
<?php

namespace Elastica\Test\Query;

use Elastica\Document;
use Elastica\Filter\Term;
use Elastica\Index;
use Elastica\Query\Match;
use Elastica\Query;
use Elastica\Test\Base as BaseTest;

class PostFilterTest extends BaseTest
{
    /**
     * @var Index
     */
    protected $_index;

    protected function setUp()
    {
        parent::setUp();
        $this->_index = $this->_createIndex("query");
        $docs = array(
            new Document("1", array("color" => "green", "make" => "ford")),
            new Document("2", array("color" => "blue", "make" => "volvo")),
            new Document("3", array("color" => "red", "make" => "ford")),
            new Document("4", array("color" => "green", "make" => "renault")),
        );
        $this->_index->getType("test")->addDocuments($docs);
        $this->_index->refresh();

    }

    protected function tearDown()
    {
        parent::tearDown();
        if ($this->_index instanceof Index) {
            $this->_index->delete();
        }
    }

    public function testToArray()
    {
        $query = new Query();

        $post_filter = new Term(array('color' => 'green'));
        $query->setPostFilter($post_filter->toArray());

        $data = $query->toArray();

        $this->assertArrayHasKey('post_filter', $data);
        $this->assertEquals(array('term' => array('color' => 'green')), $data['post_filter']);

        $query->setPostFilter(array());

        $this->assertArrayNotHasKey('post_filter', $query->toArray());
    }

    public function testQuery()
    {
        $query = new Query();

        $match = new Match();
        $match->setField('make', 'ford');

        $query->setQuery($match);

        $filter = new Term();
        $filter->setTerm('color', 'green');

        $query->setPostFilter($filter->toArray());

        $results = $this->_index->search($query);

        $this->assertEquals(1, $results->getTotalHits());

    }

    protected function _createIndex($name = 'test', $delete = true, $shards = 1)
    {
        return parent::_createIndex('test_postfilter_' . $name, $delete, $shards);
    }
}