Files
Gnuboard7/plugins/_bundled/sirsoft-message_bizppurio/tests/Unit/Jobs/SendMessageJobTest.php
T
chym1217 8aa395c3a1 fix(notification): 비활성 확장이 제공하던 알림 채널을 발송 대상에서 제외
비활성 상태의 확장이 등록했던 발송 수단(문자·알림톡 등)을 코어 via가
저장된 채널 설정만 보고 계속 발송·"건너뜀" 로깅하던 문제를 수정한다.
사용 가능한 채널 목록에 없는 채널은 로그 없이 조용히 제외하며,
알림 설정·템플릿은 그대로 유지되어 확장을 다시 켜면 복구된다.

아울러 발송이 타임아웃·연결실패로 최종 실패할 때 pending 으로 방치되던
이력을 failed 로 마감한다(webhook 이 먼저 확정한 건은 덮어쓰지 않음).
2026-07-28 12:40:13 +09:00

172 lines
6.4 KiB
PHP

<?php
namespace Plugins\Sirsoft\MessageBizppurio\Tests\Unit\Jobs;
use Illuminate\Http\Client\ConnectionException;
use Mockery;
use Plugins\Sirsoft\MessageBizppurio\Enums\DispatchStatus;
use Plugins\Sirsoft\MessageBizppurio\Exceptions\BizppurioApiException;
use Plugins\Sirsoft\MessageBizppurio\Jobs\SendMessageJob;
use Plugins\Sirsoft\MessageBizppurio\Models\BizppurioDispatch;
use Plugins\Sirsoft\MessageBizppurio\Repositories\Contracts\BizppurioDispatchRepositoryInterface;
use Plugins\Sirsoft\MessageBizppurio\Services\BizppurioApiClient;
use Plugins\Sirsoft\MessageBizppurio\Tests\PluginTestCase;
/**
* SendMessageJob — 성공/일시오류 재시도/영구실패 판정 + 이력 갱신 검증(Phase 4).
*/
class SendMessageJobTest extends PluginTestCase
{
private array $payload = [
'account' => 'acct', 'type' => 'sms', 'from' => '070', 'to' => '010',
'refkey' => 'ref1', 'content' => ['sms' => ['message' => 'hi']],
];
/**
* sendMessage 응답을 반환하는 ApiClient mock 을 만듭니다.
*
* @param array<string, mixed> $result sendMessage 반환값
*/
private function makeClient(array $result): BizppurioApiClient
{
$mock = Mockery::mock(BizppurioApiClient::class);
$mock->shouldReceive('sendMessage')->with($this->payload)->andReturn($result);
$mock->shouldReceive('isSuccess')
->andReturnUsing(fn ($r) => (string) ($r['code'] ?? '') === '1000');
return $mock;
}
/**
* 컨테이너에서 실제 리포지토리(Provider 바인딩)를 해석해 반환.
*/
private function dispatches(): BizppurioDispatchRepositoryInterface
{
return app(BizppurioDispatchRepositoryInterface::class);
}
/**
* refkey 로 pending 이력을 1건 seed.
*/
private function seedPending(string $refkey): BizppurioDispatch
{
return BizppurioDispatch::create([
'refkey' => $refkey,
'channel' => 'sms',
'to_number' => '01011112222',
'content' => 'hi',
'status' => DispatchStatus::Pending->value,
'source' => 'auto',
]);
}
public function test_성공시_예외없이_종료하고_이력을_sent로_갱신한다(): void
{
$dispatch = $this->seedPending('ref1');
$job = new SendMessageJob($this->payload, 'ref1');
$job->handle($this->makeClient(['code' => 1000, 'messagekey' => 'mk']), $this->dispatches());
$dispatch->refresh();
$this->assertSame(DispatchStatus::Sent, $dispatch->status);
$this->assertSame('mk', $dispatch->messagekey);
}
public function test_일시오류_결과코드는_예외를_던져_재시도한다(): void
{
$job = new SendMessageJob($this->payload, 'ref1');
$this->expectException(BizppurioApiException::class);
try {
$job->handle($this->makeClient(['code' => 5003, 'description' => 'temp']), $this->dispatches());
} catch (BizppurioApiException $e) {
$this->assertSame('5003', $e->getResultCode());
throw $e;
}
}
public function test_영구실패_결과코드는_예외없이_종료하고_이력을_failed로_갱신한다(): void
{
$dispatch = $this->seedPending('ref1');
// 3006(계정오류) = 영구실패 → 재시도 안 함(예외 없음)
$job = new SendMessageJob($this->payload, 'ref1');
$job->handle($this->makeClient(['code' => 3006, 'description' => 'account error']), $this->dispatches());
$dispatch->refresh();
$this->assertSame(DispatchStatus::Failed, $dispatch->status);
$this->assertSame('3006', $dispatch->result_code);
}
public function test_tries와_backoff_기본값(): void
{
$job = new SendMessageJob($this->payload, 'ref1');
$this->assertSame(2, $job->tries);
$this->assertSame(2, $job->backoff());
}
public function test_after_commit_활성화(): void
{
$job = new SendMessageJob($this->payload, 'ref1');
$this->assertTrue($job->afterCommit);
}
public function test_최종실패_콜백은_pending이력을_failed로_마감하고_사유를_기록한다(): void
{
// 타임아웃·연결실패는 결과코드 없는 ConnectionException 으로 재시도 소진 후 failed() 로 온다.
$dispatch = $this->seedPending('ref1');
$job = new SendMessageJob($this->payload, 'ref1');
$job->failed(new ConnectionException('cURL error 28: Connection timed out'));
$dispatch->refresh();
$this->assertSame(DispatchStatus::Failed, $dispatch->status);
$this->assertNull($dispatch->result_code); // 전송 실패라 결과코드 없음
$this->assertStringContainsString('timed out', (string) $dispatch->result_message);
}
public function test_최종실패_콜백은_bizppurio_api_exception의_결과코드를_보존한다(): void
{
$dispatch = $this->seedPending('ref1');
$job = new SendMessageJob($this->payload, 'ref1');
$job->failed(new BizppurioApiException('일시 오류', resultCode: '5003'));
$dispatch->refresh();
$this->assertSame(DispatchStatus::Failed, $dispatch->status);
$this->assertSame('5003', $dispatch->result_code);
}
public function test_이미_확정된_이력은_최종실패_콜백이_덮어쓰지_않는다(): void
{
// webhook 이 먼저 success 로 확정한 뒤 failed() 가 늦게 불려도 멱등해야 한다.
$dispatch = $this->seedPending('ref1');
$dispatch->update(['status' => DispatchStatus::Success->value, 'result_code' => '4000']);
$job = new SendMessageJob($this->payload, 'ref1');
$job->failed(new ConnectionException('timed out'));
$dispatch->refresh();
$this->assertSame(DispatchStatus::Success, $dispatch->status);
$this->assertSame('4000', $dispatch->result_code);
}
public function test_이력이_없으면_최종실패_콜백은_아무것도_하지_않는다(): void
{
// 이력 seed 없이 호출 — 예외 없이 조용히 종료해야 한다.
$job = new SendMessageJob($this->payload, 'missing-ref');
$job->failed(new ConnectionException('timed out'));
$this->assertDatabaseMissing('bizppurio_dispatches', ['refkey' => 'missing-ref']);
}
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
}