ErrorExtensionTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 53
rs 10
c 2
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createClientWithExtendedErrorSupport() 0 7 1
A testLuaPackingAndUnpacking() 0 38 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 Tarantool\Client\Client;
17
use Tarantool\Client\Error;
18
use Tarantool\Client\Packer\Extension\ErrorExtension;
19
use Tarantool\Client\Packer\PurePacker;
20
use Tarantool\Client\Tests\Integration\ClientBuilder;
21
use Tarantool\Client\Tests\Integration\TestCase;
22
23
final class ErrorExtensionTest extends TestCase
24
{
25
    /**
26
     * @requires Tarantool >=2.4.1 <2.10
27
     * @see https://github.com/tarantool/tarantool/issues/6428
28
     */
29
    public function testLuaPackingAndUnpacking() : void
30
    {
31
        $client = self::createClientWithExtendedErrorSupport();
32
33
        /** @var Error $error */
34
        $error = $client->evaluate('
35
            box.session.settings.error_marshaling_enabled = true
36
            err1 = box.error.new({code = 1, type = "t1", reason = "r1"})
37
            err2 = box.error.new({code = 2, type = "t2", reason = "r2",})
38
            err1:set_prev(err2)
39
            return err1
40
        ')[0];
41
42
        self::assertInstanceOf(Error::class, $error);
43
        self::assertSame('CustomError', $error->getType());
44
        self::assertSame('r1', $error->getMessage());
45
        self::assertSame(1, $error->getCode());
46
        self::assertSame(['custom_type' => 't1'], $error->getFields());
47
48
        $prevError = $error->getPrevious();
49
        self::assertInstanceOf(Error::class, $prevError);
50
        self::assertSame('CustomError', $prevError->getType());
51
        self::assertSame('r2', $prevError->getMessage());
52
        self::assertSame(2, $prevError->getCode());
53
        self::assertSame(['custom_type' => 't2'], $prevError->getFields());
54
        self::assertNull($prevError->getPrevious());
55
56
        $isEqual = $client->evaluate('
57
            box.session.settings.error_marshaling_enabled = true
58
            err1 = ...
59
            err2 = err1.prev
60
            return
61
                err1.code == 1 and err1.type == "t1" and err1.message == "r1" and
62
                err2.code == 2 and err2.type == "t2" and err2.message == "r2" and
63
                err1.prev == err2
64
        ', $error)[0];
65
66
        self::assertTrue($isEqual);
67
    }
68
69
    private static function createClientWithExtendedErrorSupport() : Client
70
    {
71
        return ClientBuilder::createFromEnv()
72
            ->setPackerFactory(static function () {
73
                return PurePacker::fromExtensions(new ErrorExtension());
74
            })
75
            ->build();
76
    }
77
}
78