Completed
Push1978d2...e2f699
passed — Build
created

ExpressionEvaluatorTest::testSetContextVariable()   A

↳ Parent: ExpressionEvaluatorTest

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 21
Code Lines 14

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 21
rs 9.3143
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
3
namespace Hateoas\Tests\Expression;
4
5
use Prophecy\Argument;
6
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
7
use Symfony\Component\ExpressionLanguage\Node\Node;
8
use Symfony\Component\ExpressionLanguage\ParsedExpression;
9
use Hateoas\Tests\TestCase;
10
use Hateoas\Expression\ExpressionEvaluator;
11
use Hateoas\Expression\ExpressionFunctionInterface;
12
13
class ExpressionEvaluatorTest extends TestCase
14
{
15
    public function testNullEvaluate()
16
    {
17
        $expressionLanguageProphecy = $this->prophesize('Symfony\Component\ExpressionLanguage\ExpressionLanguage');
18
        $expressionLanguageProphecy
19
            ->parse(Argument::any())
20
            ->shouldNotBeCalled()
21
        ;
22
        $expressionEvaluator = new ExpressionEvaluator($expressionLanguageProphecy->reveal());
23
24
        $this->assertSame('hello', $expressionEvaluator->evaluate('hello', null));
25
    }
26
27
    public function testEvaluate()
28
    {
29
        $data = new \StdClass();
30
31
        $expressionLanguageProphecy = $this->prophesize('Symfony\Component\ExpressionLanguage\ExpressionLanguage');
32
        $expressionLanguageProphecy
33
            ->parse('"42"', array('object'))
34
            ->willReturn($parsedExpression = new ParsedExpression('', new Node()))
35
        ;
36
        $expressionLanguageProphecy
37
            ->evaluate($parsedExpression, array('object' => $data))
38
            ->willReturn('42')
39
        ;
40
41
        $expressionEvaluator = new ExpressionEvaluator($expressionLanguageProphecy->reveal());
42
43
        $this->assertSame('42', $expressionEvaluator->evaluate('expr("42")', $data));
44
    }
45
46
    public function testEvaluateArray()
47
    {
48
        $parsedExpressions = array(
49
            new ParsedExpression('a', new Node()),
50
            new ParsedExpression('aa', new Node()),
51
            new ParsedExpression('aaa', new Node()),
52
        );
53
        $data = new \StdClass();
54
55
        $ELProphecy = $this->prophesize('Symfony\Component\ExpressionLanguage\ExpressionLanguage');
56
        $ELProphecy->parse('a', array('object'))->willReturn($parsedExpressions[0])->shouldBeCalledTimes(1);
57
        $ELProphecy->parse('aa', array('object'))->willReturn($parsedExpressions[1])->shouldBeCalledTimes(1);
58
        $ELProphecy->parse('aaa', array('object'))->willReturn($parsedExpressions[2])->shouldBeCalledTimes(1);
59
60
        $ELProphecy->evaluate($parsedExpressions[0], array('object' => $data))->willReturn(1);
61
        $ELProphecy->evaluate($parsedExpressions[1], array('object' => $data))->willReturn(2);
62
        $ELProphecy->evaluate($parsedExpressions[2], array('object' => $data))->willReturn(3);
63
64
        $expressionEvaluator = new ExpressionEvaluator($ELProphecy->reveal());
65
66
        $array = array(
67
            'expr(a)' => 'expr(aa)',
68
            'hello' => array('expr(aaa)'),
69
        );
70
71
        $this->assertSame(
72
            $expressionEvaluator->evaluateArray($array, $data),
73
            [
74
                1 => 2,
75
                'hello' => [3],
76
            ]
77
        );
78
    }
79
80
    public function testSetContextVariable()
81
    {
82
        $data = new \StdClass();
83
84
        $expressionLanguageProphecy = $this->prophesize('Symfony\Component\ExpressionLanguage\ExpressionLanguage');
85
        $expressionLanguageProphecy
86
            ->parse('name', array('name', 'object'))
87
            ->willReturn($parsedExpression = new ParsedExpression('', new Node()))
88
            ->shouldBeCalledTimes(1)
89
        ;
90
        $expressionLanguageProphecy
91
            ->evaluate($parsedExpression, array('object' => $data, 'name' => 'Adrien'))
92
            ->willReturn('Adrien')
93
            ->shouldBeCalledTimes(1)
94
        ;
95
96
        $expressionEvaluator = new ExpressionEvaluator($expressionLanguageProphecy->reveal());
97
        $expressionEvaluator->setContextVariable('name', 'Adrien');
98
99
        $this->assertSame('Adrien', $expressionEvaluator->evaluate('expr(name)', $data));
100
    }
101
102
    public function testRegisterFunction()
103
    {
104
        $expressionEvaluator = new ExpressionEvaluator(new ExpressionLanguage());
105
        $expressionEvaluator->registerFunction(new HelloExpressionFunction());
106
107
        $this->assertSame('Hello, toto!', $expressionEvaluator->evaluate('expr(hello("toto"))', null));
108
    }
109
110
    /**
111
     * @dataProvider getTestEvaluateNonStringData
112
     */
113
    public function testEvaluateNonString($value)
114
    {
115
        $expressionEvaluator = new ExpressionEvaluator(new ExpressionLanguage());
116
117
        $this->assertSame($value, $expressionEvaluator->evaluate($value, array()));
118
    }
119
120
    public function getTestEvaluateNonStringData()
121
    {
122
        return array(
123
            array(true),
124
            array(1.0),
125
            array(new \StdClass),
126
            array(array('foo' => 'bar')),
127
        );
128
    }
129
}
130
131
class HelloExpressionFunction implements ExpressionFunctionInterface
0 ignored issues
show
Coding Style Compatibility introduced by William DURAND
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
132
{
133
    public function getName()
134
    {
135
        return 'hello';
136
    }
137
138
    public function getCompiler()
139
    {
140
        return function ($value) {
141
            return sprintf('$hello_helper->hello(%s)', $value);
142
        };
143
    }
144
145
    public function getEvaluator()
146
    {
147
        return function (array $context, $value) {
148
            return $context['hello_helper']->hello($value);
149
        };
150
    }
151
152
    public function getContextVariables()
153
    {
154
        return array('hello_helper' => $this);
155
    }
156
157
    public function hello($name)
158
    {
159
        return sprintf('Hello, %s!', $name);
160
    }
161
}
162