71 lines
3.0 KiB
PHP
71 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Sms;
|
|
|
|
use App\Sms\Provider\KavehNegarProvider;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Psr\Log\NullLogger;
|
|
use Symfony\Component\HttpClient\MockHttpClient;
|
|
use Symfony\Component\HttpClient\Response\MockResponse;
|
|
|
|
/**
|
|
* الزام: همهی درخواستهای کاوهنگار باید GET با query باشند (مطابق مستند رسمی)،
|
|
* هرگز POST/body — GET بدنه ندارد پس Expect: 100-continue و idle timeout رخ نمیدهد.
|
|
*/
|
|
class KavehNegarProviderTest extends TestCase
|
|
{
|
|
/** @var array<string,mixed> */
|
|
private array $captured = [];
|
|
|
|
private function provider(): KavehNegarProvider
|
|
{
|
|
$client = new MockHttpClient(function (string $method, string $url, array $options): MockResponse {
|
|
$this->captured = ['method' => $method, 'url' => $url];
|
|
return new MockResponse(json_encode(['return' => ['status' => 200]]));
|
|
});
|
|
|
|
return new KavehNegarProvider($client, new NullLogger(), 'TESTKEY', '10004346');
|
|
}
|
|
|
|
public function testSendTemplateUsesGetWithQuery(): void
|
|
{
|
|
$ok = $this->provider()->sendTemplate('09120671756', 'clinicpro-otp', ['token' => '1234']);
|
|
|
|
$this->assertTrue($ok);
|
|
$this->assertSame('GET', $this->captured['method']);
|
|
$this->assertStringContainsString('/TESTKEY/verify/lookup.json', $this->captured['url']);
|
|
$this->assertStringContainsString('receptor=09120671756', $this->captured['url']);
|
|
$this->assertStringContainsString('template=clinicpro-otp', $this->captured['url']);
|
|
$this->assertStringContainsString('token=1234', $this->captured['url']);
|
|
}
|
|
|
|
public function testSendUsesGetNotPost(): void
|
|
{
|
|
$this->provider()->send('09120671756', 'hello');
|
|
|
|
$this->assertSame('GET', $this->captured['method']);
|
|
$this->assertStringContainsString('/TESTKEY/sms/send.json', $this->captured['url']);
|
|
}
|
|
|
|
/** توکن بلند (نام فارسی) باید cap شود تا URL بزرگ نشود و کاوهنگار 431 ندهد. */
|
|
public function testLongTokenIsCapped(): void
|
|
{
|
|
$long = str_repeat('ک', 200); // 200 کاراکتر فارسی
|
|
$ok = $this->provider()->sendTemplate('09120671756', 'clinicpro-welcome', ['token' => $long]);
|
|
|
|
$this->assertTrue($ok);
|
|
parse_str(parse_url($this->captured['url'], PHP_URL_QUERY), $q);
|
|
$this->assertLessThanOrEqual(60, mb_strlen($q['token'], 'UTF-8'));
|
|
}
|
|
|
|
/** خطای 4xx کاوهنگار (مثل 431) باید false برگرداند و throw نکند. */
|
|
public function testHttpErrorReturnsFalseWithoutThrowing(): void
|
|
{
|
|
$client = new MockHttpClient(fn (): MockResponse => new MockResponse('too large', ['http_code' => 431]));
|
|
$provider = new KavehNegarProvider($client, new NullLogger(), 'TESTKEY', '10004346');
|
|
|
|
$this->assertFalse($provider->sendTemplate('09120671756', 'clinicpro-welcome', ['token' => 'x']));
|
|
$this->assertFalse($provider->send('09120671756', 'hello'));
|
|
}
|
|
}
|