From b2502bd351e0ee3eec52b23340951b7f58a1e9ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Tue, 23 Jan 2024 14:44:56 -0300 Subject: [PATCH] Fix issues in test files found by SA tools MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- phpstan-baseline.neon | 161 +------------------ psalm-baseline.xml | 146 ++--------------- psalm.xml | 9 ++ src/Components/JoinKeyword.php | 16 +- src/Components/Key.php | 10 +- tests/Builder/CreateStatementTest.php | 16 +- tests/Components/ArrayObjTest.php | 5 +- tests/Components/CaseExpressionTest.php | 16 +- tests/Components/ConditionTest.php | 8 +- tests/Components/CreateDefinitionTest.php | 3 + tests/Components/ExpressionTest.php | 26 +-- tests/Components/GroupKeywordTest.php | 2 +- tests/Components/IntoKeywordTest.php | 6 +- tests/Components/JoinKeywordTest.php | 3 + tests/Components/LimitTest.php | 4 +- tests/Components/LockExpressionTest.php | 16 +- tests/Components/OptionsArrayTest.php | 11 +- tests/Components/OrderKeywordTest.php | 2 +- tests/Components/PartitionDefinitionTest.php | 3 + tests/Components/RenameOperationTest.php | 2 +- tests/Lexer/LexerTest.php | 2 +- tests/Lexer/TokenTest.php | 36 ++--- tests/Parser/LoadStatementTest.php | 1 + tests/Parser/ParserLongExportsTest.php | 1 + tests/Parser/ParserTest.php | 2 +- tests/Parser/SelectStatementTest.php | 1 + tests/Parser/WithStatementTest.php | 2 +- tests/TestCase.php | 10 +- tests/Utils/BufferedQueryTest.php | 3 +- tests/Utils/CLITest.php | 2 +- tests/Utils/FormatterTest.php | 1 - tests/Utils/QueryTest.php | 12 ++ tests/Utils/RoutineTest.php | 2 + tests/Utils/TableTest.php | 3 + tests/benchmarks/UtfStringBench.php | 5 +- 35 files changed, 164 insertions(+), 384 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index ca7a2f6f0..39e1dd6a6 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -20,41 +20,6 @@ parameters: count: 1 path: src/Components/GroupKeyword.php - - - message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\JoinKeyword\\:\\:\\$expr \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#" - count: 1 - path: src/Components/JoinKeyword.php - - - - message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\JoinKeyword\\:\\:\\$on \\(array\\\\) does not accept array\\\\|null\\.$#" - count: 1 - path: src/Components/JoinKeyword.php - - - - message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\JoinKeyword\\:\\:\\$type \\(string\\) does not accept string\\|null\\.$#" - count: 1 - path: src/Components/JoinKeyword.php - - - - message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\JoinKeyword\\:\\:\\$using \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\|null\\.$#" - count: 1 - path: src/Components/JoinKeyword.php - - - - message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\JoinKeyword\\:\\:\\$using \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\) in empty\\(\\) is not falsy\\.$#" - count: 1 - path: src/Components/JoinKeyword.php - - - - message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\Key\\:\\:\\$name \\(string\\) does not accept string\\|null\\.$#" - count: 1 - path: src/Components/Key.php - - - - message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\Key\\:\\:\\$type \\(string\\) does not accept string\\|null\\.$#" - count: 1 - path: src/Components/Key.php - - message: "#^Parameter \\#2 \\$string2 of function strcasecmp expects string, mixed given\\.$#" count: 2 @@ -296,12 +261,7 @@ parameters: path: src/Parsers/IntoKeywords.php - - message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\JoinKeyword\\:\\:\\$expr \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#" - count: 1 - path: src/Parsers/JoinKeywords.php - - - - message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\JoinKeyword\\:\\:\\$using \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\) does not accept array\\\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\.$#" + message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\JoinKeyword\\:\\:\\$using \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\|null\\) does not accept array\\\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\.$#" count: 1 path: src/Parsers/JoinKeywords.php @@ -311,12 +271,12 @@ parameters: path: src/Parsers/Keys.php - - message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\Key\\:\\:\\$name \\(string\\) does not accept mixed\\.$#" + message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\Key\\:\\:\\$name \\(string\\|null\\) does not accept mixed\\.$#" count: 1 path: src/Parsers/Keys.php - - message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\Key\\:\\:\\$type \\(string\\) does not accept mixed\\.$#" + message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\Key\\:\\:\\$type \\(string\\|null\\) does not accept mixed\\.$#" count: 1 path: src/Parsers/Keys.php @@ -930,118 +890,3 @@ parameters: count: 2 path: src/Utils/Table.php - - - message: "#^Cannot call method __toString\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#" - count: 2 - path: tests/Builder/CreateStatementTest.php - - - - message: "#^Cannot call method __toString\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#" - count: 4 - path: tests/Builder/CreateStatementTest.php - - - - message: "#^Cannot call method isEmpty\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#" - count: 4 - path: tests/Builder/CreateStatementTest.php - - - - message: "#^Cannot access offset 0 on array\\\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\.$#" - count: 1 - path: tests/Components/ArrayObjTest.php - - - - message: "#^Cannot access property \\$name on PhpMyAdmin\\\\SqlParser\\\\Components\\\\DataType\\|null\\.$#" - count: 1 - path: tests/Components/CreateDefinitionTest.php - - - - message: "#^Cannot access property \\$name on PhpMyAdmin\\\\SqlParser\\\\Components\\\\Key\\|null\\.$#" - count: 1 - path: tests/Components/CreateDefinitionTest.php - - - - message: "#^Cannot access property \\$type on PhpMyAdmin\\\\SqlParser\\\\Components\\\\Key\\|null\\.$#" - count: 1 - path: tests/Components/CreateDefinitionTest.php - - - - message: "#^Cannot call method has\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#" - count: 2 - path: tests/Components/CreateDefinitionTest.php - - - - message: "#^Cannot access property \\$alias on PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#" - count: 2 - path: tests/Components/ExpressionTest.php - - - - message: "#^Cannot access property \\$expr on PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#" - count: 4 - path: tests/Components/ExpressionTest.php - - - - message: "#^Cannot access property \\$table on PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#" - count: 2 - path: tests/Components/ExpressionTest.php - - - - message: "#^Cannot cast mixed to string\\.$#" - count: 1 - path: tests/Components/OptionsArrayTest.php - - - - message: "#^Cannot access property \\$expr on PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|string\\.$#" - count: 2 - path: tests/Components/PartitionDefinitionTest.php - - - - message: "#^Cannot call method has\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#" - count: 1 - path: tests/Parser/LoadStatementTest.php - - - - message: "#^Argument of an invalid type array\\\\|null supplied for foreach, only iterables are supported\\.$#" - count: 1 - path: tests/Parser/ParserLongExportsTest.php - - - - message: "#^Cannot call method has\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#" - count: 1 - path: tests/Parser/SelectStatementTest.php - - - - message: "#^Parameter \\#2 \\$list of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Query\\:\\:getClause\\(\\) expects PhpMyAdmin\\\\SqlParser\\\\TokensList, PhpMyAdmin\\\\SqlParser\\\\TokensList\\|null given\\.$#" - count: 9 - path: tests/Utils/QueryTest.php - - - - message: "#^Parameter \\#2 \\$list of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Query\\:\\:replaceClause\\(\\) expects PhpMyAdmin\\\\SqlParser\\\\TokensList, PhpMyAdmin\\\\SqlParser\\\\TokensList\\|null given\\.$#" - count: 4 - path: tests/Utils/QueryTest.php - - - - message: "#^Parameter \\#2 \\$list of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Query\\:\\:replaceClauses\\(\\) expects PhpMyAdmin\\\\SqlParser\\\\TokensList, PhpMyAdmin\\\\SqlParser\\\\TokensList\\|null given\\.$#" - count: 3 - path: tests/Utils/QueryTest.php - - - - message: "#^Parameter \\#1 \\$statement of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Routine\\:\\:getParameters\\(\\) expects PhpMyAdmin\\\\SqlParser\\\\Statements\\\\CreateStatement, PhpMyAdmin\\\\SqlParser\\\\Statement given\\.$#" - count: 1 - path: tests/Utils/RoutineTest.php - - - - message: "#^Parameter \\#1 \\$statement of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Table\\:\\:getFields\\(\\) expects PhpMyAdmin\\\\SqlParser\\\\Statements\\\\CreateStatement, PhpMyAdmin\\\\SqlParser\\\\Statement given\\.$#" - count: 1 - path: tests/Utils/TableTest.php - - - - message: "#^Parameter \\#1 \\$statement of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Table\\:\\:getForeignKeys\\(\\) expects PhpMyAdmin\\\\SqlParser\\\\Statements\\\\CreateStatement, PhpMyAdmin\\\\SqlParser\\\\Statement given\\.$#" - count: 1 - path: tests/Utils/TableTest.php - - - - message: "#^Expression \"\\$str1\\[\\$i\\]\" on a separate line does not do anything\\.$#" - count: 1 - path: tests/benchmarks/UtfStringBench.php - diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 06f2d44e8..7d66a7dc8 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -60,31 +60,25 @@ - - - type, self::JOINS)]]> - - $expr - $on - $type - $using - - - using)]]> - + + expr]]> + + + on)]]> + options]]> options]]> + type]]> - - $name - $type - + + name)]]> + @@ -612,9 +606,9 @@ ArrayObjs::parse($parser, $list) - - 'table'])]]> - + + type)]]> + keyword])]]> keyword])]]> @@ -1394,6 +1388,7 @@ has + function)]]> subquery)]]> group)]]> group)]]> @@ -1464,14 +1459,6 @@ - - __toString - __toString - isEmpty - isEmpty - isEmpty - isEmpty - partitionQueriesProvider @@ -1485,32 +1472,8 @@ parseProvider - - $components - $components - - - - - type->name]]> - key->name]]> - key->type]]> - - - has - - - alias]]> - alias]]> - expr]]> - expr]]> - expr]]> - expr]]> - table]]> - table]]> - mysqlCommandsProvider parseErrProvider @@ -1521,19 +1484,6 @@ provideExpressions - - - assertNull - assertNull - assertNull - - - - - assertNull - assertNull - - parseProvider @@ -1543,16 +1493,6 @@ parseErrProvider - - assertNotNull - assertNotNull - - - - - expr->expr]]> - expr->expr]]> - @@ -1622,9 +1562,6 @@ - - has - loadProvider @@ -1635,12 +1572,6 @@ - - $transactionStatement - - - statements]]> - exportFileProvider @@ -1671,9 +1602,6 @@ - - has - selectProvider @@ -1710,23 +1638,11 @@ * errors: array{lexer: list, parser: list} * }]]> - - getCode()]]> - getCode()]]> - - - - - TestContext - extractProvider - - $stmt - @@ -1744,36 +1660,14 @@ formatQueriesProviders mergeFormatsProvider - - setAccessible - - - list]]> - list]]> - list]]> - list]]> - list]]> - list]]> - list]]> - list]]> - list]]> - getFlagsProvider getTablesProvider - - $delimiter - $delimiter - $query - - - statements[0]]]> - getParameterProvider getParametersProvider @@ -1781,21 +1675,9 @@ - - statements[0]]]> - statements[0]]]> - getFieldsProvider getForeignKeysProvider - - - $testContents - - - UtfStringBench - - diff --git a/psalm.xml b/psalm.xml index 43bc9b670..9523abac1 100644 --- a/psalm.xml +++ b/psalm.xml @@ -25,4 +25,13 @@ + + + + + + + + + diff --git a/src/Components/JoinKeyword.php b/src/Components/JoinKeyword.php index 0e9cd10f2..094c0cfbf 100644 --- a/src/Components/JoinKeyword.php +++ b/src/Components/JoinKeyword.php @@ -40,38 +40,38 @@ final class JoinKeyword implements Component * * @see JoinKeyword::JOINS * - * @var string + * @var string|null */ public $type; /** * Join expression. * - * @var Expression + * @var Expression|null */ public $expr; /** * Join conditions. * - * @var Condition[] + * @var Condition[]|null */ public $on; /** * Columns in Using clause. * - * @var ArrayObj + * @var ArrayObj|null */ public $using; /** * @see JoinKeyword::JOINS * - * @param string $type Join type - * @param Expression $expr join expression - * @param Condition[] $on join conditions - * @param ArrayObj $using columns joined + * @param string|null $type Join type + * @param Expression|null $expr join expression + * @param Condition[]|null $on join conditions + * @param ArrayObj|null $using columns joined */ public function __construct( string|null $type = null, diff --git a/src/Components/Key.php b/src/Components/Key.php index 467090673..576b301b1 100644 --- a/src/Components/Key.php +++ b/src/Components/Key.php @@ -20,7 +20,7 @@ final class Key implements Component /** * The name of this key. * - * @var string + * @var string|null */ public $name; @@ -35,7 +35,7 @@ final class Key implements Component /** * The type of this key. * - * @var string + * @var string|null */ public $type; @@ -54,10 +54,10 @@ final class Key implements Component public $options; /** - * @param string $name the name of the key + * @param string|null $name the name of the key * @param array> $columns the columns covered by this key - * @param string $type the type of this key - * @param OptionsArray $options the options of this key + * @param string|null $type the type of this key + * @param OptionsArray|null $options the options of this key * @phpstan-param array{name?: string, length?: int, order?: string}[] $columns */ public function __construct( diff --git a/tests/Builder/CreateStatementTest.php b/tests/Builder/CreateStatementTest.php index b44d00103..ec84fc1e1 100644 --- a/tests/Builder/CreateStatementTest.php +++ b/tests/Builder/CreateStatementTest.php @@ -474,8 +474,8 @@ public function testBuilderCreateProcedure(): void . ' MODIFIES SQL DATA SELECT _var', ); - /** @var CreateStatement $stmt */ $stmt = $parser->statements[0]; + $this->assertInstanceOf(CreateStatement::class, $stmt); $this->assertSame( 'CREATE DEFINER=`root`@`%`' @@ -490,8 +490,8 @@ public function testBuilderCreateProcedure(): void . ' SQL SECURITY INVOKER NO SQL SQL SECURITY INVOKER SELECT _var', ); - /** @var CreateStatement $stmt */ $stmt = $parser->statements[0]; + $this->assertInstanceOf(CreateStatement::class, $stmt); $this->assertSame( 'CREATE DEFINER=`root`@`%`' @@ -500,13 +500,16 @@ public function testBuilderCreateProcedure(): void $stmt->build(), ); + $this->assertNotNull($stmt->entityOptions); $this->assertFalse($stmt->entityOptions->isEmpty()); + $this->assertNotNull($stmt->options); $this->assertFalse($stmt->options->isEmpty()); $this->assertSame( 'DEFINER=`root`@`%` PROCEDURE', $stmt->options->__toString(), ); + $this->assertNotNull($stmt->name); $this->assertSame( '`test2`', $stmt->name->__toString(), @@ -565,8 +568,8 @@ public function testBuilderCreateFunction(): void . 'END', ); - /** @var CreateStatement $stmt */ $stmt = $parser->statements[0]; + $this->assertInstanceOf(CreateStatement::class, $stmt); $this->assertSame( 'CREATE DEFINER=`root`@`localhost`' @@ -602,7 +605,9 @@ public function testBuilderCreateFunction(): void $stmt->build(), ); + $this->assertNotNull($stmt->entityOptions); $this->assertFalse($stmt->entityOptions->isEmpty()); + $this->assertNotNull($stmt->options); $this->assertFalse($stmt->options->isEmpty()); $this->assertSame( @@ -610,6 +615,7 @@ public function testBuilderCreateFunction(): void $stmt->options->__toString(), ); + $this->assertNotNull($stmt->name); $this->assertSame( '`inventory_in_stock`', $stmt->name->__toString(), @@ -737,8 +743,8 @@ public function testBuildCreateTableSortedIndex(): void SQL, ); - /** @var CreateStatement $stmt */ $stmt = $parser->statements[0]; + $this->assertInstanceOf(CreateStatement::class, $stmt); $tableBody = <<<'SQL' ( @@ -815,8 +821,8 @@ public function testBuildCreateTableComplexIndexes(): void ); // phpcs:enable - /** @var CreateStatement $stmt */ $stmt = $parser->statements[0]; + $this->assertInstanceOf(CreateStatement::class, $stmt); // phpcs:disable Generic.Files.LineLength.TooLong $tableBody = <<<'SQL' diff --git a/tests/Components/ArrayObjTest.php b/tests/Components/ArrayObjTest.php index 8e01869b2..fe12f3371 100644 --- a/tests/Components/ArrayObjTest.php +++ b/tests/Components/ArrayObjTest.php @@ -36,10 +36,11 @@ public function testParseType(): void 'typeOptions' => ['breakOnParentheses' => true], ], ); + $this->assertIsArray($components); $this->assertInstanceOf(Expression::class, $components[0]); $this->assertInstanceOf(Expression::class, $components[1]); - $this->assertEquals($components[0]->expr, '1 + 2'); - $this->assertEquals($components[1]->expr, '3 + 4'); + $this->assertEquals('1 + 2', $components[0]->expr); + $this->assertEquals('3 + 4', $components[1]->expr); } #[DataProvider('parseProvider')] diff --git a/tests/Components/CaseExpressionTest.php b/tests/Components/CaseExpressionTest.php index 62e14c202..809d54816 100644 --- a/tests/Components/CaseExpressionTest.php +++ b/tests/Components/CaseExpressionTest.php @@ -19,8 +19,8 @@ public function testParseBuild(): void $this->getTokensList($caseExprQuery), ); $this->assertEquals( - $component->build(), 'CASE 1 WHEN 1 THEN "Some" ELSE "Other" END', + $component->build(), ); } @@ -32,8 +32,8 @@ public function testParseBuild2(): void $this->getTokensList($caseExprQuery), ); $this->assertEquals( - $component->build(), 'CASE WHEN 1=1 THEN "India" ELSE "Other" END', + $component->build(), ); } @@ -46,8 +46,8 @@ public function testParseBuild3(): void $this->getTokensList($caseExprQuery), ); $this->assertEquals( - $component->build(), 'CASE 1 WHEN 1 THEN "Some" WHEN 2 THEN "SomeOther" ELSE "Other" END', + $component->build(), ); } @@ -60,8 +60,8 @@ public function testParseBuild4(): void $this->getTokensList($caseExprQuery), ); $this->assertEquals( - $component->build(), 'CASE 1 WHEN 1 THEN "Some" WHEN 2 THEN "SomeOther" END', + $component->build(), ); } @@ -74,8 +74,8 @@ public function testParseBuild5(): void $this->getTokensList($caseExprQuery), ); $this->assertEquals( - $component->build(), 'CASE WHEN 1=1 THEN "Some" WHEN 1=2 THEN "SomeOther" ELSE "Other" END', + $component->build(), ); } @@ -88,8 +88,8 @@ public function testParseBuild6(): void $this->getTokensList($caseExprQuery), ); $this->assertEquals( - $component->build(), 'CASE WHEN 1=1 THEN "Some" WHEN 1=2 THEN "SomeOther" END', + $component->build(), ); } @@ -102,8 +102,8 @@ public function testParseBuild7(): void $this->getTokensList($caseExprQuery), ); $this->assertEquals( - $component->build(), 'CASE WHEN 1=1 THEN "Some" WHEN 1=2 THEN "SomeOther" END AS `foo`', + $component->build(), ); } @@ -116,8 +116,8 @@ public function testParseBuild8(): void $this->getTokensList($caseExprQuery), ); $this->assertEquals( - $component->build(), 'CASE WHEN 1=1 THEN "Some" WHEN 1=2 THEN "SomeOther" END AS `foo`', + $component->build(), ); } diff --git a/tests/Components/ConditionTest.php b/tests/Components/ConditionTest.php index 1d305b2e2..042e8032a 100644 --- a/tests/Components/ConditionTest.php +++ b/tests/Components/ConditionTest.php @@ -13,7 +13,7 @@ class ConditionTest extends TestCase public function testParse(): void { $component = Conditions::parse(new Parser(), $this->getTokensList('/* id = */ id = 10')); - $this->assertEquals($component[0]->expr, 'id = 10'); + $this->assertEquals('id = 10', $component[0]->expr); } public function testParseBetween(): void @@ -22,8 +22,8 @@ public function testParseBetween(): void new Parser(), $this->getTokensList('(id BETWEEN 10 AND 20) OR (id BETWEEN 30 AND 40)'), ); - $this->assertEquals($component[0]->expr, '(id BETWEEN 10 AND 20)'); - $this->assertEquals($component[1]->expr, 'OR'); - $this->assertEquals($component[2]->expr, '(id BETWEEN 30 AND 40)'); + $this->assertEquals('(id BETWEEN 10 AND 20)', $component[0]->expr); + $this->assertEquals('OR', $component[1]->expr); + $this->assertEquals('(id BETWEEN 30 AND 40)', $component[2]->expr); } } diff --git a/tests/Components/CreateDefinitionTest.php b/tests/Components/CreateDefinitionTest.php index efa0d6771..b85db4a8b 100644 --- a/tests/Components/CreateDefinitionTest.php +++ b/tests/Components/CreateDefinitionTest.php @@ -18,6 +18,7 @@ public function testParse(): void $this->getTokensList('(str TEXT, FULLTEXT INDEX indx (str))'), ); $this->assertEquals('str', $component[0]->name); + $this->assertNotNull($component[1]->key); $this->assertEquals('FULLTEXT INDEX', $component[1]->key->type); $this->assertEquals('indx', $component[1]->key->name); $this->assertEquals('FULLTEXT INDEX `indx` (`str`)', (string) $component[1]); @@ -30,7 +31,9 @@ public function testParse2(): void $this->getTokensList('(str TEXT NOT NULL INVISIBLE)'), ); $this->assertEquals('str', $component[0]->name); + $this->assertNotNull($component[0]->type); $this->assertEquals('TEXT', $component[0]->type->name); + $this->assertNotNull($component[0]->options); $this->assertTrue($component[0]->options->has('INVISIBLE')); $this->assertTrue($component[0]->options->has('NOT NULL')); } diff --git a/tests/Components/ExpressionTest.php b/tests/Components/ExpressionTest.php index 28b7f31f2..91852b8f9 100644 --- a/tests/Components/ExpressionTest.php +++ b/tests/Components/ExpressionTest.php @@ -15,30 +15,36 @@ class ExpressionTest extends TestCase public function testParse(): void { $component = Expressions::parse(new Parser(), $this->getTokensList('IF(film_id > 0, film_id, film_id)')); - $this->assertEquals($component->expr, 'IF(film_id > 0, film_id, film_id)'); + $this->assertNotNull($component); + $this->assertEquals('IF(film_id > 0, film_id, film_id)', $component->expr); } public function testParse2(): void { $component = Expressions::parse(new Parser(), $this->getTokensList('col`test`')); - $this->assertEquals($component->expr, 'col'); + $this->assertNotNull($component); + $this->assertEquals('col', $component->expr); } public function testParse3(): void { $component = Expressions::parse(new Parser(), $this->getTokensList('col xx')); - $this->assertEquals($component->alias, 'xx'); + $this->assertNotNull($component); + $this->assertEquals('xx', $component->alias); $component = Expressions::parse(new Parser(), $this->getTokensList('col y')); - $this->assertEquals($component->alias, 'y'); + $this->assertNotNull($component); + $this->assertEquals('y', $component->alias); $component = Expressions::parse(new Parser(), $this->getTokensList('avg.col FROM (SELECT ev.col FROM ev)')); - $this->assertEquals($component->table, 'avg'); - $this->assertEquals($component->expr, 'avg.col'); + $this->assertNotNull($component); + $this->assertEquals('avg', $component->table); + $this->assertEquals('avg.col', $component->expr); $component = Expressions::parse(new Parser(), $this->getTokensList('x.id FROM (SELECT a.id FROM a) x')); - $this->assertEquals($component->table, 'x'); - $this->assertEquals($component->expr, 'x.id'); + $this->assertNotNull($component); + $this->assertEquals('x', $component->table); + $this->assertEquals('x.id', $component->expr); } #[DataProvider('parseErrProvider')] @@ -47,7 +53,7 @@ public function testParseErr(string $expr, string $error): void $parser = new Parser(); Expressions::parse($parser, $this->getTokensList($expr)); $errors = $this->getErrorsAsArray($parser); - $this->assertEquals($errors[0][0], $error); + $this->assertEquals($error, $errors[0][0]); } /** @return string[][] */ @@ -86,8 +92,8 @@ public function testBuildAll(): void new Expression('1 + 3', 'four'), ]; $this->assertEquals( - Expressions::buildAll($component), '1 + 2 AS `three`, 1 + 3 AS `four`', + Expressions::buildAll($component), ); } diff --git a/tests/Components/GroupKeywordTest.php b/tests/Components/GroupKeywordTest.php index 1e40dacdf..c072a7bc0 100644 --- a/tests/Components/GroupKeywordTest.php +++ b/tests/Components/GroupKeywordTest.php @@ -71,6 +71,6 @@ private static function makeComponentFrom(string $string): GroupKeyword /** @return array */ private static function makeComponentsFrom(string ...$string): array { - return array_map([self::class, 'makeComponentFrom'], $string); + return array_map(self::makeComponentFrom(...), $string); } } diff --git a/tests/Components/IntoKeywordTest.php b/tests/Components/IntoKeywordTest.php index f9e687cdb..d11f6687d 100644 --- a/tests/Components/IntoKeywordTest.php +++ b/tests/Components/IntoKeywordTest.php @@ -13,8 +13,8 @@ class IntoKeywordTest extends TestCase public function testParse(): void { $component = IntoKeywords::parse(new Parser(), $this->getTokensList('OUTFILE "/tmp/outfile.txt"')); - $this->assertEquals($component->type, 'OUTFILE'); - $this->assertEquals($component->dest, '/tmp/outfile.txt'); + $this->assertEquals('OUTFILE', $component->type); + $this->assertEquals('/tmp/outfile.txt', $component->dest); } public function testBuild(): void @@ -38,6 +38,6 @@ public function testBuildOutfile(): void public function testParseErr1(): void { $component = IntoKeywords::parse(new Parser(), $this->getTokensList('OUTFILE;')); - $this->assertEquals($component->type, 'OUTFILE'); + $this->assertEquals('OUTFILE', $component->type); } } diff --git a/tests/Components/JoinKeywordTest.php b/tests/Components/JoinKeywordTest.php index be64c2d96..07e3c459b 100644 --- a/tests/Components/JoinKeywordTest.php +++ b/tests/Components/JoinKeywordTest.php @@ -14,6 +14,7 @@ public function testParseIncomplete(): void { $component = JoinKeywords::parse(new Parser(), $this->getTokensList('JOIN a')); $this->assertCount(1, $component); + $this->assertNotNull($component[0]->expr); $this->assertEquals('a', $component[0]->expr->expr); $this->assertNull($component[0]->on); $this->assertNull($component[0]->using); @@ -23,8 +24,10 @@ public function testParseIncompleteUsing(): void { $component = JoinKeywords::parse(new Parser(), $this->getTokensList('JOIN table2 USING (id)')); $this->assertCount(1, $component); + $this->assertNotNull($component[0]->expr); $this->assertEquals('table2', $component[0]->expr->expr); $this->assertNull($component[0]->on); + $this->assertNotNull($component[0]->using); $this->assertEquals(['id'], $component[0]->using->values); } diff --git a/tests/Components/LimitTest.php b/tests/Components/LimitTest.php index 2d4d533c5..43a9a26eb 100644 --- a/tests/Components/LimitTest.php +++ b/tests/Components/LimitTest.php @@ -13,13 +13,13 @@ class LimitTest extends TestCase public function testBuildWithoutOffset(): void { $component = new Limit(1); - $this->assertEquals($component->build(), '0, 1'); + $this->assertEquals('0, 1', $component->build()); } public function testBuildWithOffset(): void { $component = new Limit(1, 2); - $this->assertEquals($component->build(), '2, 1'); + $this->assertEquals('2, 1', $component->build()); } #[DataProvider('parseProvider')] diff --git a/tests/Components/LockExpressionTest.php b/tests/Components/LockExpressionTest.php index 89135954f..3b0a48375 100644 --- a/tests/Components/LockExpressionTest.php +++ b/tests/Components/LockExpressionTest.php @@ -15,18 +15,16 @@ class LockExpressionTest extends TestCase public function testParse(): void { $component = LockExpressions::parse(new Parser(), $this->getTokensList('table1 AS t1 READ LOCAL')); - $this->assertNotNull($component->table); - $this->assertEquals($component->table->table, 'table1'); - $this->assertEquals($component->table->alias, 't1'); - $this->assertEquals($component->type, 'READ LOCAL'); + $this->assertEquals('table1', $component->table->table); + $this->assertEquals('t1', $component->table->alias); + $this->assertEquals('READ LOCAL', $component->type); } public function testParse2(): void { $component = LockExpressions::parse(new Parser(), $this->getTokensList('table1 LOW_PRIORITY WRITE')); - $this->assertNotNull($component->table); - $this->assertEquals($component->table->table, 'table1'); - $this->assertEquals($component->type, 'LOW_PRIORITY WRITE'); + $this->assertEquals('table1', $component->table->table); + $this->assertEquals('LOW_PRIORITY WRITE', $component->type); } #[DataProvider('parseErrProvider')] @@ -35,7 +33,7 @@ public function testParseErr(string $expr, string $error): void $parser = new Parser(); LockExpressions::parse($parser, $this->getTokensList($expr)); $errors = $this->getErrorsAsArray($parser); - $this->assertEquals($errors[0][0], $error); + $this->assertEquals($error, $errors[0][0]); } /** @return string[][] */ @@ -64,8 +62,8 @@ public function testBuildAll(): void LockExpressions::parse(new Parser(), $this->getTokensList('table2 LOW_PRIORITY WRITE')), ]; $this->assertEquals( - LockExpression::buildAll($component), 'table1 AS `t1` READ LOCAL, table2 LOW_PRIORITY WRITE', + LockExpression::buildAll($component), ); } } diff --git a/tests/Components/OptionsArrayTest.php b/tests/Components/OptionsArrayTest.php index b8f5a29dd..03e83aa5e 100644 --- a/tests/Components/OptionsArrayTest.php +++ b/tests/Components/OptionsArrayTest.php @@ -4,6 +4,7 @@ namespace PhpMyAdmin\SqlParser\Tests\Components; +use PhpMyAdmin\SqlParser\Components\Expression; use PhpMyAdmin\SqlParser\Components\OptionsArray; use PhpMyAdmin\SqlParser\Parser; use PhpMyAdmin\SqlParser\Parsers\OptionsArrays; @@ -57,7 +58,9 @@ public function testParseExpr(): void ], ], ); - $this->assertEquals('(3 + 5)', (string) $component->has('SUM', true)); + $sumValue = $component->has('SUM', true); + $this->assertInstanceOf(Expression::class, $sumValue); + $this->assertEquals('(3 + 5)', (string) $sumValue); $this->assertEquals('8', $component->has('RESULT')); } @@ -87,7 +90,7 @@ public function testRemove(): void $component = new OptionsArray(['a', 'b', 'c']); $this->assertTrue($component->remove('b')); $this->assertFalse($component->remove('d')); - $this->assertEquals($component->options, [0 => 'a', 2 => 'c']); + $this->assertEquals([0 => 'a', 2 => 'c'], $component->options); /* Assertion 2 */ $component = OptionsArrays::parse( @@ -111,7 +114,7 @@ public function testMerge(): void { $component = new OptionsArray(['a']); $component->merge(new OptionsArray(['b', 'c'])); - $this->assertEquals($component->options, ['a', 'b', 'c']); + $this->assertEquals(['a', 'b', 'c'], $component->options); } public function testBuild(): void @@ -128,8 +131,8 @@ public function testBuild(): void ], ); $this->assertEquals( - $component->build(), 'ALL SQL_CALC_FOUND_ROWS MAX_STATEMENT_TIME=42', + $component->build(), ); } } diff --git a/tests/Components/OrderKeywordTest.php b/tests/Components/OrderKeywordTest.php index 2ff4aa78c..ce58930e2 100644 --- a/tests/Components/OrderKeywordTest.php +++ b/tests/Components/OrderKeywordTest.php @@ -14,13 +14,13 @@ class OrderKeywordTest extends TestCase public function testBuildAll(): void { $this->assertEquals( + 'a ASC, b DESC', OrderKeywords::buildAll( [ new OrderKeyword(new Expression('a'), 'ASC'), new OrderKeyword(new Expression('b'), 'DESC'), ], ), - 'a ASC, b DESC', ); } } diff --git a/tests/Components/PartitionDefinitionTest.php b/tests/Components/PartitionDefinitionTest.php index 2763e8b79..7de302a0a 100644 --- a/tests/Components/PartitionDefinitionTest.php +++ b/tests/Components/PartitionDefinitionTest.php @@ -4,6 +4,7 @@ namespace PhpMyAdmin\SqlParser\Tests\Components; +use PhpMyAdmin\SqlParser\Components\Expression; use PhpMyAdmin\SqlParser\Parser; use PhpMyAdmin\SqlParser\Parsers\PartitionDefinitions; use PhpMyAdmin\SqlParser\Tests\TestCase; @@ -19,6 +20,7 @@ public function testParse(): void $this->assertFalse($component->isSubpartition); $this->assertEquals('p0', $component->name); $this->assertEquals('LESS THAN', $component->type); + $this->assertInstanceOf(Expression::class, $component->expr); $this->assertEquals('(1990)', $component->expr->expr); } @@ -31,6 +33,7 @@ public function testParseNameWithUnderscore(): void $this->assertFalse($component->isSubpartition); $this->assertEquals('2017_12', $component->name); $this->assertEquals('LESS THAN', $component->type); + $this->assertInstanceOf(Expression::class, $component->expr); $this->assertEquals('(\'2018-01-01 00:00:00\')', $component->expr->expr); } } diff --git a/tests/Components/RenameOperationTest.php b/tests/Components/RenameOperationTest.php index 721e3906e..35811c043 100644 --- a/tests/Components/RenameOperationTest.php +++ b/tests/Components/RenameOperationTest.php @@ -13,6 +13,6 @@ class RenameOperationTest extends TestCase public function testBuildAll(): void { $component = RenameOperations::parse(new Parser(), $this->getTokensList('a TO b, c TO d')); - $this->assertEquals(RenameOperations::buildAll($component), 'a TO b, c TO d'); + $this->assertEquals('a TO b, c TO d', RenameOperations::buildAll($component)); } } diff --git a/tests/Lexer/LexerTest.php b/tests/Lexer/LexerTest.php index 066e16e53..a1a40c8d8 100644 --- a/tests/Lexer/LexerTest.php +++ b/tests/Lexer/LexerTest.php @@ -30,11 +30,11 @@ public function testError(): void ); $this->assertEquals( - $lexer->errors, [ new LexerException('error #1', 'foo', 1, 2), new LexerException('error #2', 'bar', 3, 4), ], + $lexer->errors, ); } diff --git a/tests/Lexer/TokenTest.php b/tests/Lexer/TokenTest.php index 981314839..3e4170b04 100644 --- a/tests/Lexer/TokenTest.php +++ b/tests/Lexer/TokenTest.php @@ -13,16 +13,16 @@ class TokenTest extends TestCase public function testExtractKeyword(): void { $tok = new Token('SelecT', TokenType::Keyword, Token::FLAG_KEYWORD_RESERVED); - $this->assertEquals($tok->value, 'SELECT'); + $this->assertEquals('SELECT', $tok->value); $tok = new Token('aS', TokenType::Keyword, Token::FLAG_KEYWORD_RESERVED); - $this->assertEquals($tok->value, 'AS'); + $this->assertEquals('AS', $tok->value); } public function testExtractWhitespace(): void { $tok = new Token(" \t \r \n ", TokenType::Whitespace); - $this->assertEquals($tok->value, ' '); + $this->assertEquals(' ', $tok->value); } public function testExtractBool(): void @@ -37,57 +37,57 @@ public function testExtractBool(): void public function testExtractNumber(): void { $tok = new Token('--42', TokenType::Number, Token::FLAG_NUMBER_NEGATIVE); - $this->assertEquals($tok->value, 42); + $this->assertEquals(42, $tok->value); $tok = new Token('---42', TokenType::Number, Token::FLAG_NUMBER_NEGATIVE); - $this->assertEquals($tok->value, -42); + $this->assertEquals(-42, $tok->value); $tok = new Token('0xFE', TokenType::Number, Token::FLAG_NUMBER_HEX); - $this->assertEquals($tok->value, 0xFE); + $this->assertEquals(0xFE, $tok->value); $tok = new Token('-0xEF', TokenType::Number, Token::FLAG_NUMBER_NEGATIVE | Token::FLAG_NUMBER_HEX); - $this->assertEquals($tok->value, -0xEF); + $this->assertEquals(-0xEF, $tok->value); $tok = new Token('3.14', TokenType::Number, Token::FLAG_NUMBER_FLOAT); - $this->assertEquals($tok->value, 3.14); + $this->assertEquals(3.14, $tok->value); } public function testExtractString(): void { $tok = new Token('"foo bar "', TokenType::String); - $this->assertEquals($tok->value, 'foo bar '); + $this->assertEquals('foo bar ', $tok->value); $tok = new Token("' bar foo '", TokenType::String); - $this->assertEquals($tok->value, ' bar foo '); + $this->assertEquals(' bar foo ', $tok->value); $tok = new Token("'\''", TokenType::String); - $this->assertEquals($tok->value, '\''); + $this->assertEquals('\'', $tok->value); $tok = new Token('"\c\d\e\f\g\h\i\j\k\l\m\p\q\s\u\v\w\x\y\z"', TokenType::String); - $this->assertEquals($tok->value, 'cdefghijklmpqsuvwxyz'); + $this->assertEquals('cdefghijklmpqsuvwxyz', $tok->value); } public function testExtractSymbol(): void { $tok = new Token('@foo', TokenType::Symbol, Token::FLAG_SYMBOL_VARIABLE); - $this->assertEquals($tok->value, 'foo'); + $this->assertEquals('foo', $tok->value); $tok = new Token('`foo`', TokenType::Symbol, Token::FLAG_SYMBOL_BACKTICK); - $this->assertEquals($tok->value, 'foo'); + $this->assertEquals('foo', $tok->value); $tok = new Token('@`foo`', TokenType::Symbol, Token::FLAG_SYMBOL_VARIABLE); - $this->assertEquals($tok->value, 'foo'); + $this->assertEquals('foo', $tok->value); $tok = new Token(':foo', TokenType::Symbol, Token::FLAG_SYMBOL_PARAMETER); - $this->assertEquals($tok->value, 'foo'); + $this->assertEquals('foo', $tok->value); $tok = new Token('?', TokenType::Symbol, Token::FLAG_SYMBOL_PARAMETER); - $this->assertEquals($tok->value, '?'); + $this->assertEquals('?', $tok->value); } public function testInlineToken(): void { $token = new Token(" \r \n \t "); - $this->assertEquals($token->getInlineToken(), ' \r \n \t '); + $this->assertEquals(' \r \n \t ', $token->getInlineToken()); } } diff --git a/tests/Parser/LoadStatementTest.php b/tests/Parser/LoadStatementTest.php index 027e22c25..dc975fb78 100644 --- a/tests/Parser/LoadStatementTest.php +++ b/tests/Parser/LoadStatementTest.php @@ -15,6 +15,7 @@ public function testLoadOptions(): void $data = $this->getData('parser/parseLoad1'); $parser = new Parser($data['query']); $stmt = $parser->statements[0]; + $this->assertNotNull($stmt->options); $this->assertEquals(10, $stmt->options->has('CONCURRENT')); } diff --git a/tests/Parser/ParserLongExportsTest.php b/tests/Parser/ParserLongExportsTest.php index 046b66692..c463d3002 100644 --- a/tests/Parser/ParserLongExportsTest.php +++ b/tests/Parser/ParserLongExportsTest.php @@ -64,6 +64,7 @@ public function testParsephpMyAdminDump(): void $collectedSetStatements = []; foreach ($parser->statements as $statement) { if ($statement instanceof TransactionStatement) { + $this->assertNotNull($statement->statements); foreach ($statement->statements as $transactionStatement) { if (! $transactionStatement instanceof SetStatement) { continue; diff --git a/tests/Parser/ParserTest.php b/tests/Parser/ParserTest.php index cf0cbf344..5d7ade27f 100644 --- a/tests/Parser/ParserTest.php +++ b/tests/Parser/ParserTest.php @@ -61,11 +61,11 @@ public function testError(): void $parser->error(sprintf('%2$s #%1$d', 2, 'error'), new Token('bar'), 2); $this->assertEquals( - $parser->errors, [ new ParserException('error #1', new Token('foo'), 1), new ParserException('error #2', new Token('bar'), 2), ], + $parser->errors, ); } diff --git a/tests/Parser/SelectStatementTest.php b/tests/Parser/SelectStatementTest.php index 87754b73b..eccb35612 100644 --- a/tests/Parser/SelectStatementTest.php +++ b/tests/Parser/SelectStatementTest.php @@ -15,6 +15,7 @@ public function testSelectOptions(): void $data = $this->getData('parser/parseSelect'); $parser = new Parser($data['query']); $stmt = $parser->statements[0]; + $this->assertNotNull($stmt->options); $this->assertEquals(10, $stmt->options->has('MAX_STATEMENT_TIME')); } diff --git a/tests/Parser/WithStatementTest.php b/tests/Parser/WithStatementTest.php index 267e29625..12f32ab7d 100644 --- a/tests/Parser/WithStatementTest.php +++ b/tests/Parser/WithStatementTest.php @@ -127,7 +127,7 @@ public function testWithHasUnclosedParenthesis(): void $this->assertCount(0, $lexerErrors); $parser = new Parser($lexer->list); $parserErrors = $this->getErrorsAsArray($parser); - $this->assertEquals($parserErrors[0][0], 'A closing bracket was expected.'); + $this->assertEquals('A closing bracket was expected.', $parserErrors[0][0]); } public function testBuildBadWithKeyword(): void diff --git a/tests/TestCase.php b/tests/TestCase.php index 80226ab96..552e2b993 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -65,12 +65,12 @@ public function getErrorsAsArray(Lexer|Parser $obj): array if ($obj instanceof Lexer) { /** @var LexerException $err */ foreach ($obj->errors as $err) { - $ret[] = [$err->getMessage(), $err->ch, $err->pos, (int) $err->getCode()]; + $ret[] = [$err->getMessage(), $err->ch, $err->pos, $err->getCode()]; } - } elseif ($obj instanceof Parser) { + } else { /** @var ParserException $err */ foreach ($obj->errors as $err) { - $ret[] = [$err->getMessage(), $err->token, (int) $err->getCode()]; + $ret[] = [$err->getMessage(), $err->token, $err->getCode()]; } } @@ -112,8 +112,8 @@ public function getData(string $name): array $this->assertIsArray($data['errors']); $this->assertArrayHasKey('lexer', $data['errors']); $this->assertArrayHasKey('parser', $data['errors']); - $this->assertIsArray($data['errors']['lexer']); - $this->assertIsArray($data['errors']['parser']); + $this->assertIsList($data['errors']['lexer']); + $this->assertIsList($data['errors']['parser']); $data['query'] = file_get_contents('tests/data/' . $name . '.in'); $this->assertIsString($data['query']); diff --git a/tests/Utils/BufferedQueryTest.php b/tests/Utils/BufferedQueryTest.php index 022bb4b12..36f297dd8 100644 --- a/tests/Utils/BufferedQueryTest.php +++ b/tests/Utils/BufferedQueryTest.php @@ -9,6 +9,7 @@ use PHPUnit\Framework\Attributes\DataProvider; use function count; +use function is_string; use function str_split; class BufferedQueryTest extends TestCase @@ -44,7 +45,7 @@ public function testExtract( while ($i < $count) { $stmt = $bq->extract(); - if ($stmt) { + if (is_string($stmt) && $stmt !== '') { $statements[] = $stmt; } else { $bq->query .= $chunks[$i++]; diff --git a/tests/Utils/CLITest.php b/tests/Utils/CLITest.php index 25e2c9151..e4b754fa2 100644 --- a/tests/Utils/CLITest.php +++ b/tests/Utils/CLITest.php @@ -43,8 +43,8 @@ public function testGetopt(): void { $cli = new CLI(); $this->assertEquals( - $cli->getopt('', []), [], + $cli->getopt('', []), ); } diff --git a/tests/Utils/FormatterTest.php b/tests/Utils/FormatterTest.php index e3247bd92..557676fa0 100644 --- a/tests/Utils/FormatterTest.php +++ b/tests/Utils/FormatterTest.php @@ -54,7 +54,6 @@ public function testMergeFormats(array $default, array $overriding, array $expec ]; $reflectionMethod = new ReflectionMethod($formatter, 'getMergedOptions'); - $reflectionMethod->setAccessible(true); $this->assertEquals($expectedOptions, $reflectionMethod->invoke($formatter, $overridingOptions)); } diff --git a/tests/Utils/QueryTest.php b/tests/Utils/QueryTest.php index 3650a97df..3295e0fe7 100644 --- a/tests/Utils/QueryTest.php +++ b/tests/Utils/QueryTest.php @@ -474,6 +474,7 @@ public function testGetClause(): void 'LIMIT 0, 1 ' . 'INTO OUTFILE "/dev/null"', ); + $this->assertNotNull($parser->list); $this->assertEquals( '0, 1 INTO OUTFILE "/dev/null"', Query::getClause( @@ -511,6 +512,7 @@ public function testGetClause(): void 'ORDER BY id ASC ' . 'LIMIT 1', ); + $this->assertNotNull($parser->list); $this->assertEquals( 'number = "1DB" AND actionDate <= CURRENT_DATE()', Query::getClause( @@ -543,6 +545,7 @@ public function testGetClause(): void 'ORDER BY id ASC ' . 'LIMIT 1', ); + $this->assertNotNull($parser->list); $this->assertEquals( 'number = "1DB" AND actionDate <= CURRENT_DATE()', Query::getClause( @@ -572,6 +575,7 @@ public function testGetClause(): void public function testReplaceClause(): void { $parser = new Parser('SELECT *, (SELECT 1) FROM film LIMIT 0, 10;'); + $this->assertNotNull($parser->list); $this->assertEquals( 'SELECT *, (SELECT 1) FROM film WHERE film_id > 0 LIMIT 0, 10', Query::replaceClause( @@ -585,6 +589,7 @@ public function testReplaceClause(): void 'select supplier.city, supplier.id from supplier ' . 'union select customer.city, customer.id from customer', ); + $this->assertNotNull($parser->list); $this->assertEquals( 'select supplier.city, supplier.id from supplier ' . 'union select customer.city, customer.id from customer' @@ -600,6 +605,7 @@ public function testReplaceClause(): void public function testReplaceClauseOnlyKeyword(): void { $parser = new Parser('SELECT *, (SELECT 1) FROM film LIMIT 0, 10'); + $this->assertNotNull($parser->list); $this->assertEquals( ' SELECT SQL_CALC_FOUND_ROWS *, (SELECT 1) FROM film LIMIT 0, 10', Query::replaceClause( @@ -615,6 +621,7 @@ public function testReplaceClauseOnlyKeyword(): void public function testReplaceNonExistingPart(): void { $parser = new Parser('ALTER TABLE `sale_mast` OPTIMIZE PARTITION p3'); + $this->assertNotNull($parser->list); $this->assertEquals( ' ALTER TABLE `sale_mast` OPTIMIZE PARTITION p3', Query::replaceClause( @@ -629,6 +636,7 @@ public function testReplaceNonExistingPart(): void public function testReplaceClauses(): void { $parser = new Parser('SELECT *, (SELECT 1) FROM film LIMIT 0, 10;'); + $this->assertNotNull($parser->list); $this->assertSame('', Query::replaceClauses($parser->statements[0], $parser->list, [])); $this->assertEquals( 'SELECT *, (SELECT 1) FROM film WHERE film_id > 0 LIMIT 0, 10', @@ -652,6 +660,7 @@ public function testReplaceClauses(): void 'ORDER BY city_id ASC ' . 'LIMIT 0, 1 ', ); + $this->assertNotNull($parser->list); $this->assertEquals( 'SELECT c.city_id, c.country_id ' . 'INTO OUTFILE "/dev/null" ' . @@ -686,6 +695,7 @@ public function testGetFirstStatement(): void [$statement, $query, $delimiter] = Query::getFirstStatement($query, $delimiter); $this->assertNull($statement); $this->assertEquals('USE saki', $query); + $this->assertNull($delimiter); $query = 'USE sakila; ' . '/*test comment*/' . @@ -710,5 +720,7 @@ public function testGetFirstStatement(): void [$statement, $query, $delimiter] = Query::getFirstStatement($query, $delimiter); $this->assertEquals('SELECT * FROM actor WHERE last_name = "abc"$$', $statement); + $this->assertSame('', $query); + $this->assertSame('$$', $delimiter); } } diff --git a/tests/Utils/RoutineTest.php b/tests/Utils/RoutineTest.php index 80164b81b..f7f59ae73 100644 --- a/tests/Utils/RoutineTest.php +++ b/tests/Utils/RoutineTest.php @@ -5,6 +5,7 @@ namespace PhpMyAdmin\SqlParser\Tests\Utils; use PhpMyAdmin\SqlParser\Parser; +use PhpMyAdmin\SqlParser\Statements\CreateStatement; use PhpMyAdmin\SqlParser\Tests\TestCase; use PhpMyAdmin\SqlParser\Utils\Routine; use PHPUnit\Framework\Attributes\DataProvider; @@ -205,6 +206,7 @@ public static function getParameterProvider(): array public function testGetParameters(string $query, array $expected): void { $parser = new Parser($query); + $this->assertInstanceOf(CreateStatement::class, $parser->statements[0]); $this->assertEquals($expected, Routine::getParameters($parser->statements[0])); } diff --git a/tests/Utils/TableTest.php b/tests/Utils/TableTest.php index ac48e59e4..40f8303e2 100644 --- a/tests/Utils/TableTest.php +++ b/tests/Utils/TableTest.php @@ -5,6 +5,7 @@ namespace PhpMyAdmin\SqlParser\Tests\Utils; use PhpMyAdmin\SqlParser\Parser; +use PhpMyAdmin\SqlParser\Statements\CreateStatement; use PhpMyAdmin\SqlParser\Tests\TestCase; use PhpMyAdmin\SqlParser\Utils\Table; use PHPUnit\Framework\Attributes\DataProvider; @@ -27,6 +28,7 @@ class TableTest extends TestCase public function testGetForeignKeys(string $query, array $expected): void { $parser = new Parser($query); + $this->assertInstanceOf(CreateStatement::class, $parser->statements[0]); $this->assertEquals($expected, Table::getForeignKeys($parser->statements[0])); } @@ -151,6 +153,7 @@ public static function getForeignKeysProvider(): array public function testGetFields(string $query, array $expected): void { $parser = new Parser($query); + $this->assertInstanceOf(CreateStatement::class, $parser->statements[0]); $this->assertEquals($expected, Table::getFields($parser->statements[0])); } diff --git a/tests/benchmarks/UtfStringBench.php b/tests/benchmarks/UtfStringBench.php index f5af47647..1c3114e44 100644 --- a/tests/benchmarks/UtfStringBench.php +++ b/tests/benchmarks/UtfStringBench.php @@ -10,7 +10,7 @@ class UtfStringBench { - private string $testContents; + private string $testContents = ''; /** * @BeforeMethods("setUp") @@ -23,7 +23,8 @@ public function benchBuildUtfString(): void { $str1 = new UtfString($this->testContents); for ($i = 0; $i < $str1->length(); $i++) { - $str1[$i];// Make offset offsetGet work + // @phpstan-ignore-next-line + $str1[$i]; // Make offset offsetGet work } }