Skip to content

Commit 4e42548

Browse files
authored
Merge pull request #1069 from schmittjoh/groups-for-public
Expose and test GroupsExclusionStrategy::getGroupsFor()
2 parents 3888192 + 7ec7770 commit 4e42548

3 files changed

Lines changed: 46 additions & 1 deletion

File tree

src/JMS/Serializer/Context.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ abstract class Context
4747
public function __construct()
4848
{
4949
$this->attributes = new Map();
50+
$this->metadataStack = new \SplStack();
5051
}
5152

5253
/**

src/JMS/Serializer/Exclusion/GroupsExclusionStrategy.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,16 @@ private function shouldSkipUsingGroups(PropertyMetadata $property, $groups)
8282
return true;
8383
}
8484

85-
private function getGroupsFor(Context $navigatorContext)
85+
/**
86+
* @param Context $navigatorContext
87+
* @return array
88+
*/
89+
public function getGroupsFor(Context $navigatorContext)
8690
{
91+
if (!$this->nestedGroups) {
92+
return array_keys($this->groups);
93+
}
94+
8795
$paths = $navigatorContext->getCurrentPath();
8896

8997
$groups = $this->groups;

tests/Exclusion/GroupsExclusionStrategyTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,40 @@ public function getExclusionRules()
4949
[['foo'], [GroupsExclusionStrategy::DEFAULT_GROUP, 'foo'], false],
5050
];
5151
}
52+
53+
/**
54+
* @dataProvider getGroupsFor
55+
* @param $groups
56+
* @param $propsVisited
57+
* @param $resultingGroups
58+
*/
59+
public function testGroupsFor($groups, $propsVisited, $resultingGroups)
60+
{
61+
$exclusion = new GroupsExclusionStrategy($groups);
62+
$context = SerializationContext::create();
63+
64+
foreach ($propsVisited as $prop) {
65+
$metadata = new StaticPropertyMetadata('stdClass', $prop, 'propVal');
66+
$context->pushPropertyMetadata($metadata);
67+
}
68+
69+
$groupsFor = $exclusion->getGroupsFor($context);
70+
$this->assertEquals($groupsFor, $resultingGroups);
71+
}
72+
73+
public function getGroupsFor()
74+
{
75+
return [
76+
[['foo'], ['prop'], ['foo']],
77+
[[], ['prop'], ['Default']],
78+
79+
[['foo', 'prop' => ['bar']], ['prop'], ['bar']],
80+
[['foo', 'prop' => ['bar']], ['prop2'], ['foo', 'prop' => ['bar']]],
81+
82+
[['foo', 'prop' => ['bar']], ['prop', 'prop2'], ['Default']],
83+
84+
[['foo', 'prop' => ['xx', 'prop2' => ['def'], 'prop3' => ['def']]], ['prop', 'prop2', 'propB'], ['Default']],
85+
[['foo', 'prop' => ['xx', 'prop2' => ['def', 'prop3' => ['def']]]], ['prop', 'prop2'], ['def', 'prop3' => ['def']]],
86+
];
87+
}
5288
}

0 commit comments

Comments
 (0)