Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Members::delete #105

Merged
merged 4 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/phpunit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ on:

env:
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
DF_RUN_TOKEN: ${{ secrets.DF_RUN_TOKEN }}
IMAGE_NAME: google-api-php-client-mock
AWS_REGION: ${{ vars.AWS_REGION }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
Expand All @@ -19,4 +18,4 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4
- name: Run unit tests
run: docker-compose -f actions-services.yml run --rm google-api-php-client-mock "./run-tests.sh"
run: docker-compose -f actions-services.yml run --rm google-api-php-client-mock "./run-tests.sh"
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