Skip to content

Commit

Permalink
Add delete and associated test
Browse files Browse the repository at this point in the history
  • Loading branch information
mtompset committed Jul 24, 2024
1 parent 45be77d commit 0d905e7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
15 changes: 15 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,21 @@ public function __construct(?string $dbFile = null)
parent::__construct($dbFile, 'directory', 'members');
}

public function delete(string $groupKey, string $memberKey)
{
$directoryMemberRecords = $this->getRecords();
foreach ($directoryMemberRecords as $record) {
$decodedRecordData = json_decode($record['data'], true);
if (
$decodedRecordData['groupKey'] === $groupKey
&& $decodedRecordData['member']['email'] === $memberKey
) {
$recordId = $record['id'];
$this->deleteRecordById($recordId);
}
}
}

public function get(string $groupKey, string $memberKey): GoogleDirectory_Member
{
$members = $this->listMembers($groupKey);
Expand Down
57 changes: 52 additions & 5 deletions SilMock/tests/Google/Service/Directory/Resource/MembersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function testListMembersAll()
try {
$members = $mockGoogleServiceDirectory->members->listMembers($groupEmailAddress);
} catch (Exception $exception) {
$this->failure($exception);
$this->failure($exception, 'listMembersAll');
}
self::assertNotEmpty(
$members->getMembers(),
Expand All @@ -102,7 +102,7 @@ public function testListMembersOnlyMember()
]
);
} catch (Exception $exception) {
$this->failure($exception);
$this->failure($exception, 'listMembersOnlyMember');
}
self::assertNotEmpty(
$members->getMembers(),
Expand All @@ -123,19 +123,66 @@ public function testListMembersOnlyOwner()
]
);
} catch (Exception $exception) {
$this->failure($exception);
$this->failure($exception, 'listMembersOnlyOwner');
}
self::assertEmpty(
$members->getMembers(),
'Was expecting the members.list method to have no owner types.'
);
}

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

if (! $hasMember) {
$member = new GoogleDirectory_Member();
$member->setEmail($emailAddress);
$member->setRole('MEMBER');
$mockGoogleServiceDirectory->members->insert($groupEmailAddress, $member);
}
$mockGoogleServiceDirectory->members->delete($groupEmailAddress, $emailAddress);

try {
$result = $mockGoogleServiceDirectory->members->hasMember(
$groupEmailAddress,
'[email protected]'
);
$hasMember = $result['isMember'] ?? false;
} catch (Exception $exception) {
self::fail(
$exception->getMessage()
);
}
self::assertFalse(
$hasMember,
sprintf(
'Failed to delete %s from group %s',
$emailAddress,
$groupEmailAddress
)
);
}

protected function failure(Exception $exception, string $function): void
{
self::fail(
sprintf(
'Was expecting the members.insert method to function, but got: %s',
'Was expecting the %s method to function, but got: %s',
$function,
$exception->getMessage()
)
);
Expand Down

0 comments on commit 0d905e7

Please sign in to comment.