UtilsTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Tests\Unit\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\Network\Utils as NetworkUtils;
  9. use App\Service\Organization\Utils as OrganizationUtils;
  10. use App\Service\Utils\DatesUtils;
  11. use PHPUnit\Framework\MockObject\MockObject;
  12. use PHPUnit\Framework\TestCase;
  13. class UtilsTest extends TestCase
  14. {
  15. private const MEMBERSHIP_WAITING = 495; // Affiliation in progress
  16. private const MEMBERSHIP_NOPAYMENT = 517; // Waiting paiement
  17. private const SUBMIT_IN_PROGRESS = 540; // Affiliation in progress
  18. private MockObject|NetworkOrganizationRepository $networkOrganizationRepository;
  19. private MockObject|NetworkUtils $networkUtils;
  20. private MockObject|OrganizationUtils $organizationUtils;
  21. private MockObject|CotisationApiResourcesRepository $cotisationApiResourcesRepository;
  22. public function setUp(): void
  23. {
  24. $this->networkOrganizationRepository = $this->getMockBuilder(NetworkOrganizationRepository::class)
  25. ->disableOriginalConstructor()
  26. ->getMock();
  27. $this->networkUtils = $this->getMockBuilder(NetworkUtils::class)->disableOriginalConstructor()->getMock();
  28. $this->organizationUtils = $this->getMockBuilder(OrganizationUtils::class)->disableOriginalConstructor()->getMock();
  29. $this->cotisationApiResourcesRepository = $this->getMockBuilder(CotisationApiResourcesRepository::class)
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. }
  33. public function getCotisationUtilsMockFor(string $methodName): MockObject|CotisationUtils
  34. {
  35. return $this->getMockBuilder(CotisationUtils::class)
  36. ->setConstructorArgs([$this->networkUtils, $this->organizationUtils, $this->networkOrganizationRepository, $this->cotisationApiResourcesRepository])
  37. ->setMethodsExcept([$methodName])
  38. ->getMock();
  39. }
  40. /**
  41. * @see Utils::getAlertState()
  42. */
  43. public function testGetAlertState(): void
  44. {
  45. $cotisationUtils = $this->getCotisationUtilsMockFor('getAlertState');
  46. $organization = $this->getMockBuilder(Organization::class)->getMock();
  47. $organization->method('getId')->willReturn(1);
  48. $year = 2022;
  49. $this->cotisationApiResourcesRepository
  50. ->method('getAffiliationState')
  51. ->with($organization->getId(), $year)
  52. ->willReturnOnConsecutiveCalls(
  53. self::MEMBERSHIP_WAITING, self::SUBMIT_IN_PROGRESS, self::MEMBERSHIP_NOPAYMENT
  54. );
  55. $this->assertEquals(AlertStateEnum::AFFILIATION, $cotisationUtils->getAlertState($organization, $year));
  56. $this->assertEquals(AlertStateEnum::AFFILIATION, $cotisationUtils->getAlertState($organization, $year));
  57. $this->assertEquals(AlertStateEnum::INVOICE, $cotisationUtils->getAlertState($organization, $year));
  58. }
  59. /**
  60. * @see Utils::getAlertState()
  61. */
  62. public function testGetAlertStateInsurance(): void
  63. {
  64. $cotisationUtils = $this->getCotisationUtilsMockFor('getAlertState');
  65. $year = 2022;
  66. $organization = $this->getMockBuilder(Organization::class)->getMock();
  67. $organization->method('getId')->willReturn(1);
  68. $this->cotisationApiResourcesRepository
  69. ->method('getAffiliationState')
  70. ->with($organization->getId(), $year)
  71. ->willReturn(null);
  72. $this->cotisationApiResourcesRepository
  73. ->method('isInsuranceNotDone')
  74. ->with($organization->getId(), $year)
  75. ->willReturn(true);
  76. $this->assertEquals(AlertStateEnum::INSURANCE, $cotisationUtils->getAlertState($organization, $year));
  77. }
  78. /**
  79. * @see Utils::getAlertState()
  80. */
  81. public function testGetAlertStateAdvertisingInsurance(): void
  82. {
  83. $cotisationUtils = $this->getCotisationUtilsMockFor('getAlertState');
  84. $year = 2022;
  85. $organization = $this->getMockBuilder(Organization::class)->getMock();
  86. $organization->method('getId')->willReturn(1);
  87. $this->cotisationApiResourcesRepository
  88. ->method('getAffiliationState')
  89. ->with($organization->getId(), $year)
  90. ->willReturn(null);
  91. $this->cotisationApiResourcesRepository
  92. ->method('isNotDGVCustomer')
  93. ->with($organization->getId())
  94. ->willReturn(true);
  95. $this->assertEquals(AlertStateEnum::ADVERTISINGINSURANCE, $cotisationUtils->getAlertState($organization, $year));
  96. }
  97. /**
  98. * @see Utils::getCurrentCotisationYear()
  99. */
  100. public function testGetCurrentCotisationYear(): void
  101. {
  102. $cotisationUtils = $this->getCotisationUtilsMockFor('getCurrentCotisationYear');
  103. DatesUtils::setFakeDatetime('2022-03-01');
  104. $this->assertEquals(
  105. 2022,
  106. $cotisationUtils->getCurrentCotisationYear()
  107. );
  108. DatesUtils::setFakeDatetime('2022-10-01');
  109. $this->assertEquals(
  110. 2023,
  111. $cotisationUtils->getCurrentCotisationYear()
  112. );
  113. }
  114. }