7cd56b5b5c83c65d5c1c85041ce8efaea419ef64
[yaffs-website] / php-parser / test / PhpParser / ConstExprEvaluatorTest.php
1 <?php declare(strict_types=1);
2
3 namespace PhpParser;
4
5 use PhpParser\Node\Expr;
6 use PhpParser\Node\Scalar;
7 use PHPUnit\Framework\TestCase;
8
9 class ConstExprEvaluatorTest extends TestCase
10 {
11     /** @dataProvider provideTestEvaluate */
12     public function testEvaluate($exprString, $expected) {
13         $parser = new Parser\Php7(new Lexer());
14         $expr = $parser->parse('<?php ' . $exprString . ';')[0]->expr;
15         $evaluator = new ConstExprEvaluator();
16         $this->assertSame($expected, $evaluator->evaluateDirectly($expr));
17     }
18
19     public function provideTestEvaluate() {
20         return [
21             ['1', 1],
22             ['1.0', 1.0],
23             ['"foo"', "foo"],
24             ['[0, 1]', [0, 1]],
25             ['["foo" => "bar"]', ["foo" => "bar"]],
26             ['NULL', null],
27             ['False', false],
28             ['true', true],
29             ['+1', 1],
30             ['-1', -1],
31             ['~0', -1],
32             ['!true', false],
33             ['[0][0]', 0],
34             ['"a"[0]', "a"],
35             ['true ? 1 : (1/0)', 1],
36             ['false ? (1/0) : 1', 1],
37             ['42 ?: (1/0)', 42],
38             ['false ?: 42', 42],
39             ['false ?? 42', false],
40             ['null ?? 42', 42],
41             ['[0][0] ?? 42', 0],
42             ['[][0] ?? 42', 42],
43             ['0b11 & 0b10', 0b10],
44             ['0b11 | 0b10', 0b11],
45             ['0b11 ^ 0b10', 0b01],
46             ['1 << 2', 4],
47             ['4 >> 2', 1],
48             ['"a" . "b"', "ab"],
49             ['4 + 2', 6],
50             ['4 - 2', 2],
51             ['4 * 2', 8],
52             ['4 / 2', 2],
53             ['4 % 2', 0],
54             ['4 ** 2', 16],
55             ['1 == 1.0', true],
56             ['1 != 1.0', false],
57             ['1 < 2.0', true],
58             ['1 <= 2.0', true],
59             ['1 > 2.0', false],
60             ['1 >= 2.0', false],
61             ['1 <=> 2.0', -1],
62             ['1 === 1.0', false],
63             ['1 !== 1.0', true],
64             ['true && true', true],
65             ['true and true', true],
66             ['false && (1/0)', false],
67             ['false and (1/0)', false],
68             ['false || false', false],
69             ['false or false', false],
70             ['true || (1/0)', true],
71             ['true or (1/0)', true],
72             ['true xor false', true],
73         ];
74     }
75
76     public function testEvaluateFails() {
77         $this->expectException(ConstExprEvaluationException::class);
78         $this->expectExceptionMessage('Expression of type Expr_Variable cannot be evaluated');
79         $evaluator = new ConstExprEvaluator();
80         $evaluator->evaluateDirectly(new Expr\Variable('a'));
81     }
82
83     public function testEvaluateFallback() {
84         $evaluator = new ConstExprEvaluator(function(Expr $expr) {
85             if ($expr instanceof Scalar\MagicConst\Line) {
86                 return 42;
87             }
88             throw new ConstExprEvaluationException();
89         });
90         $expr = new Expr\BinaryOp\Plus(
91             new Scalar\LNumber(8),
92             new Scalar\MagicConst\Line()
93         );
94         $this->assertSame(50, $evaluator->evaluateDirectly($expr));
95     }
96
97     /**
98      * @dataProvider provideTestEvaluateSilently
99      */
100     public function testEvaluateSilently($expr, $exception, $msg) {
101         $evaluator = new ConstExprEvaluator();
102
103         try {
104             $evaluator->evaluateSilently($expr);
105         } catch (ConstExprEvaluationException $e) {
106             $this->assertSame(
107                 'An error occurred during constant expression evaluation',
108                 $e->getMessage()
109             );
110
111             $prev = $e->getPrevious();
112             $this->assertInstanceOf($exception, $prev);
113             $this->assertSame($msg, $prev->getMessage());
114         }
115     }
116
117     public function provideTestEvaluateSilently() {
118         return [
119             [
120                 new Expr\BinaryOp\Mod(new Scalar\LNumber(42), new Scalar\LNumber(0)),
121                 \Error::class,
122                 'Modulo by zero'
123             ],
124             [
125                 new Expr\BinaryOp\Div(new Scalar\LNumber(42), new Scalar\LNumber(0)),
126                 \ErrorException::class,
127                 'Division by zero'
128             ],
129         ];
130     }
131 }