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

namespace Elastica\Test;

use Elastica\Document;
use Elastica\Query;
use Elastica\Script;
use Elastica\ScriptFields;
use Elastica\Test\Base as BaseTest;

class ScriptFieldsTest extends BaseTest
{
    protected $index;

    public function setUp()
    {
        $this->index = $this->_createIndex();
    }

    public function tearDown()
    {
        $this->index->delete();
    }

    public function testNewScriptFields()
    {
        $script = new Script('1 + 2');

        // addScript
        $scriptFields = new ScriptFields;
        $scriptFields->addScript('test', $script);
        $this->assertEquals($scriptFields->getParam('test'), $script->toArray());

        // setScripts
        $scriptFields = new ScriptFields;
        $scriptFields->setScripts(array(
            'test' => $script
        ));
        $this->assertEquals($scriptFields->getParam('test'), $script->toArray());

        // Constructor
        $scriptFields = new ScriptFields(array(
            'test' => $script
        ));
        $this->assertEquals($scriptFields->getParam('test'), $script->toArray());
    }

    public function testSetScriptFields()
    {
        $query = new Query;
        $script = new Script('1 + 2');

        $scriptFields = new ScriptFields(array(
            'test' => $script
        ));
        $query->setScriptFields($scriptFields);
        $this->assertEquals($query->getParam('script_fields'), $scriptFields->toArray());

        $query->setScriptFields(array(
            'test' => $script
        ));
        $this->assertEquals($query->getParam('script_fields'), $scriptFields->toArray());
    }

    /**
     * @expectedException \Elastica\Exception\InvalidException
     */
    public function testNameException()
    {
        $script = new Script('1 + 2');
        $scriptFields = new ScriptFields(array($script));
    }

    public function testQuery()
    {
        $type = $this->index->getType('test');

        $doc = new Document(1, array('firstname' => 'guschti', 'lastname' => 'ruflin'));
        $type->addDocument($doc);
        $this->index->refresh();

        $query = new Query();
        $script = new Script('1 + 2');
        $scriptFields = new ScriptFields(array(
            'test' => $script
        ));
        $query->setScriptFields($scriptFields);

        $resultSet = $type->search($query);
        $first = $resultSet->current()->getData();

        // 1 + 2
        $this->assertEquals(3, $first['test'][0]);
    }
}