Skip to content

Commit

Permalink
Merge pull request #102 from silinternational/develop
Browse files Browse the repository at this point in the history
Release 2.9.0: Add batching simulation to mock
  • Loading branch information
mtompset authored Jun 24, 2024
2 parents 1b804ee + eeff34b commit efa3d4d
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 50 deletions.
18 changes: 18 additions & 0 deletions SilMock/Google/Http/Batch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace SilMock\Google\Http;

class Batch
{
public array $batch = [];

public function add($result, string $key)
{
$this->batch[$key] = $result;
}

public function execute()
{
return $this->batch;
}
}
18 changes: 17 additions & 1 deletion SilMock/Google/Service/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace SilMock\Google\Service;

use Google\Client;
use SilMock\Google\Http\Batch;
use SilMock\Google\Service\Directory\Asps;
use SilMock\Google\Service\Directory\Resource\Groups;
use SilMock\Google\Service\Directory\Resource\Members;
Expand All @@ -10,6 +12,7 @@
use SilMock\Google\Service\Directory\UsersAliasesResource;
use SilMock\Google\Service\Directory\UsersResource;
use SilMock\Google\Service\Directory\VerificationCodesResource;
use Webmozart\Assert\Assert;

class Directory
{
Expand All @@ -21,7 +24,8 @@ class Directory
public $users_aliases;
public $verificationCodes;
public $twoStepVerification;

public Client $client;

/**
* Sets the users and users_aliases properties to be instances of
* the corresponding mock classes.
Expand All @@ -39,5 +43,17 @@ public function __construct($client, ?string $dbFile = null)
$this->users_aliases = new UsersAliasesResource($dbFile);
$this->verificationCodes = new VerificationCodesResource($dbFile);
$this->twoStepVerification = new TwoStepVerification($dbFile);
$this->client = new Client();
Assert::notEmpty($client, 'Expecting a client to be passed!');
}

public function getClient()
{
return $this->client;
}

public function createBatch()
{
return new Batch();
}
}
22 changes: 8 additions & 14 deletions SilMock/Google/Service/Directory/Resource/Members.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public function listMembers($groupKey, $optParams = [])
$this->validateGroupExists($groupKey);
$pageSize = $optParams['pageSize'] ?? 10;
$pageToken = $optParams['pageToken'] ?? 0;
$query = $optParams['query'] ?? null;
$expectedRoles = $this->extractRoles($query);
$roles = $optParams['roles'] ?? null;
$expectedRoles = $this->extractRoles($roles);
$members = new GoogleDirectory_Members();
$directoryMemberRecords = $this->getRecords();
$memberCounter = 0;
Expand All @@ -81,8 +81,8 @@ public function listMembers($groupKey, $optParams = [])
&& $memberCounter >= ($pageToken * $pageSize) // Matches the subsection of all the members
&& (empty($expectedRoles) || in_array($memberData['member']['role'], $expectedRoles)) // Matches role
) {
$memberCounter = $memberCounter + 1;
$this->addToMembers($memberData, $members);
$memberCounter = $memberCounter + 1;
$this->addToMembers($memberData, $members);
}
$currentMembers = $members->getMembers();
$currentResultSize = count($currentMembers);
Expand All @@ -98,17 +98,11 @@ public function listMembers($groupKey, $optParams = [])
return $members;
}

protected function extractRoles(?string $query): array
protected function extractRoles(?string $roles): array
{
if (! empty($query) && str_contains($query, 'roles')) {
$roleSegmentStart = substr($query, strpos($query, 'roles'));
$roleSegmentEnd = strrpos($roleSegmentStart, ' ');
if ($roleSegmentEnd === false) {
$roleSegmentEnd = strlen($roleSegmentStart);
}
$roleSegment = trim(substr($roleSegmentStart, 0, $roleSegmentEnd));
$roleValue = substr($roleSegment, 6); // roles= is 0-5
$expectedRoles = explode(',', $roleValue);
if (! empty($roles)) {
$allExpectedRoles = explode(',', $roles);
$expectedRoles = array_map(function ($role) { return mb_strtoupper(trim($role)); }, $allExpectedRoles);
} else {
$expectedRoles = [];
}
Expand Down
24 changes: 24 additions & 0 deletions SilMock/tests/Google/Http/BatchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Http;

use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use SilMock\Google\Http\Batch;

class BatchTest extends TestCase
{
public string $dataFile = DATAFILE5;
public const GROUP_EMAIL_ADDRESS = '[email protected]';

public function testAddAndExecute()
{
$batch = new Batch();
$batch->add('RESULT', 'key');
$results = $batch->execute();
$expected = [
'key' => 'RESULT',
];
Assert::assertEquals($expected, $results);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function testListMembersOnlyMember()
$members = $mockGoogleServiceDirectory->members->listMembers(
$groupEmailAddress,
[
'query' => 'roles=MEMBER'
'roles' => 'MEMBER'
]
);
} catch (Exception $exception) {
Expand All @@ -119,7 +119,7 @@ public function testListMembersOnlyOwner()
$members = $mockGoogleServiceDirectory->members->listMembers(
$groupEmailAddress,
[
'query' => 'roles=OWNER'
'roles' => 'OWNER'
]
);
} catch (Exception $exception) {
Expand Down
22 changes: 19 additions & 3 deletions SilMock/tests/Google/Service/DirectoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace SilMock\tests\Google\Service;

use Google\Client;
use Google\Service\Directory\Alias as Google_Service_Directory_Alias;
use Google\Service\Directory\User as Google_Service_Directory_User;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Google_Service_Directory_Alias;
use Google_Service_Directory_User;
use SilMock\Google\Http\Batch;
use SilMock\Google\Service\Directory;
use SilMock\Google\Service\Directory\ObjectUtils;
use SilMock\DataStore\Sqlite\SqliteUtils;
Expand Down Expand Up @@ -43,6 +46,12 @@ public function getProperties($object, $propKeys = null): array
return $outArray;
}

public function testCreateBatch()
{
$batch = new Batch();
self::assertInstanceOf(Batch::class, $batch);
}

public function testDirectory()
{
$expectedKeys = [
Expand All @@ -54,7 +63,7 @@ public function testDirectory()
];
$errorMessage = " *** Directory was not initialized properly";

$directory = new Directory('whatever', $this->dataFile);
$directory = new Directory('anyclient', $this->dataFile);

$directoryAsJson = json_encode($directory);
$directoryInfo = json_decode($directoryAsJson, true);
Expand All @@ -64,6 +73,13 @@ public function testDirectory()
}
}

public function testGetClient()
{
$dir = new Directory('anyclient', $this->dataFile);
$client = $dir->getClient();
Assert::assertInstanceOf(Client::class, $client);
}

public function testUsersInsert()
{
$newUser = $this->setupSampleUser($this->dataFile);
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"ext-mbstring": "*",
"ext-pdo": "*",
"google/apiclient": "^v2.15.0",
"google/apiclient-services": "^v0.312.0"
"google/apiclient-services": "^v0.312.0",
"webmozart/assert": "1.11.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
Expand Down
Loading

0 comments on commit efa3d4d

Please sign in to comment.