Skip to content

Commit

Permalink
Merge pull request #95 from silinternational/feature/smarter-listMembers
Browse files Browse the repository at this point in the history
Add some query logic for roles into members.listMembers
  • Loading branch information
mtompset authored Jun 18, 2024
2 parents 4d3ddca + 9b90a25 commit fd07198
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
21 changes: 15 additions & 6 deletions SilMock/Google/Service/Directory/Resource/Members.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ public function listMembers($groupKey, $optParams = [])
$this->validateGroupExists($groupKey);
$pageSize = $optParams['pageSize'] ?? 10;
$pageToken = $optParams['pageToken'] ?? 0;
$query = $optParams['query'] ?? null;
$expectedRole = $query ? substr($query, strpos($query, '=') + 1) : null;
$members = new GoogleDirectory_Members();
$directoryMemberRecords = $this->getRecords();
$memberCounter = 0;
foreach ($directoryMemberRecords as $memberRecord) {
$memberData = json_decode($memberRecord['data'], true);
if ($memberData['groupKey'] === $groupKey) {
$memberCounter = $memberCounter + 1;
if ($memberCounter >= ($pageToken * $pageSize)) {
$currentMembers = $members->getMembers();
$currentMember = new GoogleDirectory_Member();
ObjectUtils::initialize($currentMember, $memberData['member']);
$currentMembers[] = $currentMember;
$members->setMembers($currentMembers);
if (is_null($expectedRole) || $expectedRole === $memberData['member']['role']) {
$memberCounter = $memberCounter + 1;
$this->addToMembers($memberData, $members);
}
}
}
$currentMembers = $members->getMembers();
Expand All @@ -98,6 +98,15 @@ public function listMembers($groupKey, $optParams = [])
return $members;
}

protected function addToMembers(array $memberData, GoogleDirectory_Members $members): void
{
$currentMembers = $members->getMembers();
$currentMember = new GoogleDirectory_Member();
ObjectUtils::initialize($currentMember, $memberData['member']);
$currentMembers[] = $currentMember;
$members->setMembers($currentMembers);
}

protected function validateGroupExists(string $groupKey): void
{
$mockGroupsObject = new Groups($this->dbFile);
Expand Down
59 changes: 56 additions & 3 deletions SilMock/tests/Google/Service/Directory/Resource/MembersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function testInsert()

$member = new GoogleDirectory_Member();
$member->setEmail($emailAddress);
$member->setRole('MEMBER');

$mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile);
try {
Expand Down Expand Up @@ -72,7 +73,7 @@ public function testHasMember()
self::assertTrue($hasMember);
}

public function testListMembers()
public function testListMembersAll()
{
$groupEmailAddress = '[email protected]';
$mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile);
Expand All @@ -88,8 +89,60 @@ public function testListMembers()
);
}
self::assertNotEmpty(
$members,
'Was expecting the members.list method to have at least one member.'
$members->getMembers(),
'Was expecting the members.list method to have at least one member entry.'
);
}

public function testListMembersOnlyMember()
{
$groupEmailAddress = '[email protected]';
$mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile);
$members = [];
try {
$members = $mockGoogleServiceDirectory->members->listMembers(
$groupEmailAddress,
[
'query' => 'roles=MEMBER'
]
);
} catch (Exception $exception) {
self::fail(
sprintf(
'Was expecting the members.list method to function, but got: %s',
$exception->getMessage()
)
);
}
self::assertNotEmpty(
$members->getMembers(),
'Was expecting the members.list method to have at least one member type entry.'
);
}

public function testListMembersOnlyOwner()
{
$groupEmailAddress = '[email protected]';
$mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile);
$members = [];
try {
$members = $mockGoogleServiceDirectory->members->listMembers(
$groupEmailAddress,
[
'query' => 'roles=OWNER'
]
);
} catch (Exception $exception) {
self::fail(
sprintf(
'Was expecting the members.list method to function, but got: %s',
$exception->getMessage()
)
);
}
self::assertEmpty(
$members->getMembers(),
'Was expecting the members.list method to have no owner types.'
);
}
}

0 comments on commit fd07198

Please sign in to comment.