Skip to content

Commit efa3d4d

Browse files
authored
Merge pull request #102 from silinternational/develop
Release 2.9.0: Add batching simulation to mock
2 parents 1b804ee + eeff34b commit efa3d4d

File tree

8 files changed

+178
-50
lines changed

8 files changed

+178
-50
lines changed

SilMock/Google/Http/Batch.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace SilMock\Google\Http;
4+
5+
class Batch
6+
{
7+
public array $batch = [];
8+
9+
public function add($result, string $key)
10+
{
11+
$this->batch[$key] = $result;
12+
}
13+
14+
public function execute()
15+
{
16+
return $this->batch;
17+
}
18+
}

SilMock/Google/Service/Directory.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace SilMock\Google\Service;
44

5+
use Google\Client;
6+
use SilMock\Google\Http\Batch;
57
use SilMock\Google\Service\Directory\Asps;
68
use SilMock\Google\Service\Directory\Resource\Groups;
79
use SilMock\Google\Service\Directory\Resource\Members;
@@ -10,6 +12,7 @@
1012
use SilMock\Google\Service\Directory\UsersAliasesResource;
1113
use SilMock\Google\Service\Directory\UsersResource;
1214
use SilMock\Google\Service\Directory\VerificationCodesResource;
15+
use Webmozart\Assert\Assert;
1316

1417
class Directory
1518
{
@@ -21,7 +24,8 @@ class Directory
2124
public $users_aliases;
2225
public $verificationCodes;
2326
public $twoStepVerification;
24-
27+
public Client $client;
28+
2529
/**
2630
* Sets the users and users_aliases properties to be instances of
2731
* the corresponding mock classes.
@@ -39,5 +43,17 @@ public function __construct($client, ?string $dbFile = null)
3943
$this->users_aliases = new UsersAliasesResource($dbFile);
4044
$this->verificationCodes = new VerificationCodesResource($dbFile);
4145
$this->twoStepVerification = new TwoStepVerification($dbFile);
46+
$this->client = new Client();
47+
Assert::notEmpty($client, 'Expecting a client to be passed!');
48+
}
49+
50+
public function getClient()
51+
{
52+
return $this->client;
53+
}
54+
55+
public function createBatch()
56+
{
57+
return new Batch();
4258
}
4359
}

SilMock/Google/Service/Directory/Resource/Members.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public function listMembers($groupKey, $optParams = [])
6969
$this->validateGroupExists($groupKey);
7070
$pageSize = $optParams['pageSize'] ?? 10;
7171
$pageToken = $optParams['pageToken'] ?? 0;
72-
$query = $optParams['query'] ?? null;
73-
$expectedRoles = $this->extractRoles($query);
72+
$roles = $optParams['roles'] ?? null;
73+
$expectedRoles = $this->extractRoles($roles);
7474
$members = new GoogleDirectory_Members();
7575
$directoryMemberRecords = $this->getRecords();
7676
$memberCounter = 0;
@@ -81,8 +81,8 @@ public function listMembers($groupKey, $optParams = [])
8181
&& $memberCounter >= ($pageToken * $pageSize) // Matches the subsection of all the members
8282
&& (empty($expectedRoles) || in_array($memberData['member']['role'], $expectedRoles)) // Matches role
8383
) {
84-
$memberCounter = $memberCounter + 1;
85-
$this->addToMembers($memberData, $members);
84+
$memberCounter = $memberCounter + 1;
85+
$this->addToMembers($memberData, $members);
8686
}
8787
$currentMembers = $members->getMembers();
8888
$currentResultSize = count($currentMembers);
@@ -98,17 +98,11 @@ public function listMembers($groupKey, $optParams = [])
9898
return $members;
9999
}
100100

101-
protected function extractRoles(?string $query): array
101+
protected function extractRoles(?string $roles): array
102102
{
103-
if (! empty($query) && str_contains($query, 'roles')) {
104-
$roleSegmentStart = substr($query, strpos($query, 'roles'));
105-
$roleSegmentEnd = strrpos($roleSegmentStart, ' ');
106-
if ($roleSegmentEnd === false) {
107-
$roleSegmentEnd = strlen($roleSegmentStart);
108-
}
109-
$roleSegment = trim(substr($roleSegmentStart, 0, $roleSegmentEnd));
110-
$roleValue = substr($roleSegment, 6); // roles= is 0-5
111-
$expectedRoles = explode(',', $roleValue);
103+
if (! empty($roles)) {
104+
$allExpectedRoles = explode(',', $roles);
105+
$expectedRoles = array_map(function ($role) { return mb_strtoupper(trim($role)); }, $allExpectedRoles);
112106
} else {
113107
$expectedRoles = [];
114108
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Http;
4+
5+
use PHPUnit\Framework\Assert;
6+
use PHPUnit\Framework\TestCase;
7+
use SilMock\Google\Http\Batch;
8+
9+
class BatchTest extends TestCase
10+
{
11+
public string $dataFile = DATAFILE5;
12+
public const GROUP_EMAIL_ADDRESS = '[email protected]';
13+
14+
public function testAddAndExecute()
15+
{
16+
$batch = new Batch();
17+
$batch->add('RESULT', 'key');
18+
$results = $batch->execute();
19+
$expected = [
20+
'key' => 'RESULT',
21+
];
22+
Assert::assertEquals($expected, $results);
23+
}
24+
}

SilMock/tests/Google/Service/Directory/Resource/MembersTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function testListMembersOnlyMember()
9898
$members = $mockGoogleServiceDirectory->members->listMembers(
9999
$groupEmailAddress,
100100
[
101-
'query' => 'roles=MEMBER'
101+
'roles' => 'MEMBER'
102102
]
103103
);
104104
} catch (Exception $exception) {
@@ -119,7 +119,7 @@ public function testListMembersOnlyOwner()
119119
$members = $mockGoogleServiceDirectory->members->listMembers(
120120
$groupEmailAddress,
121121
[
122-
'query' => 'roles=OWNER'
122+
'roles' => 'OWNER'
123123
]
124124
);
125125
} catch (Exception $exception) {

SilMock/tests/Google/Service/DirectoryTest.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace SilMock\tests\Google\Service;
44

5+
use Google\Client;
6+
use Google\Service\Directory\Alias as Google_Service_Directory_Alias;
7+
use Google\Service\Directory\User as Google_Service_Directory_User;
8+
use PHPUnit\Framework\Assert;
59
use PHPUnit\Framework\TestCase;
6-
use Google_Service_Directory_Alias;
7-
use Google_Service_Directory_User;
10+
use SilMock\Google\Http\Batch;
811
use SilMock\Google\Service\Directory;
912
use SilMock\Google\Service\Directory\ObjectUtils;
1013
use SilMock\DataStore\Sqlite\SqliteUtils;
@@ -43,6 +46,12 @@ public function getProperties($object, $propKeys = null): array
4346
return $outArray;
4447
}
4548

49+
public function testCreateBatch()
50+
{
51+
$batch = new Batch();
52+
self::assertInstanceOf(Batch::class, $batch);
53+
}
54+
4655
public function testDirectory()
4756
{
4857
$expectedKeys = [
@@ -54,7 +63,7 @@ public function testDirectory()
5463
];
5564
$errorMessage = " *** Directory was not initialized properly";
5665

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

5968
$directoryAsJson = json_encode($directory);
6069
$directoryInfo = json_decode($directoryAsJson, true);
@@ -64,6 +73,13 @@ public function testDirectory()
6473
}
6574
}
6675

76+
public function testGetClient()
77+
{
78+
$dir = new Directory('anyclient', $this->dataFile);
79+
$client = $dir->getClient();
80+
Assert::assertInstanceOf(Client::class, $client);
81+
}
82+
6783
public function testUsersInsert()
6884
{
6985
$newUser = $this->setupSampleUser($this->dataFile);

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"ext-mbstring": "*",
2828
"ext-pdo": "*",
2929
"google/apiclient": "^v2.15.0",
30-
"google/apiclient-services": "^v0.312.0"
30+
"google/apiclient-services": "^v0.312.0",
31+
"webmozart/assert": "1.11.0"
3132
},
3233
"require-dev": {
3334
"phpunit/phpunit": "^9.0",

0 commit comments

Comments
 (0)