Skip to content

Commit f2c175d

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 <[email protected]>
1 parent a9758c0 commit f2c175d

File tree

2 files changed

+114
-15
lines changed

2 files changed

+114
-15
lines changed

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: 107 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,26 @@
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',
32-
// cache: new MemoryCache,
3338
cache: new NullCache,
34-
client: (new \Guanguans\Notify\Foundation\Client)->mock([
39+
client: (new \Guanguans\Notify\Foundation\Client)->mock(array_pad(
40+
[],
41+
2,
3542
response(
36-
$successful = <<<'JSON'
43+
<<<'JSON'
3744
{
3845
"access_token": "1000.86e0701b6f279bfad7b6a05352dc304d.3106ea5d20401799c010212da3da1",
3946
"scope": "ZohoCliq.Webhooks.CREATE",
@@ -42,13 +49,24 @@
4249
"expires_in": 3600
4350
}
4451
JSON
45-
),
46-
response($successful),
47-
])
52+
)
53+
))
4854
);
49-
$client = new Client($authenticator);
50-
$message = ChannelMessage::make([
51-
'channel_unique_name' => 'guanguans',
55+
$this->client = (new Client($authenticator))->mock([
56+
// response('{"code":"oauthtoken_invalid","message":"Invalid OAuth token passed."}', 401),
57+
response(
58+
<<<'JSON_WRAP'
59+
{"code":"extra_key_found","message":"'content' is an extra key in the JSON Object."}
60+
JSON_WRAP,
61+
400
62+
),
63+
response(status: 204),
64+
]);
65+
$this->message = [
66+
// 'channel_unique_name' => 'announcements',
67+
// 'bot_unique_name' => 'botname',
68+
// 'chat_id' => 'CT_2242272070192345152_905914233-B1',
69+
// 'email_id' => fake()->email(),
5270
'text' => 'This is text.',
5371
'bot' => [
5472
'name' => 'This is bot name.',
@@ -96,7 +114,12 @@
96114
],
97115
],
98116
],
99-
])
117+
];
118+
});
119+
120+
it('can send bot message', function (): void {
121+
$botMessage = BotMessage::make($this->message)
122+
->botUniqueName('botname')
100123
->addSlide([
101124
'type' => 'list',
102125
'title' => 'This is slide list title.',
@@ -117,10 +140,79 @@
117140
],
118141
]);
119142

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

0 commit comments

Comments
 (0)