1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace PhpMyAdmin\SqlParser\Tests\Lexer; |
||
6 | |||
7 | use PhpMyAdmin\SqlParser\Context; |
||
8 | use PhpMyAdmin\SqlParser\Tests\TestCase; |
||
9 | use PhpMyAdmin\SqlParser\Token; |
||
10 | |||
11 | class IsMethodsTest extends TestCase |
||
12 | { |
||
13 | public function testIsKeyword(): void |
||
14 | { |
||
15 | $this->assertEquals(1 | Token::FLAG_KEYWORD_RESERVED, Context::isKeyword('SELECT')); |
||
16 | $this->assertEquals(1 | Token::FLAG_KEYWORD_RESERVED, Context::isKeyword('ALL')); |
||
17 | $this->assertEquals(1 | Token::FLAG_KEYWORD_RESERVED, Context::isKeyword('DISTINCT')); |
||
18 | |||
19 | $this->assertEquals( |
||
20 | 1 | Token::FLAG_KEYWORD_RESERVED | Token::FLAG_KEYWORD_COMPOSED | Token::FLAG_KEYWORD_KEY, |
||
21 | Context::isKeyword('PRIMARY KEY'), |
||
22 | ); |
||
23 | $this->assertEquals( |
||
24 | 1 | Token::FLAG_KEYWORD_RESERVED | Token::FLAG_KEYWORD_COMPOSED, |
||
25 | Context::isKeyword('CHARACTER SET'), |
||
26 | ); |
||
27 | |||
28 | $this->assertEquals(1 | Token::FLAG_KEYWORD_RESERVED, Context::isKeyword('FROM', true)); |
||
29 | $this->assertNull(Context::isKeyword('MODIFY', true)); |
||
30 | |||
31 | $this->assertNull(Context::isKeyword('foo')); |
||
32 | $this->assertNull(Context::isKeyword('bar baz')); |
||
33 | } |
||
34 | |||
35 | public function testIsOperator(): void |
||
36 | { |
||
37 | $this->assertEquals(Token::FLAG_OPERATOR_ARITHMETIC, Context::isOperator('%')); |
||
38 | $this->assertEquals(Token::FLAG_OPERATOR_LOGICAL, Context::isOperator('!')); |
||
39 | $this->assertEquals(Token::FLAG_OPERATOR_LOGICAL, Context::isOperator('&&')); |
||
40 | $this->assertEquals(Token::FLAG_OPERATOR_LOGICAL, Context::isOperator('<=>')); |
||
41 | $this->assertEquals(Token::FLAG_OPERATOR_BITWISE, Context::isOperator('&')); |
||
42 | $this->assertEquals(Token::FLAG_OPERATOR_ASSIGNMENT, Context::isOperator(':=')); |
||
43 | $this->assertEquals(Token::FLAG_OPERATOR_SQL, Context::isOperator(',')); |
||
44 | |||
45 | $this->assertNull(Context::isOperator('a')); |
||
46 | } |
||
47 | |||
48 | public function testIsWhitespace(): void |
||
49 | { |
||
50 | $this->assertTrue(Context::isWhitespace(' ')); |
||
51 | $this->assertTrue(Context::isWhitespace("\r")); |
||
52 | $this->assertTrue(Context::isWhitespace("\n")); |
||
53 | $this->assertTrue(Context::isWhitespace("\t")); |
||
54 | |||
55 | $this->assertFalse(Context::isWhitespace('a')); |
||
56 | $this->assertFalse(Context::isWhitespace("\b")); |
||
57 | $this->assertFalse(Context::isWhitespace("\u1000")); |
||
58 | } |
||
59 | |||
60 | public function testIsComment(): void |
||
61 | { |
||
62 | $this->assertEquals(Token::FLAG_COMMENT_BASH, Context::isComment('#')); |
||
63 | $this->assertEquals(Token::FLAG_COMMENT_C, Context::isComment('/*')); |
||
64 | $this->assertEquals(Token::FLAG_COMMENT_C, Context::isComment('*/')); |
||
65 | $this->assertEquals(Token::FLAG_COMMENT_SQL, Context::isComment('-- ')); |
||
66 | $this->assertEquals(Token::FLAG_COMMENT_SQL, Context::isComment("--\t")); |
||
67 | $this->assertEquals(Token::FLAG_COMMENT_SQL, Context::isComment("--\n")); |
||
68 | |||
69 | $this->assertEquals(Token::FLAG_COMMENT_BASH, Context::isComment('# a comment')); |
||
70 | $this->assertEquals(Token::FLAG_COMMENT_C, Context::isComment('/*comment */')); |
||
71 | $this->assertEquals(Token::FLAG_COMMENT_SQL, Context::isComment('-- my comment')); |
||
72 | |||
73 | $this->assertNull(Context::isComment('')); |
||
0 ignored issues
–
show
|
|||
74 | $this->assertNull(Context::isComment('--not a comment')); |
||
75 | } |
||
76 | |||
77 | public function testIsBool(): void |
||
78 | { |
||
79 | $this->assertTrue(Context::isBool('true')); |
||
80 | $this->assertTrue(Context::isBool('false')); |
||
81 | |||
82 | $this->assertFalse(Context::isBool('tru')); |
||
83 | $this->assertFalse(Context::isBool('falsee')); |
||
84 | } |
||
85 | |||
86 | public function testIsNumber(): void |
||
87 | { |
||
88 | $this->assertTrue(Context::isNumber('+')); |
||
89 | $this->assertTrue(Context::isNumber('-')); |
||
90 | $this->assertTrue(Context::isNumber('.')); |
||
91 | $this->assertTrue(Context::isNumber('0')); |
||
92 | $this->assertTrue(Context::isNumber('1')); |
||
93 | $this->assertTrue(Context::isNumber('2')); |
||
94 | $this->assertTrue(Context::isNumber('3')); |
||
95 | $this->assertTrue(Context::isNumber('4')); |
||
96 | $this->assertTrue(Context::isNumber('5')); |
||
97 | $this->assertTrue(Context::isNumber('6')); |
||
98 | $this->assertTrue(Context::isNumber('7')); |
||
99 | $this->assertTrue(Context::isNumber('8')); |
||
100 | $this->assertTrue(Context::isNumber('9')); |
||
101 | $this->assertTrue(Context::isNumber('e')); |
||
102 | $this->assertTrue(Context::isNumber('E')); |
||
103 | } |
||
104 | |||
105 | public function testIsString(): void |
||
106 | { |
||
107 | $this->assertEquals(Token::FLAG_STRING_SINGLE_QUOTES, Context::isString("'")); |
||
108 | $this->assertEquals(Token::FLAG_STRING_DOUBLE_QUOTES, Context::isString('"')); |
||
109 | |||
110 | $this->assertEquals(Token::FLAG_STRING_SINGLE_QUOTES, Context::isString("'foo bar'")); |
||
111 | $this->assertEquals(Token::FLAG_STRING_DOUBLE_QUOTES, Context::isString('"foo bar"')); |
||
112 | |||
113 | $this->assertNull(Context::isString('')); |
||
0 ignored issues
–
show
Are you sure the usage of
PhpMyAdmin\SqlParser\Context::isString('') targeting PhpMyAdmin\SqlParser\Context::isString() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
114 | $this->assertNull(Context::isString('foo bar')); |
||
115 | } |
||
116 | |||
117 | public function testIsSymbol(): void |
||
118 | { |
||
119 | $this->assertEquals(Token::FLAG_SYMBOL_VARIABLE, Context::isSymbol('@')); |
||
120 | $this->assertEquals(Token::FLAG_SYMBOL_BACKTICK, Context::isSymbol('`')); |
||
121 | |||
122 | $this->assertEquals(Token::FLAG_SYMBOL_VARIABLE, Context::isSymbol('@id')); |
||
123 | $this->assertEquals(Token::FLAG_SYMBOL_BACKTICK, Context::isSymbol('`id`')); |
||
124 | |||
125 | $this->assertNull(Context::isSymbol('')); |
||
0 ignored issues
–
show
Are you sure the usage of
PhpMyAdmin\SqlParser\Context::isSymbol('') targeting PhpMyAdmin\SqlParser\Context::isSymbol() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
126 | $this->assertNull(Context::isSymbol('id')); |
||
127 | } |
||
128 | |||
129 | public function testisSeparator(): void |
||
130 | { |
||
131 | $this->assertTrue(Context::isSeparator('+')); |
||
132 | $this->assertTrue(Context::isSeparator('.')); |
||
133 | |||
134 | $this->assertFalse(Context::isSeparator('1')); |
||
135 | $this->assertFalse(Context::isSeparator('E')); |
||
136 | $this->assertFalse(Context::isSeparator('_')); |
||
137 | } |
||
138 | } |
||
139 |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.