From 0d905e70be0e782d441b1b82bb79773ddfa5dc31 Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Wed, 24 Jul 2024 19:37:29 -0400 Subject: [PATCH] Add delete and associated test --- .../Service/Directory/Resource/Members.php | 15 +++++ .../Directory/Resource/MembersTest.php | 57 +++++++++++++++++-- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/SilMock/Google/Service/Directory/Resource/Members.php b/SilMock/Google/Service/Directory/Resource/Members.php index b6c4084..156c8fa 100644 --- a/SilMock/Google/Service/Directory/Resource/Members.php +++ b/SilMock/Google/Service/Directory/Resource/Members.php @@ -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); diff --git a/SilMock/tests/Google/Service/Directory/Resource/MembersTest.php b/SilMock/tests/Google/Service/Directory/Resource/MembersTest.php index f05fcdf..b14ac41 100644 --- a/SilMock/tests/Google/Service/Directory/Resource/MembersTest.php +++ b/SilMock/tests/Google/Service/Directory/Resource/MembersTest.php @@ -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(), @@ -102,7 +102,7 @@ public function testListMembersOnlyMember() ] ); } catch (Exception $exception) { - $this->failure($exception); + $this->failure($exception, 'listMembersOnlyMember'); } self::assertNotEmpty( $members->getMembers(), @@ -123,7 +123,7 @@ public function testListMembersOnlyOwner() ] ); } catch (Exception $exception) { - $this->failure($exception); + $this->failure($exception, 'listMembersOnlyOwner'); } self::assertEmpty( $members->getMembers(), @@ -131,11 +131,58 @@ public function testListMembersOnlyOwner() ); } - protected function failure(Exception $exception): void + public function testDelete() + { + $mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile); + $groupEmailAddress = 'sample_group@groups.example.com'; + $emailAddress = 'test@example.com'; + 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, + 'test@example.com' + ); + $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() ) );