Skip to content

Commit

Permalink
Add hasMember and testing for it
Browse files Browse the repository at this point in the history
  • Loading branch information
mtompset committed May 28, 2024
1 parent e6060f0 commit 1a456be
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
14 changes: 14 additions & 0 deletions SilMock/Google/Service/Directory/Resource/Members.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ public function __construct(?string $dbFile = null)
parent::__construct($dbFile, 'directory', 'members');
}

public function hasMember(string $groupKey, string $memberKey): array
{
$members = $this->listMembers($groupKey);
$memberList = $members->getMembers();
$memberEmailAddresses = [];
foreach ($memberList as $member) {
$memberEmailAddresses[] = mb_strtolower($member->getEmail());
}
$isMember = in_array(mb_strtolower($memberKey), $memberEmailAddresses);
return [
'isMember' => $isMember,
];
}

/**
* @throws Exception
*/
Expand Down
30 changes: 23 additions & 7 deletions SilMock/tests/Google/Service/Directory/Resource/MembersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MembersTest extends TestCase
// GroupsTest and MembersTest need to share same DB, also
// They are very dependent on order run.
// groups.insert, groups.listGroups, members.insert, members.listMembers
public $dataFile = DATAFILE5;
public string $dataFile = DATAFILE5;

public function testInsert()
{
Expand All @@ -26,15 +26,32 @@ public function testInsert()
try {
$addedMember = $mockGoogleServiceDirectory->members->insert($groupEmailAddress, $member);
} catch (Exception $exception) {
$this->assertFalse(
true,
self::fail(
sprintf(
'Was expecting the members.insert method to function, but got: %s',
$exception->getMessage()
)
);
}
$this->assertTrue($addedMember instanceof GoogleDirectory_Member);
self::assertTrue($addedMember instanceof GoogleDirectory_Member);
}

public function testHasMember()
{
$groupEmailAddress = '[email protected]';
$mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile);
try {
$result = $mockGoogleServiceDirectory->members->hasMember(
$groupEmailAddress,
'[email protected]'
);
$hasMember = $result['isMember'] ?? false;
} catch (Exception $exception) {
self::fail(
$exception->getMessage()
);
}
self::assertTrue($hasMember);
}

public function testListMembers()
Expand All @@ -45,15 +62,14 @@ public function testListMembers()
try {
$members = $mockGoogleServiceDirectory->members->listMembers($groupEmailAddress);
} catch (Exception $exception) {
$this->assertFalse(
true,
self::fail(
sprintf(
'Was expecting the members.list method to function, but got: %s',
$exception->getMessage()
)
);
}
$this->assertNotEmpty(
self::assertNotEmpty(
$members,
'Was expecting the members.list method to have at least one member.'
);
Expand Down

0 comments on commit 1a456be

Please sign in to comment.