Skip to content

Commit

Permalink
test(test): add test for concurrent message sending
Browse files Browse the repository at this point in the history
- Added test case for concurrent message sending
- Mocked responses and messages for testing
- Verified response status code and body content
  • Loading branch information
guanguans committed Jul 9, 2024
1 parent 8e3b64a commit 9492bb4
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tests/Foundation/ClientTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

/** @noinspection PhpInternalEntityUsedInspection */
/** @noinspection StaticClosureCanBeUsedInspection */
/** @noinspection PhpUnhandledExceptionInspection */

Expand All @@ -16,11 +17,48 @@

namespace Guanguans\NotifyTests\Foundation;

use Guanguans\Notify\AnPush\Authenticator;
use Guanguans\Notify\AnPush\Messages\Message;
use Guanguans\Notify\Foundation\Client;
use Guanguans\Notify\Foundation\Concerns\AsNullUri;
use Illuminate\Support\Collection;
use Pest\Expectation;
use Psr\Http\Message\ResponseInterface;

it('can dump debug info', function (): void {
expect(new class extends Client {
use AsNullUri;
})->dump()->toBeInstanceOf(Client::class);
})->group(__DIR__, __FILE__);

it('can concurrent send messages', function (): void {
$authenticator = new Authenticator('FE3LGGYQZXRZ6A50BN66M42H0BY');
$client = new \Guanguans\Notify\AnPush\Client($authenticator);
$message = Message::make([
'title' => 'This is title.',
'content' => 'This is content.',
'channel' => '94412',
// 'to' => 'ov_1i8jk39d****',
]);
$body = '{"msg":"success","code":200,"data":{"msgIds":[{"channelId":"94412","msgId":1715333937401}]}}';

['messages' => $messages, 'mock' => $mock] = Collection::times(5)->reduce(
static function (array $carry) use ($message, $body): array {
$carry['messages'][] = $message;
$carry['mock'][] = response($body);

return $carry;
},
[]
);

expect($client)
->mock($mock)
->pool($messages)
->each(
fn (Expectation $expectation) => $expectation
->toBeInstanceOf(ResponseInterface::class)
->body()->toBe($body)
->status()->toBe(200)
);
})->group(__DIR__, __FILE__);

0 comments on commit 9492bb4

Please sign in to comment.