silinternational / google-api-php-client-mock
Attempting to create an intelligent mock of the Google API PHP Client for unit and functional testing.
Installs: 14 862
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 1
Open Issues: 0
Requires
- php: ^8.2
- ext-json: *
- ext-mbstring: *
- ext-pdo: *
- google/apiclient: ^v2.15.0
- google/apiclient-services: ^v0.312.0
- webmozart/assert: 1.11.0
Requires (Dev)
- phpunit/phpunit: ^9.0
- silinternational/psr3-adapters: ^3.0
- dev-develop
- 2.14.0
- 2.13.2
- 2.13.1
- 2.13.0
- 2.12.0
- 2.11.4
- 2.11.3
- 2.11.2
- 2.11.1
- 2.11.0
- 2.10.0
- 2.9.0
- 2.8.0
- 2.7.0
- 2.6.0
- 2.5.1
- 2.5.0
- 2.4.0
- 2.3.0
- 2.2.1
- 2.2.0
- 2.1.0
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.0
- 1.4.0
- 1.3.1
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- 0.9.0
- 0.8.1
- 0.8.0
- 0.7.1
- 0.7.0
- 0.6.1
- 0.6.0
- 0.5.1
- 0.5.0
- 0.4.0
- 0.3.2
- 0.3.0
- 0.2.1
- 0.2
- 0.1
- dev-main
- dev-feature/add-domain-list
- dev-feature/search-by-group-aliases
- dev-main-1.x
- dev-develop-1.x
This package is auto-updated.
Last update: 2024-12-12 18:27:50 UTC
README
A small scale intelligent mock of the Google API PHP Client for unit and functional testing.
Overview
This is intended to mock a portion of the Google APIs related to Google Workspace accounts, particularly calls relating to users and users' aliases.
Directory
Properties of a Google Service Directory (GSD) include...
- $asps, which gets set to a GSD Asps_Resource
- $memebers, which gets set to a GSD Members_Resource
- $users, which gets set to a GSD Users_Resource
- $users_aliases, which gets set to a GSD UsersAliases_Resource
- $tokens, which gets set to a GSD Tokens_Resource
- $twoStepVerification, which gets set to a GSD TwoStepVerification_Resource
- $verificationCodes, which gets set to a GSD VerificationCodes_Resource
Asps_Resource
An Asps_Resource is for managing a user's App Specific Passwords (ASPs). This mock implements...
- listAsps()
Members_Resource
A Members_Resource is for managing members of a group. This mock implements...
- insert()
- listMembers()
Users_Resource
A Users_Resource has various methods for managing Google Apps users. Three of these that are implemented by this mock are ...
- delete()
- get()
- insert()
- update()
- listUsers()
UsersAliases_Resource
A UsersAliases_Resource has various methods for managing Google Apps users aliases. The ones implemented by this mock are ...
- delete()
- insert()
- listUsersAliases()
Tokens_Resource
A Tokens_Resource is for managing a user's OAuth access tokens. This mock implements...
- listTokens()
TwoStepVerification_Resource
A TwoStepVerification_Resource is for turning off 2SV. This mock implements...
- turnOff()
VerificationCodes_Resource
A VerificationCodes_Resource is for managing a user's OAuth access tokens. This mock implements...
- generate()
- invalidate()
- listVerificationCodes()
Gmail
Properties of the Gmail API object include...
- $users_settings
- $users_settings_delegates
- $users_settings_forwardingAddresses
UsersSettings
Methods on the UsersSettings resource that this mock implements include...
- updateImap()
- updatePop()
UsersSettingsDelegates
Methods on the UsersSettingsDelegates resource that this mock implements include...
- create()
- delete()
- get()
- listUsersSettingsDelegates()
UsersSettingsForwardingAddresses
Methods on the UsersSettingsForwardingAddresses resource that this mock implements include...
- listUsersSettingsForwardingAddresses()
Unit Testing
You should have docker and the docker compose plugin installed. To run testing:
- make it-now
Data Persistence
In order to keep data available for use by this mock, it makes use of a Sqlite database file. The default path and name of this file are ... SilMock/DataStore/Sqlite/Google_Service_Data.db. To override this, the constructors for the UsersResource and UsersAliasesResource class accept an optional string parameter.
The database is accessed/managed by SilMock/DataStore/Sqlite/SqliteUtils.php. It has one table with four columns ...
- id = INTEGER PRIMARY KEY,
- type = TEXT, e.g. "directory",
- class = TEXT, e.g. "user" or "users_alias",
- data = TEXT
The data field contains json with key-value pairs related to the properties of the GSD objects. The data is prepared by using the php json_encode function.
Test Fixtures
There is a class to assist with dealing with data for unit tests ... SilMock\Google\Service\GoogleFixtures.php. Its constructor accepts an optional parameter for the path and name of the Sqlite database file. It has two methods ...
- addFixtures($fixtures), expecting an array of 3-element arrays (type, class, data).
- removeAllFixtures()
Unit Tests for the Mock Itself
The SilMock/tests folder includes phpunit tests for the three main portions of this mock (Directory, GoogleFixtures, SqliteUtils). These should help provide examples of how to use the mock.
Examples
Switching between the Mock and the Real GSD
public static function useRealGoogle() {
return ( ! isset (\Yii::app()->params['use_real_google']) ||
\Yii::app()->params['use_real_google']);
}
public static function getGoogleServiceDirectory($client) {
if (self::useRealGoogle()) {
return new Google\Service\Directory($client);
}
$db_path = null;
if (isset(\Yii::app()->params['googleMockDbPath'])) {
$db_path = \Yii::app()->params['googleMockDbPath'];
}
return new SilMock\Google\Service\Directory($client, $db_path);
}
Managing a User
$dir = self::getGoogleServiceDirectory($client);
$google_user = new Google\Service\Directory\User();
$google_user = $dir->users->insert($google_user);
$google_user = $dir->users->get($usersEmail);
$google_user->suspended = true;
$google_user->suspensionReason = 'ADMIN';
$account = $dir->users->update($users_email, $google_user);
Managing a User's Aliases
$dir = self::getGoogleServiceDirectory($client);
$google_alias = new Google\Service\Directory\Alias();
$google_alias->setAlias($alias);
$alias = $dir->users_aliases->insert($users_email, $google_alias);
$aliases = $dir->users_aliases->listUsersAliases($users_email);
$alias = $dir->users_aliases->delete($users_email, $alias);