Skip to content

Commit

Permalink
Use zumba/json-serializer for tests data output
Browse files Browse the repository at this point in the history
Ref: zumba/json-serializer#46 (for why the JSON pretty print hack is needed)

Signed-off-by: William Desportes <[email protected]>
  • Loading branch information
williamdes committed May 19, 2021
1 parent 34d5dd8 commit 9f16b24
Show file tree
Hide file tree
Showing 277 changed files with 222,570 additions and 1,758 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"phpstan/phpstan": "^0.12.87",
"phpstan/phpstan-phpunit": "^0.12.16",
"phpunit/php-code-coverage": "*",
"phpunit/phpunit": "^7.4 || ^8 || ^9"
"phpunit/phpunit": "^7.4 || ^8 || ^9",
"zumba/json-serializer": "^3.0"
},
"conflict": {
"phpmyadmin/motranslator": "<3.0"
Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2170,11 +2170,6 @@ parameters:
count: 1
path: tests/TestCase.php

-
message: "#^Parameter \\#1 \\$variable_representation of function unserialize expects string, string\\|false given\\.$#"
count: 1
path: tests/TestCase.php

-
message: "#^Cannot access offset int on array\\<int, string\\>\\|false\\.$#"
count: 1
Expand Down
17 changes: 14 additions & 3 deletions src/Tools/TestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@
use PhpMyAdmin\SqlParser\Context;
use PhpMyAdmin\SqlParser\Lexer;
use PhpMyAdmin\SqlParser\Parser;
use Zumba\JsonSerializer\JsonSerializer;

use function file_exists;
use function file_get_contents;
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;
use function scandir;
use function serialize;
use function sprintf;
use function strpos;
use function substr;

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

/**
* Used for test generation.
*/
Expand Down Expand Up @@ -148,9 +154,14 @@ 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 JsonSerializer();
// Writing test's data.
file_put_contents($output, serialize($test));
$encoded = $serializer->serialize($test);
$encoded = json_encode(
json_decode($encoded),
JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION
);
file_put_contents($output, $encoded);

// Dumping test's data in human readable format too (if required).
if (empty($debug)) {
Expand Down
10 changes: 3 additions & 7 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\TokensList;
use PHPUnit\Framework\TestCase as BaseTestCase;
use Zumba\JsonSerializer\JsonSerializer;

use function file_get_contents;
use function strpos;
use function unserialize;

/**
* Implements useful methods for testing.
Expand Down Expand Up @@ -87,12 +87,8 @@ public function getErrorsAsArray($obj)
*/
public function getData($name)
{
/*
* The unrestricted unserialize() is needed here as we do have
* serialized objects in the tests. There should be no security risk as
* the test data comes with the repository.
*/
$data = unserialize(file_get_contents('tests/data/' . $name . '.out'));
$serializer = new JsonSerializer();
$data = $serializer->unserialize((string) file_get_contents('tests/data/' . $name . '.out'));
$data['query'] = file_get_contents('tests/data/' . $name . '.in');

return $data;
Expand Down
934 changes: 927 additions & 7 deletions tests/data/bugs/gh14.out

Large diffs are not rendered by default.

2,854 changes: 2,823 additions & 31 deletions tests/data/bugs/gh16.out

Large diffs are not rendered by default.

Loading

0 comments on commit 9f16b24

Please sign in to comment.