UtilsTest.php 10 KB

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