test: remove the two real sources of full-run flakiness, and the mock notices
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>
This commit is contained in:
@@ -11,6 +11,7 @@ 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;
|
||||
@@ -24,8 +25,8 @@ class SmsServiceLookupOnlyTest extends TestCase
|
||||
private KavehNegarProvider&MockObject $kavenegar;
|
||||
private SmsLogRepository&MockObject $logRepo;
|
||||
private MessageBusInterface&MockObject $bus;
|
||||
private SmsMessageTemplateRepository&MockObject $messageTemplateRepo;
|
||||
private SmsTextResolver&MockObject $textResolver;
|
||||
private SmsMessageTemplateRepository&Stub $messageTemplateRepo;
|
||||
private SmsTextResolver&Stub $textResolver;
|
||||
private SmsService $service;
|
||||
|
||||
protected function setUp(): void
|
||||
@@ -34,17 +35,17 @@ class SmsServiceLookupOnlyTest extends TestCase
|
||||
$this->kavenegar->method('getName')->willReturn('kavenegar');
|
||||
$this->logRepo = $this->createMock(SmsLogRepository::class);
|
||||
$this->bus = $this->createMock(MessageBusInterface::class);
|
||||
$this->messageTemplateRepo = $this->createMock(SmsMessageTemplateRepository::class);
|
||||
$this->textResolver = $this->createMock(SmsTextResolver::class);
|
||||
$this->messageTemplateRepo = $this->createStub(SmsMessageTemplateRepository::class);
|
||||
$this->textResolver = $this->createStub(SmsTextResolver::class);
|
||||
|
||||
$this->service = new SmsService(
|
||||
$this->kavenegar,
|
||||
$this->createMock(RanginehProvider::class),
|
||||
$this->createStub(RanginehProvider::class),
|
||||
$this->logRepo,
|
||||
$this->bus,
|
||||
$this->messageTemplateRepo,
|
||||
$this->textResolver,
|
||||
$this->createMock(LoggerInterface::class),
|
||||
$this->createStub(LoggerInterface::class),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -52,6 +53,8 @@ class SmsServiceLookupOnlyTest extends TestCase
|
||||
{
|
||||
$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')
|
||||
@@ -68,6 +71,7 @@ class SmsServiceLookupOnlyTest extends TestCase
|
||||
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);
|
||||
@@ -91,6 +95,9 @@ class SmsServiceLookupOnlyTest extends TestCase
|
||||
// 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');
|
||||
|
||||
@@ -102,6 +109,8 @@ class SmsServiceLookupOnlyTest extends TestCase
|
||||
// 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')
|
||||
|
||||
Reference in New Issue
Block a user