Files
Gnuboard7/plugins/_bundled/sirsoft-message_bizppurio/tests/Unit/Services/BizppurioTokenServiceTest.php
T
chym1217 fd7e216639 feat(message_bizppurio): 설정 화면 KG이니시스식 개편 + 검수 모드 전환
- 설정 화면을 공용 클래스·2단 그리드·상단 안내 패널·필드 인라인 에러로 통일
- 연동 환경(dev/live)을 검수 모드 Toggle(is_test_mode)로 전환, 독립 카드로
 분리하고 운영 경고 박스를 API 연동 카드 상단에 배치
- 검수 모드를 끄면(운영) 발송 자격증명(아이디·비밀번호·발신번호)을
 required 로 강제하는 조건부 검증 필터 훅 추가
- 리포트 수신 주소를 설정 화면에서 확인·복사 (플러그인 라우트가
 config('app.url') 기준 절대 URL 반환, 코어 무수정)
- 명칭 "메시징→메시지 발송", 설명 "자동발송→발송", "(다우기술)" 제거
- 설정 저장 테스트가 실제 런타임 설정 파일을 오염시키던 결함 격리
2026-07-28 12:36:20 +09:00

167 lines
5.3 KiB
PHP

<?php
namespace Plugins\Sirsoft\MessageBizppurio\Tests\Unit\Services;
use App\Contracts\Extension\CacheInterface;
use App\Services\PluginSettingsService;
use Illuminate\Support\Facades\Http;
use Mockery;
use Plugins\Sirsoft\MessageBizppurio\Exceptions\BizppurioApiException;
use Plugins\Sirsoft\MessageBizppurio\Services\BizppurioTokenService;
use Plugins\Sirsoft\MessageBizppurio\Tests\PluginTestCase;
/**
* BizppurioTokenService — 토큰 발급·캐시 재사용·재발급 검증.
*/
class BizppurioTokenServiceTest extends PluginTestCase
{
private const IDENTIFIER = 'sirsoft-message_bizppurio';
/**
* 메모리 배열로 remember/forget 을 재현하는 CacheInterface mock.
*
* @param array<string, mixed> $store 참조로 전달되는 캐시 저장소
*/
private function makeCache(array &$store): CacheInterface
{
$mock = Mockery::mock(CacheInterface::class);
$mock->shouldReceive('remember')->andReturnUsing(
function (string $key, callable $callback) use (&$store) {
if (! array_key_exists($key, $store)) {
$store[$key] = $callback();
}
return $store[$key];
}
);
$mock->shouldReceive('forget')->andReturnUsing(
function (string $key) use (&$store) {
unset($store[$key]);
return true;
}
);
return $mock;
}
/**
* bizppurio_id/password/is_test_mode 를 반환하는 설정 mock.
*
* @param array<string, mixed> $settings
*/
private function makeSettings(array $settings): PluginSettingsService
{
$mock = Mockery::mock(PluginSettingsService::class);
$mock->shouldReceive('get')->with(self::IDENTIFIER)->andReturn($settings);
return $mock;
}
public function test_토큰_발급_후_캐시에서_재사용된다(): void
{
Http::fake([
'*/v1/token' => Http::response(['accesstoken' => 'TOKEN_A', 'type' => 'Bearer'], 200),
]);
$store = [];
$service = new BizppurioTokenService(
$this->makeCache($store),
$this->makeSettings(['bizppurio_id' => 'acct', 'password' => 'pw', 'is_test_mode' => true]),
);
$this->assertSame('TOKEN_A', $service->getToken());
// 두 번째 호출은 캐시에서 → HTTP 1회만
$this->assertSame('TOKEN_A', $service->getToken());
Http::assertSentCount(1);
}
public function test_refresh는_forget_후_재발급한다(): void
{
$tokens = ['TOKEN_A', 'TOKEN_B'];
Http::fake([
'*/v1/token' => Http::sequence()
->push(['accesstoken' => 'TOKEN_A', 'type' => 'Bearer'], 200)
->push(['accesstoken' => 'TOKEN_B', 'type' => 'Bearer'], 200),
]);
$store = [];
$service = new BizppurioTokenService(
$this->makeCache($store),
$this->makeSettings(['bizppurio_id' => 'acct', 'password' => 'pw', 'is_test_mode' => true]),
);
$this->assertSame('TOKEN_A', $service->getToken());
// refresh → 캐시 무효화 후 새 토큰
$this->assertSame('TOKEN_B', $service->refreshToken());
Http::assertSentCount(2);
}
public function test_검수_모드는_dev_도메인을_호출한다(): void
{
Http::fake([
'dev-api.bizppurio.com/*' => Http::response(['accesstoken' => 'T', 'type' => 'Bearer'], 200),
]);
$store = [];
$service = new BizppurioTokenService(
$this->makeCache($store),
$this->makeSettings(['bizppurio_id' => 'acct', 'password' => 'pw', 'is_test_mode' => true]),
);
$service->getToken();
Http::assertSent(fn ($request) => str_contains($request->url(), 'dev-api.bizppurio.com'));
}
public function test_운영_모드는_live_도메인을_호출한다(): void
{
Http::fake([
'api.bizppurio.com/*' => Http::response(['accesstoken' => 'T', 'type' => 'Bearer'], 200),
]);
$store = [];
$service = new BizppurioTokenService(
$this->makeCache($store),
$this->makeSettings(['bizppurio_id' => 'acct', 'password' => 'pw', 'is_test_mode' => false]),
);
$service->getToken();
Http::assertSent(fn ($request) => str_contains($request->url(), 'api.bizppurio.com')
&& ! str_contains($request->url(), 'dev-api.bizppurio.com'));
}
public function test_자격증명_미설정시_예외(): void
{
$store = [];
$service = new BizppurioTokenService(
$this->makeCache($store),
$this->makeSettings(['bizppurio_id' => '', 'password' => '']),
);
$this->expectException(BizppurioApiException::class);
$service->getToken();
}
public function test_http_실패시_예외(): void
{
Http::fake(['*/v1/token' => Http::response([], 500)]);
$store = [];
$service = new BizppurioTokenService(
$this->makeCache($store),
$this->makeSettings(['bizppurio_id' => 'acct', 'password' => 'pw', 'is_test_mode' => true]),
);
$this->expectException(BizppurioApiException::class);
$service->getToken();
}
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
}