1<?php 2 3declare(strict_types=1); 4 5namespace JMS\Serializer\Tests\Exclusion; 6 7use JMS\Serializer\Exclusion\ExpressionLanguageExclusionStrategy; 8use JMS\Serializer\Expression\ExpressionEvaluator; 9use JMS\Serializer\Metadata\StaticPropertyMetadata; 10use JMS\Serializer\SerializationContext; 11use PHPUnit\Framework\TestCase; 12 13/** 14 * @author Asmir Mustafic <goetas@gmail.com> 15 */ 16class ExpressionLanguageExclusionStrategyTest extends TestCase 17{ 18 private $visitedObject; 19 private $context; 20 private $expressionEvaluator; 21 private $exclusionStrategy; 22 23 public function setUp() 24 { 25 $this->visitedObject = new \stdClass(); 26 27 $this->context = $this->getMockBuilder(SerializationContext::class)->getMock(); 28 $this->context->method('getObject')->willReturn($this->visitedObject); 29 30 $this->expressionEvaluator = $this->getMockBuilder(ExpressionEvaluator::class) 31 ->disableOriginalConstructor() 32 ->getMock(); 33 34 $this->exclusionStrategy = new ExpressionLanguageExclusionStrategy($this->expressionEvaluator); 35 } 36 37 public function testExpressionLanguageExclusionWorks() 38 { 39 $metadata = new StaticPropertyMetadata('stdClass', 'prop', 'propVal'); 40 $metadata->excludeIf = 'foo'; 41 42 $this->expressionEvaluator->expects($this->once()) 43 ->method('evaluate') 44 ->with('foo', [ 45 'context' => $this->context, 46 'property_metadata' => $metadata, 47 'object' => $this->visitedObject, 48 ]) 49 ->willReturn(true); 50 51 self::assertTrue($this->exclusionStrategy->shouldSkipProperty($metadata, $this->context)); 52 } 53 54 public function testExpressionLanguageSkipsWhenNoExpression() 55 { 56 $metadata = new StaticPropertyMetadata('stdClass', 'prop', 'propVal'); 57 58 $this->expressionEvaluator->expects($this->never())->method('evaluate'); 59 60 self::assertFalse($this->exclusionStrategy->shouldSkipProperty($metadata, $this->context)); 61 } 62} 63