The chain the spec asks for, plus the branch level that already has data: resource+option, resource+service, branch override, then the item's own value. Duration and price resolve independently. If they resolved together the first override would silently swallow the other value — a resource that only differs in how long it takes would also drop the branch's tariff. Each resolved value carries where it came from. Without that, the panel cannot label a number "from the branch" or "service default", and "why this number?" becomes a four-table investigation. Two rules worth stating: null means inherit while zero is a real value, so a free service keeps its zero instead of inheriting the parent's price; and an inactive offering is skipped whole, since "this resource does not perform this right now" is not the same as "I have no opinion on the numbers". The parent service is passed in rather than looked up from the item's group. The booking flow already holds both, and a reverse query would be a second way to answer a question that already has an answer in hand. Eight tests: one per level with the other levels populated so the winner is provable, plus independent resolution, the inactive skip, zero, and resolving the service itself without a parent. Suite 1272 green, phpstan at its 14-error baseline. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
197 lines
8.0 KiB
PHP
197 lines
8.0 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\ClinicService;
|
|
|
|
use App\ClinicService\Entity\ServiceBranchOverride;
|
|
use App\ClinicService\Entity\ServiceItem;
|
|
use App\ClinicService\Entity\ServiceSection;
|
|
use App\ClinicService\Service\ResourceServiceResolver;
|
|
use App\ClinicService\ValueObject\ResolvedServiceSpec;
|
|
use App\Clinic\Entity\Clinic;
|
|
use App\Doctor\Entity\DoctorAddress;
|
|
use App\Resource\Entity\ClinicResource;
|
|
use App\Resource\Entity\ResourceServiceOffering;
|
|
use App\Resource\Entity\ResourceType;
|
|
use App\Tests\ApiTestCase;
|
|
|
|
/**
|
|
* زنجیرهٔ حل مدت و قیمت: منبع+گزینه → منبع+سرویس → شعبه → پیشفرض آیتم.
|
|
*
|
|
* هر تست یک سطح را تنها میگذارد تا معلوم شود همان سطح برنده شده، نه سطحی که تصادفاً
|
|
* مقدار مشابه داشته.
|
|
*/
|
|
class ResourceServiceResolverTest extends ApiTestCase
|
|
{
|
|
private ResourceServiceResolver $resolver;
|
|
private DoctorAddress $address;
|
|
private ClinicResource $resource;
|
|
private ServiceItem $service;
|
|
private ServiceItem $option;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
// resolver مستقیم ساخته میشود، نه از کانتینر: سرویسهای اپ private اند و
|
|
// عمومیکردنشان فقط برای تست، پیکربندی را بهخاطر تست خم میکند.
|
|
$this->resolver = new ResourceServiceResolver(
|
|
$this->em->getRepository(ResourceServiceOffering::class),
|
|
$this->em->getRepository(ServiceBranchOverride::class),
|
|
);
|
|
|
|
$user = $this->createUser(['ROLE_USER', 'ROLE_CLINIC']);
|
|
$clinic = new Clinic($user);
|
|
$clinic->setName('کلینیک زنجیره');
|
|
$this->em->persist($clinic);
|
|
$this->em->flush();
|
|
|
|
$this->address = DoctorAddress::forClinic($clinic->getId());
|
|
$this->address->setName('شعبهٔ مرکزی');
|
|
$this->em->persist($this->address);
|
|
|
|
$type = new ResourceType('clinic', (int) $clinic->getId(), 'device', 'دستگاه');
|
|
$this->em->persist($type);
|
|
$this->em->flush();
|
|
|
|
$this->resource = new ClinicResource($this->address, $type, 'لیزر دایود');
|
|
$this->em->persist($this->resource);
|
|
|
|
$section = new ServiceSection('clinic', (int) $clinic->getId(), 'لیزر');
|
|
$this->em->persist($section);
|
|
$this->em->flush();
|
|
|
|
// سرویسِ والد «لیزر» و گزینهٔ «پا» — هر دو ServiceItem اند، همان مدل کاتالوگ.
|
|
$this->service = new ServiceItem($section, 'لیزر', 10_000_000);
|
|
$this->service->setDurationMinutes(60);
|
|
$this->em->persist($this->service);
|
|
|
|
$this->option = new ServiceItem($section, 'لیزر پا', 8_000_000);
|
|
$this->option->setDurationMinutes(30);
|
|
$this->em->persist($this->option);
|
|
$this->em->flush();
|
|
}
|
|
|
|
private function offer(ServiceItem $item, ?int $minutes, ?int $price, bool $active = true): ResourceServiceOffering
|
|
{
|
|
$offering = new ResourceServiceOffering($this->resource, $item);
|
|
$offering->setDurationMinutes($minutes)->setPriceRials($price)->setActive($active);
|
|
$this->em->persist($offering);
|
|
$this->em->flush();
|
|
|
|
return $offering;
|
|
}
|
|
|
|
private function branchOverride(ServiceItem $item, ?int $minutes, ?int $price): void
|
|
{
|
|
$override = new ServiceBranchOverride($item, $this->address);
|
|
$override->setSoloDurationMinutes($minutes)->setPriceRials($price);
|
|
$this->em->persist($override);
|
|
$this->em->flush();
|
|
}
|
|
|
|
private function resolve(): ResolvedServiceSpec
|
|
{
|
|
return $this->resolver->resolve($this->resource, $this->option, $this->address, $this->service);
|
|
}
|
|
|
|
// ── ✅ هر سطح، تنها ──────────────────────────────────────────────────────
|
|
|
|
public function testLevelOneResourcePlusOptionWins(): void
|
|
{
|
|
$this->offer($this->option, 15, 9_500_000);
|
|
$this->offer($this->service, 40, 12_000_000);
|
|
$this->branchOverride($this->option, 50, 11_000_000);
|
|
|
|
$spec = $this->resolve();
|
|
|
|
self::assertSame(15, $spec->durationMinutes);
|
|
self::assertSame(9_500_000, $spec->priceRials);
|
|
self::assertSame(ResolvedServiceSpec::SOURCE_RESOURCE_OPTION, $spec->durationSource);
|
|
self::assertSame(ResolvedServiceSpec::SOURCE_RESOURCE_OPTION, $spec->priceSource);
|
|
}
|
|
|
|
public function testLevelTwoResourcePlusServiceWinsWhenTheOptionHasNothing(): void
|
|
{
|
|
$this->offer($this->service, 40, 12_000_000);
|
|
$this->branchOverride($this->option, 50, 11_000_000);
|
|
|
|
$spec = $this->resolve();
|
|
|
|
self::assertSame(40, $spec->durationMinutes);
|
|
self::assertSame(12_000_000, $spec->priceRials);
|
|
self::assertSame(ResolvedServiceSpec::SOURCE_RESOURCE_SERVICE, $spec->durationSource);
|
|
}
|
|
|
|
public function testLevelThreeBranchWinsWhenTheResourceHasNothing(): void
|
|
{
|
|
$this->branchOverride($this->option, 50, 11_000_000);
|
|
|
|
$spec = $this->resolve();
|
|
|
|
self::assertSame(50, $spec->durationMinutes);
|
|
self::assertSame(11_000_000, $spec->priceRials);
|
|
self::assertSame(ResolvedServiceSpec::SOURCE_BRANCH, $spec->durationSource);
|
|
}
|
|
|
|
public function testLevelFourFallsBackToTheItemItself(): void
|
|
{
|
|
$spec = $this->resolve();
|
|
|
|
self::assertSame(30, $spec->durationMinutes);
|
|
self::assertSame(8_000_000, $spec->priceRials);
|
|
self::assertSame(ResolvedServiceSpec::SOURCE_SERVICE_DEFAULT, $spec->durationSource);
|
|
self::assertSame(ResolvedServiceSpec::SOURCE_SERVICE_DEFAULT, $spec->priceSource);
|
|
}
|
|
|
|
// ── ⚠️ مرزی ──────────────────────────────────────────────────────────────
|
|
|
|
public function testDurationAndPriceResolveIndependently(): void
|
|
{
|
|
// منبع فقط مدت را میگوید؛ قیمت باید تا سطح شعبه پایین برود.
|
|
$this->offer($this->option, 15, null);
|
|
$this->branchOverride($this->option, null, 11_000_000);
|
|
|
|
$spec = $this->resolve();
|
|
|
|
self::assertSame(15, $spec->durationMinutes);
|
|
self::assertSame(ResolvedServiceSpec::SOURCE_RESOURCE_OPTION, $spec->durationSource);
|
|
|
|
self::assertSame(11_000_000, $spec->priceRials);
|
|
self::assertSame(ResolvedServiceSpec::SOURCE_BRANCH, $spec->priceSource);
|
|
}
|
|
|
|
public function testAnInactiveOfferingIsSkippedEntirely(): void
|
|
{
|
|
// ردیف هست ولی خاموش: «این منبع فعلاً این را نمیدهد» یعنی اعدادش هم خوانده نمیشوند.
|
|
$this->offer($this->option, 15, 9_500_000, active: false);
|
|
|
|
$spec = $this->resolve();
|
|
|
|
self::assertSame(30, $spec->durationMinutes);
|
|
self::assertSame(8_000_000, $spec->priceRials);
|
|
self::assertSame(ResolvedServiceSpec::SOURCE_SERVICE_DEFAULT, $spec->durationSource);
|
|
}
|
|
|
|
public function testAFreeServiceKeepsItsZeroInsteadOfInheriting(): void
|
|
{
|
|
// صفر مقدار واقعی است، نه «حرفی ندارم» — وگرنه سرویس رایگان قیمت سرویس والد را میگرفت.
|
|
$this->offer($this->option, null, 0);
|
|
|
|
$spec = $this->resolve();
|
|
|
|
self::assertSame(0, $spec->priceRials);
|
|
self::assertSame(ResolvedServiceSpec::SOURCE_RESOURCE_OPTION, $spec->priceSource);
|
|
}
|
|
|
|
public function testResolvingTheServiceItselfNeedsNoParent(): void
|
|
{
|
|
$this->offer($this->service, 45, 13_000_000);
|
|
|
|
$spec = $this->resolver->resolve($this->resource, $this->service, $this->address);
|
|
|
|
self::assertSame(45, $spec->durationMinutes);
|
|
self::assertSame(13_000_000, $spec->priceRials);
|
|
self::assertSame(ResolvedServiceSpec::SOURCE_RESOURCE_OPTION, $spec->durationSource);
|
|
}
|
|
}
|