UtilsTest.php 14 KB

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