Skip to content

Commit 2feb7d3

Browse files
authored
Merge pull request #51 from silinternational/develop
Add delete() to Google mock token object
2 parents b71eccf + 59db342 commit 2feb7d3

26 files changed

+576
-369
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ COPY ./ /data
2323

2424
RUN cd /data && ./composer-install.sh
2525
RUN mv /data/composer.phar /usr/bin/composer
26+
RUN /usr/bin/composer install

SilMock/DataStore/Sqlite/SqliteUtils.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace SilMock\DataStore\Sqlite;
34

45
use PDO;
@@ -100,8 +101,8 @@ public function getRecordByDataKey($dataType, $dataClass, $dataKey, $dataValue)
100101

101102
foreach ($allOfClass as $nextEntry) {
102103
$nextData = json_decode($nextEntry['data'], true);
103-
if (isset($nextData[$dataKey]) &&
104-
$nextData[$dataKey] === $dataValue) {
104+
$nextDataValue = $nextData[$dataKey] ?? null;
105+
if ($nextDataValue === $dataValue) {
105106
return $nextEntry;
106107
}
107108
}
@@ -126,8 +127,8 @@ public function getAllRecordsByDataKey($dataType, $dataClass, $dataKey, $dataVal
126127

127128
foreach ($allOfClass as $nextEntry) {
128129
$nextData = json_decode($nextEntry['data'], true);
129-
if (isset($nextData[$dataKey]) &&
130-
$nextData[$dataKey] === $dataValue) {
130+
$nextDataValue = $nextData[$dataKey] ?? null;
131+
if ($nextDataValue === $dataValue) {
131132
$foundEntries[] = $nextEntry;
132133
}
133134
}

SilMock/Google/Service/DbClass.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace SilMock\Google\Service;
4+
5+
use Exception;
6+
use SilMock\DataStore\Sqlite\SqliteUtils;
7+
use Sil\Psr3Adapters\Psr3EchoLogger;
8+
9+
class DbClass
10+
{
11+
/** @var string|null - The path (with file name) to the SQLite database. */
12+
protected ?string $dbFile;
13+
14+
/** @var string - The 'type' field to use in the database. */
15+
protected string $dataType;
16+
17+
/** @var string - The 'class' field to use in the database */
18+
protected string $dataClass;
19+
20+
protected Psr3EchoLogger $logger;
21+
22+
public function __construct(?string $dbFile, string $dataType, string $dataClass)
23+
{
24+
$this->dbFile = $dbFile;
25+
$this->dataType = $dataType;
26+
$this->dataClass = $dataClass;
27+
$this->logger = new Psr3EchoLogger();
28+
if (empty($dbFile)) {
29+
$exception = new Exception();
30+
$exceptions = explode("\n", $exception->getTraceAsString());
31+
$previousLocationMessage = $exceptions[1];
32+
$this->logger->warning(
33+
sprintf(
34+
"Empty dbFile provided:\n%s",
35+
$previousLocationMessage
36+
)
37+
);
38+
}
39+
}
40+
41+
protected function getSqliteUtils(): SqliteUtils
42+
{
43+
return new SqliteUtils($this->dbFile);
44+
}
45+
46+
protected function getRecords(): array
47+
{
48+
$sqliteUtils = $this->getSqliteUtils();
49+
return $sqliteUtils->getData($this->dataType, $this->dataClass);
50+
}
51+
52+
protected function deleteRecordById(int $recordId)
53+
{
54+
$sqliteUtils = $this->getSqliteUtils();
55+
$sqliteUtils->deleteRecordById($recordId);
56+
}
57+
58+
protected function addRecord(string $data)
59+
{
60+
$sqliteUtils = $this->getSqliteUtils();
61+
$sqliteUtils->recordData(
62+
$this->dataType,
63+
$this->dataClass,
64+
$data
65+
);
66+
}
67+
}

SilMock/Google/Service/Directory.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace SilMock\Google\Service;
34

45
use SilMock\Google\Service\Directory\Asps;
@@ -19,10 +20,10 @@ class Directory
1920
* Sets the users and users_aliases properties to be instances of
2021
* the corresponding mock classes.
2122
*
22-
* @param $client mixed - Ignored (normally it would be a Google_Client)
23-
* @param $dbFile string (optional) - The path and file name of the database file
23+
* @param mixed $client -- Ignored (normally it would be a Google_Client)
24+
* @param string|null $dbFile -- (optional) The path and file name of the database file
2425
*/
25-
public function __construct($client, $dbFile=null)
26+
public function __construct($client, $dbFile = null)
2627
{
2728
$this->asps = new Asps($dbFile);
2829
$this->tokens = new Tokens($dbFile);

SilMock/Google/Service/Directory/Asps.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,13 @@
33
namespace SilMock\Google\Service\Directory;
44

55
use Google_Service_Directory_Asps;
6+
use SilMock\Google\Service\DbClass;
67

7-
class Asps
8+
class Asps extends DbClass
89
{
9-
/** @var string - The path (with file name) to the SQLite database. */
10-
private $dbFile;
11-
12-
/** @var string - The 'type' field to use in the database. */
13-
private $dataType = 'directory';
14-
15-
/** @var string - The 'class' field to use in the database */
16-
private $dataClass = 'asps';
17-
1810
public function __construct($dbFile = null)
1911
{
20-
$this->dbFile = $dbFile;
12+
parent::__construct($dbFile, 'directory', 'asps');
2113
}
2214

2315
public function listAsps($userKey, $optParams = array())

SilMock/Google/Service/Directory/Tokens.php

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,64 @@
22

33
namespace SilMock\Google\Service\Directory;
44

5+
use Google_Service_Directory_Token;
56
use Google_Service_Directory_Tokens;
7+
use Google_Service_Exception;
8+
use SilMock\Google\Service\DbClass;
69

7-
class Tokens
10+
class Tokens extends DbClass
811
{
9-
/** @var string - The path (with file name) to the SQLite database. */
10-
private $dbFile;
11-
12-
/** @var string - The 'type' field to use in the database. */
13-
private $dataType = 'directory';
14-
15-
/** @var string - The 'class' field to use in the database */
16-
private $dataClass = 'tokens';
17-
1812
public function __construct($dbFile = null)
1913
{
20-
$this->dbFile = $dbFile;
14+
parent::__construct($dbFile, 'directory', 'tokens');
2115
}
2216

23-
public function listTokens($userKey, $optParams = array())
17+
public function listTokens($userKey, $optParams = []): Google_Service_Directory_Tokens
18+
{
19+
return new Google_Service_Directory_Tokens([ 'items' => [] ]);
20+
}
21+
22+
/**
23+
* @throws Google_Service_Exception
24+
*/
25+
public function delete($userKey, $clientId)
26+
{
27+
$this->assertIsValidUserKey($userKey);
28+
29+
foreach ($this->listTokensFor($userKey) as $recordId => $token) {
30+
/** @var Google_Service_Directory_Token $token */
31+
if ($token->clientId === $clientId) {
32+
$this->removeToken($recordId);
33+
return;
34+
}
35+
}
36+
}
37+
38+
protected function assertIsValidUserKey(string $userId)
39+
{
40+
if (! $this->isValidEmailAddress($userId)) {
41+
throw new Google_Service_Exception('Invalid userId: ' . $userId, 400);
42+
}
43+
}
44+
45+
/**
46+
* Determine whether the given string is a valid email address.
47+
*
48+
* @param string $email The email address to check.
49+
* @return bool Whether the string is a valid email address.
50+
*/
51+
protected function isValidEmailAddress(string $email): bool
52+
{
53+
return (filter_var($email, FILTER_VALIDATE_EMAIL) !== false);
54+
}
55+
56+
protected function listTokensFor(string $userId): array
57+
{
58+
return [];
59+
}
60+
61+
protected function removeToken($recordId)
2462
{
25-
return new Google_Service_Directory_Tokens(array(
26-
'items' => array(),
27-
));
63+
$this->deleteRecordById($recordId);
2864
}
2965
}

SilMock/Google/Service/Directory/UsersAliasesResource.php

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22

33
namespace SilMock\Google\Service\Directory;
44

5+
use Exception;
6+
use Google_Service_Directory_Alias as Alias;
7+
use Google_Service_Directory_Aliases;
58
use SilMock\DataStore\Sqlite\SqliteUtils;
9+
use SilMock\Google\Service\DbClass;
10+
use SilMock\Google\Service\Directory;
611

7-
class UsersAliasesResource
12+
class UsersAliasesResource extends DbClass
813
{
9-
10-
private $_dbFile; // string for the path (with file name) for the Sqlite database
11-
private $_dataType = 'directory'; // string to put in the 'type' field in the database
12-
private $_dataClass = 'users_alias'; // string to put in the 'class' field in the database
13-
14-
1514
public function __construct($dbFile = null)
1615
{
17-
$this->_dbFile = $dbFile;
16+
parent::__construct($dbFile, 'directory', 'users_alias');
1817
}
1918

2019
/**
@@ -23,7 +22,7 @@ public function __construct($dbFile = null)
2322
* @param string $userKey The email or immutable Id of the user
2423
* @param string $alias The alias to be removed
2524
* @return true|null depending on if an alias was deleted
26-
* @throws \Exception with code 201407101645
25+
* @throws Exception with code 201407101645
2726
*/
2827
public function delete($userKey, $alias)
2928
{
@@ -34,18 +33,18 @@ public function delete($userKey, $alias)
3433
}
3534

3635
// ensure that user exists in db
37-
$dir = new \SilMock\Google\Service\Directory('anything', $this->_dbFile);
36+
$dir = new Directory('anything', $this->dbFile);
3837
$matchingUsers = $dir->users->get($userKey);
3938

4039
if ($matchingUsers === null) {
41-
throw new \Exception("Account doesn't exist: " . $userKey, 201407101645);
40+
throw new Exception("Account doesn't exist: " . $userKey, 201407101645);
4241
}
4342

4443
// Get all the aliases for that user
45-
$sqliteUtils = new SqliteUtils($this->_dbFile);
44+
$sqliteUtils = new SqliteUtils($this->dbFile);
4645
$aliases = $sqliteUtils->getAllRecordsByDataKey(
47-
$this->_dataType,
48-
$this->_dataClass,
46+
$this->dataType,
47+
$this->dataClass,
4948
$key,
5049
$userKey
5150
);
@@ -73,7 +72,7 @@ public function delete($userKey, $alias)
7372
* @param string $userKey The email or immutable Id of the user
7473
* @param Alias $postBody The array/object with the data for that alias
7574
* @return Alias - a real Google_Service_Directory_Alias instance
76-
* @throws \Exception with code 201407110830 if a matching user is not found.
75+
* @throws Exception with code 201407110830 if a matching user is not found.
7776
*/
7877
public function insert($userKey, $postBody)
7978
{
@@ -84,11 +83,11 @@ public function insert($userKey, $postBody)
8483
}
8584

8685
// ensure that user exists in db
87-
$dir = new \SilMock\Google\Service\Directory('anything', $this->_dbFile);
86+
$dir = new Directory('anything', $this->dbFile);
8887
$matchingUsers = $dir->users->get($userKey);
8988

9089
if ($matchingUsers === null) {
91-
throw new \Exception("Account doesn't exist: " . $userKey, 201407110830);
90+
throw new Exception("Account doesn't exist: " . $userKey, 201407110830);
9291
}
9392

9493
if ($postBody->$key === null) {
@@ -107,19 +106,19 @@ public function insert($userKey, $postBody)
107106
public function insertAssumingUserExists($postBody)
108107
{
109108
$entryData = json_encode(get_object_vars($postBody));
110-
$sqliteUtils = new SqliteUtils($this->_dbFile);
109+
$sqliteUtils = new SqliteUtils($this->dbFile);
111110
$sqliteUtils->recordData(
112-
$this->_dataType,
113-
$this->_dataClass,
111+
$this->dataType,
112+
$this->dataClass,
114113
$entryData
115114
);
116-
$allAliases = $sqliteUtils->getData($this->_dataType, $this->_dataClass);
115+
$allAliases = $sqliteUtils->getData($this->dataType, $this->dataClass);
117116

118117
if (! $allAliases) {
119118
return null;
120119
}
121120

122-
$newAlias = new \Google_Service_Directory_Alias();
121+
$newAlias = new Alias();
123122
ObjectUtils::initialize($newAlias, $postBody);
124123

125124
return $newAlias;
@@ -131,22 +130,22 @@ public function insertAssumingUserExists($postBody)
131130
* instances for that user
132131
*
133132
* @param string $userKey - The Email or immutable Id of the user
134-
* @return a real Google_Service_Directory_Aliases instance
135-
* @throws \Exception with code 201407101420 if a matching user is not found.
133+
* @return Google_Service_Directory_Aliases|null
134+
* @throws Exception with code 201407101420 if a matching user is not found.
136135
*/
137-
public function listUsersAliases($userKey)
136+
public function listUsersAliases($userKey): ?Google_Service_Directory_Aliases
138137
{
139138
// If the $userKey is not an email address, it must be an id
140139
$key = 'primaryEmail';
141140
if (! filter_var($userKey, FILTER_VALIDATE_EMAIL)) {
142141
$key = 'id';
143142
}
144143
// ensure that user exists in db
145-
$dir = new \SilMock\Google\Service\Directory('anything', $this->_dbFile);
144+
$dir = new Directory('anything', $this->dbFile);
146145
$matchingUsers = $dir->users->get($userKey);
147146

148147
if ($matchingUsers === null) {
149-
throw new \Exception("Account doesn't exist: " . $userKey, 201407101420);
148+
throw new Exception("Account doesn't exist: " . $userKey, 201407101420);
150149
}
151150

152151
$foundAliases = $this->fetchAliasesByUser($key, $userKey);
@@ -161,14 +160,14 @@ public function listUsersAliases($userKey)
161160
*
162161
* @param string $keyType - "Email" or "Id"
163162
* @param string $userKey - The Email or immutable Id of the user
164-
* @return null|Google_Service_Directory_Aliases
163+
* @return Google_Service_Directory_Aliases|null
165164
*/
166-
public function fetchAliasesByUser($keyType, $userKey)
165+
public function fetchAliasesByUser($keyType, $userKey): ?Google_Service_Directory_Aliases
167166
{
168-
$sqliteUtils = new SqliteUtils($this->_dbFile);
167+
$sqliteUtils = new SqliteUtils($this->dbFile);
169168
$aliases = $sqliteUtils->getAllRecordsByDataKey(
170-
$this->_dataType,
171-
$this->_dataClass,
169+
$this->dataType,
170+
$this->dataClass,
172171
$keyType,
173172
$userKey
174173
);
@@ -180,7 +179,7 @@ public function fetchAliasesByUser($keyType, $userKey)
180179
$foundAliases = array();
181180

182181
foreach ($aliases as $nextAlias) {
183-
$newAlias = new \Google_Service_Directory_Alias();
182+
$newAlias = new Alias();
184183
ObjectUtils::initialize($newAlias, json_decode($nextAlias['data'], true));
185184

186185
$foundAliases[] = $newAlias;

0 commit comments

Comments
 (0)