diff --git a/SilMock/Google/Service/Directory/Resource/Members.php b/SilMock/Google/Service/Directory/Resource/Members.php index 6d22472..1c1cab0 100644 --- a/SilMock/Google/Service/Directory/Resource/Members.php +++ b/SilMock/Google/Service/Directory/Resource/Members.php @@ -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(); @@ -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); diff --git a/SilMock/tests/Google/Service/Directory/Resource/MembersTest.php b/SilMock/tests/Google/Service/Directory/Resource/MembersTest.php index a82c0ca..2c9e824 100644 --- a/SilMock/tests/Google/Service/Directory/Resource/MembersTest.php +++ b/SilMock/tests/Google/Service/Directory/Resource/MembersTest.php @@ -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 { @@ -72,7 +73,7 @@ public function testHasMember() self::assertTrue($hasMember); } - public function testListMembers() + public function testListMembersAll() { $groupEmailAddress = 'sample_group@groups.example.com'; $mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile); @@ -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 = 'sample_group@groups.example.com'; + $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 = 'sample_group@groups.example.com'; + $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.' ); } }