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

ScriptIdTest::testCreateInvalid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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

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
            'id' => self::SCRIPT_ID,
71
        ]];
72
        $this->assertEquals($expected, $script->toArray());
73
    }
74
75
    /**
76
     * @group unit
77
     */
78
    public function testCreateScript(): void
79
    {
80
        $data = new ScriptId(self::SCRIPT_ID);
81
82
        $script = ScriptId::create($data);
83
84
        $this->assertInstanceOf(ScriptId::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
                'id' => self::SCRIPT_ID,
100
                'lang' => ScriptId::LANG_PAINLESS,
101
                'params' => $params,
102
            ],
103
        ];
104
105
        $script = ScriptId::create($array);
106
107
        $this->assertInstanceOf(ScriptId::class, $script);
108
        $this->assertEquals($array, $script->toArray());
109
110
        $this->assertEquals(self::SCRIPT_ID, $script->getScriptId());
0 ignored issues
show
Bug introduced by
The method getScriptId does only exist in Elastica\Script\ScriptId, but not in Elastica\Script\Script.

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(ScriptId::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
        ScriptId::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 ScriptId(self::SCRIPT_ID, [], ScriptId::LANG_PAINLESS);
152
153
        $this->assertSame($script, $script->setLang(ScriptId::LANG_PAINLESS));
154
        $this->assertEquals(ScriptId::LANG_PAINLESS, $script->getLang());
155
    }
156
157
    /**
158
     * @group unit
159
     */
160
    public function testSetScriptId(): void
161
    {
162
        $script = new ScriptId(self::SCRIPT_ID);
163
164
        $this->assertSame($script, $script->setScriptId('other_script'));
165
        $this->assertEquals('other_script', $script->getScriptId());
166
    }
167
}
168