Passed
Push — master ( 4db95c...69b01d )
by Eugene
07:17
created

testSqlSelectByUuidKeySucceeds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
rs 10
c 1
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\MessagePack;
15
16
use Symfony\Component\Uid\Uuid;
17
use Tarantool\Client\Client;
18
use Tarantool\Client\Packer\Extension\UuidExtension;
19
use Tarantool\Client\Packer\PurePacker;
20
use Tarantool\Client\Schema\Criteria;
21
use Tarantool\Client\Tests\Integration\ClientBuilder;
22
use Tarantool\Client\Tests\Integration\TestCase;
23
24
/**
25
 * @requires Tarantool >=2.4
26
 * @requires package symfony/uid
27
 * @requires clientPacker pure
28
 *
29
 * @lua uuid = require('uuid').fromstr('64d22e4d-ac92-4a23-899a-e59f34af5479')
30
 * @lua space = create_space('uuid_primary')
31
 * @lua space:format({{name = 'id', type = 'uuid'}})
32
 * @lua space:create_index("primary", {parts = {1, 'uuid'}})
33
 * @lua space:insert({uuid})
34
 */
35
final class UuidExtensionTest extends TestCase
36
{
37
    private const UUID_RFC4122 = '64d22e4d-ac92-4a23-899a-e59f34af5479';
38
39
    public function testBinarySelectByUuidKeySucceeds() : void
40
    {
41
        $client = self::createClientWithUuidSupport();
42
43
        $uuid = new Uuid(self::UUID_RFC4122);
44
        $space = $client->getSpace('uuid_primary');
45
        $result = $space->select(Criteria::key([$uuid]));
46
47
        self::assertTrue(isset($result[0][0]));
48
        self::assertTrue($uuid->equals($result[0][0]));
49
    }
50
51
    /**
52
     * @requires Tarantool >=2.10-stable
53
     */
54
    public function testSqlSelectByUuidKeySucceeds() : void
55
    {
56
        $client = self::createClientWithUuidSupport();
57
58
        $uuid = new Uuid(self::UUID_RFC4122);
59
        $result = $client->executeQuery('SELECT * FROM "uuid_primary" WHERE "id" = ?', $uuid);
60
61
        self::assertFalse($result->isEmpty());
62
        self::assertTrue($uuid->equals($result->getFirst()['id']));
63
    }
64
65
    public function testLuaPackingAndUnpacking() : void
66
    {
67
        $client = self::createClientWithUuidSupport();
68
69
        [$uuid] = $client->evaluate('return require("uuid").fromstr(...)', self::UUID_RFC4122);
70
        self::assertInstanceOf(Uuid::class, $uuid);
71
        self::assertSame($uuid->toRfc4122(), self::UUID_RFC4122);
72
73
        [$isEqual] = $client->evaluate(
74
            sprintf("return require('uuid').fromstr('%s') == ...", self::UUID_RFC4122),
75
            new Uuid(self::UUID_RFC4122)
76
        );
77
        self::assertTrue($isEqual);
78
    }
79
80
    private static function createClientWithUuidSupport() : Client
81
    {
82
        return ClientBuilder::createFromEnv()
83
            ->setPackerPureFactory(static function () {
84
                return PurePacker::fromExtensions(new UuidExtension());
85
            })
86
            ->build();
87
    }
88
}
89