UuidExtensionTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 52
rs 10
c 2
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testLuaPackingAndUnpacking() 0 13 1
A createClientWithUuidSupport() 0 7 1
A testBinarySelectByUuidKeySucceeds() 0 10 1
A testSqlSelectByUuidKeySucceeds() 0 9 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\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
 *
27
 * @lua uuid = require('uuid').fromstr('64d22e4d-ac92-4a23-899a-e59f34af5479')
28
 * @lua space = create_space('uuid_primary')
29
 * @lua space:format({{name = 'id', type = 'uuid'}})
30
 * @lua space:create_index("primary", {parts = {1, 'uuid'}})
31
 * @lua space:insert({uuid})
32
 */
33
final class UuidExtensionTest extends TestCase
34
{
35
    private const UUID_RFC4122 = '64d22e4d-ac92-4a23-899a-e59f34af5479';
36
37
    public function testBinarySelectByUuidKeySucceeds() : void
38
    {
39
        $client = self::createClientWithUuidSupport();
40
41
        $uuid = new Uuid(self::UUID_RFC4122);
42
        $space = $client->getSpace('uuid_primary');
43
        $result = $space->select(Criteria::key([$uuid]));
44
45
        self::assertTrue(isset($result[0][0]));
46
        self::assertTrue($uuid->equals($result[0][0]));
47
    }
48
49
    /**
50
     * @requires Tarantool >=2.10-stable
51
     */
52
    public function testSqlSelectByUuidKeySucceeds() : void
53
    {
54
        $client = self::createClientWithUuidSupport();
55
56
        $uuid = new Uuid(self::UUID_RFC4122);
57
        $result = $client->executeQuery('SELECT * FROM "uuid_primary" WHERE "id" = ?', $uuid);
58
59
        self::assertFalse($result->isEmpty());
60
        self::assertTrue($uuid->equals($result->getFirst()['id']));
61
    }
62
63
    public function testLuaPackingAndUnpacking() : void
64
    {
65
        $client = self::createClientWithUuidSupport();
66
67
        [$uuid] = $client->evaluate('return require("uuid").fromstr(...)', self::UUID_RFC4122);
68
        self::assertInstanceOf(Uuid::class, $uuid);
69
        self::assertSame($uuid->toRfc4122(), self::UUID_RFC4122);
70
71
        [$isEqual] = $client->evaluate(
72
            sprintf("return require('uuid').fromstr('%s') == ...", self::UUID_RFC4122),
73
            new Uuid(self::UUID_RFC4122)
74
        );
75
        self::assertTrue($isEqual);
76
    }
77
78
    private static function createClientWithUuidSupport() : Client
79
    {
80
        return ClientBuilder::createFromEnv()
81
            ->setPackerFactory(static function () {
82
                return PurePacker::fromExtensions(new UuidExtension());
83
            })
84
            ->build();
85
    }
86
}
87