diff --git a/Makefile b/Makefile index a45646a..f158afb 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ it-now: clean install test clean: docker-compose kill - docker system prune -f + docker-compose rm -f install: docker-compose run --rm cli bash -c "composer install" diff --git a/SilMock/Google/Service/Gmail/Resource/UsersSettingsForwardingAddresses.php b/SilMock/Google/Service/Gmail/Resource/UsersSettingsForwardingAddresses.php index ea0256b..ee4e769 100644 --- a/SilMock/Google/Service/Gmail/Resource/UsersSettingsForwardingAddresses.php +++ b/SilMock/Google/Service/Gmail/Resource/UsersSettingsForwardingAddresses.php @@ -2,8 +2,9 @@ namespace SilMock\Google\Service\Gmail\Resource; +use Google_Service_Exception; use Google_Service_Gmail_ListForwardingAddressesResponse; -use Webmozart\Assert\Assert; +use SilMock\DataStore\Sqlite\SqliteUtils; class UsersSettingsForwardingAddresses { @@ -20,11 +21,68 @@ public function __construct($dbFile = null) { $this->dbFile = $dbFile; } - + public function listUsersSettingsForwardingAddresses($userId, $optParams = array()) { return new Google_Service_Gmail_ListForwardingAddressesResponse(array( 'forwardingAddresses' => array(), )); } + + protected function getSqliteUtils(): SqliteUtils + { + return new SqliteUtils($this->dbFile); + } + + protected function assertIsValidUserId(string $userId) + { + if (! $this->isValidEmailAddress($userId)) { + throw new Google_Service_Exception('Invalid userId: ' . $userId, 400); + } + } + + protected function assertIsValidDelegateEmail($delegateEmail) + { + if (! $this->isValidEmailAddress($delegateEmail)) { + throw new Google_Service_Exception('Invalid delegate: ' . $delegateEmail, 400); + } + } + + /** + * Determine whether the given string is a valid email address. + * + * @param string $email The email address to check. + * @return bool Whether the string is a valid email address. + */ + protected function isValidEmailAddress(string $email): bool + { + return (filter_var($email, FILTER_VALIDATE_EMAIL) !== false); + } + + /** + * @throws Google_Service_Exception + */ + public function delete($userId, $forwardedAddress, $optParams = array()) + { + $this->assertIsValidUserId($userId); + $this->assertIsValidDelegateEmail($forwardedAddress); + + foreach ($this->listForwardingAddressesFor($userId) as $recordId => $forwardingAddress) { + if ($forwardingAddress->getForwardingEmail() === $forwardedAddress) { + $this->removeForwardingAddress($recordId); + return; + } + } + } + + protected function listForwardingAddressesFor(string $userId): array + { + return []; + } + + protected function removeForwardingAddress($recordId) + { + $sqliteUtils = $this->getSqliteUtils(); + $sqliteUtils->deleteRecordById($recordId); + } } diff --git a/SilMock/tests/Google/Service/Gmail/Resource/UsersSettingsForwardingAddressesTest.php b/SilMock/tests/Google/Service/Gmail/Resource/UsersSettingsForwardingAddressesTest.php new file mode 100644 index 0000000..40496da --- /dev/null +++ b/SilMock/tests/Google/Service/Gmail/Resource/UsersSettingsForwardingAddressesTest.php @@ -0,0 +1,46 @@ +emptyFixturesDataFile(); + } + + private function emptyFixturesDataFile() + { + $fixturesClass = new GoogleFixtures($this->dataFile); + $fixturesClass->removeAllFixtures(); + } + + protected function tearDown(): void + { + $this->emptyFixturesDataFile(); + } + + public function testListUsersSettingsForwardingAddresses() + { + $accountEmail = 'john_smith@example.org'; + $forwardingAddresses = new UsersSettingsForwardingAddresses(); + $result = $forwardingAddresses->listUsersSettingsForwardingAddresses($accountEmail); + // Because this is totally a skeleton. + Assert::assertEmpty($result); + } + + public function testDelete() + { + $accountEmail = 'john_smith@example.org'; + $forwardingAddresses = new UsersSettingsForwardingAddresses(); + $forwardingAddresses->delete($accountEmail, $accountEmail); + Assert::assertTrue(true, 'Because a skeleton should not explode.'); + } +}