Skip to content

Commit cca5eb8

Browse files
committed
Merge #394 - Testing the contextGenerator class
Pull-request: #394 Signed-off-by: William Desportes <[email protected]>
2 parents fd9e59f + e1fc834 commit cca5eb8

5 files changed

Lines changed: 134 additions & 0 deletions

File tree

phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
<rule ref="Squiz.Arrays.ArrayDeclaration.IndexNoNewline">
3131
<exclude-pattern>src/Contexts/*</exclude-pattern>
32+
<exclude-pattern>tests/Tools/templates/*</exclude-pattern>
3233
</rule>
3334

3435
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">

phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
<testsuite name="Utils">
3535
<directory suffix=".php">./tests/Utils</directory>
3636
</testsuite>
37+
<testsuite name="Tools">
38+
<directory suffix=".php">./tests/Tools</directory>
39+
</testsuite>
3740
</testsuites>
3841
<filter>
3942
<whitelist processUncoveredFilesFromWhitelist="true">
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\SqlParser\Tests\Tools;
6+
7+
use PhpMyAdmin\SqlParser\Tests\TestCase;
8+
use PhpMyAdmin\SqlParser\Token;
9+
use PhpMyAdmin\SqlParser\Tools\ContextGenerator;
10+
11+
use function file_get_contents;
12+
use function getcwd;
13+
14+
class ContextGeneratorTest extends TestCase
15+
{
16+
public function testFormatName(): void
17+
{
18+
$name = ContextGenerator::formatName('MySql80000');
19+
$this->assertEquals('MySQL 8.0', $name);
20+
21+
$name = ContextGenerator::formatName('MariaDb100200');
22+
$this->assertEquals('MariaDB 10.2', $name);
23+
24+
$name = ContextGenerator::formatName('MariaDb100000');
25+
$this->assertEquals('MariaDB 10.0', $name);
26+
}
27+
28+
public function testSortWords(): void
29+
{
30+
$wordsArray = ['41' => [['GEOMETRYCOLLECTION', 'DATE']], '35' => [['SCHEMA', 'REPEAT', 'VALUES']]];
31+
ContextGenerator::sortWords($wordsArray);
32+
$this->assertEquals([
33+
'41' => ['0' => ['DATE', 'GEOMETRYCOLLECTION']],
34+
'35' => ['0' => ['REPEAT', 'SCHEMA', 'VALUES']],
35+
], $wordsArray);
36+
}
37+
38+
public function testReadWords(): void
39+
{
40+
$testFiles = [getcwd() . '/tests/Tools/contexts/testContext.txt'];
41+
$readWords = ContextGenerator::readWords($testFiles);
42+
$this->assertEquals([
43+
Token::TYPE_KEYWORD | Token::FLAG_KEYWORD_RESERVED => [
44+
8 => ['RESERVED'],
45+
9 => ['RESERVED2','RESERVED3','RESERVED4','RESERVED5'],
46+
],
47+
Token::TYPE_KEYWORD | Token::FLAG_KEYWORD_FUNCTION => [8 => ['FUNCTION']],
48+
Token::TYPE_KEYWORD | Token::FLAG_KEYWORD_DATA_TYPE => [8 => ['DATATYPE']],
49+
Token::TYPE_KEYWORD | Token::FLAG_KEYWORD_KEY => [7 => ['KEYWORD']],
50+
Token::TYPE_KEYWORD => [7 => ['NO_FLAG']],
51+
Token::TYPE_KEYWORD | Token::FLAG_KEYWORD_RESERVED | 4 => [16 => ['COMPOSED KEYWORD']],
52+
], $readWords);
53+
}
54+
55+
public function testGenerate(): void
56+
{
57+
$testFiles = [getcwd() . '/tests/Tools/contexts/testContext.txt'];
58+
$readWords = ContextGenerator::readWords($testFiles);
59+
ContextGenerator::printWords($readWords);
60+
$options = [
61+
'keywords' => $readWords,
62+
'name' => 'MYSQL TEST',
63+
'class' => 'ContextTest',
64+
'link' => 'https://www.phpmyadmin.net/contribute',
65+
];
66+
$generatedTemplate = ContextGenerator::generate($options);
67+
$expectedTemplate = file_get_contents(getcwd() . '/tests/Tools/templates/ContextTest.php');
68+
$this->assertEquals($expectedTemplate, $generatedTemplate);
69+
}
70+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
RESERVED (R)
2+
RESERVED2 (R)
3+
RESERVED3 (R)
4+
RESERVED4 (R)
5+
RESERVED5 (R)
6+
7+
FUNCTION (F)
8+
DATATYPE (D)
9+
KEYWORD (K)
10+
11+
NO_FLAG
12+
COMPOSED KEYWORD
13+
14+
FUNCTION
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\SqlParser\Contexts;
6+
7+
use PhpMyAdmin\SqlParser\Context;
8+
use PhpMyAdmin\SqlParser\Token;
9+
10+
/**
11+
* Context for MYSQL TEST.
12+
*
13+
* This class was auto-generated from tools/contexts/*.txt.
14+
* Use tools/run_generators.sh for update.
15+
*
16+
* @see https://www.phpmyadmin.net/contribute
17+
*/
18+
class ContextTest extends Context
19+
{
20+
/**
21+
* List of keywords.
22+
*
23+
* The value associated to each keyword represents its flags.
24+
*
25+
* @see Token::FLAG_KEYWORD_RESERVED Token::FLAG_KEYWORD_COMPOSED
26+
* Token::FLAG_KEYWORD_DATA_TYPE Token::FLAG_KEYWORD_KEY
27+
* Token::FLAG_KEYWORD_FUNCTION
28+
*
29+
* @var array<string,int>
30+
* @phpstan-var non-empty-array<non-empty-string,Token::FLAG_KEYWORD_*|int>
31+
*/
32+
public static $KEYWORDS = [
33+
'NO_FLAG' => 1,
34+
35+
'RESERVED' => 3,
36+
'RESERVED2' => 3, 'RESERVED3' => 3, 'RESERVED4' => 3, 'RESERVED5' => 3,
37+
38+
'COMPOSED KEYWORD' => 7,
39+
40+
'DATATYPE' => 9,
41+
42+
'KEYWORD' => 17,
43+
44+
'FUNCTION' => 33,
45+
];
46+
}

0 commit comments

Comments
 (0)