ExecuteTest   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Importance

Changes 10
Bugs 1 Features 1
Metric Value
wmc 14
eloc 67
c 10
b 1
f 1
dl 0
loc 175
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testExecuteInsertsRows() 0 13 1
A testExecuteQueryBindsMixedParametersAndSubstitutesPositionalOnes() 0 6 1
A testExecuteQueryFetchesNoRows() 0 6 1
A testExecuteQueryBindsMixedParameters() 0 6 1
A testExecuteQueryBindsPositionalParameters() 0 6 1
A testExecuteQueryBindsNamedParameters() 0 6 1
A testExecuteFetchesAllRows() 0 5 1
A testExecuteQueryFetchesAllRows() 0 6 1
A testExecuteUpdateUpdatesRow() 0 6 1
A testExecuteUpdateInsertsRowsWithAutoIncrementedIds() 0 6 1
A testExecuteUpdateInsertsRows() 0 9 1
A testExecuteQueryFetchesOneRow() 0 6 1
A testSqlQueryResultHoldsExtendedMetadata() 0 21 1
A testSqlQueryResultHoldsMetadata() 0 14 1
1
<?php
2
3
/**
4
 * This file is part of the tarantool/client package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Tarantool\Client\Tests\Integration\Requests;
15
16
use Tarantool\Client\Keys;
17
use Tarantool\Client\Tests\Integration\ClientBuilder;
18
use Tarantool\Client\Tests\Integration\TestCase;
19
20
/**
21
 * @requires Tarantool >=2
22
 *
23
 * @sql DROP TABLE IF EXISTS exec_query
24
 * @sql CREATE TABLE exec_query (id INTEGER PRIMARY KEY, name VARCHAR(50))
25
 * @sql INSERT INTO exec_query VALUES (1, 'A'), (2, 'B')
26
 */
27
final class ExecuteTest extends TestCase
28
{
29
    /**
30
     * @sql DROP TABLE IF EXISTS exec_update
31
     * @sql CREATE TABLE exec_update (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(50))
32
     */
33
    public function testExecuteInsertsRows() : void
34
    {
35
        $response = $this->client->execute(
0 ignored issues
show
Bug introduced by
The method execute() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        /** @scrutinizer ignore-call */ 
36
        $response = $this->client->execute(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
            'INSERT INTO exec_update VALUES (5, :name1), (null, :name2)',
37
            [':name1' => 'A'], [':name2' => 'B']
38
        );
39
40
        $expectedSqlInfo = [
41
            Keys::SQL_INFO_ROW_COUNT => 2,
42
            Keys::SQL_INFO_AUTO_INCREMENT_IDS => [6],
43
        ];
44
45
        self::assertSame($expectedSqlInfo, $response->getBodyField(Keys::SQL_INFO));
46
    }
47
48
    public function testExecuteFetchesAllRows() : void
49
    {
50
        $response = $this->client->execute('SELECT * FROM exec_query WHERE id > 0');
51
52
        self::assertSame([[1, 'A'], [2, 'B']], $response->getBodyField(Keys::DATA));
53
    }
54
55
    /**
56
     * @sql DROP TABLE IF EXISTS exec_update
57
     * @sql CREATE TABLE exec_update (id INTEGER PRIMARY KEY, name VARCHAR(50))
58
     */
59
    public function testExecuteUpdateInsertsRows() : void
60
    {
61
        $result = $this->client->executeUpdate(
62
            'INSERT INTO exec_update VALUES (1, :name1), (2, :name2)',
63
            [':name1' => 'A'], [':name2' => 'B']
64
        );
65
66
        self::assertSame([], $result->getAutoincrementIds());
67
        self::assertSame(2, $result->count());
68
    }
69
70
    /**
71
     * @sql DROP TABLE IF EXISTS exec_update
72
     * @sql CREATE TABLE exec_update (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(50))
73
     */
74
    public function testExecuteUpdateInsertsRowsWithAutoIncrementedIds() : void
75
    {
76
        $result = $this->client->executeUpdate("INSERT INTO exec_update VALUES (100, 'A'), (null, 'B'), (120, 'C'), (null, 'D')");
77
78
        self::assertSame([101, 121], $result->getAutoincrementIds());
79
        self::assertSame(4, $result->count());
80
    }
81
82
    /**
83
     * @sql DROP TABLE IF EXISTS exec_update
84
     * @sql CREATE TABLE exec_update (id INTEGER PRIMARY KEY, name VARCHAR(50))
85
     * @sql INSERT INTO exec_update VALUES (1, 'A'), (2, 'B')
86
     */
87
    public function testExecuteUpdateUpdatesRow() : void
88
    {
89
        $result = $this->client->executeUpdate('UPDATE exec_update SET name = ? WHERE id = ?', 'BB', 2);
90
91
        self::assertSame([], $result->getAutoincrementIds());
92
        self::assertSame(1, $result->count());
93
    }
94
95
    public function testExecuteQueryFetchesAllRows() : void
96
    {
97
        $result = $this->client->executeQuery('SELECT * FROM exec_query WHERE id > 0');
98
99
        self::assertSame([[1, 'A'], [2, 'B']], $result->getData());
100
        self::assertSame(2, $result->count());
101
    }
102
103
    public function testExecuteQueryFetchesOneRow() : void
104
    {
105
        $result = $this->client->executeQuery('SELECT * FROM exec_query WHERE id = 1');
106
107
        self::assertSame([[1, 'A']], $result->getData());
108
        self::assertSame(1, $result->count());
109
    }
110
111
    public function testExecuteQueryFetchesNoRows() : void
112
    {
113
        $result = $this->client->executeQuery('SELECT * FROM exec_query WHERE id = -1');
114
115
        self::assertSame([], $result->getData());
116
        self::assertSame(0, $result->count());
117
    }
118
119
    public function testExecuteQueryBindsPositionalParameters() : void
120
    {
121
        $result = $this->client->executeQuery('SELECT ?, ?', 2, 'B');
122
123
        self::assertSame([[2, 'B']], $result->getData());
124
        self::assertSame(1, $result->count());
125
    }
126
127
    public function testExecuteQueryBindsNamedParameters() : void
128
    {
129
        $result = $this->client->executeQuery('SELECT :id, :name', [':name' => 'B'], [':id' => 2]);
130
131
        self::assertSame([[2, 'B']], $result->getData());
132
        self::assertSame(1, $result->count());
133
    }
134
135
    public function testExecuteQueryBindsMixedParameters() : void
136
    {
137
        $result = $this->client->executeQuery('SELECT ?, :name', 2, [':name' => 'B']);
138
139
        self::assertSame([[2, 'B']], $result->getData());
140
        self::assertSame(1, $result->count());
141
    }
142
143
    /**
144
     * @see https://github.com/tarantool/tarantool/issues/4782
145
     */
146
    public function testExecuteQueryBindsMixedParametersAndSubstitutesPositionalOnes() : void
147
    {
148
        $result = $this->client->executeQuery('SELECT :id, ?', 'B', [':id' => 2]);
149
150
        self::assertSame([[2, null]], $result->getData());
151
        self::assertSame(1, $result->count());
152
    }
153
154
    /**
155
     * @sql DROP TABLE IF EXISTS %target_method%
156
     * @sql CREATE TABLE %target_method% (ID INTEGER PRIMARY KEY, NAME VARCHAR(50))
157
     * @sql INSERT INTO %target_method% VALUES (1, 'A'), (2, 'B')
158
     */
159
    public function testSqlQueryResultHoldsMetadata() : void
160
    {
161
        $client = ClientBuilder::createFromEnv()->build();
162
163
        $tableName = $this->resolvePlaceholders('%target_method%');
164
        $response = $client->executeQuery("SELECT * FROM $tableName WHERE ID > 0");
165
166
        self::assertSame([[
167
            Keys::METADATA_FIELD_NAME => 'ID',
168
            Keys::METADATA_FIELD_TYPE => 'integer',
169
        ], [
170
            Keys::METADATA_FIELD_NAME => 'NAME',
171
            Keys::METADATA_FIELD_TYPE => 'string',
172
        ]], $response->getMetadata());
173
    }
174
175
    /**
176
     * @requires Tarantool >=2.6
177
     *
178
     * @sql DROP TABLE IF EXISTS %target_method%
179
     * @sql CREATE TABLE %target_method% (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME VARCHAR(50) COLLATE "unicode_ci")
180
     */
181
    public function testSqlQueryResultHoldsExtendedMetadata() : void
182
    {
183
        $client = ClientBuilder::createFromEnv()->build();
184
        $client->execute('SET SESSION "sql_full_metadata" = true');
185
186
        $tableName = $this->resolvePlaceholders('%target_method%');
187
        $response = $client->executeQuery("SELECT ID, NAME AS FULL_NAME FROM $tableName WHERE ID > 0");
188
189
        self::assertSame([[
190
            Keys::METADATA_FIELD_NAME => 'ID',
191
            Keys::METADATA_FIELD_TYPE => 'integer',
192
            Keys::METADATA_FIELD_IS_NULLABLE => false,
193
            Keys::METADATA_FIELD_IS_AUTOINCREMENT => true,
194
            Keys::METADATA_FIELD_SPAN => 'ID',
195
        ], [
196
            Keys::METADATA_FIELD_NAME => 'FULL_NAME',
197
            Keys::METADATA_FIELD_TYPE => 'string',
198
            Keys::METADATA_FIELD_COLL => 'unicode_ci',
199
            Keys::METADATA_FIELD_IS_NULLABLE => true,
200
            Keys::METADATA_FIELD_SPAN => 'NAME',
201
        ]], $response->getMetadata());
202
    }
203
}
204