Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the GetItemExpression #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Security/Authorization/Expression/Ast/GetItemExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace JMS\SecurityExtraBundle\Security\Authorization\Expression\Ast;

class GetItemExpression
class GetItemExpression implements ExpressionInterface
{
public $array;
public $key;
Expand All @@ -28,4 +28,4 @@ public function __construct(ExpressionInterface $array, ExpressionInterface $key
$this->array = $array;
$this->key = $key;
}
}
}
12 changes: 11 additions & 1 deletion Security/Authorization/Expression/ExpressionCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ public function compilePreconditions(ExpressionInterface $expr)
return $this;
}

if ($expr instanceof GetItemExpression) {
$this
->compilePreconditions($expr->array)
->compilePreconditions($expr->key);

return $this;
}

if ($expr instanceof GetPropertyExpression) {
$this->compilePreconditions($expr->object);

Expand Down Expand Up @@ -317,7 +325,9 @@ public function compileInternal(ExpressionInterface $expr)

if ($expr instanceof GetItemExpression) {
$this->compileInternal($expr->array);
$this->code .= '['.$expr->key.']';
$this->code .= '[';
$this->compileInternal($expr->key);
$this->code .= ']';

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/*
* Copyright 2011 Johannes M. Schmitt <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace JMS\SecurityExtraBundle\Tests\Security\Authorization\Expression;

use JMS\SecurityExtraBundle\Security\Authorization\Expression\Expression;
use JMS\SecurityExtraBundle\Security\Authorization\Expression\ExpressionCompiler;

class GetItemExpressionCompilerTest extends \PHPUnit_Framework_TestCase
{
private $compiler;

public function testCompile()
{
$evaluator = eval($source = $this->compiler->compileExpression(new Expression(
'object["foo"] == "bar"')));

$this->assertTrue($evaluator(array('object' => array('foo' => 'bar'))));

$this->assertFalse($evaluator(array('object' => array('foo' => 'baz'))));
}

public function testCompileWithComplexKey()
{
$evaluator = eval($source = $this->compiler->compileExpression(new Expression(
'object[key] == "bar"')));

$this->assertTrue($evaluator(array('object' => array('foo' => 'bar'), 'key' => 'foo')));

$this->assertFalse($evaluator(array('object' => array('foo' => 'baz'), 'key' => 'foo')));
}

/**
* @expectedException \RuntimeException
*/
public function testCompilePreconditionsForKey()
{
$evaluator = eval($source = $this->compiler->compileExpression(new Expression(
'object[key] == "bar"')));

$evaluator(array('object' => array('foo' => 'bar')));
}

/**
* @expectedException \RuntimeException
*/
public function testCompilePreconditionsForArray()
{
$evaluator = eval($source = $this->compiler->compileExpression(new Expression(
'object[key] == "bar"')));

$evaluator(array('key' => 'foo'));
}

protected function setUp()
{
$this->compiler = new ExpressionCompiler();
}
}