|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\PHPSA\Compiler\Expression\Operators\Comparison; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\Node; |
|
6
|
|
|
use PHPSA\CompiledExpression; |
|
7
|
|
|
use PHPSA\Compiler\Expression; |
|
8
|
|
|
|
|
9
|
|
|
abstract class BaseTestCase extends \Tests\PHPSA\TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @return array |
|
13
|
|
|
*/ |
|
14
|
|
|
public function smallerDataProvider() |
|
15
|
|
|
{ |
|
16
|
|
|
return array( |
|
17
|
|
|
array(-1, -1), |
|
18
|
|
|
array(-2, -1), |
|
19
|
|
|
array(-3, -1), |
|
20
|
|
|
array(-50, -1), |
|
21
|
|
|
array(1, 2), |
|
22
|
|
|
array(1, 5), |
|
23
|
|
|
array(6, 5), |
|
24
|
|
|
array(6, 6), |
|
25
|
|
|
array(5, 6), |
|
26
|
|
|
array(5, 5), |
|
27
|
|
|
); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param $a |
|
32
|
|
|
* @param $b |
|
33
|
|
|
* @return mixed |
|
34
|
|
|
*/ |
|
35
|
|
|
abstract protected function operator($a, $b); |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param \PhpParser\Node\Scalar $a |
|
39
|
|
|
* @param \PhpParser\Node\Scalar $b |
|
40
|
|
|
* @return \PhpParser\Node\Expr\BinaryOp |
|
41
|
|
|
*/ |
|
42
|
|
|
abstract protected function buildExpression($a, $b); |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Tests {int} $operator {int} = {int} |
|
46
|
|
|
* |
|
47
|
|
|
* @dataProvider smallerDataProvider |
|
48
|
|
|
*/ |
|
49
|
|
|
public function testSimpleSuccessCompile($a, $b) |
|
50
|
|
|
{ |
|
51
|
|
|
$baseExpression = $this->buildExpression( |
|
52
|
|
|
$this->newScalarExpr($a), |
|
|
|
|
|
|
53
|
|
|
$this->newScalarExpr($b) |
|
|
|
|
|
|
54
|
|
|
); |
|
55
|
|
|
$compiledExpression = $this->compileExpression($baseExpression); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertInstanceOfCompiledExpression($compiledExpression); |
|
58
|
|
|
$this->assertSame(CompiledExpression::BOOLEAN, $compiledExpression->getType()); |
|
59
|
|
|
$this->assertSame($this->operator($a, $b), $compiledExpression->getValue()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testFirstUnexpectedTypes() |
|
63
|
|
|
{ |
|
64
|
|
|
$baseExpression = $this->buildExpression( |
|
65
|
|
|
$this->newFakeScalarExpr(), |
|
66
|
|
|
$this->newScalarExpr(1) |
|
|
|
|
|
|
67
|
|
|
); |
|
68
|
|
|
$compiledExpression = $this->compileExpression($baseExpression); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertInstanceOfCompiledExpression($compiledExpression); |
|
71
|
|
|
$this->assertSame(CompiledExpression::UNKNOWN, $compiledExpression->getType()); |
|
72
|
|
|
$this->assertSame(null, $compiledExpression->getValue()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testSecondUnexpectedTypes() |
|
76
|
|
|
{ |
|
77
|
|
|
$baseExpression = $this->buildExpression( |
|
78
|
|
|
$this->newScalarExpr(1), |
|
|
|
|
|
|
79
|
|
|
$this->newFakeScalarExpr() |
|
80
|
|
|
); |
|
81
|
|
|
$compiledExpression = $this->compileExpression($baseExpression); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertInstanceOfCompiledExpression($compiledExpression); |
|
84
|
|
|
$this->assertSame(CompiledExpression::UNKNOWN, $compiledExpression->getType()); |
|
85
|
|
|
$this->assertSame(null, $compiledExpression->getValue()); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.