Completed
Branch master (aa12c6)
by Eugene
01:39
created

ExecuteTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 42
dl 0
loc 125
rs 10
c 6
b 0
f 0
wmc 12

12 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
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\TestCase;
18
19
/**
20
 * @requires Tarantool 2
21
 *
22
 * @eval box.execute([[ DROP TABLE IF EXISTS exec_query ]])
23
 * @eval box.execute([[ CREATE TABLE exec_query (id INTEGER PRIMARY KEY, name VARCHAR(50)) ]])
24
 * @eval box.execute([[ INSERT INTO exec_query VALUES (1, 'A'), (2, 'B') ]])
25
 */
26
final class ExecuteTest extends TestCase
27
{
28
    /**
29
     * @eval box.execute([[ DROP TABLE IF EXISTS exec_update ]])
30
     * @eval box.execute([[ CREATE TABLE exec_update (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(50)) ]])
31
     */
32
    public function testExecuteInsertsRows() : void
33
    {
34
        $response = $this->client->execute(
35
            'INSERT INTO exec_update VALUES (5, :name1), (null, :name2)',
36
            [':name1' => 'A'], [':name2' => 'B']
37
        );
38
39
        $expectedSqlInfo = [
40
            Keys::SQL_INFO_ROW_COUNT => 2,
41
            Keys::SQL_INFO_AUTO_INCREMENT_IDS => [6],
42
        ];
43
44
        self::assertSame($expectedSqlInfo, $response->getBodyField(Keys::SQL_INFO));
45
    }
46
47
    public function testExecuteFetchesAllRows() : void
48
    {
49
        $response = $this->client->execute('SELECT * FROM exec_query');
50
51
        self::assertSame([[1, 'A'], [2, 'B']], $response->getBodyField(Keys::DATA));
52
    }
53
54
    /**
55
     * @eval box.execute([[ DROP TABLE IF EXISTS exec_update ]])
56
     * @eval box.execute([[ CREATE TABLE exec_update (id INTEGER PRIMARY KEY, name VARCHAR(50)) ]])
57
     */
58
    public function testExecuteUpdateInsertsRows() : void
59
    {
60
        $result = $this->client->executeUpdate(
61
            'INSERT INTO exec_update VALUES (1, :name1), (2, :name2)',
62
            [':name1' => 'A'], [':name2' => 'B']
63
        );
64
65
        self::assertSame([], $result->getAutoincrementIds());
66
        self::assertSame(2, $result->count());
67
    }
68
69
    /**
70
     * @eval box.execute([[ DROP TABLE IF EXISTS exec_update ]])
71
     * @eval box.execute([[ CREATE TABLE exec_update (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(50)) ]])
72
     */
73
    public function testExecuteUpdateInsertsRowsWithAutoIncrementedIds() : void
74
    {
75
        $result = $this->client->executeUpdate("INSERT INTO exec_update VALUES (100, 'A'), (null, 'B'), (120, 'C'), (null, 'D')");
76
77
        self::assertSame([101, 121], $result->getAutoincrementIds());
78
        self::assertSame(4, $result->count());
79
    }
80
81
    /**
82
     * @eval box.execute([[ DROP TABLE IF EXISTS exec_update ]])
83
     * @eval box.execute([[ CREATE TABLE exec_update (id INTEGER PRIMARY KEY, name VARCHAR(50)) ]])
84
     * @eval box.execute([[ INSERT INTO exec_update VALUES (1, 'A'), (2, 'B') ]])
85
     */
86
    public function testExecuteUpdateUpdatesRow() : void
87
    {
88
        $result = $this->client->executeUpdate('UPDATE exec_update SET name = ? WHERE id = ?', 'BB', 2);
89
90
        self::assertSame([], $result->getAutoincrementIds());
91
        self::assertSame(1, $result->count());
92
    }
93
94
    public function testExecuteQueryFetchesAllRows() : void
95
    {
96
        $result = $this->client->executeQuery('SELECT * FROM exec_query');
97
98
        self::assertSame([[1, 'A'], [2, 'B']], $result->getData());
99
        self::assertSame(2, $result->count());
100
    }
101
102
    public function testExecuteQueryFetchesOneRow() : void
103
    {
104
        $result = $this->client->executeQuery('SELECT * FROM exec_query WHERE id = 1');
105
106
        self::assertSame([[1, 'A']], $result->getData());
107
        self::assertSame(1, $result->count());
108
    }
109
110
    public function testExecuteQueryFetchesNoRows() : void
111
    {
112
        $result = $this->client->executeQuery('SELECT * FROM exec_query WHERE id = -1');
113
114
        self::assertSame([], $result->getData());
115
        self::assertSame(0, $result->count());
116
    }
117
118
    public function testExecuteQueryBindsPositionalParameters() : void
119
    {
120
        $result = $this->client->executeQuery('SELECT ?, ?', 2, 'B');
121
122
        self::assertSame([[2, 'B']], $result->getData());
123
        self::assertSame(1, $result->count());
124
    }
125
126
    public function testExecuteQueryBindsNamedParameters() : void
127
    {
128
        $result = $this->client->executeQuery('SELECT :id, :name', [':name' => 'B'], [':id' => 2]);
129
130
        self::assertSame([[2, 'B']], $result->getData());
131
        self::assertSame(1, $result->count());
132
    }
133
134
    public function testExecuteQueryBindsMixedParameters() : void
135
    {
136
        $result = $this->client->executeQuery('SELECT ?, :name', 2, [':name' => 'B']);
137
138
        self::assertSame([[2, 'B']], $result->getData());
139
        self::assertSame(1, $result->count());
140
    }
141
142
    /**
143
     * @see https://github.com/tarantool/tarantool/issues/4782
144
     */
145
    public function testExecuteQueryBindsMixedParametersAndSubstitutesPositionalOnes() : void
146
    {
147
        $result = $this->client->executeQuery('SELECT :id, ?', 'B', [':id' => 2]);
148
149
        self::assertSame([[2, null]], $result->getData());
150
        self::assertSame(1, $result->count());
151
    }
152
}
153