UtilsTest.php 4.4 KB

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