1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright 2011 Johannes M. Schmitt <[email protected]> |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace JMS\SerializerBundle\Tests\ExpressionLanguage; |
20
|
|
|
|
21
|
|
|
use JMS\SerializerBundle\ExpressionLanguage\BasicSerializerFunctionsProvider; |
22
|
|
|
use PHPUnit\Framework\TestCase; |
23
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
24
|
|
|
|
25
|
|
|
class ExpressionLanguageTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
public function testFunctionProviderCompilation() |
28
|
|
|
{ |
29
|
|
|
$provider = new BasicSerializerFunctionsProvider(); |
30
|
|
|
|
31
|
|
|
$exp = new ExpressionLanguage(); |
32
|
|
|
$exp->registerProvider($provider); |
33
|
|
|
|
34
|
|
|
$this->assertEquals('$this->get("foo")', $exp->compile("service('foo')")); |
35
|
|
|
$this->assertEquals('$this->getParameter("foo")', $exp->compile("parameter('foo')")); |
36
|
|
|
$this->assertEquals('call_user_func_array(array($this->get(\'security.authorization_checker\'), \'isGranted\'), array("foo", ))', $exp->compile("is_granted('foo')")); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testFunctionProviderEvaluation() |
40
|
|
|
{ |
41
|
|
|
$provider = new BasicSerializerFunctionsProvider(); |
42
|
|
|
|
43
|
|
|
$exp = new ExpressionLanguage(); |
44
|
|
|
$exp->registerProvider($provider); |
45
|
|
|
|
46
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
47
|
|
|
$container |
48
|
|
|
->expects($this->once()) |
49
|
|
|
->method('get')->with('foo', 1) |
50
|
|
|
->will($this->returnValue('bar')); |
51
|
|
|
|
52
|
|
|
$this->assertEquals('bar', $exp->evaluate("service('foo')", ['container' => $container])); |
53
|
|
|
|
54
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
55
|
|
|
$container |
56
|
|
|
->expects($this->once()) |
57
|
|
|
->method('getParameter')->with('foo') |
58
|
|
|
->will($this->returnValue('bar')); |
59
|
|
|
|
60
|
|
|
$this->assertEquals('bar', $exp->evaluate("parameter('foo')", ['container' => $container])); |
61
|
|
|
|
62
|
|
|
$authChecker = $this->getMockBuilder('JMS\SerializerBundle\Tests\ExpressionLanguage\AuthCheckerMock')->getMock(); |
63
|
|
|
$authChecker |
64
|
|
|
->expects($this->once()) |
65
|
|
|
->method('isGranted')->with('foo') |
66
|
|
|
->will($this->returnValue('bar')); |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
70
|
|
|
$container |
71
|
|
|
->expects($this->once()) |
72
|
|
|
->method('get')->with('security.authorization_checker') |
73
|
|
|
->will($this->returnValue($authChecker)); |
74
|
|
|
|
75
|
|
|
$this->assertEquals('bar', $exp->evaluate("is_granted('foo')", ['container' => $container])); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
class AuthCheckerMock |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
public function isGranted() |
82
|
|
|
{ |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
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.