Skip to content

Commit b71eccf

Browse files
authored
Merge pull request #48 from silinternational/develop
Add delete() to UserSettingsForwardingAddresses
2 parents d7bc07c + 8d40bc8 commit b71eccf

File tree

3 files changed

+107
-3
lines changed

3 files changed

+107
-3
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ it-now: clean install test
22

33
clean:
44
docker-compose kill
5-
docker system prune -f
5+
docker-compose rm -f
66

77
install:
88
docker-compose run --rm cli bash -c "composer install"

SilMock/Google/Service/Gmail/Resource/UsersSettingsForwardingAddresses.php

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace SilMock\Google\Service\Gmail\Resource;
44

5+
use Google_Service_Exception;
56
use Google_Service_Gmail_ListForwardingAddressesResponse;
6-
use Webmozart\Assert\Assert;
7+
use SilMock\DataStore\Sqlite\SqliteUtils;
78

89
class UsersSettingsForwardingAddresses
910
{
@@ -20,11 +21,68 @@ public function __construct($dbFile = null)
2021
{
2122
$this->dbFile = $dbFile;
2223
}
23-
24+
2425
public function listUsersSettingsForwardingAddresses($userId, $optParams = array())
2526
{
2627
return new Google_Service_Gmail_ListForwardingAddressesResponse(array(
2728
'forwardingAddresses' => array(),
2829
));
2930
}
31+
32+
protected function getSqliteUtils(): SqliteUtils
33+
{
34+
return new SqliteUtils($this->dbFile);
35+
}
36+
37+
protected function assertIsValidUserId(string $userId)
38+
{
39+
if (! $this->isValidEmailAddress($userId)) {
40+
throw new Google_Service_Exception('Invalid userId: ' . $userId, 400);
41+
}
42+
}
43+
44+
protected function assertIsValidDelegateEmail($delegateEmail)
45+
{
46+
if (! $this->isValidEmailAddress($delegateEmail)) {
47+
throw new Google_Service_Exception('Invalid delegate: ' . $delegateEmail, 400);
48+
}
49+
}
50+
51+
/**
52+
* Determine whether the given string is a valid email address.
53+
*
54+
* @param string $email The email address to check.
55+
* @return bool Whether the string is a valid email address.
56+
*/
57+
protected function isValidEmailAddress(string $email): bool
58+
{
59+
return (filter_var($email, FILTER_VALIDATE_EMAIL) !== false);
60+
}
61+
62+
/**
63+
* @throws Google_Service_Exception
64+
*/
65+
public function delete($userId, $forwardedAddress, $optParams = array())
66+
{
67+
$this->assertIsValidUserId($userId);
68+
$this->assertIsValidDelegateEmail($forwardedAddress);
69+
70+
foreach ($this->listForwardingAddressesFor($userId) as $recordId => $forwardingAddress) {
71+
if ($forwardingAddress->getForwardingEmail() === $forwardedAddress) {
72+
$this->removeForwardingAddress($recordId);
73+
return;
74+
}
75+
}
76+
}
77+
78+
protected function listForwardingAddressesFor(string $userId): array
79+
{
80+
return [];
81+
}
82+
83+
protected function removeForwardingAddress($recordId)
84+
{
85+
$sqliteUtils = $this->getSqliteUtils();
86+
$sqliteUtils->deleteRecordById($recordId);
87+
}
3088
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace SilMock\tests\Google\Service\Gmail\Resource;
4+
5+
use PHPUnit\Framework\Assert;
6+
use PHPUnit\Framework\TestCase;
7+
use SilMock\Google\Service\Gmail\Resource\UsersSettingsForwardingAddresses;
8+
use SilMock\Google\Service\GoogleFixtures;
9+
10+
class UsersSettingsForwardingAddressesTest extends TestCase
11+
{
12+
public $dataFile = DATAFILE4;
13+
14+
protected function setUp(): void
15+
{
16+
$this->emptyFixturesDataFile();
17+
}
18+
19+
private function emptyFixturesDataFile()
20+
{
21+
$fixturesClass = new GoogleFixtures($this->dataFile);
22+
$fixturesClass->removeAllFixtures();
23+
}
24+
25+
protected function tearDown(): void
26+
{
27+
$this->emptyFixturesDataFile();
28+
}
29+
30+
public function testListUsersSettingsForwardingAddresses()
31+
{
32+
$accountEmail = '[email protected]';
33+
$forwardingAddresses = new UsersSettingsForwardingAddresses();
34+
$result = $forwardingAddresses->listUsersSettingsForwardingAddresses($accountEmail);
35+
// Because this is totally a skeleton.
36+
Assert::assertEmpty($result);
37+
}
38+
39+
public function testDelete()
40+
{
41+
$accountEmail = '[email protected]';
42+
$forwardingAddresses = new UsersSettingsForwardingAddresses();
43+
$forwardingAddresses->delete($accountEmail, $accountEmail);
44+
Assert::assertTrue(true, 'Because a skeleton should not explode.');
45+
}
46+
}

0 commit comments

Comments
 (0)