Skip to content

Commit

Permalink
Merge pull request #1069 from schmittjoh/groups-for-public
Browse files Browse the repository at this point in the history
Expose and test GroupsExclusionStrategy::getGroupsFor()
  • Loading branch information
goetas authored Apr 10, 2019
2 parents 3888192 + 7ec7770 commit 4e42548
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/JMS/Serializer/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ abstract class Context
public function __construct()
{
$this->attributes = new Map();
$this->metadataStack = new \SplStack();
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/JMS/Serializer/Exclusion/GroupsExclusionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,16 @@ private function shouldSkipUsingGroups(PropertyMetadata $property, $groups)
return true;
}

private function getGroupsFor(Context $navigatorContext)
/**
* @param Context $navigatorContext
* @return array
*/
public function getGroupsFor(Context $navigatorContext)
{
if (!$this->nestedGroups) {
return array_keys($this->groups);
}

$paths = $navigatorContext->getCurrentPath();

$groups = $this->groups;
Expand Down
36 changes: 36 additions & 0 deletions tests/Exclusion/GroupsExclusionStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,40 @@ public function getExclusionRules()
[['foo'], [GroupsExclusionStrategy::DEFAULT_GROUP, 'foo'], false],
];
}

/**
* @dataProvider getGroupsFor
* @param $groups
* @param $propsVisited
* @param $resultingGroups
*/
public function testGroupsFor($groups, $propsVisited, $resultingGroups)
{
$exclusion = new GroupsExclusionStrategy($groups);
$context = SerializationContext::create();

foreach ($propsVisited as $prop) {
$metadata = new StaticPropertyMetadata('stdClass', $prop, 'propVal');
$context->pushPropertyMetadata($metadata);
}

$groupsFor = $exclusion->getGroupsFor($context);
$this->assertEquals($groupsFor, $resultingGroups);
}

public function getGroupsFor()
{
return [
[['foo'], ['prop'], ['foo']],
[[], ['prop'], ['Default']],

[['foo', 'prop' => ['bar']], ['prop'], ['bar']],
[['foo', 'prop' => ['bar']], ['prop2'], ['foo', 'prop' => ['bar']]],

[['foo', 'prop' => ['bar']], ['prop', 'prop2'], ['Default']],

[['foo', 'prop' => ['xx', 'prop2' => ['def'], 'prop3' => ['def']]], ['prop', 'prop2', 'propB'], ['Default']],
[['foo', 'prop' => ['xx', 'prop2' => ['def', 'prop3' => ['def']]]], ['prop', 'prop2'], ['def', 'prop3' => ['def']]],
];
}
}

0 comments on commit 4e42548

Please sign in to comment.