Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / psy / psysh / test / ParserTestCase.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy\Test;
13
14 use PhpParser\PrettyPrinter\Standard as Printer;
15 use Psy\Exception\ParseErrorException;
16 use Psy\ParserFactory;
17
18 class ParserTestCase extends \PHPUnit\Framework\TestCase
19 {
20     protected $traverser;
21     private $parser;
22     private $printer;
23
24     public function tearDown()
25     {
26         $this->traverser = null;
27         $this->parser = null;
28         $this->printer = null;
29     }
30
31     protected function parse($code, $prefix = '<?php ')
32     {
33         $code = $prefix . $code;
34         try {
35             return $this->getParser()->parse($code);
36         } catch (\PhpParser\Error $e) {
37             if (!$this->parseErrorIsEOF($e)) {
38                 throw ParseErrorException::fromParseError($e);
39             }
40
41             try {
42                 // Unexpected EOF, try again with an implicit semicolon
43                 return $this->getParser()->parse($code . ';');
44             } catch (\PhpParser\Error $e) {
45                 return false;
46             }
47         }
48     }
49
50     protected function traverse(array $stmts)
51     {
52         if (!isset($this->traverser)) {
53             throw new \RuntimeException('Test cases must provide a traverser');
54         }
55
56         return $this->traverser->traverse($stmts);
57     }
58
59     protected function prettyPrint(array $stmts)
60     {
61         return $this->getPrinter()->prettyPrint($stmts);
62     }
63
64     protected function assertProcessesAs($from, $to)
65     {
66         $stmts = $this->parse($from);
67         $stmts = $this->traverse($stmts);
68         $toStmts = $this->parse($to);
69         $this->assertSame($this->prettyPrint($toStmts), $this->prettyPrint($stmts));
70     }
71
72     private function getParser()
73     {
74         if (!isset($this->parser)) {
75             $parserFactory = new ParserFactory();
76             $this->parser  = $parserFactory->createParser();
77         }
78
79         return $this->parser;
80     }
81
82     private function getPrinter()
83     {
84         if (!isset($this->printer)) {
85             $this->printer = new Printer();
86         }
87
88         return $this->printer;
89     }
90
91     private function parseErrorIsEOF(\PhpParser\Error $e)
92     {
93         $msg = $e->getRawMessage();
94
95         return ($msg === 'Unexpected token EOF') || (\strpos($msg, 'Syntax error, unexpected EOF') !== false);
96     }
97 }