From 5329b5b4b6a286890f378953a291e4cd73d7ab8b Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Tue, 28 May 2024 14:40:10 -0400 Subject: [PATCH] Add groups.get and a corresponding test --- .../Service/Directory/Resource/Groups.php | 18 ++++++++++-- .../Service/Directory/Resource/GroupsTest.php | 28 ++++++++++--------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/SilMock/Google/Service/Directory/Resource/Groups.php b/SilMock/Google/Service/Directory/Resource/Groups.php index 494245f..444f33c 100644 --- a/SilMock/Google/Service/Directory/Resource/Groups.php +++ b/SilMock/Google/Service/Directory/Resource/Groups.php @@ -15,6 +15,19 @@ public function __construct(?string $dbFile = null) parent::__construct($dbFile, 'directory', 'groups'); } + public function get(string $groupKey): ?GoogleDirectory_Group + { + $mockGroupsObject = new Groups($this->dbFile); + $groupsObject = $mockGroupsObject->listGroups(); + $groups = $groupsObject->getGroups(); + foreach ($groups as $group) { + if (mb_strtolower($group->getEmail()) === mb_strtolower($groupKey)) { + return $group; + } + } + return null; + } + /** * @throws Exception */ @@ -73,10 +86,9 @@ protected function isNewGroup(string $groupKey): bool $groups = $groupsObject->getGroups(); $groupEmailAddresses = []; foreach ($groups as $group) { - $groupEmailAddresses[] = $group->getEmail(); + $groupEmailAddresses[] = mb_strtoupper($group->getEmail()); } - $uppercaseGroupEmailAddresses = array_map('mb_strtoupper', $groupEmailAddresses); $uppercaseGroupEmailAddress = mb_strtoupper($groupKey); - return ! in_array($uppercaseGroupEmailAddress, $uppercaseGroupEmailAddresses); + return ! in_array($uppercaseGroupEmailAddress, $groupEmailAddresses); } } diff --git a/SilMock/tests/Google/Service/Directory/Resource/GroupsTest.php b/SilMock/tests/Google/Service/Directory/Resource/GroupsTest.php index 1606a0b..42a59ec 100644 --- a/SilMock/tests/Google/Service/Directory/Resource/GroupsTest.php +++ b/SilMock/tests/Google/Service/Directory/Resource/GroupsTest.php @@ -12,51 +12,53 @@ class GroupsTest 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 const GROUP_EMAIL_ADDRESS = 'sample_group@groups.example.com'; public function testInsert() { - $groupEmailAddress = 'sample_group@groups.example.com'; - $group = new GoogleDirectory_Group(); - $group->setEmail($groupEmailAddress); + $group->setEmail(self::GROUP_EMAIL_ADDRESS); $group->setAliases([]); $group->setName('Sample Group'); $group->setDescription('A Sample Group used for testing'); $mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile); try { - $groups = $mockGoogleServiceDirectory->groups->listGroups($groupEmailAddress); $addedGroup = $mockGoogleServiceDirectory->groups->insert($group); } catch (Exception $exception) { - $this->assertFalse( - true, + self::fail( sprintf( 'Was expecting the groups.insert method to function, but got: %s', $exception->getMessage() ) ); } - $this->assertTrue($addedGroup instanceof GoogleDirectory_Group); + self::assertTrue($addedGroup instanceof GoogleDirectory_Group); + } + + public function testGet() + { + $mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile); + $group = $mockGoogleServiceDirectory->groups->get(self::GROUP_EMAIL_ADDRESS); + self::assertInstanceOf(GoogleDirectory_Group::class, $group); } public function testListGroups() { - $groupEmailAddress = 'sample_group@groups.example.com'; $mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile); $groups = []; try { - $groups = $mockGoogleServiceDirectory->groups->listGroups($groupEmailAddress); + $groups = $mockGoogleServiceDirectory->groups->listGroups(self::GROUP_EMAIL_ADDRESS); } catch (Exception $exception) { - $this->assertFalse( - true, + self::fail( sprintf( 'Was expecting the groups.list method to function, but got: %s', $exception->getMessage() ) ); } - $this->assertNotEmpty( + self::assertNotEmpty( $groups, 'Was expecting the groups.list method to have at least one group.' );