Completed
Pull Request — master (#1851)
by
unknown
03:09
created

ScriptTest::testSetLang()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Elastica\Test\Script;
4
5
use Elastica\Script\Script;
6
use Elastica\Test\Base as BaseTest;
7
8
/**
9
 * @internal
10
 */
11
class ScriptTest extends BaseTest
12
{
13
    private const SCRIPT = "_score * doc['my_numeric_field'].value";
14
15
    /**
16
     * @group unit
17
     */
18
    public function testConstructor(): void
19
    {
20
        $script = new Script(self::SCRIPT);
21
22
        $expected = ['script' => [
23
            'source' => self::SCRIPT,
24
        ]];
25
        $this->assertEquals(self::SCRIPT, $script->getScript());
26
        $this->assertEquals($expected, $script->toArray());
27
28
        $params = [
29
            'param1' => 'one',
30
            'param2' => 10,
31
        ];
32
33
        $script = new Script(self::SCRIPT, $params);
34
35
        $expected = ['script' => [
36
            'source' => self::SCRIPT,
37
            'params' => $params,
38
        ]];
39
40
        $this->assertEquals(self::SCRIPT, $script->getScript());
41
        $this->assertEquals($params, $script->getParams());
42
        $this->assertEquals($expected, $script->toArray());
43
44
        $script = new Script(self::SCRIPT, $params, Script::LANG_PAINLESS);
45
46
        $expected = ['script' => [
47
            'source' => self::SCRIPT,
48
            'params' => $params,
49
            'lang' => Script::LANG_PAINLESS,
50
        ]];
51
52
        $this->assertEquals($expected, $script->toArray());
53
        $this->assertEquals(self::SCRIPT, $script->getScript());
54
        $this->assertEquals($params, $script->getParams());
55
        $this->assertEquals(Script::LANG_PAINLESS, $script->getLang());
56
    }
57
58
    /**
59
     * @group unit
60
     */
61
    public function testCreateString(): void
62
    {
63
        $script = Script::create(self::SCRIPT);
64
65
        $this->assertInstanceOf(Script::class, $script);
66
67
        $this->assertEquals(self::SCRIPT, $script->getScript());
0 ignored issues
show
Bug introduced by
The method getScript does only exist in Elastica\Script\Script, but not in Elastica\Script\ScriptId.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
68
69
        $expected = ['script' => [
70
            'source' => self::SCRIPT,
71
        ]];
72
        $this->assertEquals($expected, $script->toArray());
73
    }
74
75
    /**
76
     * @group unit
77
     */
78
    public function testCreateScript(): void
79
    {
80
        $data = new Script(self::SCRIPT);
81
82
        $script = Script::create($data);
83
84
        $this->assertInstanceOf(Script::class, $script);
85
        $this->assertSame($data, $script);
86
    }
87
88
    /**
89
     * @group unit
90
     */
91
    public function testCreateArray(): void
92
    {
93
        $params = [
94
            'param1' => 'one',
95
            'param2' => 1,
96
        ];
97
        $array = [
98
            'script' => [
99
                'source' => self::SCRIPT,
100
                'lang' => Script::LANG_PAINLESS,
101
                'params' => $params,
102
            ],
103
        ];
104
105
        $script = Script::create($array);
106
107
        $this->assertInstanceOf(Script::class, $script);
108
        $this->assertEquals($array, $script->toArray());
109
110
        $this->assertEquals(self::SCRIPT, $script->getScript());
0 ignored issues
show
Bug introduced by
The method getScript does only exist in Elastica\Script\Script, but not in Elastica\Script\ScriptId.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
111
        $this->assertEquals($params, $script->getParams());
112
        $this->assertEquals(Script::LANG_PAINLESS, $script->getLang());
113
    }
114
115
    /**
116
     * @group unit
117
     * @dataProvider dataProviderCreateInvalid
118
     *
119
     * @param mixed $data
120
     */
121
    public function testCreateInvalid($data): void
122
    {
123
        $this->expectException(\Elastica\Exception\InvalidException::class);
124
125
        Script::create($data);
126
    }
127
128
    /**
129
     * @return array
130
     */
131
    public function dataProviderCreateInvalid()
132
    {
133
        return [
134
            [
135
                new \stdClass(),
136
            ],
137
            [
138
                ['params' => ['param1' => 'one']],
139
            ],
140
            [
141
                ['script' => '_score * 2.0', 'params' => 'param'],
142
            ],
143
        ];
144
    }
145
146
    /**
147
     * @group unit
148
     */
149
    public function testSetLang(): void
150
    {
151
        $script = new Script(self::SCRIPT, [], Script::LANG_PAINLESS);
152
153
        $this->assertSame($script, $script->setLang(Script::LANG_PAINLESS));
154
        $this->assertEquals(Script::LANG_PAINLESS, $script->getLang());
155
    }
156
157
    /**
158
     * @group unit
159
     */
160
    public function testSetScript(): void
161
    {
162
        $script = new Script(self::SCRIPT);
163
164
        $this->assertSame($script, $script->setScript('bar'));
165
        $this->assertEquals('bar', $script->getScript());
166
    }
167
}
168