Completed
Branch master (00332a)
by Eugene
05:11
created

testCloseDeallocatesPreparedStatement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 10
rs 9.9332
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\Exception\RequestFailed;
17
use Tarantool\Client\PreparedStatement;
18
use Tarantool\Client\Tests\Integration\TestCase;
19
20
/**
21
 * @requires Tarantool >= 2.3.1-68
22
 */
23
final class PrepareTest extends TestCase
24
{
25
    public function testPreparePreparesSqlStatement() : void
26
    {
27
        [$preparedCountBefore] = $this->client->evaluate('return box.info.sql().cache.stmt_count');
0 ignored issues
show
Bug introduced by
The variable $preparedCountBefore does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
28
        $stmt = $this->client->prepare('SELECT ?');
29
        [$preparedCountAfter] = $this->client->evaluate('return box.info.sql().cache.stmt_count');
0 ignored issues
show
Bug introduced by
The variable $preparedCountAfter does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
30
31
        try {
32
            self::assertSame($preparedCountBefore + 1, $preparedCountAfter);
33
            self::assertIsInt($stmt->getId());
34
            self::assertSame(1, $stmt->getBindCount());
35
            self::assertSame([['?', 'ANY']], $stmt->getBindMetadata());
36
            // If the data type of NULL cannot be determined from context, it is BOOLEAN.
37
            // @see https://www.tarantool.io/en/doc/2.2/reference/reference_sql/sql/#column-definition-data-type
38
            self::assertSame([['?', 'boolean']], $stmt->getMetadata());
39
        } finally {
40
            $stmt->close();
41
        }
42
    }
43
44
    public function testExecuteQueryReturnsResult() : void
45
    {
46
        $stmt = $this->client->prepare('SELECT :v1, :v2');
47
48
        $selectResult1 = $stmt->executeQuery([':v1' => 1], [':v2' => 2]);
49
        $selectResult2 = $stmt->executeQuery([':v1' => 3], [':v2' => 4]);
50
51
        try {
52
            self::assertSame([':v1' => 1, ':v2' => 2], $selectResult1->getFirst());
53
            self::assertSame([':v1' => 3, ':v2' => 4], $selectResult2->getFirst());
54
        } finally {
55
            $stmt->close();
56
        }
57
    }
58
59
    /**
60
     * @eval box.execute([[ DROP TABLE IF EXISTS prepare_execute ]])
61
     * @eval box.execute([[ CREATE TABLE prepare_execute (id INTEGER PRIMARY KEY, name VARCHAR(50)) ]])
62
     */
63
    public function testExecuteUpdateUpdatesRows() : void
64
    {
65
        $stmt = $this->client->prepare('INSERT INTO prepare_execute VALUES(:id, :name)');
66
67
        $insertResult1 = $stmt->executeUpdate(1, 'foo');
68
        $insertResult2 = $stmt->executeUpdate([':name' => 'bar'], [':id' => 2]);
69
70
        $selectResult = $this->client->executeQuery('SELECT * FROM prepare_execute ORDER BY id');
71
72
        try {
73
            self::assertSame(1, $insertResult1->count());
74
            self::assertSame(1, $insertResult2->count());
75
            self::assertSame([[1, 'foo'], [2, 'bar']], $selectResult->getData());
76
        } finally {
77
            $stmt->close();
78
        }
79
    }
80
81
    public function testCloseDeallocatesPreparedStatement() : void
82
    {
83
        $stmt = $this->client->prepare('SELECT ?');
84
85
        [$preparedCountBefore] = $this->client->evaluate('return box.info.sql().cache.stmt_count');
0 ignored issues
show
Bug introduced by
The variable $preparedCountBefore does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
86
        $stmt->close();
87
        [$preparedCountAfter] = $this->client->evaluate('return box.info.sql().cache.stmt_count');
0 ignored issues
show
Bug introduced by
The variable $preparedCountAfter does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
88
89
        self::assertSame($preparedCountBefore - 1, $preparedCountAfter);
90
    }
91
92
    public function testCloseClosesPreparedInLuaSqlStatement() : void
93
    {
94
        [$data] = $this->client->evaluate("s = box.prepare('SELECT ?') return {
0 ignored issues
show
Bug introduced by
The variable $data does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
95
            id=s.stmt_id, 
96
            bind_metadata=s.params, 
97
            metadata=s.metadata, 
98
            bind_count=s.param_count
99
        }");
100
101
        $stmt = new PreparedStatement(
102
            $this->client->getHandler(),
103
            $data['id'],
104
            $data['bind_count'],
105
            $data['bind_metadata'],
106
            $data['metadata']
107
        );
108
109
        [$preparedCountBefore] = $this->client->evaluate('return box.info.sql().cache.stmt_count');
0 ignored issues
show
Bug introduced by
The variable $preparedCountBefore does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
110
        $stmt->close();
111
        [$preparedCountAfter] = $this->client->evaluate('return box.info.sql().cache.stmt_count');
0 ignored issues
show
Bug introduced by
The variable $preparedCountAfter does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
112
113
        self::assertSame($preparedCountBefore - 1, $preparedCountAfter);
114
    }
115
116
    public function testCloseFailsOnNonexistentPreparedStatement() : void
117
    {
118
        $stmt = new PreparedStatement($this->client->getHandler(), 42, 0, [], []);
119
120
        $this->expectException(RequestFailed::class);
121
        $this->expectExceptionMessage('Prepared statement with id 42 does not exist');
122
        $stmt->close();
123
    }
124
}
125