Exemplo n.º 1
0
 public function testEvaluate_roleHierarchyInvolved_variablesContainsProperRoles()
 {
     $roleHierarchy = \Mockery::mock(RoleHierarchy::class)->shouldReceive('getReachableRoles')->with(['ADMIN'])->andReturn(['ADMIN', 'MANAGER', 'USER'])->getMock();
     $this->evaluator = new ExpressionEvaluator($this->securityContext, $this->user, $this->language, $roleHierarchy);
     $this->user->shouldReceive('getRoles')->andReturn(['ADMIN']);
     $identity = \Mockery::mock(IIdentity::class);
     $this->user->shouldReceive('getIdentity')->andReturn($identity);
     $request = \Mockery::mock(Request::class)->shouldReceive('getParameters')->andReturn([])->getMock();
     $expr = \Mockery::mock(Expression::class);
     $this->language->shouldReceive('evaluate')->with($expr, \Mockery::on(function ($variables) {
         return $variables['roles'] === ['ADMIN', 'MANAGER', 'USER'];
     }))->once();
     $this->evaluator->evaluate($expr, $request);
 }
 /**
  * @param ClassType|Method $element
  * @param Request $request
  * @return bool
  * @throws \InvalidArgumentException
  */
 public function checkRequirement($element, Request $request)
 {
     if ($element instanceof ClassType) {
         $expressions = $this->getClassExpressionsToEvaluate($element);
     } else {
         if ($element instanceof Method) {
             $expressions = $this->getMethodExpressionsToEvaluate($element);
         } else {
             throw new \InvalidArgumentException("Argument 'element' must be instanceof Nette\\Reflection\\ClassType or Nette\\Reflection\\Method");
         }
     }
     if (!empty($expressions)) {
         foreach ($expressions as $expression) {
             $result = $this->expressionEvaluator->evaluate($expression, $request);
             if (!$result) {
                 $this->failedExpression = $expression;
                 return false;
             }
         }
     }
     return true;
 }