Passed
Pull Request — master (#483)
by
unknown
03:01
created

LockExpressionTest::testBuild()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Tests\Components;
6
7
use PhpMyAdmin\SqlParser\Components\LockExpression;
8
use PhpMyAdmin\SqlParser\Parser;
9
use PhpMyAdmin\SqlParser\Tests\TestCase;
10
use PHPUnit\Framework\Attributes\DataProvider;
11
12
class LockExpressionTest extends TestCase
13
{
14
    public function testParse(): void
15
    {
16
        $component = LockExpression::parse(new Parser(), $this->getTokensList('table1 AS t1 READ LOCAL'));
17
        $this->assertNotNull($component->table);
18
        $this->assertEquals($component->table->table, 'table1');
19
        $this->assertEquals($component->table->alias, 't1');
20
        $this->assertEquals($component->type, 'READ LOCAL');
21
    }
22
23
    public function testParse2(): void
24
    {
25
        $component = LockExpression::parse(new Parser(), $this->getTokensList('table1 LOW_PRIORITY WRITE'));
26
        $this->assertNotNull($component->table);
27
        $this->assertEquals($component->table->table, 'table1');
28
        $this->assertEquals($component->type, 'LOW_PRIORITY WRITE');
29
    }
30
31
    #[DataProvider('parseErrProvider')]
32
    public function testParseErr(string $expr, string $error): void
33
    {
34
        $parser = new Parser();
35
        LockExpression::parse($parser, $this->getTokensList($expr));
36
        $errors = $this->getErrorsAsArray($parser);
37
        $this->assertEquals($errors[0][0], $error);
38
    }
39
40
    /**
41
     * @return string[][]
42
     */
43
    public static function parseErrProvider(): array
44
    {
45
        return [
46
            [
47
                'table1 AS t1',
48
                'Unexpected end of LOCK expression.',
49
            ],
50
            [
51
                'table1 AS t1 READ WRITE',
52
                'Unexpected keyword.',
53
            ],
54
            [
55
                'table1 AS t1 READ 2',
56
                'Unexpected token.',
57
            ],
58
        ];
59
    }
60
61
    public function testBuildAll(): void
62
    {
63
        $component = [
64
            LockExpression::parse(new Parser(), $this->getTokensList('table1 AS t1 READ LOCAL')),
65
            LockExpression::parse(new Parser(), $this->getTokensList('table2 LOW_PRIORITY WRITE')),
66
        ];
67
        $this->assertEquals(
68
            LockExpression::buildAll($component),
69
            'table1 AS `t1` READ LOCAL, table2 LOW_PRIORITY WRITE'
70
        );
71
    }
72
}
73