UtilsTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. namespace App\Tests\Unit\Service\Network;
  3. use App\Entity\Network\Network;
  4. use App\Entity\Network\NetworkOrganization;
  5. use App\Entity\Organization\Organization;
  6. use App\Enum\Network\NetworkEnum;
  7. use App\Service\Network\Utils as NetworkUtils;
  8. use DateTime;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use PHPUnit\Framework\TestCase;
  11. use App\Service\Utils\DatesUtils;
  12. class UtilsTest extends TestCase
  13. {
  14. /**
  15. * @see Utils::isCMF()
  16. */
  17. public function testIsCmf():void
  18. {
  19. $networkUtils = $this
  20. ->getMockBuilder(NetworkUtils::class)
  21. ->setMethodsExcept(['isCMF'])
  22. ->getMock();
  23. $organization = $this->getMockBuilder(Organization::class)->getMock();
  24. $networkUtils
  25. ->expects(self::once())
  26. ->method('doesOrganizationBelongToTheNetwork')
  27. ->with($organization, NetworkEnum::CMF())
  28. ->willReturn(true);
  29. $result = $networkUtils->isCmf($organization);
  30. $this->assertTrue($result);
  31. }
  32. /**
  33. * @see Utils::isCMF()
  34. */
  35. public function testIsNotCmf():void
  36. {
  37. $networkUtils = $this
  38. ->getMockBuilder(NetworkUtils::class)
  39. ->setMethodsExcept(['isCMF'])
  40. ->getMock();
  41. $organization = $this->getMockBuilder(Organization::class)->getMock();
  42. $networkUtils
  43. ->expects(self::once())
  44. ->method('doesOrganizationBelongToTheNetwork')
  45. ->with($organization, NetworkEnum::CMF())
  46. ->willReturn(false);
  47. $result = $networkUtils->isCmf($organization);
  48. $this->assertFalse($result);
  49. }
  50. /**
  51. * @see Utils::isCMF()
  52. */
  53. public function testIsCmfAndActiveNow():void
  54. {
  55. $networkUtils = $this
  56. ->getMockBuilder(NetworkUtils::class)
  57. ->setMethodsExcept(['isCMFAndActiveNow'])
  58. ->getMock();
  59. $organization = $this->getMockBuilder(Organization::class)->getMock();
  60. $networkUtils
  61. ->expects(self::once())
  62. ->method('doesOrganizationBelongToTheNetwork')
  63. ->with($organization, NetworkEnum::CMF(), true)
  64. ->willReturn(false);
  65. $result = $networkUtils->isCMFAndActiveNow($organization);
  66. $this->assertFalse($result);
  67. }
  68. /**
  69. * @see Utils::doesOrganizationBelongToTheNetwork()
  70. */
  71. public function testDoesOrganizationBelongToTheNetwork():void
  72. {
  73. $networkUtils = $this
  74. ->getMockBuilder(NetworkUtils::class)
  75. ->setMethodsExcept(['doesOrganizationBelongToTheNetwork'])
  76. ->getMock();
  77. $organization = $this->getMockBuilder(Organization::class)->getMock();
  78. $network1 = $this->getMockBuilder(Network::class)->getMock();
  79. $network1->method('getId')->willReturn(NetworkEnum::CMF()->getValue());
  80. $networkOrganization1 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  81. $networkOrganization1->method('getNetwork')->willReturn($network1);
  82. $network2 = $this->getMockBuilder(Network::class)->getMock();
  83. $network2->method('getId')->willReturn(NetworkEnum::FFEC()->getValue());
  84. $networkOrganization2 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  85. $networkOrganization2->method('getNetwork')->willReturn($network2);
  86. $organization
  87. ->expects(self::once())
  88. ->method('getNetworkOrganizations')
  89. ->willReturn(new ArrayCollection([$networkOrganization1, $networkOrganization2]));
  90. $networkUtils->expects(self::never())->method('isNetworkOrganizationActiveNow');
  91. $result = $networkUtils->doesOrganizationBelongToTheNetwork($organization, NetworkEnum::FFEC());
  92. $this->assertTrue($result);
  93. }
  94. /**
  95. * @see Utils::doesOrganizationBelongToTheNetwork()
  96. */
  97. public function testDoesOrganizationBelongToTheNetworkAndActive():void
  98. {
  99. $networkUtils = $this
  100. ->getMockBuilder(NetworkUtils::class)
  101. ->setMethodsExcept(['doesOrganizationBelongToTheNetwork'])
  102. ->getMock();
  103. $organization = $this->getMockBuilder(Organization::class)->getMock();
  104. $network1 = $this->getMockBuilder(Network::class)->getMock();
  105. $network1->method('getId')->willReturn(NetworkEnum::CMF()->getValue());
  106. $networkOrganization1 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  107. $networkOrganization1->method('getNetwork')->willReturn($network1);
  108. $network2 = $this->getMockBuilder(Network::class)->getMock();
  109. $network2->method('getId')->willReturn(NetworkEnum::FFEC()->getValue());
  110. $networkOrganization2 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  111. $networkOrganization2->method('getNetwork')->willReturn($network2);
  112. $organization
  113. ->expects(self::once())
  114. ->method('getNetworkOrganizations')
  115. ->willReturn(new ArrayCollection([$networkOrganization1, $networkOrganization2]));
  116. $networkUtils->expects(self::once())->method('isNetworkOrganizationActiveNow')->with($networkOrganization2)->willReturn(true);
  117. $result = $networkUtils->doesOrganizationBelongToTheNetwork($organization, NetworkEnum::FFEC(), true);
  118. $this->assertTrue($result);
  119. }
  120. /**
  121. * @see Utils::doesOrganizationBelongToTheNetwork()
  122. */
  123. public function testDoesOrganizationBelongToTheNetworkFalse():void
  124. {
  125. $networkUtils = $this
  126. ->getMockBuilder(NetworkUtils::class)
  127. ->setMethodsExcept(['doesOrganizationBelongToTheNetwork'])
  128. ->getMock();
  129. $organization = $this->getMockBuilder(Organization::class)->getMock();
  130. $network1 = $this->getMockBuilder(Network::class)->getMock();
  131. $network1->method('getId')->willReturn(NetworkEnum::CMF()->getValue());
  132. $networkOrganization1 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  133. $networkOrganization1->method('getNetwork')->willReturn($network1);
  134. $network2 = $this->getMockBuilder(Network::class)->getMock();
  135. $network2->method('getId')->willReturn(NetworkEnum::FFEC()->getValue());
  136. $networkOrganization2 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  137. $networkOrganization2->method('getNetwork')->willReturn($network2);
  138. $organization
  139. ->expects(self::once())
  140. ->method('getNetworkOrganizations')
  141. ->willReturn(new ArrayCollection([$networkOrganization1, $networkOrganization2]));
  142. $networkUtils->expects(self::never())->method('isNetworkOrganizationActiveNow');
  143. $result = $networkUtils->doesOrganizationBelongToTheNetwork($organization, NetworkEnum::CFBF());
  144. $this->assertFalse($result);
  145. }
  146. /**
  147. * @see Utils::isNetworkOrganizationActiveNow()
  148. */
  149. public function testIsOrganizationActiveNow():void
  150. {
  151. $networkUtils = $this
  152. ->getMockBuilder(NetworkUtils::class)
  153. ->setMethodsExcept(['isNetworkOrganizationActiveNow'])
  154. ->getMock();
  155. DatesUtils::setFakeDatetime('2023-01-01');
  156. $dateNow = DatesUtils::new();
  157. $startDate = DatesUtils::new('2022-01-01');
  158. $endDate = DatesUtils::new('2024-01-01');
  159. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  160. $networkOrganization
  161. ->expects(self::exactly(2))
  162. ->method('getStartDate')
  163. ->willReturn($startDate);
  164. $networkOrganization
  165. ->expects(self::once())
  166. ->method('getEndDate')
  167. ->willReturn($endDate);
  168. $this->assertTrue(
  169. $networkUtils->isNetworkOrganizationActiveNow($networkOrganization)
  170. );
  171. }
  172. /**
  173. * @see Utils::isNetworkOrganizationActiveNow()
  174. */
  175. public function testIsOrganizationActiveNowNotActive():void
  176. {
  177. $networkUtils = $this
  178. ->getMockBuilder(NetworkUtils::class)
  179. ->setMethodsExcept(['isNetworkOrganizationActiveNow'])
  180. ->getMock();
  181. DatesUtils::setFakeDatetime('2023-01-01');
  182. $dateNow = DatesUtils::new();
  183. $startDate = DatesUtils::new('2021-01-01');
  184. $endDate = DatesUtils::new('2022-01-01');
  185. $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  186. $networkOrganization
  187. ->expects(self::exactly(2))
  188. ->method('getStartDate')
  189. ->willReturn($startDate);
  190. $networkOrganization
  191. ->expects(self::once())
  192. ->method('getEndDate')
  193. ->willReturn($endDate);
  194. $this->assertFalse(
  195. $networkUtils->isNetworkOrganizationActiveNow($networkOrganization)
  196. );
  197. }
  198. }