createUser() drew a random mobile and recovered from a collision by catching the unique-constraint violation and calling resetManager(). That hands back a brand-new EntityManager, which detaches every entity the running test had built so far; its next flush died with "Multiple non-persisted new entities were found", always in a different test and never reproducible in isolation. The number is now checked before the insert, so the collision never reaches the database and the manager stays open. testParentIdAddsNoQueryPerSpecialty counted queries on the first request of each size, so one-shot per-process caches — site config, subscription plan, Doctrine metadata — landed inside the count or not depending on which tests had run before it. Both requests are now warmed first; the assertion measures steady-state growth, which is what it was always about. The 23 PHPUnit notices were all one complaint: doubles created with createMock() that never had an expectation. The ones that only stub return values became createStub(); in SmsServiceLookupOnlyTest the provider and the bus got the expectations they were missing, since "dispatch does not touch the provider" and "sendNow does not enqueue" are exactly what that suite is there to prove. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
131 lines
5.8 KiB
PHP
131 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Sms;
|
|
|
|
use App\Sms\Entity\SmsLog;
|
|
use App\Sms\Message\SendSmsMessage;
|
|
use App\Sms\Provider\KavehNegarProvider;
|
|
use App\Sms\Provider\RanginehProvider;
|
|
use App\Sms\Repository\SmsLogRepository;
|
|
use App\Sms\Repository\SmsMessageTemplateRepository;
|
|
use App\Sms\Service\SmsService;
|
|
use App\Sms\Service\SmsTextResolver;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
use PHPUnit\Framework\MockObject\Stub;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\Messenger\Envelope;
|
|
use Symfony\Component\Messenger\MessageBusInterface;
|
|
|
|
/**
|
|
* سیاست lookup-only: هیچ مسیری نباید به sms/send.json خام (provider->send) سقوط کند.
|
|
*/
|
|
class SmsServiceLookupOnlyTest extends TestCase
|
|
{
|
|
private KavehNegarProvider&MockObject $kavenegar;
|
|
private SmsLogRepository&MockObject $logRepo;
|
|
private MessageBusInterface&MockObject $bus;
|
|
private SmsMessageTemplateRepository&Stub $messageTemplateRepo;
|
|
private SmsTextResolver&Stub $textResolver;
|
|
private SmsService $service;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->kavenegar = $this->createMock(KavehNegarProvider::class);
|
|
$this->kavenegar->method('getName')->willReturn('kavenegar');
|
|
$this->logRepo = $this->createMock(SmsLogRepository::class);
|
|
$this->bus = $this->createMock(MessageBusInterface::class);
|
|
$this->messageTemplateRepo = $this->createStub(SmsMessageTemplateRepository::class);
|
|
$this->textResolver = $this->createStub(SmsTextResolver::class);
|
|
|
|
$this->service = new SmsService(
|
|
$this->kavenegar,
|
|
$this->createStub(RanginehProvider::class),
|
|
$this->logRepo,
|
|
$this->bus,
|
|
$this->messageTemplateRepo,
|
|
$this->textResolver,
|
|
$this->createStub(LoggerInterface::class),
|
|
);
|
|
}
|
|
|
|
public function testSendNowWithoutTemplateCodeRefusesRawSendAndLogsFailure(): void
|
|
{
|
|
$this->kavenegar->expects($this->never())->method('send');
|
|
$this->kavenegar->expects($this->never())->method('sendTemplate');
|
|
// sendNow مسیر همزمان است؛ نباید چیزی به صف بیندازد.
|
|
$this->bus->expects($this->never())->method('dispatch');
|
|
|
|
$captured = null;
|
|
$this->logRepo->expects($this->once())->method('save')
|
|
->willReturnCallback(function (SmsLog $log) use (&$captured) { $captured = $log; });
|
|
|
|
$msg = new SendSmsMessage('09120000000', 'hello', 'kavenegar', tag: SmsLog::TAG_GLOBAL);
|
|
$ok = $this->service->sendNow($msg);
|
|
|
|
$this->assertFalse($ok);
|
|
$this->assertNotNull($captured);
|
|
$this->assertFalse($captured->toArray()['success']);
|
|
}
|
|
|
|
public function testSendNowWithTemplateCodeUsesLookup(): void
|
|
{
|
|
$this->kavenegar->expects($this->never())->method('send');
|
|
$this->bus->expects($this->never())->method('dispatch');
|
|
$this->kavenegar->expects($this->once())->method('sendTemplate')
|
|
->with('09120000000', 'clinicpro-otp', ['token' => '1234'])
|
|
->willReturn(true);
|
|
|
|
$captured = null;
|
|
$this->logRepo->expects($this->once())->method('save')
|
|
->willReturnCallback(function (SmsLog $log) use (&$captured) { $captured = $log; });
|
|
|
|
$msg = new SendSmsMessage(
|
|
'09120000000', 'code 1234', 'kavenegar',
|
|
templateVars: ['token' => '1234'], templateCode: 'clinicpro-otp', tag: SmsLog::TAG_OTP,
|
|
);
|
|
|
|
$this->assertTrue($this->service->sendNow($msg));
|
|
// template بهکاررفته باید در SmsLog ثبت شود تا در پنل ادمین دیده شود.
|
|
$this->assertSame('clinicpro-otp', $captured->toArray()['template_code']);
|
|
}
|
|
|
|
public function testDispatchTemplateForTagWithoutTemplateDoesNotEnqueueAndLogsFailure(): void
|
|
{
|
|
// TAG_GLOBAL در DEFAULTS نیست و در DB هم چیزی نداریم → باید رد شود، نه ارسال خام.
|
|
$this->messageTemplateRepo->method('findByTag')->willReturn(null);
|
|
$this->textResolver->method('resolve')->willReturn('any body');
|
|
// صفکردن نباید در همان لحظه سراغ provider برود — نه خام و نه lookup.
|
|
$this->kavenegar->expects($this->never())->method('send');
|
|
$this->kavenegar->expects($this->never())->method('sendTemplate');
|
|
$this->bus->expects($this->never())->method('dispatch');
|
|
$this->logRepo->expects($this->once())->method('save');
|
|
|
|
$this->service->dispatchTemplate(SmsLog::TAG_GLOBAL, '09120000000', ['x' => 'y']);
|
|
}
|
|
|
|
public function testDispatchTemplateForKnownTagEnqueuesWithTemplateCode(): void
|
|
{
|
|
// TAG_OTP در DEFAULTS الگو + token_map دارد → باید با templateCode به صف برود.
|
|
$this->messageTemplateRepo->method('findByTag')->willReturn(null);
|
|
$this->textResolver->method('resolve')->willReturn('کد شما: 1234');
|
|
$this->kavenegar->expects($this->never())->method('send');
|
|
$this->kavenegar->expects($this->never())->method('sendTemplate');
|
|
|
|
$captured = null;
|
|
$this->bus->expects($this->once())->method('dispatch')
|
|
->willReturnCallback(function (SendSmsMessage $m) use (&$captured) {
|
|
$captured = $m;
|
|
return new Envelope($m);
|
|
});
|
|
$this->logRepo->expects($this->never())->method('save');
|
|
|
|
$this->service->dispatchTemplate(SmsLog::TAG_OTP, '09120000000', ['code' => '1234', 'site' => 'یزد']);
|
|
|
|
$this->assertNotNull($captured);
|
|
$this->assertSame('clinicpro-otp', $captured->templateCode);
|
|
$this->assertArrayHasKey('token', $captured->templateVars);
|
|
$this->assertSame('1234', $captured->templateVars['token']);
|
|
}
|
|
}
|