HelloAssoServiceTest.php 48 KB

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