diff --git a/SilMock/Google/Service/Directory/Resource/Members.php b/SilMock/Google/Service/Directory/Resource/Members.php index 0f5c320..5b56513 100644 --- a/SilMock/Google/Service/Directory/Resource/Members.php +++ b/SilMock/Google/Service/Directory/Resource/Members.php @@ -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 */ diff --git a/SilMock/tests/Google/Service/Directory/Resource/MembersTest.php b/SilMock/tests/Google/Service/Directory/Resource/MembersTest.php index 6e57081..95f9ca4 100644 --- a/SilMock/tests/Google/Service/Directory/Resource/MembersTest.php +++ b/SilMock/tests/Google/Service/Directory/Resource/MembersTest.php @@ -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() { @@ -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 = 'sample_group@groups.example.com'; + $mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile); + try { + $result = $mockGoogleServiceDirectory->members->hasMember( + $groupEmailAddress, + 'test@example.com' + ); + $hasMember = $result['isMember'] ?? false; + } catch (Exception $exception) { + self::fail( + $exception->getMessage() + ); + } + self::assertTrue($hasMember); } public function testListMembers() @@ -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.' );