createUser(['ROLE_DOCTOR', 'ROLE_CLINIC']); $doctor = new Doctor($user, 'دکتر تست'); $this->em->persist($doctor); $clinic = new Clinic($user); $clinic->setName('کلینیک تست'); $this->em->persist($clinic); $this->em->flush(); return [$user, $doctor, $clinic]; } private function makePeriod(): SubscriptionPeriod { $plan = new SubscriptionPlan('pro-' . bin2hex(random_bytes(4)), 5, 2, ['patient_records' => true]); $this->em->persist($plan); $period = new SubscriptionPeriod($plan, 'سه ماهه', 3, 1_000_000); $this->em->persist($period); $this->em->flush(); return $period; } private function setActiveContext(User $user, string $dbUuid, string $dbType): void { $this->em->persist(new UserActiveContext($user, $dbUuid, $dbType)); $this->em->flush(); } private function service(): SubscriptionService { return static::getContainer()->get(SubscriptionService::class); } public function testGrantingTheClinicLeavesThePersonalPracticeOnFree(): void { [$user, $doctor, $clinic] = $this->makeUserWithBothEnvironments(); $period = $this->makePeriod(); $this->service()->grant('clinic', $clinic->getId(), $period->getUuid(), $user); self::assertSame( $period->getPlan()->getId(), $this->service()->getEffectivePlan('clinic', $clinic->getId())?->getId(), ); self::assertSame('free', $this->service()->getEffectivePlan('doctor', $doctor->getId())?->getName()); self::assertNull($this->service()->getActiveSubscription('doctor', $doctor->getId())); } public function testMyReportsTheClinicPlanOnlyWhileStandingInTheClinic(): void { [$user, $doctor, $clinic] = $this->makeUserWithBothEnvironments(); $period = $this->makePeriod(); $this->service()->grant('clinic', $clinic->getId(), $period->getUuid(), $user); $this->setActiveContext($user, $clinic->getUuid(), EntityContext::TYPE_CLINIC); $body = $this->authJson('GET', '/api/v1/subscription/my', $user); self::assertSame($period->getPlan()->getName(), $body['data']['effective_plan']['name']); self::assertNotNull($body['data']['subscription']); } public function testMyFallsBackToFreeWhileStandingInThePersonalPractice(): void { [$user, $doctor, $clinic] = $this->makeUserWithBothEnvironments(); $period = $this->makePeriod(); $this->service()->grant('clinic', $clinic->getId(), $period->getUuid(), $user); $this->setActiveContext($user, $doctor->getUuid(), EntityContext::TYPE_DOCTOR); $body = $this->authJson('GET', '/api/v1/subscription/my', $user); self::assertSame('free', $body['data']['effective_plan']['name']); self::assertNull($body['data']['subscription']); } }