UtilsTest.php 4.5 KB

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