Test Failed
Push — main ( c76fcf...a2096a )
by Bingo
14:02
created

SimpleContextTest::testMethodInvocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 15
rs 9.9332
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests\Bpmn\Engine\Language;
4
5
use PHPUnit\Framework\TestCase;
6
use Jabe\Engine\Impl\Juel\{
7
    Builder,
8
    Feature,
9
    ExpressionFactoryImpl,
10
    SimpleContext,
11
    SimpleResolver,
12
    TreeStore,
13
    TreeMethodExpression,
14
    TreeValueExpression
15
};
16
use Jabe\Engine\Impl\Util\El\BeanELResolver;
17
18
class ExpressionLanguageTest extends TestCase
19
{
20
    public function testNumericVariables(): void
21
    {
22
        $factory = new ExpressionFactoryImpl();
23
        $context = new SimpleContext();
24
25
        $context->setVariable("e", $factory->createValueExpression(null, null, M_E, "double"));
26
        $context->setVariable("pi", $factory->createValueExpression(null, null, M_PI, "double"));
27
28
        $vmapper = $context->getVariableMapper();
29
30
        $this->assertEquals(M_E, $vmapper->resolveVariable("e")->getValue($context));
31
        $this->assertEquals(M_PI, $vmapper->resolveVariable("pi")->getValue($context));
32
33
        $expr = $factory->createValueExpression($context, '${e < pi}', null, "boolean");
34
        $this->assertTrue($expr->getValue($context));
35
36
        $expr = $factory->createValueExpression($context, '${e + 1}', null, "double");
37
        $this->assertEquals(M_E + 1, $expr->getValue($context));
38
39
        $expr = $factory->createValueExpression($context, '${pi}', null, "double");
40
        $this->assertEquals(M_PI, $expr->getValue($context));
41
42
        $context->setVariable("a", $factory->createValueExpression(null, null, 1, "integer"));
43
        $context->setVariable("b", $factory->createValueExpression(null, null, 2, "integer"));
44
        $expr = $factory->createValueExpression($context, '${a + b}', null, "integer");
45
        $this->assertEquals(3, $expr->getValue($context));
46
47
        $context->setVariable("c", $factory->createValueExpression(null, null, 3, "integer"));
48
        $expr = $factory->createValueExpression($context, '${a + b * c}', null, "integer");
49
        $this->assertEquals(7, $expr->getValue($context));
50
51
        $expr = $factory->createValueExpression($context, '${(a + b) * c}', null, "integer");
52
        $this->assertEquals(9, $expr->getValue($context));
53
    }
54
55
    public function testNumericMethods(): void
56
    {
57
        $factory = new ExpressionFactoryImpl();
58
        $context = new SimpleContext();
59
60
        $wrapper =  new \ReflectionClass(SimpleClass::class);
61
62
        $context->setVariable("e", $factory->createValueExpression(null, null, M_E, "double"));
63
        $context->setVariable("pi", $factory->createValueExpression(null, null, M_PI, "double"));
64
        $context->setVariable("arr", $factory->createValueExpression(null, null, [1, 2, 3], "array"));
65
66
        $context->setFunction("", "sin", $wrapper->getMethod("sin"));
67
        $context->setFunction("", "cos", $wrapper->getMethod("cos"));
68
        $context->setFunction("", "in_array", $wrapper->getMethod("inArray"));
69
70
        $fmapper = $context->getFunctionMapper();
71
72
        $this->assertEquals(1, $fmapper->resolveFunction("", "sin")->invoke(null, M_PI / 2));
73
        $this->assertEquals(0, $fmapper->resolveFunction("", "cos")->invoke(null, M_PI / 2));
74
75
        $expr = $factory->createValueExpression($context, '${sin(pi / 2) + cos(pi / 4) * sin(pi / 3)}', null, "double");
76
        $this->assertEquals(sin(M_PI / 2) + cos(M_PI / 4) * sin(M_PI / 3), $expr->getValue($context));
77
78
        $expr = $factory->createValueExpression($context, '${in_array(2, arr)}', null, "boolean");
79
        $this->assertTrue($expr->getValue($context));
80
81
        $expr = $factory->createValueExpression($context, '${in_array(5, arr)}', null, "boolean");
82
        $this->assertFalse($expr->getValue($context));
83
    }
84
85
    public function testBooleanMethods(): void
86
    {
87
        $factory = new ExpressionFactoryImpl();
88
        $context = new SimpleContext();
89
90
        $expr = $factory->createValueExpression($context, '${true}', null, "boolean");
91
        $this->assertTrue($expr->getValue($context));
92
93
        $expr = $factory->createValueExpression($context, '${false}', null, "boolean");
94
        $this->assertFalse($expr->getValue($context));
95
96
        $expr = $factory->createValueExpression($context, '${true or false}', null, "boolean");
97
        $this->assertTrue($expr->getValue($context));
98
99
        $expr = $factory->createValueExpression($context, '${true and false}', null, "boolean");
100
        $this->assertFalse($expr->getValue($context));
101
    }
102
103
    public function testMethodInvocation(): void
104
    {
105
        $context = new SimpleContext(new SimpleResolver(new BeanELResolver()));
106
        $store = new TreeStore(new Builder([Feature::METHOD_INVOCATIONS]), null);
107
108
        $simple = new SimpleClass();
109
        $factory = new ExpressionFactoryImpl();
0 ignored issues
show
Unused Code introduced by
The assignment to $factory is dead and can be removed.
Loading history...
110
111
        $context->getELResolver()->setValue($context, null, "base", $simple);
112
        $expr = new TreeMethodExpression($store, null, null, null, '${base.foo}', null);
113
        $this->assertEquals(1, $expr->invoke($context));
114
115
        $ser = serialize($expr);
116
        $des = unserialize($ser);
117
        $this->assertEquals(1, $des->invoke($context));
118
    }
119
120
    public function testExpressionString(): void
121
    {
122
        $store = new TreeStore(new Builder(), null);
123
        $this->assertEquals("foo", (new TreeValueExpression($store, null, null, null, "foo", "object"))->getExpressionString());
124
    }
125
126
    public function testIsDeferred(): void
127
    {
128
        $store = new TreeStore(new Builder(), null);
129
        $this->assertFalse((new TreeValueExpression($store, null, null, null, "foo", "object"))->isDeferred());
130
        $this->assertFalse((new TreeValueExpression($store, null, null, null, '${foo}', "object"))->isDeferred());
131
        $this->assertTrue((new TreeValueExpression($store, null, null, null, "#{foo}", "object"))->isDeferred());
132
    }
133
134
    public function testGetExpectedType(): void
135
    {
136
        $store = new TreeStore(new Builder(), null);
137
        $this->assertEquals("object", (new TreeValueExpression($store, null, null, null, '${foo}', "object"))->getExpectedType());
138
        $this->assertEquals("string", (new TreeValueExpression($store, null, null, null, '${foo}', "string"))->getExpectedType());
139
    }
140
141
    public function testGetType(): void
142
    {
143
        $store = new TreeStore(new Builder(), null);
144
        $context = new SimpleContext();
145
        $this->assertFalse((new TreeValueExpression($store, null, null, null, '${property_foo}', "object"))->isReadOnly($context));
146
    }
147
}
148