Chris@13
|
1 <?php declare(strict_types=1);
|
Chris@13
|
2
|
Chris@13
|
3 namespace PhpParser;
|
Chris@13
|
4
|
Chris@17
|
5 class JsonDecoderTest extends \PHPUnit\Framework\TestCase
|
Chris@13
|
6 {
|
Chris@13
|
7 public function testRoundTrip() {
|
Chris@13
|
8 $code = <<<'PHP'
|
Chris@13
|
9 <?php
|
Chris@13
|
10 // comment
|
Chris@13
|
11 /** doc comment */
|
Chris@13
|
12 function functionName(&$a = 0, $b = 1.0) {
|
Chris@13
|
13 echo 'Foo';
|
Chris@13
|
14 }
|
Chris@13
|
15 PHP;
|
Chris@13
|
16
|
Chris@13
|
17 $parser = new Parser\Php7(new Lexer());
|
Chris@13
|
18 $stmts = $parser->parse($code);
|
Chris@13
|
19 $json = json_encode($stmts);
|
Chris@13
|
20
|
Chris@13
|
21 $jsonDecoder = new JsonDecoder();
|
Chris@13
|
22 $decodedStmts = $jsonDecoder->decode($json);
|
Chris@13
|
23 $this->assertEquals($stmts, $decodedStmts);
|
Chris@13
|
24 }
|
Chris@13
|
25
|
Chris@13
|
26 /** @dataProvider provideTestDecodingError */
|
Chris@13
|
27 public function testDecodingError($json, $expectedMessage) {
|
Chris@13
|
28 $jsonDecoder = new JsonDecoder();
|
Chris@13
|
29 $this->expectException(\RuntimeException::class);
|
Chris@13
|
30 $this->expectExceptionMessage($expectedMessage);
|
Chris@13
|
31 $jsonDecoder->decode($json);
|
Chris@13
|
32 }
|
Chris@13
|
33
|
Chris@13
|
34 public function provideTestDecodingError() {
|
Chris@13
|
35 return [
|
Chris@13
|
36 ['???', 'JSON decoding error: Syntax error'],
|
Chris@13
|
37 ['{"nodeType":123}', 'Node type must be a string'],
|
Chris@13
|
38 ['{"nodeType":"Name","attributes":123}', 'Attributes must be an array'],
|
Chris@13
|
39 ['{"nodeType":"Comment"}', 'Comment must have text'],
|
Chris@13
|
40 ['{"nodeType":"xxx"}', 'Unknown node type "xxx"'],
|
Chris@13
|
41 ];
|
Chris@13
|
42 }
|
Chris@13
|
43 }
|