UtilsTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. namespace App\Tests\Service\Cotisation;
  3. use App\Entity\Organization\Organization;
  4. use App\Enum\Cotisation\AlertStateEnum;
  5. use App\Repository\Cotisation\CotisationApiResourcesRepository;
  6. use App\Repository\Network\NetworkOrganizationRepository;
  7. use App\Service\Cotisation\Utils as CotisationUtils;
  8. use App\Service\Organization\Utils as OrganizationUtils;
  9. use App\Service\Utils\DatesUtils;
  10. use PHPUnit\Framework\MockObject\MockObject;
  11. use PHPUnit\Framework\TestCase;
  12. use App\Service\Network\Utils as NetworkUtils;
  13. class UtilsTest extends TestCase
  14. {
  15. private const MEMBERSHIP_WAITING = 495; // Affiliation in progress
  16. private const MEMBERSHIP_NOPAYMENT = 517; // Waiting paiement
  17. private const SUBMIT_IN_PROGRESS = 540; // Affiliation in progress
  18. private MockObject | NetworkOrganizationRepository $networkOrganizationRepository;
  19. private MockObject | NetworkUtils $networkUtils;
  20. private MockObject | OrganizationUtils $organizationUtils;
  21. private MockObject | CotisationApiResourcesRepository $cotisationApiResourcesRepository;
  22. public function setUp(): void
  23. {
  24. $this->networkOrganizationRepository = $this->getMockBuilder(NetworkOrganizationRepository::class)
  25. ->disableOriginalConstructor()
  26. ->getMock();
  27. $this->networkUtils = $this->getMockBuilder(NetworkUtils::class)->disableOriginalConstructor()->getMock();
  28. $this->organizationUtils = $this->getMockBuilder(OrganizationUtils::class)->getMock();
  29. $this->cotisationApiResourcesRepository = $this->getMockBuilder(CotisationApiResourcesRepository::class)
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. }
  33. /**
  34. * @see Utils::isLastParentAndCMF()
  35. */
  36. public function testIsLastParentAndCMF(): void
  37. {
  38. $cotisationUtils = $this->getMockBuilder(CotisationUtils::class)
  39. ->setConstructorArgs([$this->networkUtils, $this->organizationUtils, $this->networkOrganizationRepository, $this->cotisationApiResourcesRepository])
  40. ->setMethodsExcept(['isLastParentAndCMF'])
  41. ->getMock();
  42. $organization1 = $this->getMockBuilder(Organization::class)->getMock();
  43. $organization2 = $this->getMockBuilder(Organization::class)->getMock();
  44. $organization3 = $this->getMockBuilder(Organization::class)->getMock();
  45. $organization4 = $this->getMockBuilder(Organization::class)->getMock();
  46. $this->networkOrganizationRepository
  47. ->method('isLastParent')
  48. ->willReturnMap([
  49. [$organization1, true],
  50. [$organization2, true],
  51. [$organization3, false],
  52. [$organization4, false],
  53. ]);
  54. $this->networkUtils
  55. ->method('isCMF')
  56. ->willReturnMap([
  57. [$organization1, true],
  58. [$organization2, false],
  59. [$organization3, true],
  60. [$organization4, false],
  61. ]);
  62. $this->assertTrue($cotisationUtils->isLastParentAndCMF($organization1));
  63. $this->assertFalse($cotisationUtils->isLastParentAndCMF($organization2));
  64. $this->assertFalse($cotisationUtils->isLastParentAndCMF($organization3));
  65. $this->assertFalse($cotisationUtils->isLastParentAndCMF($organization4));
  66. }
  67. /**
  68. * @see Utils::isStructureAndCMF()
  69. */
  70. public function testIsStructureAndCMF(): void
  71. {
  72. $cotisationUtils = $this->getMockBuilder(CotisationUtils::class)
  73. ->setConstructorArgs([$this->networkUtils, $this->organizationUtils, $this->networkOrganizationRepository, $this->cotisationApiResourcesRepository])
  74. ->setMethodsExcept(['isStructureAndCMF'])
  75. ->getMock();
  76. $organization1 = $this->getMockBuilder(Organization::class)->getMock();
  77. $organization2 = $this->getMockBuilder(Organization::class)->getMock();
  78. $organization3 = $this->getMockBuilder(Organization::class)->getMock();
  79. $organization4 = $this->getMockBuilder(Organization::class)->getMock();
  80. $this->organizationUtils
  81. ->method('isStructure')
  82. ->willReturnMap([
  83. [$organization1, true],
  84. [$organization2, false],
  85. [$organization3, true],
  86. [$organization4, false],
  87. ]);
  88. $this->networkUtils
  89. ->method('isCMF')
  90. ->with($organization1)
  91. ->willReturnMap([
  92. [$organization1, true],
  93. [$organization2, false],
  94. [$organization3, false],
  95. [$organization4, true],
  96. ]);
  97. $this->assertTrue($cotisationUtils->isStructureAndCMF($organization1));
  98. $this->assertFalse($cotisationUtils->isStructureAndCMF($organization2));
  99. $this->assertFalse($cotisationUtils->isStructureAndCMF($organization3));
  100. $this->assertFalse($cotisationUtils->isStructureAndCMF($organization4));
  101. }
  102. /**
  103. * @see Utils::isManagerAndCMF()
  104. */
  105. public function testIsManagerAndCMF(): void
  106. {
  107. $cotisationUtils = $this->getMockBuilder(CotisationUtils::class)
  108. ->setConstructorArgs([$this->networkUtils, $this->organizationUtils, $this->networkOrganizationRepository, $this->cotisationApiResourcesRepository])
  109. ->setMethodsExcept(['isManagerAndCMF'])
  110. ->getMock();
  111. $organization1 = $this->getMockBuilder(Organization::class)->getMock();
  112. $organization2 = $this->getMockBuilder(Organization::class)->getMock();
  113. $organization3 = $this->getMockBuilder(Organization::class)->getMock();
  114. $organization4 = $this->getMockBuilder(Organization::class)->getMock();
  115. $this->organizationUtils
  116. ->method('isManager')
  117. ->willReturnMap([
  118. [$organization1, true],
  119. [$organization2, false],
  120. [$organization3, true],
  121. [$organization4, false],
  122. ]);
  123. $this->networkUtils
  124. ->method('isCMF')
  125. ->willReturnMap([
  126. [$organization1, true],
  127. [$organization2, false],
  128. [$organization3, false],
  129. [$organization4, true],
  130. ]);
  131. $this->assertTrue($cotisationUtils->isManagerAndCMF($organization1));
  132. $this->assertFalse($cotisationUtils->isManagerAndCMF($organization2));
  133. $this->assertFalse($cotisationUtils->isManagerAndCMF($organization3));
  134. $this->assertFalse($cotisationUtils->isManagerAndCMF($organization4));
  135. }
  136. /**
  137. * @see Utils::isManagerAndNotLastParentAndCMF()
  138. */
  139. public function testIsManagerAndLastParentAndCMF(): void
  140. {
  141. $cotisationUtils = $this->getMockBuilder(CotisationUtils::class)
  142. ->setConstructorArgs([$this->networkUtils, $this->organizationUtils, $this->networkOrganizationRepository, $this->cotisationApiResourcesRepository])
  143. ->setMethodsExcept(['isManagerAndLastParentAndCMF'])
  144. ->getMock();
  145. $organization1 = $this->getMockBuilder(Organization::class)->getMock();
  146. $organization2 = $this->getMockBuilder(Organization::class)->getMock();
  147. $organization3 = $this->getMockBuilder(Organization::class)->getMock();
  148. $organization4 = $this->getMockBuilder(Organization::class)->getMock();
  149. $this->organizationUtils
  150. ->method('isManager')
  151. ->willReturnMap([
  152. [$organization1, true],
  153. [$organization2, false],
  154. [$organization3, true],
  155. [$organization4, false],
  156. ]);
  157. $cotisationUtils
  158. ->method('isLastParentAndCMF')
  159. ->willReturnMap([
  160. [$organization1, true],
  161. [$organization2, false],
  162. [$organization3, false],
  163. [$organization4, true],
  164. ]);
  165. $this->assertTrue($cotisationUtils->isManagerAndLastParentAndCMF($organization1));
  166. $this->assertFalse($cotisationUtils->isManagerAndLastParentAndCMF($organization2));
  167. $this->assertFalse($cotisationUtils->isManagerAndLastParentAndCMF($organization3));
  168. $this->assertFalse($cotisationUtils->isManagerAndLastParentAndCMF($organization4));
  169. }
  170. /**
  171. * @see Utils::isManagerAndNotLastParentAndCMF()
  172. */
  173. public function testIsManagerAndNotLastParentAndCMF(): void
  174. {
  175. $cotisationUtils = $this->getMockBuilder(CotisationUtils::class)
  176. ->setConstructorArgs([$this->networkUtils, $this->organizationUtils, $this->networkOrganizationRepository, $this->cotisationApiResourcesRepository])
  177. ->setMethodsExcept(['isManagerAndNotLastParentAndCMF'])
  178. ->getMock();
  179. $organization1 = $this->getMockBuilder(Organization::class)->getMock();
  180. $organization2 = $this->getMockBuilder(Organization::class)->getMock();
  181. $organization3 = $this->getMockBuilder(Organization::class)->getMock();
  182. $organization4 = $this->getMockBuilder(Organization::class)->getMock();
  183. $this->organizationUtils
  184. ->method('isManager')
  185. ->willReturnMap([
  186. [$organization1, true],
  187. [$organization2, false],
  188. [$organization3, true],
  189. [$organization4, true],
  190. ]);
  191. $this->networkOrganizationRepository
  192. ->method('isLastParent')
  193. ->willReturnMap([
  194. [$organization1, false],
  195. [$organization2, false],
  196. [$organization3, true],
  197. [$organization4, false],
  198. ]);
  199. $this->networkUtils
  200. ->method('isCMF')
  201. ->willReturnMap([
  202. [$organization1, true],
  203. [$organization2, true],
  204. [$organization3, true],
  205. [$organization4, false],
  206. ]);
  207. $this->assertTrue($cotisationUtils->isManagerAndNotLastParentAndCMF($organization1));
  208. $this->assertFalse($cotisationUtils->isManagerAndNotLastParentAndCMF($organization2));
  209. $this->assertFalse($cotisationUtils->isManagerAndNotLastParentAndCMF($organization3));
  210. $this->assertFalse($cotisationUtils->isManagerAndNotLastParentAndCMF($organization4));
  211. }
  212. /**
  213. * @see Utils::getAlertState()
  214. */
  215. public function testGetAlertState(): void
  216. {
  217. $cotisationUtils = $this->getMockBuilder(CotisationUtils::class)
  218. ->setConstructorArgs([$this->networkUtils, $this->organizationUtils, $this->networkOrganizationRepository, $this->cotisationApiResourcesRepository])
  219. ->setMethodsExcept(['getAlertState'])
  220. ->getMock();
  221. $organization = $this->getMockBuilder(Organization::class)->getMock();
  222. $organization->method('getId')->willReturn(1);
  223. $year = 2022;
  224. $this->cotisationApiResourcesRepository
  225. ->method('getAffiliationState')
  226. ->with($organization->getId(), $year)
  227. ->willReturnOnConsecutiveCalls(
  228. self::MEMBERSHIP_WAITING, self::SUBMIT_IN_PROGRESS, self::MEMBERSHIP_NOPAYMENT
  229. );
  230. $this->assertEquals(AlertStateEnum::AFFILIATION()->getValue(), $cotisationUtils->getAlertState($organization, $year) );
  231. $this->assertEquals(AlertStateEnum::AFFILIATION()->getValue(), $cotisationUtils->getAlertState($organization, $year) );
  232. $this->assertEquals(AlertStateEnum::INVOICE()->getValue(), $cotisationUtils->getAlertState($organization, $year) );
  233. }
  234. /**
  235. * @see Utils::getAlertState()
  236. */
  237. public function testGetAlertStateInsurance(): void
  238. {
  239. $cotisationUtils = $this->getMockBuilder(CotisationUtils::class)
  240. ->setConstructorArgs([$this->networkUtils, $this->organizationUtils, $this->networkOrganizationRepository, $this->cotisationApiResourcesRepository])
  241. ->setMethodsExcept(['getAlertState'])
  242. ->getMock();
  243. $year = 2022;
  244. $organization = $this->getMockBuilder(Organization::class)->getMock();
  245. $organization->method('getId')->willReturn(1);
  246. $this->cotisationApiResourcesRepository
  247. ->method('getAffiliationState')
  248. ->with($organization->getId(), $year)
  249. ->willReturn(null);
  250. $this->cotisationApiResourcesRepository
  251. ->method('isInsuranceNotDone')
  252. ->with($organization->getId(), $year)
  253. ->willReturn(true);
  254. $this->assertEquals(AlertStateEnum::INSURANCE()->getValue(), $cotisationUtils->getAlertState($organization, $year) );
  255. }
  256. /**
  257. * @see Utils::getAlertState()
  258. */
  259. public function testGetAlertStateAdvertisingInsurance(): void
  260. {
  261. $cotisationUtils = $this->getMockBuilder(CotisationUtils::class)
  262. ->setConstructorArgs([$this->networkUtils, $this->organizationUtils, $this->networkOrganizationRepository, $this->cotisationApiResourcesRepository])
  263. ->setMethodsExcept(['getAlertState'])
  264. ->getMock();
  265. $year = 2022;
  266. $organization = $this->getMockBuilder(Organization::class)->getMock();
  267. $organization->method('getId')->willReturn(1);
  268. $this->cotisationApiResourcesRepository
  269. ->method('getAffiliationState')
  270. ->with($organization->getId(), $year)
  271. ->willReturn(null);
  272. $this->cotisationApiResourcesRepository
  273. ->method('isNotDGVCustomer')
  274. ->with($organization->getId())
  275. ->willReturn(true);
  276. $this->assertEquals(AlertStateEnum::ADVERTISINGINSURANCE()->getValue(), $cotisationUtils->getAlertState($organization, $year) );
  277. }
  278. /**
  279. * @see Utils::getCurrentCotisationYear()
  280. */
  281. public function testGetCurrentCotisationYear(): void
  282. {
  283. $cotisationUtils = $this->getMockBuilder(CotisationUtils::class)
  284. ->setConstructorArgs([$this->networkUtils, $this->organizationUtils, $this->networkOrganizationRepository, $this->cotisationApiResourcesRepository])
  285. ->setMethodsExcept(['getCurrentCotisationYear'])
  286. ->getMock();
  287. DatesUtils::setFakeDatetime('2022-03-01');
  288. $this->assertEquals(
  289. 2022,
  290. $cotisationUtils->getCurrentCotisationYear()
  291. );
  292. DatesUtils::setFakeDatetime('2022-10-01');
  293. $this->assertEquals(
  294. 2023,
  295. $cotisationUtils->getCurrentCotisationYear()
  296. );
  297. }
  298. }