Skip to content

Commit e09a937

Browse files
committed
refactor(ClientTest): simplify mock responses and remove unused cache
- Removed the MemoryCache instantiation in the Authenticator. - Simplified the client mock responses by using array_pad for consistency. - Cleaned up commented-out code for better readability. Signed-off-by: guanguans <ityaozm@gmail.com>
1 parent a9758c0 commit e09a937

File tree

3 files changed

+123
-17
lines changed

3 files changed

+123
-17
lines changed

src/ZohoCliq/Authenticator.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private function getToken(): string
168168
* @throws \Psr\SimpleCache\InvalidArgumentException
169169
* @throws \ReflectionException
170170
*
171-
* ```json
171+
* ```200
172172
* {
173173
* "access_token": "1000.86e0701b6f279bfad7b6a05352dc304d.3106ea5d20401799c010212da3da1",
174174
* "scope": "ZohoCliq.Webhooks.CREATE",
@@ -178,16 +178,21 @@ private function getToken(): string
178178
* }
179179
* ```
180180
*
181-
* ```json
181+
* ```200
182182
* {"error":"invalid_client_secret"}
183183
* ```
184+
*
185+
* ```400
186+
* {"error_description":"You have made too many requests continuously. Please try again after some time.","error":"Access Denied","status":"failure"}
187+
* {"error_description":"您已连续提出过多请求,请稍后再试。","error":"拒绝访问","status":"failure"}
188+
* ```
184189
*/
185190
private function refreshToken(): string
186191
{
187192
$response = $this->client->send(AccessTokenMessage::make([
188193
'client_id' => $this->clientId,
189194
'client_secret' => $this->clientSecret,
190-
]));
195+
]))->throw();
191196

192197
if (!$token = $response->json('access_token')) {
193198
throw RequestException::create($response->request(), $response);

tests/Foundation/ResponseTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,13 @@
297297
->transferStats(new TransferStats(\Mockery::spy(RequestInterface::class)))->toBeInstanceOf(TransferStats::class);
298298
})->group(__DIR__, __FILE__);
299299

300+
it('can get handlerStats', function (): void {
301+
expect(Response::fromPsrResponse(new \GuzzleHttp\Psr7\Response))
302+
->handlerStats()->toBeNull()
303+
->transferStats(new TransferStats(\Mockery::spy(RequestInterface::class)))->toBeInstanceOf(TransferStats::class)
304+
->handlerStats()->toBeArray();
305+
})->group(__DIR__, __FILE__);
306+
300307
it('can close the body', function (): void {
301308
expect(Response::fromPsrResponse(new \GuzzleHttp\Psr7\Response))
302309
->close()->toBeInstanceOf(Response::class);

tests/ZohoCliq/ClientTest.php

Lines changed: 108 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,28 @@
2121

2222
use Guanguans\Notify\Foundation\Caches\MemoryCache;
2323
use Guanguans\Notify\Foundation\Caches\NullCache;
24+
use Guanguans\Notify\Foundation\Exceptions\InvalidArgumentException;
25+
use Guanguans\Notify\Foundation\Exceptions\RequestException;
2426
use Guanguans\Notify\ZohoCliq\Authenticator;
2527
use Guanguans\Notify\ZohoCliq\Client;
28+
use Guanguans\Notify\ZohoCliq\DataCenter;
29+
use Guanguans\Notify\ZohoCliq\Messages\BotMessage;
2630
use Guanguans\Notify\ZohoCliq\Messages\ChannelMessage;
31+
use Guanguans\Notify\ZohoCliq\Messages\ChatMessage;
32+
use Guanguans\Notify\ZohoCliq\Messages\UserMessage;
2733

28-
it('can send message', function (): void {
34+
beforeEach(function (): void {
2935
$authenticator = new Authenticator(
3036
clientId: '1000.TTFROV098VVFG8NB686LR98TCDR',
3137
clientSecret: 'ffddfbd23c86a677e024003b4b8b8b7f2371ac6',
38+
// dataCenter: DataCenter::US,
3239
// cache: new MemoryCache,
3340
cache: new NullCache,
34-
client: (new \Guanguans\Notify\Foundation\Client)->mock([
41+
client: (new \Guanguans\Notify\Foundation\Client)->mock(array_pad(
42+
[],
43+
2,
3544
response(
36-
$successful = <<<'JSON'
45+
<<<'JSON'
3746
{
3847
"access_token": "1000.86e0701b6f279bfad7b6a05352dc304d.3106ea5d20401799c010212da3da1",
3948
"scope": "ZohoCliq.Webhooks.CREATE",
@@ -42,13 +51,24 @@
4251
"expires_in": 3600
4352
}
4453
JSON
45-
),
46-
response($successful),
47-
])
54+
)
55+
))
4856
);
49-
$client = new Client($authenticator);
50-
$message = ChannelMessage::make([
51-
'channel_unique_name' => 'guanguans',
57+
$this->client = (new Client($authenticator))->mock([
58+
// response('{"code":"oauthtoken_invalid","message":"Invalid OAuth token passed."}', 401),
59+
response(
60+
<<<'JSON_WRAP'
61+
{"code":"extra_key_found","message":"'content' is an extra key in the JSON Object."}
62+
JSON_WRAP,
63+
400
64+
),
65+
response(status: 204),
66+
]);
67+
$this->message = [
68+
// 'channel_unique_name' => 'announcements',
69+
// 'bot_unique_name' => 'botname',
70+
// 'chat_id' => 'CT_2242272070192345152_905914233-B1',
71+
// 'email_id' => fake()->email(),
5272
'text' => 'This is text.',
5373
'bot' => [
5474
'name' => 'This is bot name.',
@@ -96,7 +116,12 @@
96116
],
97117
],
98118
],
99-
])
119+
];
120+
});
121+
122+
it('can send bot message', function (): void {
123+
$botMessage = BotMessage::make($this->message)
124+
->botUniqueName('botname')
100125
->addSlide([
101126
'type' => 'list',
102127
'title' => 'This is slide list title.',
@@ -117,10 +142,79 @@
117142
],
118143
]);
119144

120-
expect($client)
145+
expect($this->client)->assertCanSendMessage($botMessage);
146+
})->group(__DIR__, __FILE__);
147+
148+
it('can send channel message', function (): void {
149+
$channelMessage = ChannelMessage::make($this->message)->channelUniqueName('announcements');
150+
151+
expect($this->client)->assertCanSendMessage($channelMessage);
152+
})->group(__DIR__, __FILE__);
153+
154+
it('can send chat message', function (): void {
155+
$chatMessage = ChatMessage::make($this->message)->chatId('CT_2242272070192345152_905914233-B1');
156+
157+
expect($this->client)->assertCanSendMessage($chatMessage);
158+
})->group(__DIR__, __FILE__);
159+
160+
it('can send user message', function (): void {
161+
$userMessage = UserMessage::make($this->message)->emailId(fake()->email());
162+
163+
expect($this->client)->assertCanSendMessage($userMessage);
164+
})->group(__DIR__, __FILE__);
165+
166+
it('can retry send user message', function (): void {
167+
/** @var \Guanguans\Notify\Foundation\Response $response */
168+
$response = $this
169+
->client
121170
->mock([
122-
response(status: 204),
123171
response('{"code":"oauthtoken_invalid","message":"Invalid OAuth token passed."}', 401),
172+
response(status: 204),
173+
])
174+
->send(UserMessage::make($this->message)->emailId(fake()->email()));
175+
176+
expect($response)
177+
->body()->toBeEmpty()
178+
->status()->toBe(204);
179+
})->group(__DIR__, __FILE__);
180+
181+
it('can throw InvalidArgumentException when data center is invalid', function (): void {
182+
new DataCenter('invalid_data_center');
183+
})->group(__DIR__, __FILE__)->throws(InvalidArgumentException::class);
184+
185+
it('can get token from cache', function (): void {
186+
$authenticator = new Authenticator(
187+
clientId: '1000.TTFROV098VVFG8NB686LR98TCDR',
188+
clientSecret: 'ffddfbd23c86a677e024003b4b8b8b7f2371ac6',
189+
cache: new MemoryCache,
190+
client: (new \Guanguans\Notify\Foundation\Client)->mock([
191+
response(
192+
<<<'JSON'
193+
{
194+
"access_token": "1000.86e0701b6f279bfad7b6a05352dc304d.3106ea5d20401799c010212da3da1",
195+
"scope": "ZohoCliq.Webhooks.CREATE",
196+
"api_domain": "https://www.zohoapis.com",
197+
"token_type": "Bearer",
198+
"expires_in": 3600
199+
}
200+
JSON
201+
),
202+
])
203+
);
204+
expect((string) $authenticator)->toEqual((string) $authenticator);
205+
})->group(__DIR__, __FILE__);
206+
207+
it('can throw RequestException when request failed', function (): void {
208+
expect((string) new Authenticator(
209+
clientId: '1000.TTFROV098VVFG8NB686LR98TCDR',
210+
clientSecret: 'ffddfbd23c86a677e024003b4b8b8b7f2371ac6',
211+
cache: new NullCache,
212+
client: (new \Guanguans\Notify\Foundation\Client)->mock([
213+
response(
214+
<<<'JSON'
215+
{"error":"invalid_client_secret"}
216+
JSON
217+
),
124218
])
125-
->assertCanSendMessage($message);
126-
})->group(__DIR__, __FILE__)->skip();
219+
))->toBeString();
220+
})->group(__DIR__, __FILE__)->throws(RequestException::class);

0 commit comments

Comments
 (0)