HelloAssoServiceTest.php 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. <?php
  2. namespace App\Tests\Unit\Service\HelloAsso;
  3. use App\ApiResources\HelloAsso\AuthUrl;
  4. use App\ApiResources\HelloAsso\EventForm;
  5. use App\ApiResources\HelloAsso\HelloAssoProfile;
  6. use App\Entity\Booking\Event;
  7. use App\Entity\HelloAsso\HelloAsso;
  8. use App\Entity\Organization\Organization;
  9. use App\Repository\Booking\EventRepository;
  10. use App\Repository\Organization\OrganizationRepository;
  11. use App\Service\HelloAsso\HelloAssoService;
  12. use App\Service\Security\OAuthPkceGenerator;
  13. use App\Service\Utils\DatesUtils;
  14. use App\Service\Utils\UrlBuilder;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use PHPUnit\Framework\MockObject\MockObject;
  17. use PHPUnit\Framework\TestCase;
  18. use Psr\Log\LoggerInterface;
  19. use Symfony\Contracts\HttpClient\HttpClientInterface;
  20. use Symfony\Contracts\HttpClient\ResponseInterface;
  21. // Classe testable pour accéder aux méthodes protégées
  22. class TestableHelloAssoService extends HelloAssoService
  23. {
  24. public function getHelloAssoEntityFor(int $organizationId, bool $shallHaveToken = true): HelloAsso
  25. {
  26. return parent::getHelloAssoEntityFor($organizationId, $shallHaveToken);
  27. }
  28. public function makeHelloAssoEventForm(array $formData): EventForm
  29. {
  30. return parent::makeHelloAssoEventForm($formData);
  31. }
  32. public function getCallbackUrl(): string
  33. {
  34. return parent::getCallbackUrl();
  35. }
  36. public function fetchAccessToken(?string $authorizationCode = null, ?string $challengeVerifier = null): array
  37. {
  38. return parent::fetchAccessToken($authorizationCode, $challengeVerifier);
  39. }
  40. public function updateDomain(array|string $accessToken, string $domain): void
  41. {
  42. if (is_array($accessToken)) {
  43. $accessToken = $accessToken['access_token'];
  44. }
  45. parent::updateDomain($accessToken, $domain);
  46. }
  47. public function refreshTokenIfNeeded(HelloAsso $helloAssoEntity): HelloAsso
  48. {
  49. return parent::refreshTokenIfNeeded($helloAssoEntity);
  50. }
  51. public function refreshTokens(HelloAsso $helloAssoEntity): HelloAsso
  52. {
  53. return parent::refreshTokens($helloAssoEntity);
  54. }
  55. }
  56. class HelloAssoServiceTest extends TestCase
  57. {
  58. private HttpClientInterface|MockObject $httpClient;
  59. private OrganizationRepository|MockObject $organizationRepository;
  60. private EventRepository|MockObject $eventRepository;
  61. private EntityManagerInterface|MockObject $entityManager;
  62. private LoggerInterface|MockObject $logger;
  63. private string $baseUrl = 'https://test-base.com';
  64. private string $publicAppBaseUrl = 'https://test-public.com';
  65. private string $helloAssoApiBaseUrl = 'https://api.helloasso.com/v5';
  66. private string $helloAssoAuthBaseUrl = 'https://auth.helloasso.com';
  67. private string $helloAssoClientId = 'test-client-id';
  68. private string $helloAssoClientSecret = 'test-client-secret';
  69. public function setUp(): void
  70. {
  71. $this->httpClient = $this->getMockBuilder(HttpClientInterface::class)
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $this->organizationRepository = $this->getMockBuilder(OrganizationRepository::class)
  75. ->disableOriginalConstructor()
  76. ->getMock();
  77. $this->eventRepository = $this->getMockBuilder(EventRepository::class)
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)
  81. ->disableOriginalConstructor()
  82. ->getMock();
  83. $this->logger = $this->getMockBuilder(LoggerInterface::class)
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. }
  87. private function getHelloAssoServiceMockFor(string $methodName): TestableHelloAssoService|MockObject
  88. {
  89. return $this->getMockBuilder(TestableHelloAssoService::class)
  90. ->setConstructorArgs([
  91. $this->httpClient,
  92. $this->organizationRepository,
  93. $this->eventRepository,
  94. $this->baseUrl,
  95. $this->publicAppBaseUrl,
  96. $this->helloAssoApiBaseUrl,
  97. $this->helloAssoAuthBaseUrl,
  98. $this->helloAssoClientId,
  99. $this->helloAssoClientSecret,
  100. $this->entityManager,
  101. $this->logger,
  102. ])
  103. ->setMethodsExcept([$methodName])
  104. ->getMock();
  105. }
  106. /**
  107. * @see HelloAssoService::setupOpentalentDomain()
  108. */
  109. public function testSetupOpentalentDomain(): void
  110. {
  111. $service = $this->getHelloAssoServiceMockFor('setupOpentalentDomain');
  112. $tokensData = ['access_token' => 'test-token'];
  113. $service->expects(self::once())
  114. ->method('fetchAccessToken')
  115. ->with(null)
  116. ->willReturn($tokensData);
  117. $service->expects(self::once())
  118. ->method('updateDomain')
  119. ->with($tokensData, 'https://*.opentalent.fr');
  120. $service->setupOpentalentDomain();
  121. }
  122. /**
  123. * @see HelloAssoService::getAuthUrl()
  124. */
  125. public function testGetAuthUrl(): void
  126. {
  127. $service = $this->getHelloAssoServiceMockFor('getAuthUrl');
  128. $organizationId = 1;
  129. $organization = $this->getMockBuilder(Organization::class)
  130. ->disableOriginalConstructor()
  131. ->getMock();
  132. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  133. ->disableOriginalConstructor()
  134. ->getMock();
  135. $this->organizationRepository->expects(self::once())
  136. ->method('find')
  137. ->with($organizationId)
  138. ->willReturn($organization);
  139. $organization->expects(self::once())
  140. ->method('getHelloAsso')
  141. ->willReturn($helloAssoEntity);
  142. $helloAssoEntity->expects(self::once())
  143. ->method('setChallengeVerifier')
  144. ->with(self::isType('string'));
  145. $this->entityManager->expects(self::once())
  146. ->method('persist')
  147. ->with($helloAssoEntity);
  148. $this->entityManager->expects(self::once())
  149. ->method('flush');
  150. $result = $service->getAuthUrl($organizationId);
  151. $this->assertInstanceOf(AuthUrl::class, $result);
  152. }
  153. /**
  154. * @see HelloAssoService::connect()
  155. */
  156. public function testConnect(): void
  157. {
  158. $service = $this->getHelloAssoServiceMockFor('connect');
  159. $organizationId = 1;
  160. $authorizationCode = 'test-auth-code';
  161. $organization = $this->getMockBuilder(Organization::class)
  162. ->disableOriginalConstructor()
  163. ->getMock();
  164. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  165. ->disableOriginalConstructor()
  166. ->getMock();
  167. $challengeVerifier = 'test-verifier';
  168. $tokensData = [
  169. 'access_token' => 'test-access-token',
  170. 'refresh_token' => 'test-refresh-token',
  171. 'expires_in' => 3600,
  172. 'token_type' => 'bearer',
  173. ];
  174. $this->organizationRepository->expects(self::once())
  175. ->method('find')
  176. ->with($organizationId)
  177. ->willReturn($organization);
  178. $organization->expects(self::once())
  179. ->method('getHelloAsso')
  180. ->willReturn($helloAssoEntity);
  181. $helloAssoEntity->expects(self::once())
  182. ->method('getChallengeVerifier')
  183. ->willReturn($challengeVerifier);
  184. $service->expects(self::once())
  185. ->method('fetchAccessToken')
  186. ->with($authorizationCode, $challengeVerifier)
  187. ->willReturn($tokensData);
  188. $helloAssoEntity->expects(self::once())
  189. ->method('setToken')
  190. ->with('test-access-token');
  191. $helloAssoEntity->expects(self::once())
  192. ->method('setTokenCreatedAt')
  193. ->with(self::isInstanceOf(\DateTime::class));
  194. $helloAssoEntity->expects(self::once())
  195. ->method('setRefreshToken')
  196. ->with('test-refresh-token');
  197. $helloAssoEntity->expects(self::once())
  198. ->method('setRefreshTokenCreatedAt')
  199. ->with(self::isInstanceOf(\DateTime::class));
  200. $helloAssoEntity->expects(self::once())
  201. ->method('setOrganizationSlug')
  202. ->with(null);
  203. $helloAssoEntity->expects(self::once())
  204. ->method('setChallengeVerifier')
  205. ->with(null);
  206. $this->entityManager->expects(self::once())
  207. ->method('persist')
  208. ->with($helloAssoEntity);
  209. $this->entityManager->expects(self::once())
  210. ->method('flush');
  211. $result = $service->connect($organizationId, $authorizationCode);
  212. $this->assertSame($helloAssoEntity, $result);
  213. }
  214. /**
  215. * @see HelloAssoService::makeHelloAssoProfile()
  216. */
  217. public function testMakeHelloAssoProfile(): void
  218. {
  219. $service = $this->getHelloAssoServiceMockFor('makeHelloAssoProfile');
  220. $organizationId = 1;
  221. $organization = $this->getMockBuilder(Organization::class)
  222. ->disableOriginalConstructor()
  223. ->getMock();
  224. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  225. ->disableOriginalConstructor()
  226. ->getMock();
  227. $this->organizationRepository->expects(self::once())
  228. ->method('find')
  229. ->with($organizationId)
  230. ->willReturn($organization);
  231. $organization->expects(self::once())
  232. ->method('getHelloAsso')
  233. ->willReturn($helloAssoEntity);
  234. $helloAssoEntity->expects(self::once())
  235. ->method('getToken')
  236. ->willReturn('test-token');
  237. $helloAssoEntity->expects(self::once())
  238. ->method('getOrganizationSlug')
  239. ->willReturn('test-org-slug');
  240. $result = $service->makeHelloAssoProfile($organizationId);
  241. $this->assertInstanceOf(HelloAssoProfile::class, $result);
  242. }
  243. /**
  244. * @see HelloAssoService::unlinkHelloAssoAccount()
  245. */
  246. public function testUnlinkHelloAssoAccount(): void
  247. {
  248. $service = $this->getHelloAssoServiceMockFor('unlinkHelloAssoAccount');
  249. $organizationId = 1;
  250. $organization = $this->getMockBuilder(Organization::class)
  251. ->disableOriginalConstructor()
  252. ->getMock();
  253. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  254. ->disableOriginalConstructor()
  255. ->getMock();
  256. $this->organizationRepository->expects(self::once())
  257. ->method('find')
  258. ->with($organizationId)
  259. ->willReturn($organization);
  260. $organization->expects(self::once())
  261. ->method('getHelloAsso')
  262. ->willReturn($helloAssoEntity);
  263. $helloAssoEntity->expects(self::once())
  264. ->method('setToken')
  265. ->with(null);
  266. $helloAssoEntity->expects(self::once())
  267. ->method('setTokenCreatedAt')
  268. ->with(null);
  269. $helloAssoEntity->expects(self::once())
  270. ->method('setRefreshToken')
  271. ->with(null);
  272. $helloAssoEntity->expects(self::once())
  273. ->method('setRefreshTokenCreatedAt')
  274. ->with(null);
  275. $helloAssoEntity->expects(self::once())
  276. ->method('setOrganizationSlug')
  277. ->with(null);
  278. $this->entityManager->expects(self::once())
  279. ->method('persist')
  280. ->with($helloAssoEntity);
  281. $this->entityManager->expects(self::once())
  282. ->method('flush');
  283. $service->unlinkHelloAssoAccount($organizationId);
  284. }
  285. /**
  286. * @see HelloAssoService::getResource()
  287. */
  288. public function testGetResource(): void
  289. {
  290. $service = $this->getHelloAssoServiceMockFor('getResource');
  291. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  292. ->disableOriginalConstructor()
  293. ->getMock();
  294. $response = $this->getMockBuilder(ResponseInterface::class)
  295. ->disableOriginalConstructor()
  296. ->getMock();
  297. $routeParts = ['organizations', 'test-slug', 'forms'];
  298. $expectedData = ['test' => 'data'];
  299. $helloAssoEntity->expects(self::once())
  300. ->method('getOrganizationSlug')
  301. ->willReturn('test-org-slug');
  302. $helloAssoEntity->expects(self::atLeastOnce())
  303. ->method('getToken')
  304. ->willReturn('test-token');
  305. $service->expects(self::once())
  306. ->method('refreshTokenIfNeeded')
  307. ->with($helloAssoEntity)
  308. ->willReturn($helloAssoEntity);
  309. $service->expects(self::once())
  310. ->method('get')
  311. ->with('https://api.helloasso.com/v5/v5/organizations/test-slug/forms', [], [
  312. 'headers' => [
  313. 'accept' => 'application/json',
  314. 'authorization' => 'Bearer test-token',
  315. ],
  316. ])
  317. ->willReturn($response);
  318. $response->expects(self::once())
  319. ->method('getStatusCode')
  320. ->willReturn(200);
  321. $response->expects(self::once())
  322. ->method('getContent')
  323. ->willReturn(json_encode($expectedData));
  324. $result = $service->getResource($helloAssoEntity, $routeParts);
  325. $this->assertSame($expectedData, $result);
  326. }
  327. /**
  328. * @see HelloAssoService::getHelloAssoEventForms()
  329. */
  330. public function testGetHelloAssoEventForms(): void
  331. {
  332. $service = $this->getHelloAssoServiceMockFor('getHelloAssoEventForms');
  333. $organizationId = 1;
  334. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  335. ->disableOriginalConstructor()
  336. ->getMock();
  337. $formsData = [
  338. 'data' => [
  339. ['formSlug' => 'form1', 'title' => 'Event 1'],
  340. ['formSlug' => 'form2', 'title' => 'Event 2'],
  341. ]
  342. ];
  343. $service->expects(self::once())
  344. ->method('getHelloAssoEntityFor')
  345. ->with($organizationId, true)
  346. ->willReturn($helloAssoEntity);
  347. $helloAssoEntity
  348. ->method('getOrganizationSlug')
  349. ->willReturn('test-org-slug');
  350. $helloAssoEntity
  351. ->method('getToken')
  352. ->willReturn('abcd123');
  353. $service->expects(self::once())
  354. ->method('getResource')
  355. ->with($helloAssoEntity, ['organizations', 'test-org-slug', 'forms'])
  356. ->willReturn($formsData);
  357. $result = $service->getHelloAssoEventForms($organizationId);
  358. $this->assertIsArray($result);
  359. $this->assertCount(2, $result);
  360. $this->assertInstanceOf(EventForm::class, $result[0]);
  361. $this->assertInstanceOf(EventForm::class, $result[1]);
  362. }
  363. /**
  364. * @see HelloAssoService::getHelloAssoEventForms()
  365. */
  366. public function testGetHelloAssoEventFormsWhenIncomplete(): void
  367. {
  368. $service = $this->getHelloAssoServiceMockFor('getHelloAssoEventForms');
  369. $organizationId = 1;
  370. $service->expects(self::once())
  371. ->method('getHelloAssoEntityFor')
  372. ->with($organizationId, true)
  373. ->willThrowException(new \RuntimeException('HelloAsso entity incomplete'));
  374. $this->expectException(\RuntimeException::class);
  375. $this->expectExceptionMessage('HelloAsso entity incomplete');
  376. $service->getHelloAssoEventForms($organizationId);
  377. }
  378. /**
  379. * @see HelloAssoService::getHelloAssoEventForm()
  380. */
  381. public function testGetHelloAssoEventForm(): void
  382. {
  383. $service = $this->getHelloAssoServiceMockFor('getHelloAssoEventForm');
  384. $organizationId = 1;
  385. $formSlug = 'test-form-slug';
  386. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  387. ->disableOriginalConstructor()
  388. ->getMock();
  389. $formData = [
  390. 'formSlug' => 'test-form-slug',
  391. 'title' => 'Test Event',
  392. 'widgetFullUrl' => 'https://widget.url'
  393. ];
  394. $service->expects(self::once())
  395. ->method('getHelloAssoEntityFor')
  396. ->with($organizationId, true)
  397. ->willReturn($helloAssoEntity);
  398. $helloAssoEntity->expects(self::atLeastOnce())
  399. ->method('getOrganizationSlug')
  400. ->willReturn('test-org-slug');
  401. $service->expects(self::once())
  402. ->method('getResource')
  403. ->with($helloAssoEntity, ['organizations', 'test-org-slug', 'forms', 'Event', 'test-form-slug', 'public'])
  404. ->willReturn($formData);
  405. $service->expects(self::once())
  406. ->method('makeHelloAssoEventForm')
  407. ->with($formData)
  408. ->willReturn(new EventForm());
  409. $result = $service->getHelloAssoEventForm($organizationId, $formSlug);
  410. $this->assertInstanceOf(EventForm::class, $result);
  411. }
  412. /**
  413. * @see HelloAssoService::getHelloAssoEventFormByEventId()
  414. */
  415. public function testGetHelloAssoEventFormByEventId(): void
  416. {
  417. $service = $this->getHelloAssoServiceMockFor('getHelloAssoEventFormByEventId');
  418. $eventId = 1;
  419. $organization = $this->getMockBuilder(Organization::class)
  420. ->disableOriginalConstructor()
  421. ->getMock();
  422. $event = $this->getMockBuilder(Event::class)
  423. ->disableOriginalConstructor()
  424. ->getMock();
  425. $eventForm = new EventForm();
  426. $this->eventRepository->expects(self::once())
  427. ->method('find')
  428. ->with($eventId)
  429. ->willReturn($event);
  430. $event->expects(self::once())
  431. ->method('getOrganization')
  432. ->willReturn($organization);
  433. $organization->expects(self::once())
  434. ->method('getId')
  435. ->willReturn(1);
  436. $event->expects(self::once())
  437. ->method('getHelloAssoSlug')
  438. ->willReturn('test-hello-asso-slug');
  439. $service->expects(self::once())
  440. ->method('getHelloAssoEventForm')
  441. ->with(1, 'test-hello-asso-slug')
  442. ->willReturn($eventForm);
  443. $result = $service->getHelloAssoEventFormByEventId($eventId);
  444. $this->assertSame($eventForm, $result);
  445. }
  446. /**
  447. * @see HelloAssoService::getHelloAssoEventFormByEventId()
  448. */
  449. public function testGetHelloAssoEventFormByEventIdWhenEventNotFound(): void
  450. {
  451. $service = $this->getHelloAssoServiceMockFor('getHelloAssoEventFormByEventId');
  452. $eventId = 1;
  453. $this->eventRepository->expects(self::once())
  454. ->method('find')
  455. ->with($eventId)
  456. ->willReturn(null);
  457. $this->expectException(\RuntimeException::class);
  458. $this->expectExceptionMessage('Event not found');
  459. $service->getHelloAssoEventFormByEventId($eventId);
  460. }
  461. /**
  462. * @see HelloAssoService::getHelloAssoEventFormByEventId()
  463. */
  464. public function testGetHelloAssoEventFormByEventIdWhenNoHelloAssoSlug(): void
  465. {
  466. $service = $this->getHelloAssoServiceMockFor('getHelloAssoEventFormByEventId');
  467. $eventId = 1;
  468. $organization = $this->getMockBuilder(Organization::class)
  469. ->disableOriginalConstructor()
  470. ->getMock();
  471. $event = $this->getMockBuilder(Event::class)
  472. ->disableOriginalConstructor()
  473. ->getMock();
  474. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  475. ->disableOriginalConstructor()
  476. ->getMock();
  477. $this->eventRepository->expects(self::once())
  478. ->method('find')
  479. ->with($eventId)
  480. ->willReturn($event);
  481. $event->expects(self::once())
  482. ->method('getHelloAssoSlug')
  483. ->willReturn(null);
  484. $this->expectException(\RuntimeException::class);
  485. $this->expectExceptionMessage('HelloAsso form slug not found');
  486. $service->getHelloAssoEventFormByEventId($eventId);
  487. }
  488. /**
  489. * @see HelloAssoService::getHelloAssoEntityFor()
  490. */
  491. public function testGetHelloAssoEntityFor(): void
  492. {
  493. $service = $this->getHelloAssoServiceMockFor('getHelloAssoEntityFor');
  494. $organizationId = 1;
  495. $organization = $this->getMockBuilder(Organization::class)
  496. ->disableOriginalConstructor()
  497. ->getMock();
  498. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  499. ->disableOriginalConstructor()
  500. ->getMock();
  501. $this->organizationRepository->expects(self::once())
  502. ->method('find')
  503. ->with($organizationId)
  504. ->willReturn($organization);
  505. $organization->expects(self::once())
  506. ->method('getHelloAsso')
  507. ->willReturn($helloAssoEntity);
  508. $helloAssoEntity->expects(self::once())
  509. ->method('getOrganizationSlug')
  510. ->willReturn('test-org-slug');
  511. $helloAssoEntity->expects(self::once())
  512. ->method('getToken')
  513. ->willReturn('test-token');
  514. $result = $service->getHelloAssoEntityFor($organizationId);
  515. $this->assertSame($helloAssoEntity, $result);
  516. }
  517. /**
  518. * @see HelloAssoService::getHelloAssoEntityFor()
  519. */
  520. public function testGetHelloAssoEntityForWhenIncomplete(): void
  521. {
  522. $service = $this->getHelloAssoServiceMockFor('getHelloAssoEntityFor');
  523. $organizationId = 1;
  524. $organization = $this->getMockBuilder(Organization::class)
  525. ->disableOriginalConstructor()
  526. ->getMock();
  527. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  528. ->disableOriginalConstructor()
  529. ->getMock();
  530. $this->organizationRepository->expects(self::once())
  531. ->method('find')
  532. ->with($organizationId)
  533. ->willReturn($organization);
  534. $organization->expects(self::once())
  535. ->method('getHelloAsso')
  536. ->willReturn($helloAssoEntity);
  537. $helloAssoEntity->expects(self::once())
  538. ->method('getOrganizationSlug')
  539. ->willReturn('test-org-slug');
  540. $helloAssoEntity->expects(self::once())
  541. ->method('getToken')
  542. ->willReturn(null);
  543. $this->expectException(\RuntimeException::class);
  544. $this->expectExceptionMessage('HelloAsso entity incomplete');
  545. $service->getHelloAssoEntityFor($organizationId);
  546. }
  547. /**
  548. * @see HelloAssoService::getHelloAssoEntityFor()
  549. */
  550. public function testGetHelloAssoEntityForWhenOrganizationNotFound(): void
  551. {
  552. $service = $this->getHelloAssoServiceMockFor('getHelloAssoEntityFor');
  553. $organizationId = 1;
  554. $this->organizationRepository->expects(self::once())
  555. ->method('find')
  556. ->with($organizationId)
  557. ->willReturn(null);
  558. $this->expectException(\RuntimeException::class);
  559. $this->expectExceptionMessage('Organization not found');
  560. $service->getHelloAssoEntityFor($organizationId);
  561. }
  562. /**
  563. * @see HelloAssoService::getHelloAssoEntityFor()
  564. */
  565. public function testGetHelloAssoEntityForWhenHelloAssoNotFound(): void
  566. {
  567. $service = $this->getHelloAssoServiceMockFor('getHelloAssoEntityFor');
  568. $organizationId = 1;
  569. $organization = $this->getMockBuilder(Organization::class)
  570. ->disableOriginalConstructor()
  571. ->getMock();
  572. $this->organizationRepository->expects(self::once())
  573. ->method('find')
  574. ->with($organizationId)
  575. ->willReturn($organization);
  576. $organization->expects(self::once())
  577. ->method('getHelloAsso')
  578. ->willReturn(null);
  579. $this->expectException(\RuntimeException::class);
  580. $this->expectExceptionMessage('HelloAsso entity not found');
  581. $service->getHelloAssoEntityFor($organizationId);
  582. }
  583. /**
  584. * @see HelloAssoService::makeHelloAssoEventForm()
  585. */
  586. public function testMakeHelloAssoEventForm(): void
  587. {
  588. $service = $this->getHelloAssoServiceMockFor('makeHelloAssoEventForm');
  589. $formData = [
  590. 'formSlug' => 'test-form-slug',
  591. 'title' => 'Test Event Title',
  592. 'widgetFullUrl' => 'https://widget.full.url'
  593. ];
  594. $result = $service->makeHelloAssoEventForm($formData);
  595. $this->assertInstanceOf(EventForm::class, $result);
  596. }
  597. /**
  598. * @see HelloAssoService::getCallbackUrl()
  599. */
  600. public function testGetCallbackUrl(): void
  601. {
  602. $service = $this->getHelloAssoServiceMockFor('getCallbackUrl');
  603. $result = $service->getCallbackUrl();
  604. $this->assertEquals('https://test-public.com/helloasso/callback', $result);
  605. }
  606. /**
  607. * @see HelloAssoService::fetchAccessToken()
  608. */
  609. public function testFetchAccessToken(): void
  610. {
  611. $service = $this->getHelloAssoServiceMockFor('fetchAccessToken');
  612. $response = $this->getMockBuilder(ResponseInterface::class)
  613. ->disableOriginalConstructor()
  614. ->getMock();
  615. $expectedTokenData = [
  616. 'access_token' => 'test-access-token',
  617. 'refresh_token' => 'test-refresh-token',
  618. 'token_type' => 'Bearer',
  619. 'expires_in' => 3600,
  620. 'organization_slug' => null
  621. ];
  622. $this->httpClient->expects(self::once())
  623. ->method('request')
  624. ->with('POST', 'https://api.helloasso.com/v5/oauth2/token', [
  625. 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
  626. 'body' => [
  627. 'grant_type' => 'client_credentials',
  628. 'client_id' => 'test-client-id',
  629. 'client_secret' => 'test-client-secret',
  630. ]
  631. ])
  632. ->willReturn($response);
  633. $response->expects(self::once())
  634. ->method('getStatusCode')
  635. ->willReturn(200);
  636. $response->expects(self::once())
  637. ->method('getContent')
  638. ->willReturn(json_encode($expectedTokenData));
  639. $result = $service->fetchAccessToken();
  640. $this->assertSame($expectedTokenData, $result);
  641. }
  642. /**
  643. * @see HelloAssoService::fetchAccessToken()
  644. */
  645. public function testFetchAccessTokenWithAuthCode(): void
  646. {
  647. $service = $this->getHelloAssoServiceMockFor('fetchAccessToken');
  648. $response = $this->getMockBuilder(ResponseInterface::class)
  649. ->disableOriginalConstructor()
  650. ->getMock();
  651. $expectedTokenData = [
  652. 'access_token' => 'test-access-token',
  653. 'refresh_token' => 'test-refresh-token',
  654. 'token_type' => 'Bearer',
  655. 'expires_in' => 3600,
  656. 'organization_slug' => null
  657. ];
  658. $service->expects(self::once())
  659. ->method('getCallbackUrl')
  660. ->willReturn('https://test-public.com/helloasso/callback');
  661. $this->httpClient->expects(self::once())
  662. ->method('request')
  663. ->with('POST', 'https://api.helloasso.com/v5/oauth2/token', [
  664. 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
  665. 'body' => [
  666. 'grant_type' => 'authorization_code',
  667. 'client_id' => 'test-client-id',
  668. 'client_secret' => 'test-client-secret',
  669. 'code' => 'test-auth-code',
  670. 'redirect_uri' => 'https://test-public.com/helloasso/callback',
  671. 'code_verifier' => 'test-verifier'
  672. ]
  673. ])
  674. ->willReturn($response);
  675. $response->expects(self::once())
  676. ->method('getStatusCode')
  677. ->willReturn(200);
  678. $response->expects(self::once())
  679. ->method('getContent')
  680. ->willReturn(json_encode($expectedTokenData));
  681. $result = $service->fetchAccessToken('test-auth-code', 'test-verifier');
  682. $this->assertSame($expectedTokenData, $result);
  683. }
  684. /**
  685. * @see HelloAssoService::updateDomain()
  686. */
  687. public function testUpdateDomain(): void
  688. {
  689. $service = $this->getHelloAssoServiceMockFor('updateDomain');
  690. $response = $this->getMockBuilder(ResponseInterface::class)
  691. ->disableOriginalConstructor()
  692. ->getMock();
  693. $service->expects(self::once())
  694. ->method('put')
  695. ->with('https://api.helloasso.com/v5/v5/partners/me/api-clients', ['domain' => 'https://test-domain.com'], [], [
  696. 'headers' => [
  697. 'Authorization' => 'Bearer test-access-token',
  698. 'Content-Type' => 'application/json',
  699. ],
  700. ])
  701. ->willReturn($response);
  702. $response->expects(self::once())
  703. ->method('getStatusCode')
  704. ->willReturn(200);
  705. $service->updateDomain('test-access-token', 'https://test-domain.com');
  706. }
  707. /**
  708. * @see HelloAssoService::refreshTokenIfNeeded()
  709. */
  710. public function testRefreshTokenIfNeeded(): void
  711. {
  712. $service = $this->getHelloAssoServiceMockFor('refreshTokenIfNeeded');
  713. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  714. ->disableOriginalConstructor()
  715. ->getMock();
  716. $futureDate = new \DateTime('+1 hour');
  717. $helloAssoEntity->expects(self::once())
  718. ->method('getRefreshToken')
  719. ->willReturn('test-refresh-token');
  720. $helloAssoEntity->expects(self::atLeastOnce())
  721. ->method('getRefreshTokenCreatedAt')
  722. ->willReturn($futureDate);
  723. $result = $service->refreshTokenIfNeeded($helloAssoEntity);
  724. $this->assertSame($helloAssoEntity, $result);
  725. }
  726. /**
  727. * @see HelloAssoService::refreshTokenIfNeeded()
  728. */
  729. public function testRefreshTokenIfNeededWhenTokenExpired(): void
  730. {
  731. // Set fake current time to control the test scenario
  732. DatesUtils::setFakeDatetime('2024-01-01 12:00:00');
  733. $service = $this->getHelloAssoServiceMockFor('refreshTokenIfNeeded');
  734. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  735. ->disableOriginalConstructor()
  736. ->getMock();
  737. $refreshedEntity = $this->getMockBuilder(HelloAsso::class)
  738. ->disableOriginalConstructor()
  739. ->getMock();
  740. // Create a date that when 25 minutes are added, will be less than current fake time
  741. $pastDate = DatesUtils::new('2024-01-01 11:30:00');
  742. $helloAssoEntity->expects(self::once())
  743. ->method('getRefreshToken')
  744. ->willReturn('test-refresh-token');
  745. $helloAssoEntity->expects(self::atLeastOnce())
  746. ->method('getRefreshTokenCreatedAt')
  747. ->willReturn($pastDate);
  748. $service->expects(self::once())
  749. ->method('refreshTokens')
  750. ->with($helloAssoEntity)
  751. ->willReturn($helloAssoEntity);
  752. $result = $service->refreshTokenIfNeeded($helloAssoEntity);
  753. $this->assertSame($helloAssoEntity, $result);
  754. // Clean up fake datetime
  755. DatesUtils::clearFakeDatetime();
  756. }
  757. /**
  758. * @see HelloAssoService::refreshTokens()
  759. */
  760. public function testRefreshTokens(): void
  761. {
  762. $service = $this->getHelloAssoServiceMockFor('refreshTokens');
  763. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  764. ->disableOriginalConstructor()
  765. ->getMock();
  766. $response = $this->getMockBuilder(ResponseInterface::class)
  767. ->disableOriginalConstructor()
  768. ->getMock();
  769. $tokensData = [
  770. 'access_token' => 'new-access-token',
  771. 'refresh_token' => 'new-refresh-token',
  772. 'expires_in' => 3600
  773. ];
  774. $helloAssoEntity->expects(self::atLeastOnce())
  775. ->method('getRefreshToken')
  776. ->willReturn('current-refresh-token');
  777. $helloAssoEntity->expects(self::once())
  778. ->method('getRefreshTokenCreatedAt')
  779. ->willReturn(new \DateTime());
  780. $this->httpClient->expects(self::once())
  781. ->method('request')
  782. ->with('POST', 'https://api.helloasso.com/v5/oauth2/token', [
  783. 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
  784. 'body' => [
  785. 'grant_type' => 'refresh_token',
  786. 'refresh_token' => 'current-refresh-token'
  787. ]
  788. ])
  789. ->willReturn($response);
  790. $response->expects(self::once())
  791. ->method('getStatusCode')
  792. ->willReturn(200);
  793. $response->expects(self::once())
  794. ->method('getContent')
  795. ->willReturn(json_encode($tokensData));
  796. $helloAssoEntity->expects(self::once())
  797. ->method('setToken')
  798. ->with('new-access-token');
  799. $helloAssoEntity->expects(self::once())
  800. ->method('setRefreshToken')
  801. ->with('new-refresh-token');
  802. $helloAssoEntity->expects(self::once())
  803. ->method('setTokenCreatedAt')
  804. ->with(self::isInstanceOf(\DateTime::class));
  805. $helloAssoEntity->expects(self::once())
  806. ->method('setRefreshTokenCreatedAt')
  807. ->with(self::isInstanceOf(\DateTime::class));
  808. $this->entityManager->expects(self::once())
  809. ->method('persist')
  810. ->with($helloAssoEntity);
  811. $this->entityManager->expects(self::once())
  812. ->method('flush');
  813. $result = $service->refreshTokens($helloAssoEntity);
  814. $this->assertSame($helloAssoEntity, $result);
  815. }
  816. }