Skip to content

Remove dependency on zumba/json-serializer #598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"symfony/polyfill-php80": "^1.16"
},
"require-dev": {
"ext-json": "*",
"phpbench/phpbench": "^1.1",
"phpmyadmin/coding-standard": "^3.0",
"phpmyadmin/motranslator": "^4.0 || ^5.0",
Expand All @@ -35,8 +36,7 @@
"phpstan/phpstan-phpunit": "^1.3.3",
"phpunit/phpunit": "^8.5 || ^9.6",
"psalm/plugin-phpunit": "^0.16.1",
"vimeo/psalm": "^4.11",
"zumba/json-serializer": "~3.0.2"
"vimeo/psalm": "^4.11"
},
"conflict": {
"phpmyadmin/motranslator": "<3.0"
Expand Down
15 changes: 0 additions & 15 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ parameters:
count: 1
path: src/Components/AlterOperation.php

-
message: "#^array\\<PhpMyAdmin\\\\SqlParser\\\\Token\\>\\|string does not accept PhpMyAdmin\\\\SqlParser\\\\Token\\.$#"
count: 3
path: src/Components/AlterOperation.php

-
message: "#^Cannot access property \\$values on array\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\.$#"
count: 1
Expand Down Expand Up @@ -635,11 +630,6 @@ parameters:
count: 1
path: src/Statements/CreateStatement.php

-
message: "#^array\\<PhpMyAdmin\\\\SqlParser\\\\Token\\>\\|string does not accept PhpMyAdmin\\\\SqlParser\\\\Token\\.$#"
count: 4
path: src/Statements/CreateStatement.php

-
message: "#^Argument of an invalid type array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\>\\|null supplied for foreach, only iterables are supported\\.$#"
count: 1
Expand Down Expand Up @@ -815,11 +805,6 @@ parameters:
count: 1
path: src/Tools/ContextGenerator.php

-
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Tools\\\\CustomJsonSerializer\\:\\:extractObjectData\\(\\) has parameter \\$ref with generic class ReflectionClass but does not specify its types\\: T$#"
count: 1
path: src/Tools/CustomJsonSerializer.php

-
message: "#^Argument of an invalid type array\\<int, string\\>\\|false supplied for foreach, only iterables are supported\\.$#"
count: 1
Expand Down
18 changes: 0 additions & 18 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1171,18 +1171,6 @@
<code>is_array($list)</code>
</RedundantConditionGivenDocblockType>
</file>
<file src="src/Tools/CustomJsonSerializer.php">
<MixedAssignment occurrences="2">
<code>$data[$property]</code>
<code>$data[$property]</code>
</MixedAssignment>
<MoreSpecificImplementedParamType occurrences="1">
<code>$properties</code>
</MoreSpecificImplementedParamType>
<PropertyNotSetInConstructor occurrences="1">
<code>CustomJsonSerializer</code>
</PropertyNotSetInConstructor>
</file>
<file src="src/Tools/TestGenerator.php">
<MixedOperand occurrences="1">
<code>$debug</code>
Expand Down Expand Up @@ -1625,12 +1613,6 @@
<code>new stdClass()</code>
</InvalidArgument>
</file>
<file src="tests/TestCase.php">
<LessSpecificReturnStatement occurrences="1">
<code>$data</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType occurrences="1"/>
</file>
<file src="tests/Utils/MiscTest.php">
<ArgumentTypeCoercion occurrences="1">
<code>$statement</code>
Expand Down
76 changes: 0 additions & 76 deletions src/Tools/CustomJsonSerializer.php

This file was deleted.

6 changes: 1 addition & 5 deletions src/Tools/TestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use function file_put_contents;
use function in_array;
use function is_dir;
use function json_decode;
use function json_encode;
use function mkdir;
use function print_r;
Expand Down Expand Up @@ -171,12 +170,9 @@ public static function build($type, $input, $output, $debug = null, $ansi = fals

// unset mode, reset to default every time, to be sure
Context::setMode();
$serializer = new CustomJsonSerializer();
// Writing test's data.
$encoded = $serializer->serialize($test);

$encoded = (string) json_encode(
json_decode($encoded),
$test,
JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION | JSON_UNESCAPED_SLASHES
);

Expand Down
7 changes: 5 additions & 2 deletions tests/Parser/LoadStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Tests\TestCase;

use function file_get_contents;

class LoadStatementTest extends TestCase
{
public function testLoadOptions(): void
{
$data = $this->getData('parser/parseLoad1');
$parser = new Parser($data['query']);
$sql = file_get_contents('tests/data/parser/parseLoad1.in');
self::assertIsString($sql);
$parser = new Parser($sql);
$stmt = $parser->statements[0];
$this->assertEquals(10, $stmt->options->has('CONCURRENT'));
}
Expand Down
7 changes: 5 additions & 2 deletions tests/Parser/ParserLongExportsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use PhpMyAdmin\SqlParser\Statements\TransactionStatement;
use PhpMyAdmin\SqlParser\Tests\TestCase;

use function file_get_contents;

class ParserLongExportsTest extends TestCase
{
public function testMysqldump(): void
Expand Down Expand Up @@ -58,8 +60,9 @@ public function testMysqldump(): void

public function testParsephpMyAdminDump(): void
{
$data = $this->getData('parser/parsephpMyAdminExport1');
$parser = new Parser($data['query']);
$sql = file_get_contents('tests/data/parser/parsephpMyAdminExport1.in');
self::assertIsString($sql);
$parser = new Parser($sql);
$collectedSetStatements = [];
foreach ($parser->statements as $statement) {
if ($statement instanceof TransactionStatement) {
Expand Down
7 changes: 5 additions & 2 deletions tests/Parser/SelectStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Tests\TestCase;

use function file_get_contents;

class SelectStatementTest extends TestCase
{
public function testSelectOptions(): void
{
$data = $this->getData('parser/parseSelect');
$parser = new Parser($data['query']);
$sql = file_get_contents('tests/data/parser/parseSelect.in');
self::assertIsString($sql);
$parser = new Parser($sql);
$stmt = $parser->statements[0];
$this->assertEquals(10, $stmt->options->has('MAX_STATEMENT_TIME'));
}
Expand Down
7 changes: 5 additions & 2 deletions tests/Parser/TransactionStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Tests\TestCase;

use function file_get_contents;

class TransactionStatementTest extends TestCase
{
public function testBuildWithoutEnd(): void
{
$data = $this->getData('parser/parseTransaction4');
$parser = new Parser($data['query']);
$sql = file_get_contents('tests/data/parser/parseTransaction4.in');
self::assertIsString($sql);
$parser = new Parser($sql);
$stmt = $parser->statements[0];
$this->assertEquals(
'START TRANSACTION;SET time_zone = "+00:00";',
Expand Down
81 changes: 23 additions & 58 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\Tools\CustomJsonSerializer;
use PHPUnit\Framework\TestCase as BaseTestCase;

use function file_get_contents;
use function json_encode;
use function str_contains;
use function strpos;
use function substr;

use const JSON_PRESERVE_ZERO_FRACTION;
use const JSON_PRETTY_PRINT;
use const JSON_UNESCAPED_SLASHES;
use const JSON_UNESCAPED_UNICODE;

/**
* Implements useful methods for testing.
*/
Expand Down Expand Up @@ -78,61 +83,15 @@ public function getErrorsAsArray($obj): array
return $ret;
}

/**
* Gets test's input and expected output.
*
* @param string $name the name of the test
*
* @return array<string, string|Lexer|Parser|array<string, array<int, int|string|Token>[]>|null>
* @psalm-return array{
* query: string,
* lexer: Lexer,
* parser: Parser|null,
* errors: array{lexer: list<array{string, string, int, int}>, parser: list<array{string, Token, int}>}
* }
*/
public function getData(string $name): array
{
$serializedData = file_get_contents('tests/data/' . $name . '.out');
$this->assertIsString($serializedData);

$serializer = new CustomJsonSerializer();
$data = $serializer->unserialize($serializedData);

$this->assertIsArray($data);
$this->assertArrayHasKey('query', $data);
$this->assertArrayHasKey('lexer', $data);
$this->assertArrayHasKey('parser', $data);
$this->assertArrayHasKey('errors', $data);
$this->assertIsString($data['query']);
$this->assertInstanceOf(Lexer::class, $data['lexer']);
if ($data['parser'] !== null) {
$this->assertInstanceOf(Parser::class, $data['parser']);
}

$this->assertIsArray($data['errors']);
$this->assertArrayHasKey('lexer', $data['errors']);
$this->assertArrayHasKey('parser', $data['errors']);
$this->assertIsArray($data['errors']['lexer']);
$this->assertIsArray($data['errors']['parser']);

$data['query'] = file_get_contents('tests/data/' . $name . '.in');
$this->assertIsString($data['query']);

return $data;
}

/**
* Runs a test.
*
* @param string $name the name of the test
*/
public function runParserTest(string $name): void
{
/**
* Test's data.
*/
$data = $this->getData($name);
$sql = file_get_contents('tests/data/' . $name . '.in');
self::assertIsString($sql);

if (str_contains($name, '/ansi/')) {
// set mode if appropriate
Expand All @@ -144,28 +103,34 @@ public function runParserTest(string $name): void
// set context
$mariaDbVersion = (int) substr($name, $mariaDbPos + 9, 6);
Context::load('MariaDb' . $mariaDbVersion);
} else {
Context::load('');
}

// Lexer.
$lexer = new Lexer($data['query']);
$lexer = new Lexer($sql);
$lexerErrors = $this->getErrorsAsArray($lexer);
$lexer->errors = [];

// Parser.
$parser = empty($data['parser']) ? null : new Parser($lexer->list);
$parser = str_contains($name, 'lex') ? null : new Parser($lexer->list);
$parserErrors = [];
if ($parser !== null) {
$parserErrors = $this->getErrorsAsArray($parser);
$parser->errors = [];
}

// Testing objects.
$this->assertEquals($data['lexer'], $lexer);
$this->assertEquals($data['parser'], $parser);

// Testing errors.
$this->assertEquals($data['errors']['parser'], $parserErrors);
$this->assertEquals($data['errors']['lexer'], $lexerErrors);
$encoded = (string) json_encode(
[
'query' => $sql,
'lexer' => $lexer,
'parser' => $parser,
'errors' => ['lexer' => $lexerErrors, 'parser' => $parserErrors],
],
JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION | JSON_UNESCAPED_SLASHES
);

self::assertJsonStringEqualsJsonFile('tests/data/' . $name . '.out', $encoded);

// reset mode after test run
Context::setMode();
Expand Down
Loading
Loading