summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/ScriptTest.php
blob: b42d8646b89a3485d828f680d8f39bc992bda3f5 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace Elastica\Test;

use Elastica\Script;
use Elastica\Test\Base as BaseTest;

class ScriptTest extends BaseTest
{
    /**
     * @group unit
     */
    public function testConstructor()
    {
        $value = "_score * doc['my_numeric_field'].value";
        $script = new Script($value);

        $expected = array(
            'script' => $value,
        );
        $this->assertEquals($value, $script->getScript());
        $this->assertEquals($expected, $script->toArray());

        $params = array(
            'param1' => 'one',
            'param2' => 10,
        );

        $script = new Script($value, $params);

        $expected = array(
            'script' => $value,
            'params' => $params,
        );

        $this->assertEquals($value, $script->getScript());
        $this->assertEquals($params, $script->getParams());
        $this->assertEquals($expected, $script->toArray());

        $lang = 'mvel';

        $script = new Script($value, $params, $lang);

        $expected = array(
            'script' => $value,
            'params' => $params,
            'lang' => $lang,
        );

        $this->assertEquals($value, $script->getScript());
        $this->assertEquals($params, $script->getParams());
        $this->assertEquals($lang, $script->getLang());
        $this->assertEquals($expected, $script->toArray());
    }

    /**
     * @group unit
     */
    public function testCreateString()
    {
        $string = '_score * 2.0';
        $script = Script::create($string);

        $this->assertInstanceOf('Elastica\Script', $script);

        $this->assertEquals($string, $script->getScript());

        $expected = array(
            'script' => $string,
        );
        $this->assertEquals($expected, $script->toArray());
    }

    /**
     * @group unit
     */
    public function testCreateScript()
    {
        $data = new Script('_score * 2.0');

        $script = Script::create($data);

        $this->assertInstanceOf('Elastica\Script', $script);
        $this->assertSame($data, $script);
    }

    /**
     * @group unit
     */
    public function testCreateArray()
    {
        $string = '_score * 2.0';
        $lang = 'mvel';
        $params = array(
            'param1' => 'one',
            'param2' => 1,
        );
        $array = array(
            'script' => $string,
            'lang' => $lang,
            'params' => $params,
        );

        $script = Script::create($array);

        $this->assertInstanceOf('Elastica\Script', $script);

        $this->assertEquals($string, $script->getScript());
        $this->assertEquals($params, $script->getParams());
        $this->assertEquals($lang, $script->getLang());

        $this->assertEquals($array, $script->toArray());
    }

    /**
     * @group unit
     * @dataProvider dataProviderCreateInvalid
     * @expectedException \Elastica\Exception\InvalidException
     */
    public function testCreateInvalid($data)
    {
        Script::create($data);
    }

    /**
     * @return array
     */
    public function dataProviderCreateInvalid()
    {
        return array(
            array(
                new \stdClass(),
            ),
            array(
                array('params' => array('param1' => 'one')),
            ),
            array(
                array('script' => '_score * 2.0', 'params' => 'param'),
            ),
        );
    }

    /**
     * @group unit
     */
    public function testSetLang()
    {
        $script = new Script('foo', array(), Script::LANG_GROOVY);
        $this->assertEquals(Script::LANG_GROOVY, $script->getLang());

        $script->setLang(Script::LANG_PYTHON);
        $this->assertEquals(Script::LANG_PYTHON, $script->getLang());

        $this->assertInstanceOf('Elastica\Script', $script->setLang(Script::LANG_PYTHON));
    }

    /**
     * @group unit
     */
    public function testSetScript()
    {
        $script = new Script('foo');
        $this->assertEquals('foo', $script->getScript());

        $script->setScript('bar');
        $this->assertEquals('bar', $script->getScript());

        $this->assertInstanceOf('Elastica\Script', $script->setScript('foo'));
    }
}