Passed
Push — master ( 8f298b...a177b7 )
by Eugene
06:12
created

ExecuteTest::testExecuteQueryFetchesNoRows()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
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\Tests\Integration\TestCase;
17
18
/**
19
 * @requires Tarantool 2
20
 *
21
 * @eval box.execute([[ DROP TABLE IF EXISTS exec_query ]])
22
 * @eval box.execute([[ CREATE TABLE exec_query (id INTEGER PRIMARY KEY, name VARCHAR(50)) ]])
23
 * @eval box.execute([[ INSERT INTO exec_query VALUES (1, 'A'), (2, 'B') ]])
24
 */
25
final class ExecuteTest extends TestCase
26
{
27
    /**
28
     * @eval box.execute([[ DROP TABLE IF EXISTS exec_update ]])
29
     * @eval box.execute([[ CREATE TABLE exec_update (id INTEGER PRIMARY KEY, name VARCHAR(50)) ]])
30
     */
31
    public function testExecuteUpdateInsertsRows() : void
32
    {
33
        $result = $this->client->executeUpdate(
34
            'INSERT INTO exec_update VALUES (1, :name1), (2, :name2)',
35
            [':name1' => 'A'], [':name2' => 'B']
36
        );
37
38
        self::assertNull($result->getAutoincrementIds());
39
        self::assertSame(2, $result->count());
40
    }
41
42
    /**
43
     * @eval box.execute([[ DROP TABLE IF EXISTS exec_update ]])
44
     * @eval box.execute([[ CREATE TABLE exec_update (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(50)) ]])
45
     */
46
    public function testExecuteUpdateInsertsRowsWithAutoIncrementedIds() : void
47
    {
48
        $result = $this->client->executeUpdate("INSERT INTO exec_update VALUES (100, 'A'), (null, 'B'), (120, 'C'), (null, 'D')");
49
50
        self::assertSame([101, 121], $result->getAutoincrementIds());
51
        self::assertSame(4, $result->count());
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
     * @eval box.execute([[ INSERT INTO exec_update VALUES (1, 'A'), (2, 'B') ]])
58
     */
59
    public function testExecuteUpdateUpdatesRow() : void
60
    {
61
        $result = $this->client->executeUpdate('UPDATE exec_update SET name = ? WHERE id = ?', 'BB', 2);
62
63
        self::assertNull($result->getAutoincrementIds());
64
        self::assertSame(1, $result->count());
65
    }
66
67
    public function testExecuteQueryFetchesAllRows() : void
68
    {
69
        $result = $this->client->executeQuery('SELECT * FROM exec_query');
70
71
        self::assertSame([[1, 'A'], [2, 'B']], $result->getData());
72
        self::assertSame(2, $result->count());
73
    }
74
75
    public function testExecuteQueryFetchesOneRow() : void
76
    {
77
        $result = $this->client->executeQuery('SELECT * FROM exec_query WHERE id = 1');
78
79
        self::assertSame([[1, 'A']], $result->getData());
80
        self::assertSame(1, $result->count());
81
    }
82
83
    public function testExecuteQueryFetchesNoRows() : void
84
    {
85
        $result = $this->client->executeQuery('SELECT * FROM exec_query WHERE id = -1');
86
87
        self::assertSame([], $result->getData());
88
        self::assertSame(0, $result->count());
89
    }
90
91
    public function testExecuteQueryBindsPositionalParameters() : void
92
    {
93
        $result = $this->client->executeQuery('SELECT * FROM exec_query WHERE id = ? AND name = ?', 2, 'B');
94
95
        self::assertSame([[2, 'B']], $result->getData());
96
        self::assertSame(1, $result->count());
97
    }
98
99
    public function testExecuteQueryBindsNamedParameters() : void
100
    {
101
        $result = $this->client->executeQuery('SELECT * FROM exec_query WHERE id = :id AND name = :name', [':name' => 'B'], [':id' => 2]);
102
103
        self::assertSame([[2, 'B']], $result->getData());
104
        self::assertSame(1, $result->count());
105
    }
106
107
    public function testExecuteQueryBindsMixedParameters() : void
108
    {
109
        $result = $this->client->executeQuery('SELECT * FROM exec_query WHERE id = ? AND name = :name', 2, [':name' => 'B']);
110
111
        // this doesn't work, see https://github.com/tarantool/doc/issues/1096
112
        // $result = $this->client->executeQuery('SELECT * FROM exec_query WHERE id = :id AND name = ?', 'B', [':id' => 2]);
113
114
        self::assertSame([[2, 'B']], $result->getData());
115
        self::assertSame(1, $result->count());
116
    }
117
}
118