ShopServiceTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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, NewStructureArtistPremiumTrialRequest|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($request): void
  55. {
  56. parent::validateNewStructureArtistPremiumTrialRequest($request);
  57. }
  58. public function createOrganizationCreationRequestFromTrialRequest(
  59. NewStructureArtistPremiumTrialRequest $trialRequest,
  60. bool $forValidationOnly = false,
  61. ): OrganizationCreationRequest {
  62. return parent::createOrganizationCreationRequestFromTrialRequest($trialRequest, $forValidationOnly);
  63. }
  64. }
  65. /**
  66. * Unit tests for ShopService.
  67. */
  68. class ShopServiceTest extends TestCase
  69. {
  70. private readonly MockObject|EntityManagerInterface $entityManager;
  71. private readonly MockObject|Mailer $mailer;
  72. private string $publicBaseUrl;
  73. private readonly MockObject|OrganizationFactory $organizationFactory;
  74. private readonly MockObject|SerializerInterface $serializer;
  75. private readonly MockObject|LoggerInterface $logger;
  76. private readonly MockObject|MessageBusInterface $messageBus;
  77. private readonly MockObject|Trial $trial;
  78. public function setUp(): void
  79. {
  80. $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
  81. $this->mailer = $this->getMockBuilder(Mailer::class)->disableOriginalConstructor()->getMock();
  82. $this->publicBaseUrl = 'https://example.com';
  83. $this->organizationFactory = $this->getMockBuilder(OrganizationFactory::class)->disableOriginalConstructor()->getMock();
  84. $this->serializer = $this->getMockBuilder(SerializerInterface::class)->disableOriginalConstructor()->getMock();
  85. $this->logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
  86. $this->messageBus = $this->getMockBuilder(MessageBusInterface::class)->disableOriginalConstructor()->getMock();
  87. $this->trial = $this->getMockBuilder(Trial::class)->disableOriginalConstructor()->getMock();
  88. }
  89. private function getShopServiceMockFor(string $methodName): TestableShopService|MockObject
  90. {
  91. $shopService = $this
  92. ->getMockBuilder(TestableShopService::class)
  93. ->setConstructorArgs(
  94. [
  95. $this->entityManager,
  96. $this->mailer,
  97. $this->publicBaseUrl,
  98. $this->organizationFactory,
  99. $this->serializer,
  100. $this->logger,
  101. $this->messageBus,
  102. $this->trial,
  103. ]
  104. )
  105. ->setMethodsExcept([$methodName])
  106. ->getMock();
  107. return $shopService;
  108. }
  109. public function tearDown(): void
  110. {
  111. DatesUtils::clearFakeDatetime();
  112. }
  113. /**
  114. * Test registerNewShopRequest method.
  115. */
  116. public function testRegisterNewShopRequest(): void
  117. {
  118. $shopService = $this->getShopServiceMockFor('registerNewShopRequest');
  119. $type = ShopRequestType::NEW_STRUCTURE_ARTIST_PREMIUM_TRIAL;
  120. $data = [
  121. 'representativeEmail' => 'test@example.com',
  122. 'representativeFirstName' => 'John',
  123. 'representativeLastName' => 'Doe',
  124. 'structureName' => 'Test Structure',
  125. ];
  126. $shopRequest = $this->getMockBuilder(ShopRequest::class)->getMock();
  127. $shopService->expects(self::once())
  128. ->method('controlShopRequestData')
  129. ->with($type, $data);
  130. $shopService->expects(self::once())
  131. ->method('createRequest')
  132. ->with($type, $data)
  133. ->willReturn($shopRequest);
  134. $shopService->expects(self::once())
  135. ->method('sendRequestValidationLink')
  136. ->with($shopRequest);
  137. $result = $shopService->registerNewShopRequest($type, $data);
  138. $this->assertSame($shopRequest, $result);
  139. }
  140. /**
  141. * Test controlShopRequestData method for NEW_STRUCTURE_ARTIST_PREMIUM_TRIAL type.
  142. */
  143. public function testControlShopRequestDataForNewStructureArtistPremiumTrial(): void
  144. {
  145. $shopService = $this->getShopServiceMockFor('controlShopRequestData');
  146. $type = ShopRequestType::NEW_STRUCTURE_ARTIST_PREMIUM_TRIAL;
  147. $data = [
  148. 'representativeEmail' => 'test@example.com',
  149. 'representativeFirstName' => 'John',
  150. 'representativeLastName' => 'Doe',
  151. 'structureName' => 'Test Structure',
  152. ];
  153. $shopService->expects(self::once())
  154. ->method('validateNewStructureArtistPremiumTrialRequest')
  155. ->with($data);
  156. $shopService->controlShopRequestData($type, $data);
  157. }
  158. /**
  159. * Test processShopRequest method for NEW_STRUCTURE_ARTIST_PREMIUM_TRIAL type.
  160. */
  161. public function testProcessShopRequest(): void
  162. {
  163. $shopService = $this->getShopServiceMockFor('processShopRequest');
  164. $shopRequest = $this->getMockBuilder(ShopRequest::class)->getMock();
  165. $shopRequest->method('getType')->willReturn(ShopRequestType::NEW_STRUCTURE_ARTIST_PREMIUM_TRIAL);
  166. $shopRequest->method('getToken')->willReturn('test-token');
  167. $this->messageBus
  168. ->expects(self::once())
  169. ->method('dispatch')
  170. ->with(self::callback(function ($message) {
  171. return $message instanceof NewStructureArtistPremiumTrial
  172. && $message->getToken() === 'test-token';
  173. }))
  174. ->willReturn(new Envelope(new NewStructureArtistPremiumTrial('azerty')));
  175. $shopRequest
  176. ->expects(self::once())
  177. ->method('setStatus')
  178. ->with(ShopRequestStatus::VALIDATED);
  179. $this->entityManager->expects(self::once())
  180. ->method('persist')
  181. ->with($shopRequest);
  182. $this->entityManager->expects(self::once())
  183. ->method('flush');
  184. $shopService->processShopRequest($shopRequest);
  185. }
  186. /**
  187. * Test createRequest method.
  188. */
  189. public function testCreateRequest(): void
  190. {
  191. $shopService = $this->getShopServiceMockFor('createRequest');
  192. $uuidRx = '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i';
  193. $type = ShopRequestType::NEW_STRUCTURE_ARTIST_PREMIUM_TRIAL;
  194. $data = [
  195. 'representativeEmail' => 'test@example.com',
  196. 'representativeFirstName' => 'John',
  197. 'representativeLastName' => 'Doe',
  198. 'structureName' => 'Test Structure',
  199. ];
  200. $this->entityManager->expects(self::once())
  201. ->method('persist')
  202. ->with(self::callback(function ($shopRequest) use ($type, $data, $uuidRx) {
  203. return $shopRequest instanceof ShopRequest
  204. && $shopRequest->getType() === $type
  205. && $shopRequest->getData() === $data
  206. && preg_match($uuidRx, $shopRequest->getToken());
  207. }));
  208. $this->entityManager->expects(self::once())
  209. ->method('flush');
  210. $result = $shopService->createRequest($type, $data);
  211. $this->assertInstanceOf(ShopRequest::class, $result);
  212. $this->assertSame($type, $result->getType());
  213. $this->assertSame($data, $result->getData());
  214. $this->assertMatchesRegularExpression($uuidRx, $result->getToken());
  215. }
  216. /**
  217. * Test sendRequestValidationLink method.
  218. */
  219. public function testSendRequestValidationLink(): void
  220. {
  221. $shopService = $this->getShopServiceMockFor('sendRequestValidationLink');
  222. $token = 'test-token';
  223. $data = [
  224. 'representativeEmail' => 'test@example.com',
  225. 'representativeFirstName' => 'John',
  226. 'representativeLastName' => 'Doe',
  227. 'structureName' => 'Test Structure',
  228. ];
  229. $shopRequest = $this->getMockBuilder(ShopRequest::class)->getMock();
  230. $shopRequest->method('getToken')->willReturn($token);
  231. $shopRequest->method('getData')->willReturn($data);
  232. $this->mailer
  233. ->expects(self::once())
  234. ->method('main')
  235. ->with(self::callback(function ($model) use ($token, $data) {
  236. var_dump($model);
  237. return $model->getToken() === $token
  238. && $model->getRepresentativeEmail() === $data['representativeEmail']
  239. && $model->getRepresentativeFirstName() === $data['representativeFirstName']
  240. && $model->getRepresentativeLastName() === $data['representativeLastName']
  241. && $model->getStructureName() === $data['structureName']
  242. && $model->getValidationUrl() === 'https://example.com/api/public/shop/validate/'.$token;
  243. }));
  244. $shopRequest->expects(self::once())
  245. ->method('setStatus')
  246. ->with(ShopRequestStatus::ACTIVATION_LINK_SENT);
  247. $this->entityManager->expects(self::once())
  248. ->method('persist')
  249. ->with($shopRequest);
  250. $this->entityManager->expects(self::once())
  251. ->method('flush');
  252. $shopService->sendRequestValidationLink($shopRequest);
  253. }
  254. /**
  255. * Test handleNewStructureArtistPremiumTrialRequest method.
  256. */
  257. public function testHandleNewStructureArtistPremiumTrialRequest(): void
  258. {
  259. $shopService = $this->getShopServiceMockFor('handleNewStructureArtistPremiumTrialRequest');
  260. $token = 'test-token';
  261. $data = [
  262. 'representativeEmail' => 'test@example.com',
  263. 'representativeFirstName' => 'John',
  264. 'representativeLastName' => 'Doe',
  265. 'structureName' => 'Test Structure',
  266. ];
  267. $shopRequest = $this->getMockBuilder(ShopRequest::class)->getMock();
  268. $shopRequest->method('getToken')->willReturn($token);
  269. $shopRequest->method('getData')->willReturn($data);
  270. $this->entityManager
  271. ->expects(self::once())
  272. ->method('find')
  273. ->with(ShopRequest::class, $token)
  274. ->willReturn($shopRequest);
  275. $trialRequest = $this->getMockBuilder(NewStructureArtistPremiumTrialRequest::class)
  276. ->getMock();
  277. $this->serializer
  278. ->expects(self::once())
  279. ->method('deserialize')
  280. ->with(json_encode($data), NewStructureArtistPremiumTrialRequest::class, 'json')
  281. ->willReturn($trialRequest);
  282. $organization = $this->getMockBuilder(Organization::class)->getMock();
  283. $shopService
  284. ->expects(self::once())
  285. ->method('createOrganization')
  286. ->with($trialRequest)
  287. ->willReturn($organization);
  288. // Add expectation for password setting
  289. $trialRequest->method('getPassword')->willReturn('Password123!');
  290. $this->organizationFactory
  291. ->expects(self::once())
  292. ->method('setAdminAccountPassword')
  293. ->with($organization, 'Password123!');
  294. $this->trial
  295. ->expects(self::once())
  296. ->method('startArtistPremiumTrialForNewStructure')
  297. ->with($organization, $trialRequest);
  298. $this->logger->expects(self::once())
  299. ->method('info')
  300. ->with('Successfully processed NewStructureArtistPremiumTrial for token: '.$token);
  301. $shopService->handleNewStructureArtistPremiumTrialRequest($token);
  302. }
  303. /**
  304. * Test handleNewStructureArtistPremiumTrialRequest method with non-existent token.
  305. */
  306. public function testHandleNewStructureArtistPremiumTrialRequestWithNonExistentToken(): void
  307. {
  308. $shopService = $this->getShopServiceMockFor('handleNewStructureArtistPremiumTrialRequest');
  309. $this->entityManager->expects(self::once())
  310. ->method('find')
  311. ->with(ShopRequest::class, 'non-existent-token')
  312. ->willReturn(null);
  313. $this->logger->expects(self::once())
  314. ->method('error')
  315. ->with('Cannot find ShopRequest with token: non-existent-token');
  316. $shopService->handleNewStructureArtistPremiumTrialRequest('non-existent-token');
  317. }
  318. /**
  319. * Test createOrganization method.
  320. */
  321. public function testCreateOrganization(): void
  322. {
  323. $shopService = $this->getShopServiceMockFor('createOrganization');
  324. $trialRequest = $this
  325. ->getMockBuilder(NewStructureArtistPremiumTrialRequest::class)
  326. ->getMock();
  327. $organizationCreationRequest = $this
  328. ->getMockBuilder(OrganizationCreationRequest::class)
  329. ->getMock();
  330. $organization = $this->getMockBuilder(Organization::class)->getMock();
  331. $shopService->expects(self::once())
  332. ->method('createOrganizationCreationRequestFromTrialRequest')
  333. ->with($trialRequest)
  334. ->willReturn($organizationCreationRequest);
  335. $this->organizationFactory
  336. ->expects(self::once())
  337. ->method('create')
  338. ->with($organizationCreationRequest)
  339. ->willReturn($organization);
  340. $result = $shopService->createOrganization($trialRequest);
  341. $this->assertSame($organization, $result);
  342. }
  343. /**
  344. * Test validateNewStructureArtistPremiumTrialRequest method with valid data.
  345. */
  346. public function testValidateNewStructureArtistPremiumTrialRequest(): void
  347. {
  348. $shopService = $this->getShopServiceMockFor('validateNewStructureArtistPremiumTrialRequest');
  349. $phoneNumberUtil = $this
  350. ->getMockBuilder(PhoneNumberUtil::class)
  351. ->disableOriginalConstructor()
  352. ->getMock();
  353. $shopService->phoneNumberUtil = $phoneNumberUtil;
  354. $trialRequest = $this
  355. ->getMockBuilder(NewStructureArtistPremiumTrialRequest::class)
  356. ->getMock();
  357. $trialRequest->method('getRepresentativePhone')->willReturn('+33123456789');
  358. $organizationCreationRequest = $this
  359. ->getMockBuilder(OrganizationCreationRequest::class)
  360. ->getMock();
  361. $shopService->expects(self::once())
  362. ->method('createOrganizationCreationRequestFromTrialRequest')
  363. ->with($trialRequest)
  364. ->willReturn($organizationCreationRequest);
  365. $phoneNumberUtil->expects(self::once())
  366. ->method('isPossibleNumber')
  367. ->with('+33123456789')
  368. ->willReturn(true);
  369. $this->organizationFactory->expects(self::once())
  370. ->method('interruptIfOrganizationExists')
  371. ->with($organizationCreationRequest);
  372. $shopService->validateNewStructureArtistPremiumTrialRequest($trialRequest);
  373. }
  374. /**
  375. * Test validateNewStructureArtistPremiumTrialRequest method with invalid phone number.
  376. */
  377. public function testValidateNewStructureArtistPremiumTrialRequestWithInvalidPhoneNumber(): void
  378. {
  379. $shopService = $this->getShopServiceMockFor('validateNewStructureArtistPremiumTrialRequest');
  380. $phoneNumberUtil = $this->getMockBuilder(PhoneNumberUtil::class)
  381. ->disableOriginalConstructor()
  382. ->getMock();
  383. $shopService->phoneNumberUtil = $phoneNumberUtil;
  384. $trialRequest = $this
  385. ->getMockBuilder(NewStructureArtistPremiumTrialRequest::class)
  386. ->getMock();
  387. $trialRequest->method('getRepresentativePhone')->willReturn('invalid-phone');
  388. // Mock the phoneNumberUtil to return false for isPossibleNumber
  389. $phoneNumberUtil->expects(self::once())
  390. ->method('isPossibleNumber')
  391. ->with('invalid-phone')
  392. ->willReturn(false);
  393. $this->expectException(\RuntimeException::class);
  394. $this->expectExceptionMessage('Invalid phone number');
  395. $shopService->validateNewStructureArtistPremiumTrialRequest($trialRequest);
  396. }
  397. /**
  398. * Test createOrganizationCreationRequestFromTrialRequest method with forValidationOnly=false.
  399. */
  400. public function testCreateOrganizationCreationRequestFromTrialRequestComplete(): void
  401. {
  402. $shopService = $this->getShopServiceMockFor('createOrganizationCreationRequestFromTrialRequest');
  403. $trialRequest = $this->getMockBuilder(NewStructureArtistPremiumTrialRequest::class)
  404. ->getMock();
  405. $trialRequest->method('getStructureName')->willReturn('Test Structure');
  406. $trialRequest->method('getCity')->willReturn('Test City');
  407. $trialRequest->method('getPostalCode')->willReturn('12345');
  408. $trialRequest->method('getAddress')->willReturn('Test Address');
  409. $trialRequest->method('getAddressComplement')->willReturn('Test Address Complement');
  410. $trialRequest->method('getRepresentativePhone')->willReturn('+33123456789');
  411. $trialRequest->method('getStructureEmail')->willReturn('structure@example.com');
  412. $trialRequest->method('getStructureType')->willReturn(PrincipalTypeEnum::ARTISTIC_EDUCATION_ONLY);
  413. $trialRequest->method('getLegalStatus')->willReturn(LegalEnum::ASSOCIATION_LAW_1901);
  414. $trialRequest->method('getSiren')->willReturn('123456789');
  415. $trialRequest->method('getStructureIdentifier')->willReturn('test-structure');
  416. // Set a fake date for testing
  417. $fakeDate = '2023-05-15 10:30:00';
  418. DatesUtils::setFakeDatetime($fakeDate);
  419. // Test with forValidationOnly = false (default)
  420. $result = $shopService->createOrganizationCreationRequestFromTrialRequest($trialRequest);
  421. // Verify that all fields are set
  422. $this->assertEquals('Test Structure', $result->getName());
  423. $this->assertEquals('Test City', $result->getCity());
  424. $this->assertEquals('12345', $result->getPostalCode());
  425. $this->assertEquals('Test Address', $result->getStreetAddress1());
  426. $this->assertEquals('Test Address Complement', $result->getStreetAddress2());
  427. $this->assertEquals('structure@example.com', $result->getEmail());
  428. $this->assertEquals(PrincipalTypeEnum::ARTISTIC_EDUCATION_ONLY, $result->getPrincipalType());
  429. $this->assertEquals(LegalEnum::ASSOCIATION_LAW_1901, $result->getLegalStatus());
  430. $this->assertEquals('123456789', $result->getSiretNumber());
  431. $this->assertEquals('+33123456789', $result->getPhoneNumber());
  432. $this->assertEquals('test-structure', $result->getSubdomain());
  433. $this->assertEquals(SettingsProductEnum::FREEMIUM, $result->getProduct());
  434. $this->assertFalse($result->getCreateWebsite());
  435. $this->assertFalse($result->isClient());
  436. $this->assertEquals(new \DateTime($fakeDate), $result->getCreationDate());
  437. }
  438. }