-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathNodeExpressionResolverTest.php
More file actions
108 lines (92 loc) · 4.06 KB
/
NodeExpressionResolverTest.php
File metadata and controls
108 lines (92 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
declare(strict_types=1);
namespace Go\ParserReflection\Resolver;
use PHPUnit\Framework\TestCase;
use PhpParser\Parser;
use PhpParser\ParserFactory;
class NodeExpressionResolverTest extends TestCase
{
protected Parser $parser;
protected function setUp(): void
{
$this->parser = (new ParserFactory)->createForHostVersion();
}
/**
* Testing passing PhpParser\Node\Expr as class for constant fetch
*
* We're already testing constant fetch with a explicit class name
* elsewhere.
*/
public function testResolveConstFetchFromExpressionAsClass(): void
{
$expressionNodeTree = $this->parser->parse("<?php ('\\\\Date' . 'Time')::ATOM;");
$expressionSolver = new NodeExpressionResolver(NULL);
$expressionSolver->process($expressionNodeTree[0]);
$this->assertSame(\DateTime::ATOM, $expressionSolver->getValue());
$this->assertTrue($expressionSolver->isConstant());
$this->assertSame('DateTime::ATOM', $expressionSolver->getConstantName());
}
/**
* Testing passing PhpParser\Node\Expr as class for constant fetch
*
* Evaluating a run-time value like a variable should throw an exception.
*/
public function testResolveConstFetchFromVariableAsClass(): void
{
$this->expectException(\Go\ParserReflection\ReflectionException::class);
$this->expectExceptionMessage('Could not find handler for the Go\ParserReflection\Resolver\NodeExpressionResolver::resolveExprVariable method');
$expressionNodeTree = $this->parser->parse("<?php \$someVariable::FOO;");
$expressionSolver = new NodeExpressionResolver(NULL);
$expressionSolver->process($expressionNodeTree[0]);
}
/**
* Testing passing non-expression as class for constant fetch
*
* Non-expressions should be invalid.
*/
public function testResolveConstFetchFromNonExprAsClass(): void
{
$this->expectException(\Go\ParserReflection\ReflectionException::class);
$this->expectExceptionMessage('Could not find handler for the Go\ParserReflection\Resolver\NodeExpressionResolver::resolveStmtIf method');
$expressionNodeTree = $this->parser->parse("<?php ClassNameToReplace::Bar;");
$notAnExpressionNodeTree = $this->parser->parse("<?php if (true) { \$baz = 3; }");
// This should never happen...
$expressionNodeTree[0]->expr->class = $notAnExpressionNodeTree[0];
$expressionSolver = new NodeExpressionResolver(NULL);
$expressionSolver->process($expressionNodeTree[0]);
}
/**
* Testing resolving new expression with a simple instantiation
*/
public function testResolveNewExpression(): void
{
$expressionNodeTree = $this->parser->parse("<?php new \\DateTime('2023-01-01');");
$expressionSolver = new NodeExpressionResolver(NULL);
$expressionSolver->process($expressionNodeTree[0]);
$value = $expressionSolver->getValue();
$this->assertInstanceOf(\DateTime::class, $value);
$this->assertSame('2023-01-01', $value->format('Y-m-d'));
}
/**
* Testing resolving new expression without constructor arguments
*/
public function testResolveNewExpressionWithoutArguments(): void
{
$expressionNodeTree = $this->parser->parse("<?php new \\stdClass();");
$expressionSolver = new NodeExpressionResolver(NULL);
$expressionSolver->process($expressionNodeTree[0]);
$value = $expressionSolver->getValue();
$this->assertInstanceOf(\stdClass::class, $value);
}
/**
* Testing resolving new expression with DateTimeImmutable as in the issue
*/
public function testResolveNewExpressionDateTimeImmutable(): void
{
$expressionNodeTree = $this->parser->parse("<?php new \\DateTimeImmutable('today');");
$expressionSolver = new NodeExpressionResolver(NULL);
$expressionSolver->process($expressionNodeTree[0]);
$value = $expressionSolver->getValue();
$this->assertInstanceOf(\DateTimeImmutable::class, $value);
}
}