|
21 | 21 |
|
22 | 22 | use Guanguans\Notify\Foundation\Caches\MemoryCache; |
23 | 23 | use Guanguans\Notify\Foundation\Caches\NullCache; |
| 24 | +use Guanguans\Notify\Foundation\Exceptions\InvalidArgumentException; |
| 25 | +use Guanguans\Notify\Foundation\Exceptions\RequestException; |
24 | 26 | use Guanguans\Notify\ZohoCliq\Authenticator; |
25 | 27 | use Guanguans\Notify\ZohoCliq\Client; |
| 28 | +use Guanguans\Notify\ZohoCliq\DataCenter; |
| 29 | +use Guanguans\Notify\ZohoCliq\Messages\BotMessage; |
26 | 30 | use Guanguans\Notify\ZohoCliq\Messages\ChannelMessage; |
| 31 | +use Guanguans\Notify\ZohoCliq\Messages\ChatMessage; |
| 32 | +use Guanguans\Notify\ZohoCliq\Messages\UserMessage; |
27 | 33 |
|
28 | | -it('can send message', function (): void { |
| 34 | +beforeEach(function (): void { |
29 | 35 | $authenticator = new Authenticator( |
30 | 36 | clientId: '1000.TTFROV098VVFG8NB686LR98TCDR', |
31 | 37 | clientSecret: 'ffddfbd23c86a677e024003b4b8b8b7f2371ac6', |
32 | | - // cache: new MemoryCache, |
33 | 38 | cache: new NullCache, |
34 | | - client: (new \Guanguans\Notify\Foundation\Client)->mock([ |
| 39 | + client: (new \Guanguans\Notify\Foundation\Client)->mock(array_pad( |
| 40 | + [], |
| 41 | + 2, |
35 | 42 | response( |
36 | | - $successful = <<<'JSON' |
| 43 | + <<<'JSON' |
37 | 44 | { |
38 | 45 | "access_token": "1000.86e0701b6f279bfad7b6a05352dc304d.3106ea5d20401799c010212da3da1", |
39 | 46 | "scope": "ZohoCliq.Webhooks.CREATE", |
|
42 | 49 | "expires_in": 3600 |
43 | 50 | } |
44 | 51 | JSON |
45 | | - ), |
46 | | - response($successful), |
47 | | - ]) |
| 52 | + ) |
| 53 | + )) |
48 | 54 | ); |
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(), |
52 | 70 | 'text' => 'This is text.', |
53 | 71 | 'bot' => [ |
54 | 72 | 'name' => 'This is bot name.', |
|
96 | 114 | ], |
97 | 115 | ], |
98 | 116 | ], |
99 | | - ]) |
| 117 | + ]; |
| 118 | +}); |
| 119 | + |
| 120 | +it('can send bot message', function (): void { |
| 121 | + $botMessage = BotMessage::make($this->message) |
| 122 | + ->botUniqueName('botname') |
100 | 123 | ->addSlide([ |
101 | 124 | 'type' => 'list', |
102 | 125 | 'title' => 'This is slide list title.', |
|
117 | 140 | ], |
118 | 141 | ]); |
119 | 142 |
|
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 |
121 | 168 | ->mock([ |
122 | | - response(status: 204), |
123 | 169 | 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 | + ), |
124 | 216 | ]) |
125 | | - ->assertCanSendMessage($message); |
126 | | -})->group(__DIR__, __FILE__)->skip(); |
| 217 | + ))->toBeString(); |
| 218 | +})->group(__DIR__, __FILE__)->throws(RequestException::class); |
0 commit comments