Passed
Push — master ( 210d3e...8cc5d0 )
by Eugene
02:24
created

ExecuteTest::provideSeqScan()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
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
    public function testSqlQueryResultHoldsMetadata() : void
155
    {
156
        $client = ClientBuilder::createFromEnv()->build();
157
158
        $response = $client->executeQuery('SELECT * FROM exec_query WHERE id > 0');
159
160
        self::assertSame([[
161
            Keys::METADATA_FIELD_NAME => 'ID',
162
            Keys::METADATA_FIELD_TYPE => 'integer',
163
        ], [
164
            Keys::METADATA_FIELD_NAME => 'NAME',
165
            Keys::METADATA_FIELD_TYPE => 'string',
166
        ]], $response->getMetadata());
167
    }
168
169
    /**
170
     * @requires Tarantool >=2.6
171
     *
172
     * @sql DROP TABLE IF EXISTS %target_method%
173
     * @sql CREATE TABLE %target_method% (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(50) COLLATE "unicode_ci")
174
     */
175
    public function testSqlQueryResultHoldsExtendedMetadata() : void
176
    {
177
        $client = ClientBuilder::createFromEnv()->build();
178
        $client->execute('SET SESSION "sql_full_metadata" = true');
179
180
        $tableName = $this->resolvePlaceholders('%target_method%');
181
        $response = $client->executeQuery("SELECT id, name AS full_name FROM $tableName WHERE id > 0");
182
183
        self::assertSame([[
184
            Keys::METADATA_FIELD_NAME => 'ID',
185
            Keys::METADATA_FIELD_TYPE => 'integer',
186
            Keys::METADATA_FIELD_IS_NULLABLE => false,
187
            Keys::METADATA_FIELD_IS_AUTOINCREMENT => true,
188
            Keys::METADATA_FIELD_SPAN => 'id',
189
        ], [
190
            Keys::METADATA_FIELD_NAME => 'FULL_NAME',
191
            Keys::METADATA_FIELD_TYPE => 'string',
192
            Keys::METADATA_FIELD_COLL => 'unicode_ci',
193
            Keys::METADATA_FIELD_IS_NULLABLE => true,
194
            Keys::METADATA_FIELD_SPAN => 'name',
195
        ]], $response->getMetadata());
196
    }
197
}
198