File: ContextGeneratorTest.php

package info (click to toggle)
phpmyadmin-sql-parser 5.10.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,244 kB
  • sloc: php: 52,958; makefile: 13; sh: 8
file content (79 lines) | stat: -rw-r--r-- 2,885 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php

declare(strict_types=1);

namespace PhpMyAdmin\SqlParser\Tests\Tools;

use PhpMyAdmin\SqlParser\Tests\TestCase;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\Tools\ContextGenerator;

use function file_get_contents;
use function getcwd;

class ContextGeneratorTest extends TestCase
{
    public function testFormatName(): void
    {
        $name = ContextGenerator::formatName('Invalid00Format00');
        $this->assertEquals('Invalid00Format00', $name);

        $name = ContextGenerator::formatName('MySql80000');
        $this->assertEquals('MySQL 8.0', $name);

        $name = ContextGenerator::formatName('MariaDb100200');
        $this->assertEquals('MariaDB 10.2', $name);

        $name = ContextGenerator::formatName('MariaDb100000');
        $this->assertEquals('MariaDB 10.0', $name);

        $name = ContextGenerator::formatName('FutureDBMS45784012500');
        $this->assertEquals('FutureDBMS 4.57.84.1.25', $name);
    }

    public function testSortWords(): void
    {
        $wordsArray = ['41' => ['GEOMETRYCOLLECTION', 'DATE'], '35' => ['SCHEMA', 'REPEAT', 'VALUES']];
        ContextGenerator::sortWords($wordsArray);
        $this->assertEquals([
            '41' => ['DATE', 'GEOMETRYCOLLECTION'],
            '35' => ['REPEAT', 'SCHEMA', 'VALUES'],
        ], $wordsArray);
    }

    public function testReadWords(): void
    {
        $testFiles = [getcwd() . '/tests/Tools/contexts/testContext.txt'];
        $readWords = ContextGenerator::readWords($testFiles);
        $this->assertEquals([
            Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_RESERVED => [
                'RESERVED',
                'RESERVED2',
                'RESERVED3',
                'RESERVED4',
                'RESERVED5',
            ],
            Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_FUNCTION => ['FUNCTION'],
            Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_DATA_TYPE => ['DATATYPE'],
            Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_KEY => ['KEYWORD'],
            Token::FLAG_KEYWORD => ['NO_FLAG'],
            Token::FLAG_KEYWORD | Token::FLAG_KEYWORD_RESERVED | Token::FLAG_KEYWORD_COMPOSED => ['COMPOSED KEYWORD'],
        ], $readWords);
    }

    public function testGenerate(): void
    {
        $testFiles = [getcwd() . '/tests/Tools/contexts/testContext.txt'];
        $readWords = ContextGenerator::readWords($testFiles);
        ContextGenerator::printWords($readWords);
        $options = [
            'keywords' => $readWords,
            'name' => 'MYSQL TEST',
            'class' => 'TestContext',
            'link' => 'https://www.phpmyadmin.net/contribute',
        ];
        $generatedTemplate = ContextGenerator::generate($options);
        $expectedTemplate = file_get_contents(getcwd() . '/tests/Tools/templates/TestContext.php');
        $this->assertEquals($expectedTemplate, $generatedTemplate);
    }
}