ShopServiceTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Unit\Service\Shop;
  4. use App\ApiResources\Organization\OrganizationCreationRequest;
  5. use App\ApiResources\Shop\NewStructureArtistPremiumTrialRequest;
  6. use App\Entity\Organization\Organization;
  7. use App\Entity\Shop\ShopRequest;
  8. use App\Enum\Organization\LegalEnum;
  9. use App\Enum\Organization\PrincipalTypeEnum;
  10. use App\Enum\Organization\SettingsProductEnum;
  11. use App\Enum\Shop\ShopRequestStatus;
  12. use App\Enum\Shop\ShopRequestType;
  13. use App\Message\Message\Shop\NewStructureArtistPremiumTrial;
  14. use App\Service\Mailer\Mailer;
  15. use App\Service\Organization\OrganizationFactory;
  16. use App\Service\Shop\ShopService;
  17. use App\Service\Shop\Trial;
  18. use App\Service\Utils\DatesUtils;
  19. use Doctrine\ORM\EntityManagerInterface;
  20. use libphonenumber\PhoneNumberUtil;
  21. use PHPUnit\Framework\MockObject\MockObject;
  22. use PHPUnit\Framework\TestCase;
  23. use Psr\Log\LoggerInterface;
  24. use Symfony\Component\Messenger\Envelope;
  25. use Symfony\Component\Messenger\MessageBusInterface;
  26. use Symfony\Component\Serializer\SerializerInterface;
  27. class TestableShopService extends ShopService
  28. {
  29. public PhoneNumberUtil $phoneNumberUtil;
  30. public function controlShopRequestData(ShopRequestType $type, array $data): void
  31. {
  32. parent::controlShopRequestData($type, $data);
  33. }
  34. public function createRequest(ShopRequestType $type, array $data): ShopRequest
  35. {
  36. return parent::createRequest($type, $data);
  37. }
  38. public function sendRequestValidationLink(ShopRequest $shopRequest): void
  39. {
  40. parent::sendRequestValidationLink($shopRequest);
  41. }
  42. public function handleNewStructureArtistPremiumTrialRequest(string $token): void
  43. {
  44. parent::handleNewStructureArtistPremiumTrialRequest($token);
  45. }
  46. public function createOrganization(NewStructureArtistPremiumTrialRequest $trialRequest): Organization
  47. {
  48. return parent::createOrganization($trialRequest);
  49. }
  50. public function generateSubdomain(string $name): string
  51. {
  52. return parent::generateSubdomain($name);
  53. }
  54. public function validateNewStructureArtistPremiumTrialRequest(array $data): void
  55. {
  56. parent::validateNewStructureArtistPremiumTrialRequest($data);
  57. }
  58. public function createOrganizationCreationRequestFromTrialRequest(
  59. NewStructureArtistPremiumTrialRequest $trialRequest,
  60. ): OrganizationCreationRequest {
  61. return parent::createOrganizationCreationRequestFromTrialRequest($trialRequest);
  62. }
  63. public function sendMailToSalesAdministration(NewStructureArtistPremiumTrialRequest $trialRequest): void
  64. {
  65. parent::sendMailToSalesAdministration($trialRequest);
  66. }
  67. public function sendConfirmationMailToRepresentative(NewStructureArtistPremiumTrialRequest $trialRequest): void
  68. {
  69. parent::sendConfirmationMailToRepresentative($trialRequest);
  70. }
  71. }
  72. /**
  73. * Unit tests for ShopService.
  74. */
  75. class ShopServiceTest extends TestCase
  76. {
  77. private readonly MockObject|EntityManagerInterface $entityManager;
  78. private readonly MockObject|Mailer $mailer;
  79. private string $publicBaseUrl;
  80. private string $adminBaseUrl;
  81. private readonly MockObject|OrganizationFactory $organizationFactory;
  82. private readonly MockObject|SerializerInterface $serializer;
  83. private readonly MockObject|LoggerInterface $logger;
  84. private readonly MockObject|MessageBusInterface $messageBus;
  85. private readonly MockObject|Trial $trial;
  86. private string $faqUrl;
  87. private string $softwareWebsiteUrl;
  88. public function setUp(): void
  89. {
  90. $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
  91. $this->mailer = $this->getMockBuilder(Mailer::class)->disableOriginalConstructor()->getMock();
  92. $this->publicBaseUrl = 'https://example.com';
  93. $this->adminBaseUrl = 'https://admin.example.com';
  94. $this->organizationFactory = $this->getMockBuilder(OrganizationFactory::class)->disableOriginalConstructor()->getMock();
  95. $this->serializer = $this->getMockBuilder(SerializerInterface::class)->disableOriginalConstructor()->getMock();
  96. $this->logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
  97. $this->messageBus = $this->getMockBuilder(MessageBusInterface::class)->disableOriginalConstructor()->getMock();
  98. $this->trial = $this->getMockBuilder(Trial::class)->disableOriginalConstructor()->getMock();
  99. $this->faqUrl = 'https://faq.example.com';
  100. $this->softwareWebsiteUrl = 'https://software.example.com';
  101. }
  102. private function getShopServiceMockFor(string $methodName): TestableShopService|MockObject
  103. {
  104. $shopService = $this
  105. ->getMockBuilder(TestableShopService::class)
  106. ->setConstructorArgs(
  107. [
  108. $this->entityManager,
  109. $this->mailer,
  110. $this->publicBaseUrl,
  111. $this->adminBaseUrl,
  112. $this->organizationFactory,
  113. $this->serializer,
  114. $this->logger,
  115. $this->messageBus,
  116. $this->trial,
  117. $this->faqUrl,
  118. $this->softwareWebsiteUrl,
  119. ]
  120. )
  121. ->setMethodsExcept([$methodName])
  122. ->getMock();
  123. return $shopService;
  124. }
  125. public function tearDown(): void
  126. {
  127. DatesUtils::clearFakeDatetime();
  128. }
  129. /**
  130. * Test registerNewShopRequest method.
  131. */
  132. public function testRegisterNewShopRequest(): void
  133. {
  134. $shopService = $this->getShopServiceMockFor('registerNewShopRequest');
  135. $type = ShopRequestType::NEW_STRUCTURE_ARTIST_PREMIUM_TRIAL;
  136. $data = [
  137. 'representativeEmail' => 'test@example.com',
  138. 'representativeFirstName' => 'John',
  139. 'representativeLastName' => 'Doe',
  140. 'structureName' => 'Test Structure',
  141. ];
  142. $shopRequest = $this->getMockBuilder(ShopRequest::class)->getMock();
  143. $shopService->expects(self::once())
  144. ->method('controlShopRequestData')
  145. ->with($type, $data);
  146. $shopService->expects(self::once())
  147. ->method('createRequest')
  148. ->with($type, $data)
  149. ->willReturn($shopRequest);
  150. $shopService->expects(self::once())
  151. ->method('sendRequestValidationLink')
  152. ->with($shopRequest);
  153. $result = $shopService->registerNewShopRequest($type, $data);
  154. $this->assertSame($shopRequest, $result);
  155. }
  156. /**
  157. * Test controlShopRequestData method for NEW_STRUCTURE_ARTIST_PREMIUM_TRIAL type.
  158. */
  159. public function testControlShopRequestDataForNewStructureArtistPremiumTrial(): void
  160. {
  161. $shopService = $this->getShopServiceMockFor('controlShopRequestData');
  162. $type = ShopRequestType::NEW_STRUCTURE_ARTIST_PREMIUM_TRIAL;
  163. $data = [
  164. 'representativeEmail' => 'test@example.com',
  165. 'representativeFirstName' => 'John',
  166. 'representativeLastName' => 'Doe',
  167. 'structureName' => 'Test Structure',
  168. ];
  169. $shopService->expects(self::once())
  170. ->method('validateNewStructureArtistPremiumTrialRequest')
  171. ->with($data);
  172. $shopService->controlShopRequestData($type, $data);
  173. }
  174. /**
  175. * Test processShopRequest method for NEW_STRUCTURE_ARTIST_PREMIUM_TRIAL type.
  176. */
  177. public function testProcessShopRequest(): void
  178. {
  179. $shopService = $this->getShopServiceMockFor('processShopRequest');
  180. $shopRequest = $this->getMockBuilder(ShopRequest::class)->getMock();
  181. $shopRequest->method('getType')->willReturn(ShopRequestType::NEW_STRUCTURE_ARTIST_PREMIUM_TRIAL);
  182. $shopRequest->method('getToken')->willReturn('test-token');
  183. $this->messageBus
  184. ->expects(self::once())
  185. ->method('dispatch')
  186. ->with(self::callback(function ($message) {
  187. return $message instanceof NewStructureArtistPremiumTrial
  188. && $message->getToken() === 'test-token';
  189. }))
  190. ->willReturn(new Envelope(new NewStructureArtistPremiumTrial('azerty')));
  191. $shopRequest
  192. ->expects(self::once())
  193. ->method('setStatus')
  194. ->with(ShopRequestStatus::VALIDATED);
  195. $this->entityManager->expects(self::once())
  196. ->method('persist')
  197. ->with($shopRequest);
  198. $this->entityManager->expects(self::once())
  199. ->method('flush');
  200. $shopService->processShopRequest($shopRequest);
  201. }
  202. /**
  203. * Test createRequest method.
  204. */
  205. public function testCreateRequest(): void
  206. {
  207. $shopService = $this->getShopServiceMockFor('createRequest');
  208. $uuidRx = '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i';
  209. $type = ShopRequestType::NEW_STRUCTURE_ARTIST_PREMIUM_TRIAL;
  210. $data = [
  211. 'representativeEmail' => 'test@example.com',
  212. 'representativeFirstName' => 'John',
  213. 'representativeLastName' => 'Doe',
  214. 'structureName' => 'Test Structure',
  215. ];
  216. $this->entityManager->expects(self::once())
  217. ->method('persist')
  218. ->with(self::callback(function ($shopRequest) use ($type, $data, $uuidRx) {
  219. return $shopRequest instanceof ShopRequest
  220. && $shopRequest->getType() === $type
  221. && $shopRequest->getData() === $data
  222. && preg_match($uuidRx, $shopRequest->getToken());
  223. }));
  224. $this->entityManager->expects(self::once())
  225. ->method('flush');
  226. $result = $shopService->createRequest($type, $data);
  227. $this->assertInstanceOf(ShopRequest::class, $result);
  228. $this->assertSame($type, $result->getType());
  229. $this->assertSame($data, $result->getData());
  230. $this->assertMatchesRegularExpression($uuidRx, $result->getToken());
  231. }
  232. /**
  233. * Test sendRequestValidationLink method.
  234. */
  235. public function testSendRequestValidationLink(): void
  236. {
  237. $shopService = $this->getShopServiceMockFor('sendRequestValidationLink');
  238. $token = 'test-token';
  239. $data = [
  240. 'representativeEmail' => 'test@example.com',
  241. 'representativeFirstName' => 'John',
  242. 'representativeLastName' => 'Doe',
  243. 'structureName' => 'Test Structure',
  244. ];
  245. $shopRequest = $this->getMockBuilder(ShopRequest::class)->getMock();
  246. $shopRequest->method('getToken')->willReturn($token);
  247. $shopRequest->method('getData')->willReturn($data);
  248. $this->mailer
  249. ->expects(self::once())
  250. ->method('main')
  251. ->with(self::callback(function ($model) use ($token, $data) {
  252. return $model->getToken() === $token
  253. && $model->getRepresentativeEmail() === $data['representativeEmail']
  254. && $model->getRepresentativeFirstName() === $data['representativeFirstName']
  255. && $model->getRepresentativeLastName() === $data['representativeLastName']
  256. && $model->getStructureName() === $data['structureName']
  257. && $model->getValidationUrl() === 'https://software.example.com/shop/try/validation?token='.$token;
  258. }));
  259. $shopRequest->expects(self::once())
  260. ->method('setStatus')
  261. ->with(ShopRequestStatus::ACTIVATION_LINK_SENT);
  262. $this->entityManager->expects(self::once())
  263. ->method('persist')
  264. ->with($shopRequest);
  265. $this->entityManager->expects(self::once())
  266. ->method('flush');
  267. $shopService->sendRequestValidationLink($shopRequest);
  268. }
  269. /**
  270. * Test handleNewStructureArtistPremiumTrialRequest method.
  271. */
  272. public function testHandleNewStructureArtistPremiumTrialRequest(): void
  273. {
  274. $shopService = $this->getShopServiceMockFor('handleNewStructureArtistPremiumTrialRequest');
  275. $token = 'test-token';
  276. $data = [
  277. 'representativeEmail' => 'test@example.com',
  278. 'representativeFirstName' => 'John',
  279. 'representativeLastName' => 'Doe',
  280. 'structureName' => 'Test Structure',
  281. ];
  282. $shopRequest = $this->getMockBuilder(ShopRequest::class)->getMock();
  283. $shopRequest->method('getToken')->willReturn($token);
  284. $shopRequest->method('getData')->willReturn($data);
  285. $this->entityManager
  286. ->expects(self::once())
  287. ->method('find')
  288. ->with(ShopRequest::class, $token)
  289. ->willReturn($shopRequest);
  290. $trialRequest = $this->getMockBuilder(NewStructureArtistPremiumTrialRequest::class)
  291. ->getMock();
  292. $this->serializer
  293. ->expects(self::once())
  294. ->method('deserialize')
  295. ->with(json_encode($data), NewStructureArtistPremiumTrialRequest::class, 'json')
  296. ->willReturn($trialRequest);
  297. $organization = $this->getMockBuilder(Organization::class)->getMock();
  298. $shopService
  299. ->expects(self::once())
  300. ->method('createOrganization')
  301. ->with($trialRequest)
  302. ->willReturn($organization);
  303. // Add expectation for password setting
  304. $trialRequest->method('getPassword')->willReturn('Password123!');
  305. $this->organizationFactory
  306. ->expects(self::once())
  307. ->method('setAdminAccountPassword')
  308. ->with($organization, 'Password123!');
  309. $this->trial
  310. ->expects(self::once())
  311. ->method('startArtistPremiumTrialForNewStructure')
  312. ->with($organization, $trialRequest);
  313. // Mock the sendMailToSalesAdministration and sendConfirmationMailToRepresentative methods
  314. $shopService->expects(self::once())
  315. ->method('sendMailToSalesAdministration')
  316. ->with($trialRequest);
  317. $shopService->expects(self::once())
  318. ->method('sendConfirmationMailToRepresentative')
  319. ->with($trialRequest);
  320. $this->logger->expects(self::once())
  321. ->method('info')
  322. ->with('Successfully processed NewStructureArtistPremiumTrial for token: '.$token);
  323. $shopService->handleNewStructureArtistPremiumTrialRequest($token);
  324. }
  325. /**
  326. * Test handleNewStructureArtistPremiumTrialRequest method with non-existent token.
  327. */
  328. public function testHandleNewStructureArtistPremiumTrialRequestWithNonExistentToken(): void
  329. {
  330. $shopService = $this->getShopServiceMockFor('handleNewStructureArtistPremiumTrialRequest');
  331. $this->entityManager->expects(self::once())
  332. ->method('find')
  333. ->with(ShopRequest::class, 'non-existent-token')
  334. ->willReturn(null);
  335. $this->logger->expects(self::once())
  336. ->method('error')
  337. ->with('Cannot find ShopRequest with token: non-existent-token');
  338. $shopService->handleNewStructureArtistPremiumTrialRequest('non-existent-token');
  339. }
  340. /**
  341. * Test handleNewStructureArtistPremiumTrialRequest method when an exception occurs.
  342. */
  343. public function testHandleNewStructureArtistPremiumTrialRequestWithException(): void
  344. {
  345. $shopService = $this->getShopServiceMockFor('handleNewStructureArtistPremiumTrialRequest');
  346. $token = 'test-token';
  347. $data = [
  348. 'representativeEmail' => 'test@example.com',
  349. 'representativeFirstName' => 'John',
  350. 'representativeLastName' => 'Doe',
  351. 'structureName' => 'Test Structure',
  352. ];
  353. $shopRequest = $this->getMockBuilder(ShopRequest::class)->getMock();
  354. $shopRequest->method('getToken')->willReturn($token);
  355. $shopRequest->method('getData')->willReturn($data);
  356. // Expect setStatus to be called with ERROR
  357. $shopRequest->expects(self::once())
  358. ->method('setStatus')
  359. ->with(ShopRequestStatus::ERROR);
  360. $this->entityManager
  361. ->expects(self::once())
  362. ->method('find')
  363. ->with(ShopRequest::class, $token)
  364. ->willReturn($shopRequest);
  365. // Simulate an exception during deserialization
  366. $this->serializer
  367. ->expects(self::once())
  368. ->method('deserialize')
  369. ->willThrowException(new \Exception('Test exception'));
  370. // Expect persist and flush to be called to save the ERROR status
  371. $this->entityManager
  372. ->expects(self::once())
  373. ->method('persist')
  374. ->with($shopRequest);
  375. $this->entityManager
  376. ->expects(self::once())
  377. ->method('flush');
  378. // Expect error to be logged
  379. $this->logger->expects(self::once())
  380. ->method('error')
  381. ->with(self::stringContains('Error processing NewStructureArtistPremiumTrial for token: '.$token));
  382. // Expect the exception to be re-thrown
  383. $this->expectException(\Exception::class);
  384. $this->expectExceptionMessage('Test exception');
  385. $shopService->handleNewStructureArtistPremiumTrialRequest($token);
  386. }
  387. /**
  388. * Test createOrganization method.
  389. */
  390. public function testCreateOrganization(): void
  391. {
  392. $shopService = $this->getShopServiceMockFor('createOrganization');
  393. $trialRequest = $this
  394. ->getMockBuilder(NewStructureArtistPremiumTrialRequest::class)
  395. ->getMock();
  396. $organizationCreationRequest = $this
  397. ->getMockBuilder(OrganizationCreationRequest::class)
  398. ->getMock();
  399. $organization = $this->getMockBuilder(Organization::class)->getMock();
  400. $shopService->expects(self::once())
  401. ->method('createOrganizationCreationRequestFromTrialRequest')
  402. ->with($trialRequest)
  403. ->willReturn($organizationCreationRequest);
  404. $this->organizationFactory
  405. ->expects(self::once())
  406. ->method('create')
  407. ->with($organizationCreationRequest)
  408. ->willReturn($organization);
  409. $result = $shopService->createOrganization($trialRequest);
  410. $this->assertSame($organization, $result);
  411. }
  412. /**
  413. * Test validateNewStructureArtistPremiumTrialRequest method with valid data.
  414. */
  415. public function testValidateNewStructureArtistPremiumTrialRequest(): void
  416. {
  417. $shopService = $this->getShopServiceMockFor('validateNewStructureArtistPremiumTrialRequest');
  418. $phoneNumberUtil = $this
  419. ->getMockBuilder(PhoneNumberUtil::class)
  420. ->disableOriginalConstructor()
  421. ->getMock();
  422. $shopService->phoneNumberUtil = $phoneNumberUtil;
  423. $trialRequest = $this
  424. ->getMockBuilder(NewStructureArtistPremiumTrialRequest::class)
  425. ->getMock();
  426. $trialRequest->method('getRepresentativePhone')->willReturn('+33123456789');
  427. $organizationCreationRequest = $this
  428. ->getMockBuilder(OrganizationCreationRequest::class)
  429. ->getMock();
  430. $shopService->expects(self::once())
  431. ->method('createOrganizationCreationRequestFromTrialRequest')
  432. ->with($trialRequest)
  433. ->willReturn($organizationCreationRequest);
  434. $phoneNumberUtil->expects(self::once())
  435. ->method('isPossibleNumber')
  436. ->with('+33123456789')
  437. ->willReturn(true);
  438. $this->organizationFactory->expects(self::once())
  439. ->method('interruptIfOrganizationExists')
  440. ->with($organizationCreationRequest);
  441. // Convert the trial request to an array for validation
  442. $data = [
  443. 'representativePhone' => '+33123456789',
  444. ];
  445. $this->serializer->expects(self::once())
  446. ->method('deserialize')
  447. ->with(json_encode($data), NewStructureArtistPremiumTrialRequest::class, 'json')
  448. ->willReturn($trialRequest);
  449. $shopService->validateNewStructureArtistPremiumTrialRequest($data);
  450. }
  451. /**
  452. * Test validateNewStructureArtistPremiumTrialRequest method with invalid phone number.
  453. */
  454. public function testValidateNewStructureArtistPremiumTrialRequestWithInvalidPhoneNumber(): void
  455. {
  456. $shopService = $this->getShopServiceMockFor('validateNewStructureArtistPremiumTrialRequest');
  457. $phoneNumberUtil = $this->getMockBuilder(PhoneNumberUtil::class)
  458. ->disableOriginalConstructor()
  459. ->getMock();
  460. $shopService->phoneNumberUtil = $phoneNumberUtil;
  461. $trialRequest = $this
  462. ->getMockBuilder(NewStructureArtistPremiumTrialRequest::class)
  463. ->getMock();
  464. $trialRequest->method('getRepresentativePhone')->willReturn('invalid-phone');
  465. // Mock the phoneNumberUtil to return false for isPossibleNumber
  466. $phoneNumberUtil->expects(self::once())
  467. ->method('isPossibleNumber')
  468. ->with('invalid-phone')
  469. ->willReturn(false);
  470. // Convert the trial request to an array for validation
  471. $data = [
  472. 'representativePhone' => 'invalid-phone',
  473. ];
  474. $this->serializer->expects(self::once())
  475. ->method('deserialize')
  476. ->with(json_encode($data), NewStructureArtistPremiumTrialRequest::class, 'json')
  477. ->willReturn($trialRequest);
  478. $this->expectException(\RuntimeException::class);
  479. $this->expectExceptionMessage('Invalid phone number');
  480. $shopService->validateNewStructureArtistPremiumTrialRequest($data);
  481. }
  482. /**
  483. * Test createOrganizationCreationRequestFromTrialRequest method with forValidationOnly=false.
  484. */
  485. public function testCreateOrganizationCreationRequestFromTrialRequestComplete(): void
  486. {
  487. $shopService = $this->getShopServiceMockFor('createOrganizationCreationRequestFromTrialRequest');
  488. $trialRequest = $this->getMockBuilder(NewStructureArtistPremiumTrialRequest::class)
  489. ->getMock();
  490. $trialRequest->method('getStructureName')->willReturn('Test Structure');
  491. $trialRequest->method('getCity')->willReturn('Test City');
  492. $trialRequest->method('getPostalCode')->willReturn('12345');
  493. $trialRequest->method('getAddress')->willReturn('Test Address');
  494. $trialRequest->method('getAddressComplement')->willReturn('Test Address Complement');
  495. $trialRequest->method('getRepresentativePhone')->willReturn('+33123456789');
  496. $trialRequest->method('getStructureEmail')->willReturn('structure@example.com');
  497. $trialRequest->method('getStructureType')->willReturn(PrincipalTypeEnum::ARTISTIC_EDUCATION_ONLY);
  498. $trialRequest->method('getLegalStatus')->willReturn(LegalEnum::ASSOCIATION_LAW_1901);
  499. $trialRequest->method('getSiren')->willReturn('123456789');
  500. $trialRequest->method('getStructureIdentifier')->willReturn('test-structure');
  501. // Set a fake date for testing
  502. $fakeDate = '2023-05-15 10:30:00';
  503. DatesUtils::setFakeDatetime($fakeDate);
  504. // Create the organization creation request
  505. $result = $shopService->createOrganizationCreationRequestFromTrialRequest($trialRequest);
  506. // Verify that all fields are set
  507. $this->assertEquals('Test Structure', $result->getName());
  508. $this->assertEquals('Test City', $result->getCity());
  509. $this->assertEquals('12345', $result->getPostalCode());
  510. $this->assertEquals('Test Address', $result->getStreetAddress1());
  511. $this->assertEquals('Test Address Complement', $result->getStreetAddress2());
  512. $this->assertEquals('structure@example.com', $result->getEmail());
  513. $this->assertEquals(PrincipalTypeEnum::ARTISTIC_EDUCATION_ONLY, $result->getPrincipalType());
  514. $this->assertEquals(LegalEnum::ASSOCIATION_LAW_1901, $result->getLegalStatus());
  515. $this->assertEquals('123456789', $result->getSiretNumber());
  516. $this->assertEquals('+33123456789', $result->getPhoneNumber());
  517. $this->assertEquals('test-structure', $result->getSubdomain());
  518. $this->assertEquals(SettingsProductEnum::FREEMIUM, $result->getProduct());
  519. $this->assertFalse($result->getCreateWebsite());
  520. $this->assertFalse($result->isClient());
  521. $this->assertEquals(new \DateTime($fakeDate), $result->getCreationDate());
  522. }
  523. /**
  524. * Test sendMailToSalesAdministration method.
  525. */
  526. public function testSendMailToSalesAdministration(): void
  527. {
  528. $shopService = $this->getShopServiceMockFor('sendMailToSalesAdministration');
  529. $trialRequest = $this->getMockBuilder(NewStructureArtistPremiumTrialRequest::class)
  530. ->getMock();
  531. $this->mailer
  532. ->expects(self::once())
  533. ->method('main')
  534. ->with(self::callback(function ($model) use ($trialRequest) {
  535. return $model instanceof \App\Service\Mailer\Model\Shop\NewStructureArtistPremium\NotificationToSalesAdminModel
  536. && $model->getTrialRequest() === $trialRequest
  537. && $model->getSenderId() === \App\Enum\Access\AccessIdsEnum::ADMIN_2IOPENSERVICE->value;
  538. }));
  539. $shopService->sendMailToSalesAdministration($trialRequest);
  540. }
  541. /**
  542. * Test sendConfirmationMailToRepresentative method.
  543. */
  544. public function testSendConfirmationMailToRepresentative(): void
  545. {
  546. $shopService = $this->getShopServiceMockFor('sendConfirmationMailToRepresentative');
  547. $trialRequest = $this->getMockBuilder(NewStructureArtistPremiumTrialRequest::class)
  548. ->getMock();
  549. $trialRequest->method('getStructureIdentifier')->willReturn('test-structure');
  550. $this->mailer
  551. ->expects(self::once())
  552. ->method('main')
  553. ->with(self::callback(function ($model) use ($trialRequest) {
  554. return $model instanceof \App\Service\Mailer\Model\Shop\NewStructureArtistPremium\ConfirmationToRepresentativeModel
  555. && $model->getTrialRequest() === $trialRequest
  556. && $model->getSenderId() === \App\Enum\Access\AccessIdsEnum::ADMIN_2IOPENSERVICE->value
  557. && $model->getAccountCreationUrl() === 'https://example.com/account/create'
  558. && $model->getFaqUrl() === 'https://faq.example.com'
  559. && $model->getAdminUsername() === 'admintest-structure'
  560. && $model->getAdminLoginUrl() === 'https://admin.example.com/#/login/';
  561. }));
  562. $shopService->sendConfirmationMailToRepresentative($trialRequest);
  563. }
  564. }