Skip to content

Commit

Permalink
Add groups.get and a corresponding test
Browse files Browse the repository at this point in the history
  • Loading branch information
mtompset committed May 28, 2024
1 parent e6060f0 commit 5329b5b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
18 changes: 15 additions & 3 deletions SilMock/Google/Service/Directory/Resource/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
}
}
28 changes: 15 additions & 13 deletions SilMock/tests/Google/Service/Directory/Resource/GroupsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '[email protected]';

public function testInsert()
{
$groupEmailAddress = '[email protected]';

$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 = '[email protected]';
$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.'
);
Expand Down

0 comments on commit 5329b5b

Please sign in to comment.