Passed
Pull Request — master (#94)
by
unknown
09:30
created

BoxErrorTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 5
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;
15
16
use Tarantool\Client\Error;
17
use Tarantool\Client\Exception\RequestFailed;
18
19
final class BoxErrorTest extends TestCase
20
{
21
    public function testExceptionIsThrown() : void
22
    {
23
        $this->expectException(RequestFailed::class);
24
        $this->expectExceptionMessage('Because I can');
25
        $this->expectExceptionCode(42);
26
27
        $this->client->evaluate('box.error({code = 42, reason = "Because I can"})');
28
    }
29
30
    /**
31
     * @requires Tarantool >=2.4.1
32
     */
33
    public function testExceptionWithErrorIsThrown() : void
34
    {
35
        try {
36
            /*
37
             * Triggers "AccessDeniedError", which includes the fields
38
             * "object_type", "object_name" and "access_type".
39
             * See https://www.tarantool.io/en/doc/2.4/dev_guide/internals/box_protocol/#binary-protocol-responses-for-errors-extra.
40
             */
41
            $this->client->evaluate("
42
                local temp_user = 'user_with_no_privileges'
43
                local curr_user = box.session.user()
44
                box.schema.user.create(temp_user)
45
                box.session.su(temp_user)
46
                local _, err = pcall(box.schema.user.grant, temp_user, 'execute', 'universe')
47
                box.session.su(curr_user)
48
                box.schema.user.drop(temp_user)
49
                box.error(err)
50
            ");
51
            self::fail(sprintf('"%s" exception was not thrown', RequestFailed::class));
52
        } catch (RequestFailed $e) {
53
            self::assertSame("Write access to space '_priv' is denied for user 'user_with_no_privileges'", $e->getMessage());
54
            self::assertSame(42, $e->getCode());
55
56
            $error = $e->getError();
57
            self::assertInstanceOf(Error::class, $error);
58
            self::assertSame('AccessDeniedError', $error->getType());
59
            self::assertSame("Write access to space '_priv' is denied for user 'user_with_no_privileges'", $error->getMessage());
60
            self::assertSame(42, $error->getCode());
61
            /* Checks that error fields match expected. */
62
            $fields_expected = [
63
                'object_type' => 'space',
64
                'object_name' => '_priv',
65
                'access_type' => 'Write',
66
            ];
67
            $diff = array_diff_assoc($fields_expected, $error->getFields()));
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected ')' on line 67 at column 75
Loading history...
68
            self::assertTrue(!$diff, 'these fields are different: ' .
69
                                     print_r($diff, true));
70
            self::assertNull($error->getPrevious());
71
        }
72
    }
73
74
    /**
75
     * @requires Tarantool >=2.4.1
76
     */
77
    public function testExceptionWithNestedErrorIsThrown() : void
78
    {
79
        try {
80
            $this->client->evaluate('
81
                err1 = box.error.new({code = 1, type = "t1", reason = "r1"})
82
                err2 = box.error.new({code = 2, type = "t2", reason = "r2",})
83
                err1:set_prev(err2)
84
                box.error(err1)
85
            ');
86
            self::fail(sprintf('"%s" exception was not thrown', RequestFailed::class));
87
        } catch (RequestFailed $e) {
88
            self::assertSame('r1', $e->getMessage());
89
            self::assertSame(1, $e->getCode());
90
91
            $error = $e->getError();
92
            self::assertInstanceOf(Error::class, $error);
93
            self::assertSame('CustomError', $error->getType());
94
            self::assertSame('r1', $error->getMessage());
95
            self::assertSame(1, $error->getCode());
96
            self::assertSame(['custom_type' => 't1'], $error->getFields());
97
98
            $prevError = $error->getPrevious();
99
            self::assertInstanceOf(Error::class, $prevError);
100
            self::assertSame('CustomError', $prevError->getType());
101
            self::assertSame('r2', $prevError->getMessage());
102
            self::assertSame(2, $prevError->getCode());
103
            self::assertSame(['custom_type' => 't2'], $prevError->getFields());
104
            self::assertNull($prevError->getPrevious());
105
        }
106
    }
107
}
108