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

Expose and test GroupsExclusionStrategy::getGroupsFor() #1069

Merged
merged 1 commit into from
Apr 10, 2019
Merged
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
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']]],
];
}
}