Completed
Pull Request — master (#304)
by Enrico
02:50
created

AbstractBinaryOp::testSecondArgUnexpectedType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 12
c 0
b 0
f 0
cc 1
eloc 8
nop 0
rs 9.4285
1
<?php
2
3
namespace Tests\PHPSA\Compiler\Expression;
4
5
use PhpParser\Node;
6
use PHPSA\CompiledExpression;
7
use PHPSA\Compiler\Expression;
8
9
abstract class AbstractBinaryOp extends \Tests\PHPSA\TestCase
10
{
11
    /**
12
     * @param $a
13
     * @param $b
14
     * @return Node\Expr
15
     */
16
    abstract protected function buildExpression($a, $b);
17
18
    /**
19
     * @param $a
20
     * @param $b
21
     * @return mixed
22
     */
23
    abstract protected function process($a, $b);
24
25
    /**
26
     * @return array
27
     */
28
    abstract protected function getSupportedTypes();
29
30
    /**
31
     * Data provider for tests
32
     */
33
    public $data = [
34
            CompiledExpression::INTEGER => 6,
35
            CompiledExpression::DOUBLE => 2.5,
36
            CompiledExpression::STRING => "test",
37
            CompiledExpression::BOOLEAN => true,
38
            CompiledExpression::NULL => null,
39
        ];
40
41
    /**
42
     * Tests {left-expr} $operator {right-expr}
43
     */
44
    public function testOperatorCompile()
45
    {
46
        foreach ($this->getSupportedTypes() as $type1) {
47
            foreach ($this->getSupportedTypes() as $type2) {
48
                $baseExpression = $this->buildExpression(
49
                    $this->newScalarExpr($this->data[$type1]),
50
                    $this->newScalarExpr($this->data[$type2])
51
                );
52
                $compiledExpression = $this->compileExpression($baseExpression);
53
54
                $this->assertInstanceOfCompiledExpression($compiledExpression);
55
                //$this->assertSame($this->getExpressionType($a, $b), $compiledExpression->getType());
56
                $this->assertSame($this->process($this->data[$type1], $this->data[$type2]), $compiledExpression->getValue());
57
            }
58
        }
59
    }
60
61
    /**
62
     * Tests {left-expr::UNKNOWN} $operator {right-expr}
63
     */
64
    public function testFirstArgUnexpectedType()
65
    {
66
        $baseExpression = $this->buildExpression(
67
            $this->newFakeScalarExpr(),
68
            $this->newScalarExpr(1)
69
        );
70
        $compiledExpression = $this->compileExpression($baseExpression);
71
72
        $this->assertInstanceOfCompiledExpression($compiledExpression);
73
        $this->assertSame(CompiledExpression::UNKNOWN, $compiledExpression->getType());
74
        $this->assertSame(null, $compiledExpression->getValue());
75
    }
76
77
    /**
78
     * Tests {left-expr} $operator {right-expr::UNKNOWN}
79
     */
80
    public function testSecondArgUnexpectedType()
81
    {
82
        $baseExpression = $this->buildExpression(
83
            $this->newScalarExpr(1),
84
            $this->newFakeScalarExpr()
85
        );
86
        $compiledExpression = $this->compileExpression($baseExpression);
87
88
        $this->assertInstanceOfCompiledExpression($compiledExpression);
89
        $this->assertSame(CompiledExpression::UNKNOWN, $compiledExpression->getType());
90
        $this->assertSame(null, $compiledExpression->getValue());
91
    }
92
}
93