HelloAssoServiceTest.php 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  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\Utils\DatesUtils;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use PHPUnit\Framework\MockObject\MockObject;
  15. use PHPUnit\Framework\TestCase;
  16. use Symfony\Contracts\HttpClient\HttpClientInterface;
  17. use Symfony\Contracts\HttpClient\ResponseInterface;
  18. // Classe testable pour accéder aux méthodes protégées
  19. class TestableHelloAssoService extends HelloAssoService
  20. {
  21. public function getHelloAssoEntityFor(int $organizationId, bool $shallHaveToken = true): HelloAsso
  22. {
  23. return parent::getHelloAssoEntityFor($organizationId, $shallHaveToken);
  24. }
  25. public function makeHelloAssoEventForm(array $formData): EventForm
  26. {
  27. return parent::makeHelloAssoEventForm($formData);
  28. }
  29. public function getCallbackUrl(): string
  30. {
  31. return parent::getCallbackUrl();
  32. }
  33. public function fetchAccessToken(?string $authorizationCode = null, ?string $challengeVerifier = null): array
  34. {
  35. return parent::fetchAccessToken($authorizationCode, $challengeVerifier);
  36. }
  37. public function updateDomain(array|string $accessToken, string $domain): void
  38. {
  39. if (is_array($accessToken)) {
  40. $accessToken = $accessToken['access_token'];
  41. }
  42. parent::updateDomain($accessToken, $domain);
  43. }
  44. public function refreshTokenIfNeeded(HelloAsso $helloAssoEntity): HelloAsso
  45. {
  46. return parent::refreshTokenIfNeeded($helloAssoEntity);
  47. }
  48. public function refreshTokens(HelloAsso $helloAssoEntity): HelloAsso
  49. {
  50. return parent::refreshTokens($helloAssoEntity);
  51. }
  52. }
  53. class HelloAssoServiceTest extends TestCase
  54. {
  55. private HttpClientInterface|MockObject $httpClient;
  56. private OrganizationRepository|MockObject $organizationRepository;
  57. private EventRepository|MockObject $eventRepository;
  58. private EntityManagerInterface|MockObject $entityManager;
  59. private string $baseUrl = 'https://test-base.com';
  60. private string $publicAppBaseUrl = 'https://test-public.com';
  61. private string $helloAssoApiBaseUrl = 'https://api.helloasso.com/v5';
  62. private string $helloAssoAuthBaseUrl = 'https://auth.helloasso.com';
  63. private string $helloAssoClientId = 'test-client-id';
  64. private string $helloAssoClientSecret = 'test-client-secret';
  65. public function setUp(): void
  66. {
  67. $this->httpClient = $this->getMockBuilder(HttpClientInterface::class)
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $this->organizationRepository = $this->getMockBuilder(OrganizationRepository::class)
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $this->eventRepository = $this->getMockBuilder(EventRepository::class)
  74. ->disableOriginalConstructor()
  75. ->getMock();
  76. $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)
  77. ->disableOriginalConstructor()
  78. ->getMock();
  79. }
  80. private function getHelloAssoServiceMockFor(string $methodName): TestableHelloAssoService|MockObject
  81. {
  82. return $this->getMockBuilder(TestableHelloAssoService::class)
  83. ->setConstructorArgs([
  84. $this->httpClient,
  85. $this->organizationRepository,
  86. $this->eventRepository,
  87. $this->publicAppBaseUrl,
  88. $this->helloAssoApiBaseUrl,
  89. $this->helloAssoAuthBaseUrl,
  90. $this->helloAssoClientId,
  91. $this->helloAssoClientSecret,
  92. $this->entityManager,
  93. ])
  94. ->setMethodsExcept([$methodName])
  95. ->getMock();
  96. }
  97. /**
  98. * @see HelloAssoService::setupOpentalentDomain()
  99. */
  100. public function testSetupOpentalentDomain(): void
  101. {
  102. $service = $this->getHelloAssoServiceMockFor('setupOpentalentDomain');
  103. $tokensData = ['access_token' => 'test-token'];
  104. $service->expects(self::once())
  105. ->method('fetchAccessToken')
  106. ->with(null, null)
  107. ->willReturn($tokensData);
  108. $service->expects(self::once())
  109. ->method('updateDomain')
  110. ->with('test-token', 'https://*.opentalent.fr');
  111. $service->setupOpentalentDomain();
  112. }
  113. /**
  114. * @see HelloAssoService::getAuthUrl()
  115. */
  116. public function testGetAuthUrl(): void
  117. {
  118. $service = $this->getHelloAssoServiceMockFor('getAuthUrl');
  119. $organizationId = 1;
  120. $organization = $this->getMockBuilder(Organization::class)
  121. ->disableOriginalConstructor()
  122. ->getMock();
  123. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  124. ->disableOriginalConstructor()
  125. ->getMock();
  126. $this->organizationRepository->expects(self::once())
  127. ->method('find')
  128. ->with($organizationId)
  129. ->willReturn($organization);
  130. $organization->expects(self::once())
  131. ->method('getHelloAsso')
  132. ->willReturn($helloAssoEntity);
  133. $helloAssoEntity->expects(self::once())
  134. ->method('setChallengeVerifier')
  135. ->with(self::isType('string'));
  136. $this->entityManager->expects(self::once())
  137. ->method('persist')
  138. ->with($helloAssoEntity);
  139. $this->entityManager->expects(self::once())
  140. ->method('flush');
  141. $result = $service->getAuthUrl($organizationId);
  142. $this->assertInstanceOf(AuthUrl::class, $result);
  143. }
  144. /**
  145. * @see HelloAssoService::getAuthUrl()
  146. */
  147. public function testGetAuthUrlWhenOrganizationNotFound(): void
  148. {
  149. $service = $this->getHelloAssoServiceMockFor('getAuthUrl');
  150. $organizationId = 1;
  151. $this->organizationRepository->expects(self::once())
  152. ->method('find')
  153. ->with($organizationId)
  154. ->willReturn(null);
  155. $this->expectException(\RuntimeException::class);
  156. $this->expectExceptionMessage('Organization not found');
  157. $service->getAuthUrl($organizationId);
  158. }
  159. /**
  160. * @see HelloAssoService::getAuthUrl()
  161. */
  162. public function testGetAuthUrlWhenHelloAssoEntityNotExists(): void
  163. {
  164. $service = $this->getHelloAssoServiceMockFor('getAuthUrl');
  165. $organizationId = 1;
  166. $organization = $this->getMockBuilder(Organization::class)
  167. ->disableOriginalConstructor()
  168. ->getMock();
  169. $this->organizationRepository->expects(self::once())
  170. ->method('find')
  171. ->with($organizationId)
  172. ->willReturn($organization);
  173. $organization->expects(self::once())
  174. ->method('getHelloAsso')
  175. ->willReturn(null);
  176. $this->entityManager->expects(self::once())
  177. ->method('persist')
  178. ->with(self::isInstanceOf(HelloAsso::class));
  179. $this->entityManager->expects(self::once())
  180. ->method('flush');
  181. $result = $service->getAuthUrl($organizationId);
  182. $this->assertInstanceOf(AuthUrl::class, $result);
  183. }
  184. /**
  185. * @see HelloAssoService::connect()
  186. */
  187. public function testConnect(): void
  188. {
  189. $service = $this->getHelloAssoServiceMockFor('connect');
  190. $organizationId = 1;
  191. $authorizationCode = 'test-auth-code';
  192. $organization = $this->getMockBuilder(Organization::class)
  193. ->disableOriginalConstructor()
  194. ->getMock();
  195. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  196. ->disableOriginalConstructor()
  197. ->getMock();
  198. $challengeVerifier = 'test-verifier';
  199. $tokensData = [
  200. 'access_token' => 'test-access-token',
  201. 'refresh_token' => 'test-refresh-token',
  202. 'expires_in' => 3600,
  203. 'token_type' => 'bearer',
  204. ];
  205. $this->organizationRepository->expects(self::once())
  206. ->method('find')
  207. ->with($organizationId)
  208. ->willReturn($organization);
  209. $organization->expects(self::once())
  210. ->method('getHelloAsso')
  211. ->willReturn($helloAssoEntity);
  212. $helloAssoEntity->expects(self::once())
  213. ->method('getChallengeVerifier')
  214. ->willReturn($challengeVerifier);
  215. $service->expects(self::once())
  216. ->method('fetchAccessToken')
  217. ->with($authorizationCode, $challengeVerifier)
  218. ->willReturn($tokensData);
  219. $helloAssoEntity->expects(self::once())
  220. ->method('setToken')
  221. ->with('test-access-token');
  222. $helloAssoEntity->expects(self::once())
  223. ->method('setTokenCreatedAt')
  224. ->with(self::isInstanceOf(\DateTime::class));
  225. $helloAssoEntity->expects(self::once())
  226. ->method('setRefreshToken')
  227. ->with('test-refresh-token');
  228. $helloAssoEntity->expects(self::once())
  229. ->method('setRefreshTokenCreatedAt')
  230. ->with(self::isInstanceOf(\DateTime::class));
  231. $helloAssoEntity->expects(self::once())
  232. ->method('setOrganizationSlug')
  233. ->with(null);
  234. $helloAssoEntity->expects(self::once())
  235. ->method('setChallengeVerifier')
  236. ->with(null);
  237. $this->entityManager->expects(self::once())
  238. ->method('persist')
  239. ->with($helloAssoEntity);
  240. $this->entityManager->expects(self::once())
  241. ->method('flush');
  242. $result = $service->connect($organizationId, $authorizationCode);
  243. $this->assertSame($helloAssoEntity, $result);
  244. }
  245. /**
  246. * @see HelloAssoService::connect()
  247. */
  248. public function testConnectWhenOrganizationNotFound(): void
  249. {
  250. $service = $this->getHelloAssoServiceMockFor('connect');
  251. $organizationId = 1;
  252. $authorizationCode = 'test-auth-code';
  253. $this->organizationRepository->expects(self::once())
  254. ->method('find')
  255. ->with($organizationId)
  256. ->willReturn(null);
  257. $this->expectException(\RuntimeException::class);
  258. $this->expectExceptionMessage('Organization not found');
  259. $service->connect($organizationId, $authorizationCode);
  260. }
  261. /**
  262. * @see HelloAssoService::connect()
  263. */
  264. public function testConnectWhenHelloAssoEntityNotFound(): void
  265. {
  266. $service = $this->getHelloAssoServiceMockFor('connect');
  267. $organizationId = 1;
  268. $authorizationCode = 'test-auth-code';
  269. $organization = $this->getMockBuilder(Organization::class)
  270. ->disableOriginalConstructor()
  271. ->getMock();
  272. $this->organizationRepository->expects(self::once())
  273. ->method('find')
  274. ->with($organizationId)
  275. ->willReturn($organization);
  276. $organization->expects(self::once())
  277. ->method('getHelloAsso')
  278. ->willReturn(null);
  279. $this->expectException(\RuntimeException::class);
  280. $this->expectExceptionMessage('HelloAsso entity not found');
  281. $service->connect($organizationId, $authorizationCode);
  282. }
  283. /**
  284. * @see HelloAssoService::connect()
  285. */
  286. public function testConnectWhenInvalidTokenType(): void
  287. {
  288. $service = $this->getHelloAssoServiceMockFor('connect');
  289. $organizationId = 1;
  290. $authorizationCode = 'test-auth-code';
  291. $organization = $this->getMockBuilder(Organization::class)
  292. ->disableOriginalConstructor()
  293. ->getMock();
  294. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  295. ->disableOriginalConstructor()
  296. ->getMock();
  297. $challengeVerifier = 'test-verifier';
  298. $tokensData = [
  299. 'access_token' => 'test-access-token',
  300. 'refresh_token' => 'test-refresh-token',
  301. 'expires_in' => 3600,
  302. 'token_type' => 'invalid',
  303. ];
  304. $this->organizationRepository->expects(self::once())
  305. ->method('find')
  306. ->with($organizationId)
  307. ->willReturn($organization);
  308. $organization->expects(self::once())
  309. ->method('getHelloAsso')
  310. ->willReturn($helloAssoEntity);
  311. $helloAssoEntity->expects(self::once())
  312. ->method('getChallengeVerifier')
  313. ->willReturn($challengeVerifier);
  314. $service->expects(self::once())
  315. ->method('fetchAccessToken')
  316. ->with($authorizationCode, $challengeVerifier)
  317. ->willReturn($tokensData);
  318. $this->expectException(\RuntimeException::class);
  319. $this->expectExceptionMessage('Invalid token type received');
  320. $service->connect($organizationId, $authorizationCode);
  321. }
  322. /**
  323. * @see HelloAssoService::makeHelloAssoProfile()
  324. */
  325. public function testMakeHelloAssoProfile(): void
  326. {
  327. $service = $this->getHelloAssoServiceMockFor('makeHelloAssoProfile');
  328. $organizationId = 1;
  329. $organization = $this->getMockBuilder(Organization::class)
  330. ->disableOriginalConstructor()
  331. ->getMock();
  332. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  333. ->disableOriginalConstructor()
  334. ->getMock();
  335. $this->organizationRepository->expects(self::once())
  336. ->method('find')
  337. ->with($organizationId)
  338. ->willReturn($organization);
  339. $organization->expects(self::once())
  340. ->method('getHelloAsso')
  341. ->willReturn($helloAssoEntity);
  342. $helloAssoEntity->expects(self::once())
  343. ->method('getToken')
  344. ->willReturn('test-token');
  345. $helloAssoEntity->expects(self::once())
  346. ->method('getOrganizationSlug')
  347. ->willReturn('test-org-slug');
  348. $result = $service->makeHelloAssoProfile($organizationId);
  349. $this->assertInstanceOf(HelloAssoProfile::class, $result);
  350. }
  351. /**
  352. * @see HelloAssoService::makeHelloAssoProfile()
  353. */
  354. public function testMakeHelloAssoProfileWhenOrganizationNotFound(): void
  355. {
  356. $service = $this->getHelloAssoServiceMockFor('makeHelloAssoProfile');
  357. $organizationId = 1;
  358. $this->organizationRepository->expects(self::once())
  359. ->method('find')
  360. ->with($organizationId)
  361. ->willReturn(null);
  362. $this->expectException(\RuntimeException::class);
  363. $this->expectExceptionMessage('Organization not found');
  364. $service->makeHelloAssoProfile($organizationId);
  365. }
  366. /**
  367. * @see HelloAssoService::makeHelloAssoProfile()
  368. */
  369. public function testMakeHelloAssoProfileWhenHelloAssoEntityNotFound(): void
  370. {
  371. $service = $this->getHelloAssoServiceMockFor('makeHelloAssoProfile');
  372. $organizationId = 1;
  373. $organization = $this->getMockBuilder(Organization::class)
  374. ->disableOriginalConstructor()
  375. ->getMock();
  376. $this->organizationRepository->expects(self::once())
  377. ->method('find')
  378. ->with($organizationId)
  379. ->willReturn($organization);
  380. $organization->expects(self::once())
  381. ->method('getHelloAsso')
  382. ->willReturn(null);
  383. $result = $service->makeHelloAssoProfile($organizationId);
  384. $this->assertInstanceOf(HelloAssoProfile::class, $result);
  385. $this->assertFalse($result->isExisting());
  386. }
  387. /**
  388. * @see HelloAssoService::unlinkHelloAssoAccount()
  389. */
  390. public function testUnlinkHelloAssoAccount(): void
  391. {
  392. $service = $this->getHelloAssoServiceMockFor('unlinkHelloAssoAccount');
  393. $organizationId = 1;
  394. $organization = $this->getMockBuilder(Organization::class)
  395. ->disableOriginalConstructor()
  396. ->getMock();
  397. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  398. ->disableOriginalConstructor()
  399. ->getMock();
  400. $this->organizationRepository->expects(self::once())
  401. ->method('find')
  402. ->with($organizationId)
  403. ->willReturn($organization);
  404. $organization->expects(self::once())
  405. ->method('getHelloAsso')
  406. ->willReturn($helloAssoEntity);
  407. $helloAssoEntity->expects(self::once())
  408. ->method('setToken')
  409. ->with(null);
  410. $helloAssoEntity->expects(self::once())
  411. ->method('setTokenCreatedAt')
  412. ->with(null);
  413. $helloAssoEntity->expects(self::once())
  414. ->method('setRefreshToken')
  415. ->with(null);
  416. $helloAssoEntity->expects(self::once())
  417. ->method('setRefreshTokenCreatedAt')
  418. ->with(null);
  419. $helloAssoEntity->expects(self::once())
  420. ->method('setOrganizationSlug')
  421. ->with(null);
  422. $this->entityManager->expects(self::once())
  423. ->method('persist')
  424. ->with($helloAssoEntity);
  425. $this->entityManager->expects(self::once())
  426. ->method('flush');
  427. $service->unlinkHelloAssoAccount($organizationId);
  428. }
  429. /**
  430. * @see HelloAssoService::unlinkHelloAssoAccount()
  431. */
  432. public function testUnlinkHelloAssoAccountWhenOrganizationNotFound(): void
  433. {
  434. $service = $this->getHelloAssoServiceMockFor('unlinkHelloAssoAccount');
  435. $organizationId = 1;
  436. $this->organizationRepository->expects(self::once())
  437. ->method('find')
  438. ->with($organizationId)
  439. ->willReturn(null);
  440. $this->expectException(\RuntimeException::class);
  441. $this->expectExceptionMessage('Organization not found');
  442. $service->unlinkHelloAssoAccount($organizationId);
  443. }
  444. /**
  445. * @see HelloAssoService::unlinkHelloAssoAccount()
  446. */
  447. public function testUnlinkHelloAssoAccountWhenHelloAssoEntityNotFound(): void
  448. {
  449. $service = $this->getHelloAssoServiceMockFor('unlinkHelloAssoAccount');
  450. $organizationId = 1;
  451. $organization = $this->getMockBuilder(Organization::class)
  452. ->disableOriginalConstructor()
  453. ->getMock();
  454. $this->organizationRepository->expects(self::once())
  455. ->method('find')
  456. ->with($organizationId)
  457. ->willReturn($organization);
  458. $organization->expects(self::once())
  459. ->method('getHelloAsso')
  460. ->willReturn(null);
  461. $this->entityManager->expects(self::never())
  462. ->method('persist');
  463. $this->entityManager->expects(self::never())
  464. ->method('flush');
  465. $service->unlinkHelloAssoAccount($organizationId);
  466. }
  467. /**
  468. * @see HelloAssoService::getResource()
  469. */
  470. public function testGetResource(): void
  471. {
  472. $service = $this->getHelloAssoServiceMockFor('getResource');
  473. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  474. ->disableOriginalConstructor()
  475. ->getMock();
  476. $response = $this->getMockBuilder(ResponseInterface::class)
  477. ->disableOriginalConstructor()
  478. ->getMock();
  479. $routeParts = ['organizations', 'test-slug', 'forms'];
  480. $expectedData = ['test' => 'data'];
  481. $helloAssoEntity->expects(self::once())
  482. ->method('getOrganizationSlug')
  483. ->willReturn('test-org-slug');
  484. $helloAssoEntity->expects(self::atLeastOnce())
  485. ->method('getToken')
  486. ->willReturn('test-token');
  487. $service->expects(self::once())
  488. ->method('refreshTokenIfNeeded')
  489. ->with($helloAssoEntity)
  490. ->willReturn($helloAssoEntity);
  491. $service->expects(self::once())
  492. ->method('get')
  493. ->with('https://api.helloasso.com/v5/v5/organizations/test-slug/forms', [], [
  494. 'headers' => [
  495. 'accept' => 'application/json',
  496. 'authorization' => 'Bearer test-token',
  497. ],
  498. ])
  499. ->willReturn($response);
  500. $response->expects(self::once())
  501. ->method('getStatusCode')
  502. ->willReturn(200);
  503. $response->expects(self::once())
  504. ->method('getContent')
  505. ->willReturn(json_encode($expectedData));
  506. $result = $service->getResource($helloAssoEntity, $routeParts);
  507. $this->assertSame($expectedData, $result);
  508. }
  509. /**
  510. * @see HelloAssoService::getResource()
  511. */
  512. public function testGetResourceWhenIncompleteEntity(): void
  513. {
  514. $service = $this->getHelloAssoServiceMockFor('getResource');
  515. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  516. ->disableOriginalConstructor()
  517. ->getMock();
  518. $routeParts = ['organizations', 'test-slug', 'forms'];
  519. $helloAssoEntity->expects(self::once())
  520. ->method('getOrganizationSlug')
  521. ->willReturn(null);
  522. // getToken() won't be called because getOrganizationSlug() returns null first
  523. $this->expectException(\RuntimeException::class);
  524. $this->expectExceptionMessage('HelloAsso entity incomplete');
  525. $service->getResource($helloAssoEntity, $routeParts);
  526. }
  527. /**
  528. * @see HelloAssoService::getResource()
  529. */
  530. public function testGetResourceWhenHttpError(): void
  531. {
  532. $service = $this->getHelloAssoServiceMockFor('getResource');
  533. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  534. ->disableOriginalConstructor()
  535. ->getMock();
  536. $response = $this->getMockBuilder(ResponseInterface::class)
  537. ->disableOriginalConstructor()
  538. ->getMock();
  539. $routeParts = ['organizations', 'test-slug', 'forms'];
  540. $helloAssoEntity->expects(self::once())
  541. ->method('getOrganizationSlug')
  542. ->willReturn('test-org-slug');
  543. $helloAssoEntity->expects(self::atLeastOnce())
  544. ->method('getToken')
  545. ->willReturn('test-token');
  546. $service->expects(self::once())
  547. ->method('refreshTokenIfNeeded')
  548. ->with($helloAssoEntity)
  549. ->willReturn($helloAssoEntity);
  550. $service->expects(self::once())
  551. ->method('get')
  552. ->willReturn($response);
  553. $response->expects(self::atLeastOnce())
  554. ->method('getStatusCode')
  555. ->willReturn(404);
  556. $response->expects(self::once())
  557. ->method('getContent')
  558. ->with(false)
  559. ->willReturn('Not Found');
  560. $this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class);
  561. $this->expectExceptionMessage('Failed to fetch resource: [404] Not Found');
  562. $service->getResource($helloAssoEntity, $routeParts);
  563. }
  564. /**
  565. * @see HelloAssoService::getHelloAssoEventForms()
  566. */
  567. public function testGetHelloAssoEventForms(): void
  568. {
  569. $service = $this->getHelloAssoServiceMockFor('getHelloAssoEventForms');
  570. $organizationId = 1;
  571. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  572. ->disableOriginalConstructor()
  573. ->getMock();
  574. $formsData = [
  575. 'data' => [
  576. ['formSlug' => 'form1', 'title' => 'Event 1'],
  577. ['formSlug' => 'form2', 'title' => 'Event 2'],
  578. ],
  579. ];
  580. $service->expects(self::once())
  581. ->method('getHelloAssoEntityFor')
  582. ->with($organizationId, true)
  583. ->willReturn($helloAssoEntity);
  584. $helloAssoEntity
  585. ->method('getOrganizationSlug')
  586. ->willReturn('test-org-slug');
  587. $helloAssoEntity
  588. ->method('getToken')
  589. ->willReturn('abcd123');
  590. $service->expects(self::once())
  591. ->method('getResource')
  592. ->with($helloAssoEntity, ['organizations', 'test-org-slug', 'forms'])
  593. ->willReturn($formsData);
  594. $result = $service->getHelloAssoEventForms($organizationId);
  595. $this->assertIsArray($result);
  596. $this->assertCount(2, $result);
  597. $this->assertInstanceOf(EventForm::class, $result[0]);
  598. $this->assertInstanceOf(EventForm::class, $result[1]);
  599. }
  600. /**
  601. * @see HelloAssoService::getHelloAssoEventForms()
  602. */
  603. public function testGetHelloAssoEventFormsWhenIncomplete(): void
  604. {
  605. $service = $this->getHelloAssoServiceMockFor('getHelloAssoEventForms');
  606. $organizationId = 1;
  607. $service->expects(self::once())
  608. ->method('getHelloAssoEntityFor')
  609. ->with($organizationId, true)
  610. ->willThrowException(new \RuntimeException('HelloAsso entity incomplete'));
  611. $this->expectException(\RuntimeException::class);
  612. $this->expectExceptionMessage('HelloAsso entity incomplete');
  613. $service->getHelloAssoEventForms($organizationId);
  614. }
  615. /**
  616. * @see HelloAssoService::getHelloAssoEventForm()
  617. */
  618. public function testGetHelloAssoEventForm(): void
  619. {
  620. $service = $this->getHelloAssoServiceMockFor('getHelloAssoEventForm');
  621. $organizationId = 1;
  622. $formSlug = 'test-form-slug';
  623. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  624. ->disableOriginalConstructor()
  625. ->getMock();
  626. $formData = [
  627. 'formSlug' => 'test-form-slug',
  628. 'title' => 'Test Event',
  629. 'widgetFullUrl' => 'https://widget.url',
  630. ];
  631. $service->expects(self::once())
  632. ->method('getHelloAssoEntityFor')
  633. ->with($organizationId, true)
  634. ->willReturn($helloAssoEntity);
  635. $helloAssoEntity->expects(self::atLeastOnce())
  636. ->method('getOrganizationSlug')
  637. ->willReturn('test-org-slug');
  638. $service->expects(self::once())
  639. ->method('getResource')
  640. ->with($helloAssoEntity, ['organizations', 'test-org-slug', 'forms', 'Event', 'test-form-slug', 'public'])
  641. ->willReturn($formData);
  642. $service->expects(self::once())
  643. ->method('makeHelloAssoEventForm')
  644. ->with($formData)
  645. ->willReturn(new EventForm());
  646. $result = $service->getHelloAssoEventForm($organizationId, $formSlug);
  647. $this->assertInstanceOf(EventForm::class, $result);
  648. }
  649. /**
  650. * @see HelloAssoService::getHelloAssoEventFormByEventId()
  651. */
  652. public function testGetHelloAssoEventFormByEventId(): void
  653. {
  654. $service = $this->getHelloAssoServiceMockFor('getHelloAssoEventFormByEventId');
  655. $eventId = 1;
  656. $organization = $this->getMockBuilder(Organization::class)
  657. ->disableOriginalConstructor()
  658. ->getMock();
  659. $event = $this->getMockBuilder(Event::class)
  660. ->disableOriginalConstructor()
  661. ->getMock();
  662. $eventForm = new EventForm();
  663. $this->eventRepository->expects(self::once())
  664. ->method('find')
  665. ->with($eventId)
  666. ->willReturn($event);
  667. $event->expects(self::once())
  668. ->method('getOrganization')
  669. ->willReturn($organization);
  670. $organization->expects(self::once())
  671. ->method('getId')
  672. ->willReturn(1);
  673. $event->expects(self::once())
  674. ->method('getHelloAssoSlug')
  675. ->willReturn('test-hello-asso-slug');
  676. $service->expects(self::once())
  677. ->method('getHelloAssoEventForm')
  678. ->with(1, 'test-hello-asso-slug')
  679. ->willReturn($eventForm);
  680. $result = $service->getHelloAssoEventFormByEventId($eventId);
  681. $this->assertSame($eventForm, $result);
  682. }
  683. /**
  684. * @see HelloAssoService::getHelloAssoEventFormByEventId()
  685. */
  686. public function testGetHelloAssoEventFormByEventIdWhenEventNotFound(): void
  687. {
  688. $service = $this->getHelloAssoServiceMockFor('getHelloAssoEventFormByEventId');
  689. $eventId = 1;
  690. $this->eventRepository->expects(self::once())
  691. ->method('find')
  692. ->with($eventId)
  693. ->willReturn(null);
  694. $this->expectException(\RuntimeException::class);
  695. $this->expectExceptionMessage('Event not found');
  696. $service->getHelloAssoEventFormByEventId($eventId);
  697. }
  698. /**
  699. * @see HelloAssoService::getHelloAssoEventFormByEventId()
  700. */
  701. public function testGetHelloAssoEventFormByEventIdWhenNoHelloAssoSlug(): void
  702. {
  703. $service = $this->getHelloAssoServiceMockFor('getHelloAssoEventFormByEventId');
  704. $eventId = 1;
  705. $organization = $this->getMockBuilder(Organization::class)
  706. ->disableOriginalConstructor()
  707. ->getMock();
  708. $event = $this->getMockBuilder(Event::class)
  709. ->disableOriginalConstructor()
  710. ->getMock();
  711. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  712. ->disableOriginalConstructor()
  713. ->getMock();
  714. $this->eventRepository->expects(self::once())
  715. ->method('find')
  716. ->with($eventId)
  717. ->willReturn($event);
  718. $event->expects(self::once())
  719. ->method('getHelloAssoSlug')
  720. ->willReturn(null);
  721. $this->expectException(\RuntimeException::class);
  722. $this->expectExceptionMessage('HelloAsso form slug not found');
  723. $service->getHelloAssoEventFormByEventId($eventId);
  724. }
  725. /**
  726. * @see HelloAssoService::getHelloAssoEntityFor()
  727. */
  728. public function testGetHelloAssoEntityFor(): void
  729. {
  730. $service = $this->getHelloAssoServiceMockFor('getHelloAssoEntityFor');
  731. $organizationId = 1;
  732. $organization = $this->getMockBuilder(Organization::class)
  733. ->disableOriginalConstructor()
  734. ->getMock();
  735. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  736. ->disableOriginalConstructor()
  737. ->getMock();
  738. $this->organizationRepository->expects(self::once())
  739. ->method('find')
  740. ->with($organizationId)
  741. ->willReturn($organization);
  742. $organization->expects(self::once())
  743. ->method('getHelloAsso')
  744. ->willReturn($helloAssoEntity);
  745. $helloAssoEntity->expects(self::once())
  746. ->method('getOrganizationSlug')
  747. ->willReturn('test-org-slug');
  748. $helloAssoEntity->expects(self::once())
  749. ->method('getToken')
  750. ->willReturn('test-token');
  751. $result = $service->getHelloAssoEntityFor($organizationId);
  752. $this->assertSame($helloAssoEntity, $result);
  753. }
  754. /**
  755. * @see HelloAssoService::getHelloAssoEntityFor()
  756. */
  757. public function testGetHelloAssoEntityForWhenIncomplete(): void
  758. {
  759. $service = $this->getHelloAssoServiceMockFor('getHelloAssoEntityFor');
  760. $organizationId = 1;
  761. $organization = $this->getMockBuilder(Organization::class)
  762. ->disableOriginalConstructor()
  763. ->getMock();
  764. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  765. ->disableOriginalConstructor()
  766. ->getMock();
  767. $this->organizationRepository->expects(self::once())
  768. ->method('find')
  769. ->with($organizationId)
  770. ->willReturn($organization);
  771. $organization->expects(self::once())
  772. ->method('getHelloAsso')
  773. ->willReturn($helloAssoEntity);
  774. $helloAssoEntity->expects(self::once())
  775. ->method('getOrganizationSlug')
  776. ->willReturn('test-org-slug');
  777. $helloAssoEntity->expects(self::once())
  778. ->method('getToken')
  779. ->willReturn(null);
  780. $this->expectException(\RuntimeException::class);
  781. $this->expectExceptionMessage('HelloAsso entity incomplete');
  782. $service->getHelloAssoEntityFor($organizationId);
  783. }
  784. /**
  785. * @see HelloAssoService::getHelloAssoEntityFor()
  786. */
  787. public function testGetHelloAssoEntityForWhenOrganizationNotFound(): void
  788. {
  789. $service = $this->getHelloAssoServiceMockFor('getHelloAssoEntityFor');
  790. $organizationId = 1;
  791. $this->organizationRepository->expects(self::once())
  792. ->method('find')
  793. ->with($organizationId)
  794. ->willReturn(null);
  795. $this->expectException(\RuntimeException::class);
  796. $this->expectExceptionMessage('Organization not found');
  797. $service->getHelloAssoEntityFor($organizationId);
  798. }
  799. /**
  800. * @see HelloAssoService::getHelloAssoEntityFor()
  801. */
  802. public function testGetHelloAssoEntityForWhenHelloAssoNotFound(): void
  803. {
  804. $service = $this->getHelloAssoServiceMockFor('getHelloAssoEntityFor');
  805. $organizationId = 1;
  806. $organization = $this->getMockBuilder(Organization::class)
  807. ->disableOriginalConstructor()
  808. ->getMock();
  809. $this->organizationRepository->expects(self::once())
  810. ->method('find')
  811. ->with($organizationId)
  812. ->willReturn($organization);
  813. $organization->expects(self::once())
  814. ->method('getHelloAsso')
  815. ->willReturn(null);
  816. $this->expectException(\RuntimeException::class);
  817. $this->expectExceptionMessage('HelloAsso entity not found');
  818. $service->getHelloAssoEntityFor($organizationId);
  819. }
  820. /**
  821. * @see HelloAssoService::makeHelloAssoEventForm()
  822. */
  823. public function testMakeHelloAssoEventForm(): void
  824. {
  825. $service = $this->getHelloAssoServiceMockFor('makeHelloAssoEventForm');
  826. $formData = [
  827. 'formSlug' => 'test-form-slug',
  828. 'title' => 'Test Event Title',
  829. 'widgetFullUrl' => 'https://widget.full.url',
  830. ];
  831. $result = $service->makeHelloAssoEventForm($formData);
  832. $this->assertInstanceOf(EventForm::class, $result);
  833. }
  834. /**
  835. * @see HelloAssoService::getCallbackUrl()
  836. */
  837. public function testGetCallbackUrl(): void
  838. {
  839. $service = $this->getHelloAssoServiceMockFor('getCallbackUrl');
  840. $result = $service->getCallbackUrl();
  841. $this->assertEquals('https://test-public.com/helloasso/callback', $result);
  842. }
  843. /**
  844. * @see HelloAssoService::fetchAccessToken()
  845. */
  846. public function testFetchAccessToken(): void
  847. {
  848. $service = $this->getHelloAssoServiceMockFor('fetchAccessToken');
  849. $response = $this->getMockBuilder(ResponseInterface::class)
  850. ->disableOriginalConstructor()
  851. ->getMock();
  852. $expectedTokenData = [
  853. 'access_token' => 'test-access-token',
  854. 'refresh_token' => 'test-refresh-token',
  855. 'token_type' => 'Bearer',
  856. 'expires_in' => 3600,
  857. 'organization_slug' => null,
  858. ];
  859. $this->httpClient->expects(self::once())
  860. ->method('request')
  861. ->with('POST', 'https://api.helloasso.com/v5/oauth2/token', [
  862. 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
  863. 'body' => [
  864. 'grant_type' => 'client_credentials',
  865. 'client_id' => 'test-client-id',
  866. 'client_secret' => 'test-client-secret',
  867. ],
  868. ])
  869. ->willReturn($response);
  870. $response->expects(self::once())
  871. ->method('getStatusCode')
  872. ->willReturn(200);
  873. $response->expects(self::once())
  874. ->method('getContent')
  875. ->willReturn(json_encode($expectedTokenData));
  876. $result = $service->fetchAccessToken();
  877. $this->assertSame($expectedTokenData, $result);
  878. }
  879. /**
  880. * @see HelloAssoService::fetchAccessToken()
  881. */
  882. public function testFetchAccessTokenWithAuthCode(): void
  883. {
  884. $service = $this->getHelloAssoServiceMockFor('fetchAccessToken');
  885. $response = $this->getMockBuilder(ResponseInterface::class)
  886. ->disableOriginalConstructor()
  887. ->getMock();
  888. $expectedTokenData = [
  889. 'access_token' => 'test-access-token',
  890. 'refresh_token' => 'test-refresh-token',
  891. 'token_type' => 'Bearer',
  892. 'expires_in' => 3600,
  893. 'organization_slug' => null,
  894. ];
  895. $service->expects(self::once())
  896. ->method('getCallbackUrl')
  897. ->willReturn('https://test-public.com/helloasso/callback');
  898. $this->httpClient->expects(self::once())
  899. ->method('request')
  900. ->with('POST', 'https://api.helloasso.com/v5/oauth2/token', [
  901. 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
  902. 'body' => [
  903. 'grant_type' => 'authorization_code',
  904. 'client_id' => 'test-client-id',
  905. 'client_secret' => 'test-client-secret',
  906. 'code' => 'test-auth-code',
  907. 'redirect_uri' => 'https://test-public.com/helloasso/callback',
  908. 'code_verifier' => 'test-verifier',
  909. ],
  910. ])
  911. ->willReturn($response);
  912. $response->expects(self::once())
  913. ->method('getStatusCode')
  914. ->willReturn(200);
  915. $response->expects(self::once())
  916. ->method('getContent')
  917. ->willReturn(json_encode($expectedTokenData));
  918. $result = $service->fetchAccessToken('test-auth-code', 'test-verifier');
  919. $this->assertSame($expectedTokenData, $result);
  920. }
  921. /**
  922. * @see HelloAssoService::fetchAccessToken()
  923. */
  924. public function testFetchAccessTokenWhenHttpError(): void
  925. {
  926. $service = $this->getHelloAssoServiceMockFor('fetchAccessToken');
  927. $response = $this->getMockBuilder(ResponseInterface::class)
  928. ->disableOriginalConstructor()
  929. ->getMock();
  930. $this->httpClient->expects(self::once())
  931. ->method('request')
  932. ->willReturn($response);
  933. $response->expects(self::once())
  934. ->method('getStatusCode')
  935. ->willReturn(400);
  936. $response->expects(self::once())
  937. ->method('getContent')
  938. ->with(false)
  939. ->willReturn('Bad Request');
  940. $this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class);
  941. $this->expectExceptionMessage('Failed to fetch access token: Bad Request');
  942. $service->fetchAccessToken();
  943. }
  944. /**
  945. * @see HelloAssoService::fetchAccessToken()
  946. */
  947. public function testFetchAccessTokenWhenJsonError(): void
  948. {
  949. $service = $this->getHelloAssoServiceMockFor('fetchAccessToken');
  950. $response = $this->getMockBuilder(ResponseInterface::class)
  951. ->disableOriginalConstructor()
  952. ->getMock();
  953. $this->httpClient->expects(self::once())
  954. ->method('request')
  955. ->willReturn($response);
  956. $response->expects(self::once())
  957. ->method('getStatusCode')
  958. ->willReturn(200);
  959. $response->expects(self::once())
  960. ->method('getContent')
  961. ->willReturn('invalid-json');
  962. $this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class);
  963. $this->expectExceptionMessage('Failed to parse authentication response:');
  964. $service->fetchAccessToken();
  965. }
  966. /**
  967. * @see HelloAssoService::updateDomain()
  968. */
  969. public function testUpdateDomain(): void
  970. {
  971. $service = $this->getHelloAssoServiceMockFor('updateDomain');
  972. $response = $this->getMockBuilder(ResponseInterface::class)
  973. ->disableOriginalConstructor()
  974. ->getMock();
  975. $service->expects(self::once())
  976. ->method('put')
  977. ->with('https://api.helloasso.com/v5/v5/partners/me/api-clients', ['domain' => 'https://test-domain.com'], [], [
  978. 'headers' => [
  979. 'Authorization' => 'Bearer test-access-token',
  980. 'Content-Type' => 'application/json',
  981. ],
  982. ])
  983. ->willReturn($response);
  984. $response->expects(self::once())
  985. ->method('getStatusCode')
  986. ->willReturn(200);
  987. $service->updateDomain('test-access-token', 'https://test-domain.com');
  988. }
  989. /**
  990. * @see HelloAssoService::updateDomain()
  991. */
  992. public function testUpdateDomainWhenHttpError(): void
  993. {
  994. $service = $this->getHelloAssoServiceMockFor('updateDomain');
  995. $response = $this->getMockBuilder(ResponseInterface::class)
  996. ->disableOriginalConstructor()
  997. ->getMock();
  998. $service->expects(self::once())
  999. ->method('put')
  1000. ->willReturn($response);
  1001. $response->expects(self::once())
  1002. ->method('getStatusCode')
  1003. ->willReturn(403);
  1004. $response->expects(self::once())
  1005. ->method('getContent')
  1006. ->willReturn('Forbidden');
  1007. $this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class);
  1008. $this->expectExceptionMessage('Failed to update domain: Forbidden');
  1009. $service->updateDomain('test-access-token', 'https://test-domain.com');
  1010. }
  1011. /**
  1012. * @see HelloAssoService::refreshTokenIfNeeded()
  1013. */
  1014. public function testRefreshTokenIfNeeded(): void
  1015. {
  1016. $service = $this->getHelloAssoServiceMockFor('refreshTokenIfNeeded');
  1017. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  1018. ->disableOriginalConstructor()
  1019. ->getMock();
  1020. $futureDate = new \DateTime('+1 hour');
  1021. $helloAssoEntity->expects(self::once())
  1022. ->method('getRefreshToken')
  1023. ->willReturn('test-refresh-token');
  1024. $helloAssoEntity->expects(self::atLeastOnce())
  1025. ->method('getRefreshTokenCreatedAt')
  1026. ->willReturn($futureDate);
  1027. $result = $service->refreshTokenIfNeeded($helloAssoEntity);
  1028. $this->assertSame($helloAssoEntity, $result);
  1029. }
  1030. /**
  1031. * @see HelloAssoService::refreshTokenIfNeeded()
  1032. */
  1033. public function testRefreshTokenIfNeededWhenTokenExpired(): void
  1034. {
  1035. // Set fake current time to control the test scenario
  1036. DatesUtils::setFakeDatetime('2024-01-01 12:00:00');
  1037. $service = $this->getHelloAssoServiceMockFor('refreshTokenIfNeeded');
  1038. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  1039. ->disableOriginalConstructor()
  1040. ->getMock();
  1041. $refreshedEntity = $this->getMockBuilder(HelloAsso::class)
  1042. ->disableOriginalConstructor()
  1043. ->getMock();
  1044. // Create a date that when 25 minutes are added, will be less than current fake time
  1045. $pastDate = DatesUtils::new('2024-01-01 11:30:00');
  1046. $helloAssoEntity->expects(self::once())
  1047. ->method('getRefreshToken')
  1048. ->willReturn('test-refresh-token');
  1049. $helloAssoEntity->expects(self::atLeastOnce())
  1050. ->method('getRefreshTokenCreatedAt')
  1051. ->willReturn($pastDate);
  1052. $service->expects(self::once())
  1053. ->method('refreshTokens')
  1054. ->with($helloAssoEntity)
  1055. ->willReturn($helloAssoEntity);
  1056. $result = $service->refreshTokenIfNeeded($helloAssoEntity);
  1057. $this->assertSame($helloAssoEntity, $result);
  1058. // Clean up fake datetime
  1059. DatesUtils::clearFakeDatetime();
  1060. }
  1061. /**
  1062. * @see HelloAssoService::refreshTokens()
  1063. */
  1064. public function testRefreshTokens(): void
  1065. {
  1066. $service = $this->getHelloAssoServiceMockFor('refreshTokens');
  1067. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  1068. ->disableOriginalConstructor()
  1069. ->getMock();
  1070. $response = $this->getMockBuilder(ResponseInterface::class)
  1071. ->disableOriginalConstructor()
  1072. ->getMock();
  1073. $tokensData = [
  1074. 'access_token' => 'new-access-token',
  1075. 'refresh_token' => 'new-refresh-token',
  1076. 'expires_in' => 3600,
  1077. ];
  1078. $helloAssoEntity->expects(self::atLeastOnce())
  1079. ->method('getRefreshToken')
  1080. ->willReturn('current-refresh-token');
  1081. $helloAssoEntity->expects(self::once())
  1082. ->method('getRefreshTokenCreatedAt')
  1083. ->willReturn(new \DateTime());
  1084. $this->httpClient->expects(self::once())
  1085. ->method('request')
  1086. ->with('POST', 'https://api.helloasso.com/v5/oauth2/token', [
  1087. 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
  1088. 'body' => [
  1089. 'grant_type' => 'refresh_token',
  1090. 'refresh_token' => 'current-refresh-token',
  1091. ],
  1092. ])
  1093. ->willReturn($response);
  1094. $response->expects(self::once())
  1095. ->method('getStatusCode')
  1096. ->willReturn(200);
  1097. $response->expects(self::once())
  1098. ->method('getContent')
  1099. ->willReturn(json_encode($tokensData));
  1100. $helloAssoEntity->expects(self::once())
  1101. ->method('setToken')
  1102. ->with('new-access-token');
  1103. $helloAssoEntity->expects(self::once())
  1104. ->method('setRefreshToken')
  1105. ->with('new-refresh-token');
  1106. $helloAssoEntity->expects(self::once())
  1107. ->method('setTokenCreatedAt')
  1108. ->with(self::isInstanceOf(\DateTime::class));
  1109. $helloAssoEntity->expects(self::once())
  1110. ->method('setRefreshTokenCreatedAt')
  1111. ->with(self::isInstanceOf(\DateTime::class));
  1112. $this->entityManager->expects(self::once())
  1113. ->method('persist')
  1114. ->with($helloAssoEntity);
  1115. $this->entityManager->expects(self::once())
  1116. ->method('flush');
  1117. $result = $service->refreshTokens($helloAssoEntity);
  1118. $this->assertSame($helloAssoEntity, $result);
  1119. }
  1120. /**
  1121. * @see HelloAssoService::refreshTokens()
  1122. */
  1123. public function testRefreshTokensWhenIncompleteEntity(): void
  1124. {
  1125. $service = $this->getHelloAssoServiceMockFor('refreshTokens');
  1126. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  1127. ->disableOriginalConstructor()
  1128. ->getMock();
  1129. $helloAssoEntity->expects(self::once())
  1130. ->method('getRefreshToken')
  1131. ->willReturn(null);
  1132. // getRefreshTokenCreatedAt() won't be called because getRefreshToken() returns null first
  1133. $this->expectException(\RuntimeException::class);
  1134. $this->expectExceptionMessage('HelloAsso entity incomplete');
  1135. $service->refreshTokens($helloAssoEntity);
  1136. }
  1137. /**
  1138. * @see HelloAssoService::refreshTokens()
  1139. */
  1140. public function testRefreshTokensWhenHttpError(): void
  1141. {
  1142. $service = $this->getHelloAssoServiceMockFor('refreshTokens');
  1143. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  1144. ->disableOriginalConstructor()
  1145. ->getMock();
  1146. $response = $this->getMockBuilder(ResponseInterface::class)
  1147. ->disableOriginalConstructor()
  1148. ->getMock();
  1149. $helloAssoEntity->expects(self::atLeastOnce())
  1150. ->method('getRefreshToken')
  1151. ->willReturn('current-refresh-token');
  1152. $helloAssoEntity->expects(self::once())
  1153. ->method('getRefreshTokenCreatedAt')
  1154. ->willReturn(new \DateTime());
  1155. $this->httpClient->expects(self::once())
  1156. ->method('request')
  1157. ->willReturn($response);
  1158. $response->expects(self::once())
  1159. ->method('getStatusCode')
  1160. ->willReturn(401);
  1161. $response->expects(self::once())
  1162. ->method('getContent')
  1163. ->with(false)
  1164. ->willReturn('Unauthorized');
  1165. $this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class);
  1166. $this->expectExceptionMessage('Failed to refresh access token: Unauthorized');
  1167. $service->refreshTokens($helloAssoEntity);
  1168. }
  1169. /**
  1170. * @see HelloAssoService::refreshTokenIfNeeded()
  1171. */
  1172. public function testRefreshTokenIfNeededWhenIncompleteEntity(): void
  1173. {
  1174. $service = $this->getHelloAssoServiceMockFor('refreshTokenIfNeeded');
  1175. $helloAssoEntity = $this->getMockBuilder(HelloAsso::class)
  1176. ->disableOriginalConstructor()
  1177. ->getMock();
  1178. $helloAssoEntity->expects(self::once())
  1179. ->method('getRefreshToken')
  1180. ->willReturn('test-refresh-token');
  1181. $helloAssoEntity->expects(self::once())
  1182. ->method('getRefreshTokenCreatedAt')
  1183. ->willReturn(null);
  1184. $this->expectException(\RuntimeException::class);
  1185. $this->expectExceptionMessage('HelloAsso entity incomplete');
  1186. $service->refreshTokenIfNeeded($helloAssoEntity);
  1187. }
  1188. }