UtilsTest.php 10 KB

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