Skip to content

Commit

Permalink
Merge pull request schmittjoh#778 from schmittjoh/bblue-master
Browse files Browse the repository at this point in the history
Add propertyMetdata to dynamic expression variables
  • Loading branch information
goetas authored May 24, 2017
2 parents a1ae8be + aed29cf commit e3b2c36
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Exclusion/ExpressionLanguageExclusionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function shouldSkipProperty(PropertyMetadata $property, Context $navigato

$variables = [
'context' => $navigatorContext,
'property_metadata' => $property,
];
if ($navigatorContext instanceof SerializationContext) {
$variables['object'] = $navigatorContext->getObject();
Expand Down
75 changes: 75 additions & 0 deletions tests/Exclusion/ExpressionLanguageExclusionStrategyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/*
* Copyright 2016 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\Serializer\Tests\Exclusion;

use JMS\Serializer\Exclusion\ExpressionLanguageExclusionStrategy;
use JMS\Serializer\Expression\ExpressionEvaluator;
use JMS\Serializer\Metadata\StaticPropertyMetadata;
use JMS\Serializer\SerializationContext;

/**
* @author Asmir Mustafic <[email protected]>
*/
class ExpressionLanguageExclusionStrategyTest extends \PHPUnit_Framework_TestCase
{
private $visitedObject;
private $context;
private $expressionEvaluator;
private $exclusionStrategy;

public function setUp()
{
$this->visitedObject = new \stdClass();

$this->context = $this->getMockBuilder(SerializationContext::class)->getMock();
$this->context->method('getObject')->willReturn($this->visitedObject);

$this->expressionEvaluator = $this->getMockBuilder(ExpressionEvaluator::class)
->disableOriginalConstructor()
->getMock();

$this->exclusionStrategy = new ExpressionLanguageExclusionStrategy($this->expressionEvaluator);
}

public function testExpressionLanguageExclusionWorks()
{
$metadata = new StaticPropertyMetadata('stdClass', 'prop', 'propVal');
$metadata->excludeIf = 'foo';

$this->expressionEvaluator->expects($this->once())
->method('evaluate')
->with('foo', array(
'context' => $this->context,
'property_metadata' => $metadata,
'object' => $this->visitedObject,
))
->willReturn(true);

$this->assertSame(true, $this->exclusionStrategy->shouldSkipProperty($metadata, $this->context));
}

public function testExpressionLanguageSkipsWhenNoExpression()
{
$metadata = new StaticPropertyMetadata('stdClass', 'prop', 'propVal');

$this->expressionEvaluator->expects($this->never())->method('evaluate');

$this->assertSame(false, $this->exclusionStrategy->shouldSkipProperty($metadata, $this->context));
}
}

0 comments on commit e3b2c36

Please sign in to comment.