annotate vendor/nikic/php-parser/test/PhpParser/JsonDecoderTest.php @ 13:5fb285c0d0e3
Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've
been lucky to get away with this so far, as we don't support self-registration
which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5
was vulnerable to.
author |
Chris Cannam |
date |
Mon, 23 Apr 2018 09:33:26 +0100 |
parents |
|
children |
129ea1e6d783 |
rev |
line source |
Chris@13
|
1 <?php declare(strict_types=1);
|
Chris@13
|
2
|
Chris@13
|
3 namespace PhpParser;
|
Chris@13
|
4
|
Chris@13
|
5 use PHPUnit\Framework\TestCase;
|
Chris@13
|
6
|
Chris@13
|
7 class JsonDecoderTest extends TestCase
|
Chris@13
|
8 {
|
Chris@13
|
9 public function testRoundTrip() {
|
Chris@13
|
10 $code = <<<'PHP'
|
Chris@13
|
11 <?php
|
Chris@13
|
12 // comment
|
Chris@13
|
13 /** doc comment */
|
Chris@13
|
14 function functionName(&$a = 0, $b = 1.0) {
|
Chris@13
|
15 echo 'Foo';
|
Chris@13
|
16 }
|
Chris@13
|
17 PHP;
|
Chris@13
|
18
|
Chris@13
|
19 $parser = new Parser\Php7(new Lexer());
|
Chris@13
|
20 $stmts = $parser->parse($code);
|
Chris@13
|
21 $json = json_encode($stmts);
|
Chris@13
|
22
|
Chris@13
|
23 $jsonDecoder = new JsonDecoder();
|
Chris@13
|
24 $decodedStmts = $jsonDecoder->decode($json);
|
Chris@13
|
25 $this->assertEquals($stmts, $decodedStmts);
|
Chris@13
|
26 }
|
Chris@13
|
27
|
Chris@13
|
28 /** @dataProvider provideTestDecodingError */
|
Chris@13
|
29 public function testDecodingError($json, $expectedMessage) {
|
Chris@13
|
30 $jsonDecoder = new JsonDecoder();
|
Chris@13
|
31 $this->expectException(\RuntimeException::class);
|
Chris@13
|
32 $this->expectExceptionMessage($expectedMessage);
|
Chris@13
|
33 $jsonDecoder->decode($json);
|
Chris@13
|
34 }
|
Chris@13
|
35
|
Chris@13
|
36 public function provideTestDecodingError() {
|
Chris@13
|
37 return [
|
Chris@13
|
38 ['???', 'JSON decoding error: Syntax error'],
|
Chris@13
|
39 ['{"nodeType":123}', 'Node type must be a string'],
|
Chris@13
|
40 ['{"nodeType":"Name","attributes":123}', 'Attributes must be an array'],
|
Chris@13
|
41 ['{"nodeType":"Comment"}', 'Comment must have text'],
|
Chris@13
|
42 ['{"nodeType":"xxx"}', 'Unknown node type "xxx"'],
|
Chris@13
|
43 ];
|
Chris@13
|
44 }
|
Chris@13
|
45 }
|