Skip to content

Commit 6f6f6a3

Browse files
author
Jeremiah VALERIE
committed
Merge branch 'expression_language_and_controllers_tests_coverage'
2 parents 8f549c6 + 38df539 commit 6f6f6a3

13 files changed

+517
-67
lines changed

ExpressionLanguage/AuthorizationExpressionProvider.php

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,11 @@ class AuthorizationExpressionProvider implements ExpressionFunctionProviderInter
1919
public function getFunctions()
2020
{
2121
return [
22-
new ExpressionFunction('hasRole', function ($role) {
23-
return sprintf('$authChecker->isGranted(%s)', $role);
24-
}, function (array $variables, $role) {
22+
new ExpressionFunction('hasRole', function () {}, function (array $variables, $role) {
2523
return $variables['container']->get('security.authorization_checker')->isGranted($role);
2624
}),
2725

28-
new ExpressionFunction('hasAnyRole', function (array $roles) {
29-
$compiler = 'false';
30-
foreach ($roles as $role) {
31-
$compiler .= ' || ';
32-
$compiler .= sprintf('$authChecker->isGranted(%s)', $role);
33-
}
34-
35-
return $compiler;
36-
}, function (array $variables, array $roles) {
26+
new ExpressionFunction('hasAnyRole', function () {}, function (array $variables, array $roles) {
3727
foreach ($roles as $role) {
3828
if ($variables['container']->get('security.authorization_checker')->isGranted($role)) {
3929
return true;
@@ -43,47 +33,29 @@ public function getFunctions()
4333
return false;
4434
}),
4535

46-
new ExpressionFunction('isAnonymous', function () {
47-
return '$authChecker->isGranted("IS_AUTHENTICATED_ANONYMOUSLY")';
48-
}, function (array $variables) {
36+
new ExpressionFunction('isAnonymous', function () {}, function (array $variables) {
4937
return $variables['container']->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_ANONYMOUSLY');
5038
}),
5139

52-
new ExpressionFunction('isRememberMe', function () {
53-
return '$authChecker->isGranted("IS_AUTHENTICATED_REMEMBERED")';
54-
}, function (array $variables) {
40+
new ExpressionFunction('isRememberMe', function () {}, function (array $variables) {
5541
return $variables['container']->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED');
5642
}),
5743

58-
new ExpressionFunction('isFullyAuthenticated', function () {
59-
return sprintf('$authChecker->isGranted("IS_AUTHENTICATED_FULLY")');
60-
}, function (array $variables) {
44+
new ExpressionFunction('isFullyAuthenticated', function () {}, function (array $variables) {
6145
return $variables['container']->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY');
6246
}),
6347

64-
new ExpressionFunction('isAuthenticated', function () {
65-
return '$authChecker->isGranted("IS_AUTHENTICATED_REMEMBERED") || $authChecker->isGranted("IS_AUTHENTICATED_FULLY")';
66-
}, function (array $variables) {
48+
new ExpressionFunction('isAuthenticated', function () {}, function (array $variables) {
6749
return
6850
$variables['container']->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED')
6951
|| $variables['container']->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY');
7052
}),
7153

72-
new ExpressionFunction('hasPermission', function ($object, $permission) {
73-
return sprintf('$authChecker->isGranted(%s, $object)', $permission);
74-
}, function (array $variables, $object, $permission) {
54+
new ExpressionFunction('hasPermission', function () {}, function (array $variables, $object, $permission) {
7555
return $variables['container']->get('security.authorization_checker')->isGranted($permission, $object);
7656
}),
7757

78-
new ExpressionFunction('hasAnyPermission', function ($object, array $permissions) {
79-
$compiler = 'false';
80-
foreach ($permissions as $permission) {
81-
$compiler .= ' || ';
82-
$compiler .= sprintf('$authChecker->isGranted("%s", $object)', $permission);
83-
}
84-
85-
return $compiler;
86-
}, function (array $variables, $object, array $permissions) {
58+
new ExpressionFunction('hasAnyPermission', function () {}, function (array $variables, $object, array $permissions) {
8759
foreach ($permissions as $permission) {
8860
if ($variables['container']->get('security.authorization_checker')->isGranted($permission, $object)) {
8961
return true;

ExpressionLanguage/ConfigExpressionProvider.php

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,58 +20,37 @@ class ConfigExpressionProvider implements ExpressionFunctionProviderInterface
2020
public function getFunctions()
2121
{
2222
return [
23-
new ExpressionFunction('service', function ($arg) {
24-
return sprintf('$container->get(%s)', $arg);
25-
}, function (array $variables, $value) {
23+
new ExpressionFunction('service', function () {}, function (array $variables, $value) {
2624
return $variables['container']->get($value);
2725
}),
2826

29-
new ExpressionFunction('parameter', function ($arg) {
30-
return sprintf('$container->getParameter(%s)', $arg);
31-
}, function (array $variables, $value) {
27+
new ExpressionFunction('parameter', function () {}, function (array $variables, $value) {
3228
return $variables['container']->getParameter($value);
3329
}),
3430

35-
new ExpressionFunction('isTypeOf', function ($className) {
36-
return sprintf('$value instanceof %s', $className);
37-
}, function (array $variables, $className) {
31+
new ExpressionFunction('isTypeOf', function () {}, function (array $variables, $className) {
3832
return $variables['value'] instanceof $className;
3933
}),
4034

41-
new ExpressionFunction('resolver', function ($alias, array $args = []) {
42-
return sprintf('$container->get("overblog_graphql.resolver_resolver")->resolve([%s, $args])', $alias);
43-
}, function (array $variables, $alias, array $args = []) {
35+
new ExpressionFunction('resolver', function () {}, function (array $variables, $alias, array $args = []) {
4436
return $variables['container']->get('overblog_graphql.resolver_resolver')->resolve([$alias, $args]);
4537
}),
4638

47-
new ExpressionFunction('mutation', function ($alias, array $args = []) {
48-
return sprintf('$container->get("overblog_graphql.mutation_resolver")->resolve([%s, $args])', $alias);
49-
}, function (array $variables, $alias, array $args = []) {
39+
new ExpressionFunction('mutation', function () {}, function (array $variables, $alias, array $args = []) {
5040
return $variables['container']->get('overblog_graphql.mutation_resolver')->resolve([$alias, $args]);
5141
}),
5242

53-
new ExpressionFunction('globalId', function ($id, $typeName = null) {
54-
return sprintf(
55-
'\\Overblog\\GraphQLBundle\\Relay\\Node\\GlobalId::toGlobalId(!empty(%s) ? %s : $info->parentType->name, %s)',
56-
$typeName,
57-
$typeName,
58-
$id
59-
);
60-
}, function (array $variables, $id, $typeName = null) {
43+
new ExpressionFunction('globalId', function () {}, function (array $variables, $id, $typeName = null) {
6144
$type = !empty($typeName) ? $typeName : $variables['info']->parentType->name;
6245

6346
return GlobalId::toGlobalId($type, $id);
6447
}),
6548

66-
new ExpressionFunction('fromGlobalId', function ($globalId) {
67-
return sprintf('\\Overblog\\GraphQLBundle\\Relay\\Node\\GlobalId::fromGlobalId(%s)', $globalId);
68-
}, function (array $variables, $globalId) {
49+
new ExpressionFunction('fromGlobalId', function () {}, function (array $variables, $globalId) {
6950
return GlobalId::fromGlobalId($globalId);
7051
}),
7152

72-
new ExpressionFunction('newObject', function ($className, array $args = []) {
73-
return sprintf('(new \ReflectionClass("%s"))->newInstanceArgs($args)', $className);
74-
}, function (array $variables, $className, array $args = []) {
53+
new ExpressionFunction('newObject', function () {}, function (array $variables, $className, array $args = []) {
7554
return (new \ReflectionClass($className))->newInstanceArgs($args);
7655
}),
7756
];

Tests/DIContainerMockTrait.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the OverblogGraphQLBundle package.
5+
*
6+
* (c) Overblog <http://github.com/overblog/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Overblog\GraphQLBundle\Tests;
13+
14+
trait DIContainerMockTrait
15+
{
16+
private function getDIContainerMock(array $services = [], array $parameters = [])
17+
{
18+
$container = $this->getMock('Symfony\\Component\\DependencyInjection\\Container', ['get', 'getParameter', 'has']);
19+
20+
$getMethod = $container->expects($this->any())->method('get');
21+
22+
foreach ($services as $id => $service) {
23+
$getMethod
24+
->with($id)
25+
->willReturn($service)
26+
;
27+
}
28+
29+
$getParameterMethod = $container->expects($this->any())->method('getParameter');
30+
31+
foreach ($parameters as $id => $parameter) {
32+
$getParameterMethod
33+
->with($id)
34+
->willReturn($parameter)
35+
;
36+
}
37+
38+
return $container;
39+
}
40+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the OverblogGraphQLBundle package.
5+
*
6+
* (c) Overblog <http://github.com/overblog/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Overblog\GraphQLBundle\Tests\Error;
13+
14+
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionLanguage;
15+
use Overblog\GraphQLBundle\Tests\DIContainerMockTrait;
16+
17+
class AuthorizationExpressionProviderTest extends \PHPUnit_Framework_TestCase
18+
{
19+
use DIContainerMockTrait;
20+
21+
/**
22+
* @var ExpressionLanguage
23+
*/
24+
private $expressionLanguage;
25+
26+
public function setUp()
27+
{
28+
$this->expressionLanguage = new ExpressionLanguage();
29+
}
30+
31+
public function testHasRole()
32+
{
33+
$this->assertExpressionEvaluate('hasRole("ROLE_USER")', 'ROLE_USER');
34+
}
35+
36+
public function testHasAnyRole()
37+
{
38+
$this->assertExpressionEvaluate('hasAnyRole(["ROLE_ADMIN", "ROLE_USER"])', 'ROLE_ADMIN');
39+
40+
$this->assertExpressionEvaluate(
41+
'hasAnyRole(["ROLE_ADMIN", "ROLE_USER"])',
42+
$this->matchesRegularExpression('/^ROLE_(USER|ADMIN)$/'),
43+
[],
44+
$this->exactly(2),
45+
false,
46+
'assertFalse'
47+
);
48+
}
49+
50+
public function testIsAnonymous()
51+
{
52+
$this->assertExpressionEvaluate('isAnonymous()', 'IS_AUTHENTICATED_ANONYMOUSLY');
53+
}
54+
55+
public function testIsRememberMe()
56+
{
57+
$this->assertExpressionEvaluate('isRememberMe()', 'IS_AUTHENTICATED_REMEMBERED');
58+
}
59+
60+
public function testIsFullyAuthenticated()
61+
{
62+
$this->assertExpressionEvaluate('isFullyAuthenticated()', 'IS_AUTHENTICATED_FULLY');
63+
}
64+
65+
public function testIsAuthenticated()
66+
{
67+
$this->assertExpressionEvaluate('isAuthenticated()', $this->matchesRegularExpression('/^IS_AUTHENTICATED_(REMEMBERED|FULLY)$/'));
68+
}
69+
70+
public function testHasPermission()
71+
{
72+
$object = new \stdClass();
73+
74+
$this->assertExpressionEvaluate(
75+
'hasPermission(object,"OWNER")',
76+
[
77+
'OWNER',
78+
$this->identicalTo($object),
79+
],
80+
[
81+
'object' => $object,
82+
]
83+
);
84+
}
85+
86+
public function testHasAnyPermission()
87+
{
88+
$object = new \stdClass();
89+
90+
$this->assertExpressionEvaluate(
91+
'hasAnyPermission(object,["OWNER", "WRITER"])',
92+
[
93+
$this->matchesRegularExpression('/^(OWNER|WRITER)$/'),
94+
$this->identicalTo($object),
95+
],
96+
[
97+
'object' => $object,
98+
]
99+
);
100+
101+
$this->assertExpressionEvaluate(
102+
'hasAnyPermission(object,["OWNER", "WRITER"])',
103+
[
104+
$this->matchesRegularExpression('/^(OWNER|WRITER)$/'),
105+
$this->identicalTo($object),
106+
],
107+
[
108+
'object' => $object,
109+
],
110+
$this->exactly(2),
111+
false,
112+
'assertFalse'
113+
);
114+
}
115+
116+
private function assertExpressionEvaluate($expression, $with, array $expressionValues = [], $expects = null, $return = true, $assertMethod = 'assertTrue')
117+
{
118+
$authChecker = $this->getAuthorizationCheckerIsGrantedWithExpectation($with, $expects, $return);
119+
120+
$container = $this->getDIContainerMock(['security.authorization_checker' => $authChecker]);
121+
$this->expressionLanguage->setContainer($container);
122+
$this->$assertMethod($this->expressionLanguage->evaluate($expression, $expressionValues));
123+
}
124+
125+
private function getAuthorizationCheckerIsGrantedWithExpectation($with, $expects = null, $return = true)
126+
{
127+
if (null === $expects) {
128+
$expects = $this->once();
129+
}
130+
$authChecker = $this->getAuthorizationCheckerMock();
131+
132+
if ($return instanceof \PHPUnit_Framework_MockObject_Stub_Return) {
133+
$returnValue = $return;
134+
} else {
135+
$returnValue = $this->returnValue($return);
136+
}
137+
138+
$methodExpectation = $authChecker
139+
->expects($expects)
140+
->method('isGranted');
141+
142+
call_user_func_array([$methodExpectation, 'with'], is_array($with) ? $with : [$with]);
143+
144+
$methodExpectation->will($returnValue);
145+
146+
return $authChecker;
147+
}
148+
149+
private function getAuthorizationCheckerMock()
150+
{
151+
$AuthorizationChecker = $this->getMockBuilder('Symfony\\Component\Security\\Core\Authorization\\AuthorizationCheckerInterface')
152+
->disableOriginalConstructor()
153+
->setMethods(['isGranted'])
154+
->getMock()
155+
;
156+
157+
return $AuthorizationChecker;
158+
}
159+
}

0 commit comments

Comments
 (0)