Skip to content

Commit

Permalink
Merge pull request #48 from silinternational/develop
Browse files Browse the repository at this point in the history
Add delete() to UserSettingsForwardingAddresses
  • Loading branch information
mtompset authored Mar 16, 2022
2 parents d7bc07c + 8d40bc8 commit b71eccf
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace SilMock\tests\Google\Service\Gmail\Resource;

use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use SilMock\Google\Service\Gmail\Resource\UsersSettingsForwardingAddresses;
use SilMock\Google\Service\GoogleFixtures;

class UsersSettingsForwardingAddressesTest extends TestCase
{
public $dataFile = DATAFILE4;

protected function setUp(): void
{
$this->emptyFixturesDataFile();
}

private function emptyFixturesDataFile()
{
$fixturesClass = new GoogleFixtures($this->dataFile);
$fixturesClass->removeAllFixtures();
}

protected function tearDown(): void
{
$this->emptyFixturesDataFile();
}

public function testListUsersSettingsForwardingAddresses()
{
$accountEmail = '[email protected]';
$forwardingAddresses = new UsersSettingsForwardingAddresses();
$result = $forwardingAddresses->listUsersSettingsForwardingAddresses($accountEmail);
// Because this is totally a skeleton.
Assert::assertEmpty($result);
}

public function testDelete()
{
$accountEmail = '[email protected]';
$forwardingAddresses = new UsersSettingsForwardingAddresses();
$forwardingAddresses->delete($accountEmail, $accountEmail);
Assert::assertTrue(true, 'Because a skeleton should not explode.');
}
}

0 comments on commit b71eccf

Please sign in to comment.