UtilsTest.php 8.5 KB

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